Info.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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,num_sell+num_sell_add as num_sell,show_page'];
  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+num_sell_add as num_sell,show_page,show_submit,show_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. # 获取资源规格信息
  105. public function getSpec($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. return Dever::load(\Api\Lib\Spec::class)->getList(['info_id' => $info['id']], $info['sku_id'], 'psource');
  112. }
  113. # 根据资源规格获取价格
  114. public function getSku($id, $spec = '', $default_sku_id = 0)
  115. {
  116. $info = Dever::db('psource/info')->find(['id' => $id, 'status' => 1], ['col' => 'id,sku_id,cate_id,pic']);
  117. if (!$info) {
  118. Dever::error('资源不存在');
  119. }
  120. if (!$spec) {
  121. $where = ['id' => $info['sku_id'], 'info_id' => $info['id']];
  122. } else {
  123. $where = ['info_id' => $info['id'], 'key' => $spec];
  124. }
  125. $sku = $this->getSkuPrice($where, $info);
  126. if ((!$sku && $default_sku_id) || ($sku['price'] == null && $sku['code'] == null)) {
  127. # 如果没有,获取默认值
  128. $where = ['id' => $default_sku_id, 'info_id' => $info['id']];
  129. $sku = $this->getSkuPrice($where, $info);
  130. if ($sku) {
  131. $sku['stock']['num'] = 0;
  132. }
  133. }
  134. return $sku;
  135. }
  136. # 获取价格
  137. public function getSkuPrice($where, $info)
  138. {
  139. $sku = Dever::load(\Api\Lib\Sku::class)->getInfo($where, 'psource');
  140. if ($sku) {
  141. if ($sku['price'] === '') {
  142. return false;
  143. }
  144. if (!$sku['pic']) {
  145. $pic = explode(',', $info['pic']);
  146. $sku['pic'] = $pic[0];
  147. }
  148. # 分类
  149. $cate = Dever::load(Cate::class)->getInfo($info['cate_id']);
  150. $sku = Dever::load(Price::class)->get($sku, $cate, -1);
  151. }
  152. return $sku;
  153. }
  154. # 获取资源基本信息
  155. public function getBaseInfo($source_id)
  156. {
  157. $info = Dever::db('psource/info')->find($source_id, ['col' => 'id,name,info,channel_id,cate_parent_id']);
  158. if (!$info) {
  159. Dever::error('资源不存在');
  160. }
  161. return $info;
  162. }
  163. # 获取资源编号或者ID
  164. public function getCode($id, $encode = true)
  165. {
  166. if (Dever::getData('muser')) {
  167. $salt = Dever::getData('muser')['select']['data_id'];//品牌id
  168. $xorkey = Dever::getData('muser')['id'];//当前登录的管理员id
  169. } else {
  170. $place = Dever::load(\Place::class);
  171. $salt = $place['info']['id'];//品牌id
  172. $xorkey = $place['uid'];//当前登录的管理员id
  173. }
  174. return \Dever\Helper\Str::uid($id, $encode, $salt, $xorkey);
  175. }
  176. # 根据$cate获取where条件
  177. public function getWhereByCate($cate, $prefix = '')
  178. {
  179. if ($cate) {
  180. $cate = explode(',', $cate);
  181. if (isset($cate[2]) && $cate[2]) {
  182. $where = [$prefix . 'cate_child_id' => $cate[2]];
  183. } elseif (isset($cate[1]) && $cate[1]) {
  184. $where = [$prefix . 'cate_parent_id' => $cate[1]];
  185. } else {
  186. $where = [$prefix . 'channel_id' => $cate[0]];
  187. }
  188. }
  189. $where[$prefix . 'status'] = 1;
  190. return $where;
  191. }
  192. # 根据$cate获取资源列表
  193. public function getListByCate($cate)
  194. {
  195. $where = $this->getWhereByCate($cate);
  196. return Dever::db('psource/info')->select($where);
  197. }
  198. # 根据$cate获取资源sku列表
  199. public function getSkuByCate($cate)
  200. {
  201. $set['join'] = array
  202. (
  203. array
  204. (
  205. 'table' => 'psource_info',
  206. 'type' => 'left join',
  207. 'on' => 'main.info_id=t0.id',
  208. ),
  209. );
  210. $set['col'] = 'main.id,main.info_id,main.key,main.pic,main.price,main.code';
  211. $set['order'] = 'main.id asc';
  212. $where = $this->getWhereByCate($cate, 't0.');
  213. $where['main.state'] = 1;
  214. return Dever::db('psource/sku')->select($where, $set);
  215. }
  216. # 建立规格
  217. public function createSku($i, $id, $name, $code, $price, $pic, $type = 1)
  218. {
  219. if ($name) {
  220. $createCode = false;
  221. if (!$code) {
  222. $createCode = true;
  223. }
  224. if (!$type) {
  225. $type = 1;
  226. }
  227. foreach ($name as $v) {
  228. if ($createCode) {
  229. $code = $this->createCode($i);
  230. }
  231. $temp = explode('&', $v);
  232. $spec_value_id = [];
  233. foreach ($temp as $k => $t) {
  234. # 颜色:白色 & 尺寸:35
  235. $t = trim($t);
  236. list($spec_name, $spec_value) = explode(':', $t);
  237. $spec_data = ['info_id' => $id, 'name' => $spec_name];
  238. $spec = Dever::db('psource/spec')->find($spec_data);
  239. if ($spec) {
  240. $spec_id = $spec['id'];
  241. } else {
  242. $spec_data['sort'] = $i;
  243. $spec_id = Dever::db('psource/spec')->insert($spec_data);
  244. }
  245. if ($spec_id) {
  246. $spec_data = ['info_id' => $id, 'spec_id' => $spec_id, 'value' => $spec_value];
  247. $spec_value = Dever::db('psource/spec_value')->find($spec_data);
  248. if ($spec_value) {
  249. $spec_value_id[] = $spec_value['id'];
  250. } else {
  251. $spec_data['sort'] = $i;
  252. if ($type == 1) {
  253. $spec_data['pic'] = $pic;
  254. }
  255. $spec_value_id[] = Dever::db('psource/spec_value')->insert($spec_data);
  256. }
  257. }
  258. }
  259. $update = [];
  260. if ($spec_value_id) {
  261. $spec_value_id = implode(',', $spec_value_id);
  262. $update['spec_type'] = 3;
  263. } else {
  264. $spec_value_id = -1;
  265. $update['spec_type'] = 2;
  266. }
  267. $sku_data = ['info_id' => $id, 'key' => $spec_value_id];
  268. $sku = Dever::db('psource/sku')->find($sku_data);
  269. $sku_data['price'] = $price;
  270. $sku_data['code'] = $code;
  271. if ($type == 2) {
  272. $sku_data['pic'] = $pic;
  273. }
  274. if ($sku) {
  275. Dever::db('psource/sku')->update($sku['id'], $sku_data);
  276. } else {
  277. Dever::db('psource/sku')->insert($sku_data);
  278. }
  279. $sku = Dever::load(\Api\Lib\Sku::class)->getPrice(['info_id' => $id], $update['spec_type'], 'psource');
  280. $update['price'] = $sku['price'];
  281. $update['sku_id'] = $sku['id'];
  282. Dever::db('psource/info')->update($id, $update);
  283. $i++;
  284. }
  285. }
  286. return $i;
  287. }
  288. public function createPic($pic, $info_pic = '')
  289. {
  290. if ($pic) {
  291. $temp = explode('?', $pic);
  292. if (!strstr($info_pic, $temp[0])) {
  293. $info_pic = explode(',', $info_pic);
  294. $info_pic[] = $pic;
  295. return implode(',', $info_pic);
  296. }
  297. }
  298. return '';
  299. }
  300. private function createCode($i)
  301. {
  302. // 流水号,补足4位,不足左边补0
  303. $line = str_pad((string)($i + 1), 4, '0', STR_PAD_LEFT);
  304. // 当前时间 (年月日时分)
  305. $dateStr = date("YmdHi"); // Y=年, m=月, d=日, H=时, i=分
  306. return $dateStr . $line;
  307. }
  308. }