Info.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php namespace Psource\Lib;
  2. use Dever;
  3. use Pact\Lib\Core as Act;
  4. class Info
  5. {
  6. # 获取资源列表
  7. public function getList($cate, $limit, $page = false)
  8. {
  9. if (isset($cate['parent_id'])) {
  10. $key = 'cate_parent_id';
  11. if ($cate['parent_id'] > 0) {
  12. $key = 'cate_child_id';
  13. }
  14. } else {
  15. $key = 'channel_id';
  16. }
  17. $where = [$key => $cate['id'], 'status' => 1];
  18. $search = Dever::input('search');
  19. if ($search) {
  20. $where['name'] = ['like', $search];
  21. }
  22. $set = ['col' => 'id,sku_id,cate_child_id,name,info,price,pic,type,view,num_sell'];
  23. if ($limit) {
  24. $set['limit'] = $limit;
  25. }
  26. if ($page) {
  27. $set['num'] = $page;
  28. }
  29. $order = Dever::input('order');
  30. if ($order == 1) {
  31. # 销量排序
  32. $set['order'] = 'num_sell desc';
  33. } elseif ($order == 3) {
  34. # 价格正序
  35. $set['order'] = 'price asc';
  36. } elseif ($order == 4) {
  37. # 价格倒序
  38. $set['order'] = 'price desc';
  39. }
  40. $data = Dever::db('psource/info')->select($where, $set);
  41. if ($data) {
  42. foreach ($data as &$v) {
  43. $v = $this->get($v, $cate);
  44. }
  45. }
  46. return $data;
  47. }
  48. # 获取资源信息
  49. public function get($info, $cate = [])
  50. {
  51. if (is_numeric($info)) {
  52. $info = Dever::db('psource/info')->find(['id' => $info, 'status' => 1], ['col' => 'id,name,pic,price,cate_id']);
  53. }
  54. if ($info) {
  55. if (!$cate) {
  56. $cate = Dever::load(Cate::class)->getInfo($info['cate_id']);
  57. }
  58. # 获取封面
  59. $info['cover'] = '';
  60. $pic = explode(',', $info['pic']);
  61. unset($info['pic']);
  62. if (isset($pic[0])) {
  63. $info['cover'] = $pic[0];
  64. }
  65. $info = Dever::load(Price::class)->get($info, $cate, -1);
  66. }
  67. return $info;
  68. }
  69. # 获取资源详细信息
  70. public function getInfo($uid, $id)
  71. {
  72. $info = Dever::db('psource/info')->find(['id' => $id, 'status' => 1], ['col' => 'id,name,info,price,sku_id,pic,content,channel_id,cate_parent_id,cate_id,have_vip,num_sell']);
  73. if (!$info) {
  74. Dever::error('资源不存在');
  75. }
  76. # 分类
  77. $cate = Dever::load(Cate::class)->getInfo($info['cate_id']);
  78. # 价格
  79. $info = Dever::load(Price::class)->get($info, $cate, -1);
  80. $info['pic'] = explode(',', $info['pic']);
  81. $info['content'] = htmlspecialchars_decode($info['content']);
  82. if ($info['content'] == '<p><br></p>') {
  83. $info['content'] = '';
  84. }
  85. # 获取sku基本信息
  86. $sku = Dever::db('psource/sku')->find($info['sku_id']);
  87. if ($sku) {
  88. $info['sku_name'] = Dever::load(\Api\Lib\Sku::class)->getName($sku['key'], 'psource');
  89. }
  90. # 查看是否专享
  91. $info['have_vip'] = Dever::load(Price::class)->getHave($info);
  92. # 展示说明
  93. $info['help'] = Dever::load(Help::class)->getList($cate['help_cate_id']);
  94. # 是否收藏
  95. $info['collect'] = false;
  96. if ($uid) {
  97. $act = Act::load('collect', $uid, 1, $info['id']);
  98. if ($act->getInfo()) {
  99. $info['collect'] = true;
  100. }
  101. }
  102. return $info;
  103. }
  104. # 获取资源sku信息
  105. public function getSku($id)
  106. {
  107. $info = Dever::db('psource/info')->find(['id' => $id, 'status' => 1], ['col' => 'id,sku_id,cate_id']);
  108. if (!$info) {
  109. Dever::error('资源不存在');
  110. }
  111. # 分类
  112. $cate = Dever::load(Cate::class)->getInfo($info['cate_id']);
  113. $sku = Dever::load(\Api\Lib\Sku::class)->getList(['info_id' => $info['id']], $info['sku_id'], 'psource');
  114. if ($sku['info']) {
  115. $sku['info'] = Dever::load(Price::class)->get($sku['info'], $cate, -1);
  116. }
  117. if ($sku['price']) {
  118. foreach ($sku['price'] as &$price) {
  119. $price = Dever::load(Price::class)->get($price, $cate, -1);
  120. }
  121. }
  122. return $sku;
  123. }
  124. # 获取资源基本信息
  125. public function getBaseInfo($source_id)
  126. {
  127. $info = Dever::db('psource/info')->find($source_id, ['col' => 'id,name,info,channel_id,cate_parent_id']);
  128. if (!$info) {
  129. Dever::error('资源不存在');
  130. }
  131. return $info;
  132. }
  133. # 获取资源编号或者ID
  134. public function getCode($id, $encode = true)
  135. {
  136. if (Dever::getData('muser')) {
  137. $salt = Dever::getData('muser')['select']['data_id'];//品牌id
  138. $xorkey = Dever::getData('muser')['id'];//当前登录的管理员id
  139. } else {
  140. $place = Dever::load(\Place::class);
  141. $salt = $place['info']['id'];//品牌id
  142. $xorkey = $place['uid'];//当前登录的管理员id
  143. }
  144. return \Dever\Helper\Str::uid($id, $encode, $salt, $xorkey);
  145. }
  146. # 根据$cate获取where条件
  147. public function getWhereByCate($cate, $prefix = '')
  148. {
  149. if ($cate) {
  150. $cate = explode(',', $cate);
  151. if (isset($cate[2]) && $cate[2]) {
  152. $where = [$prefix . 'cate_child_id' => $cate[2]];
  153. } elseif (isset($cate[1]) && $cate[1]) {
  154. $where = [$prefix . 'cate_parent_id' => $cate[1]];
  155. } else {
  156. $where = [$prefix . 'channel_id' => $cate[0]];
  157. }
  158. }
  159. $where[$prefix . 'status'] = 1;
  160. return $where;
  161. }
  162. # 根据$cate获取资源列表
  163. public function getListByCate($cate)
  164. {
  165. $where = $this->getWhereByCate($cate);
  166. return Dever::db('psource/info')->select($where);
  167. }
  168. # 根据$cate获取资源sku列表
  169. public function getSkuByCate($cate)
  170. {
  171. $set['join'] = array
  172. (
  173. array
  174. (
  175. 'table' => 'psource_info',
  176. 'type' => 'left join',
  177. 'on' => 'main.info_id=t0.id',
  178. ),
  179. );
  180. $set['col'] = 'main.id,main.info_id,main.key,main.pic,main.price,main.code';
  181. $set['order'] = 'main.id asc';
  182. $where = $this->getWhereByCate($cate, 't0.');
  183. $where['main.state'] = 1;
  184. return Dever::db('psource/sku')->select($where, $set);
  185. }
  186. }