Resource.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php namespace Place\Lib;
  2. use Dever;
  3. use Place;
  4. class Resource
  5. {
  6. public $app;
  7. public $type;
  8. protected $db;
  9. protected $where;
  10. protected $set;
  11. public function __construct($type)
  12. {
  13. $this->type = $type;
  14. if (!$this->type) {
  15. Dever::error('资源参数错误');
  16. }
  17. if (isset(Dever::config('setting')['type'][$this->type])) {
  18. $this->app = Dever::config('setting')['type'][$this->type];
  19. $this->db = Dever::db('info', $this->app);
  20. } else {
  21. Dever::error('资源参数错误');
  22. }
  23. $this->where = array('status' => 1);
  24. $this->set['col'] = Dever::load('info', $this->app)->getCol();
  25. }
  26. # 获取列表
  27. public function getList($cate = false, $id = false, $num = 10)
  28. {
  29. if ($cate) {
  30. $cate = explode(',', $cate);
  31. $this->where['cate_parent_id'] = $cate[0];
  32. if (isset($cate[1])) {
  33. $this->where['cate_child_id'] = $cate[0];
  34. }
  35. }
  36. $name = Dever::input('search');
  37. if ($name) {
  38. $this->where['name'] = array('like', $name);
  39. }
  40. if ($id) {
  41. $this->set['limit'] = '0,6';
  42. $this->where['id'] = array('!=', $id);
  43. } else {
  44. $this->set['num'] = $num;
  45. }
  46. $info = $this->db->select($this->where, $this->set);
  47. $result = array();
  48. foreach ($info as $k => $v) {
  49. $v = $this->handleInfo($v);
  50. $result[] = $v;
  51. }
  52. return $result;
  53. }
  54. public function getInfo($id, $state = true)
  55. {
  56. if (!$id) {
  57. Dever::error('资源不存在');
  58. }
  59. $this->where['id'] = $id;
  60. $info = $this->db->find($this->where);
  61. if (!$info) {
  62. if ($state) {
  63. Dever::error('资源不存在');
  64. }
  65. return false;
  66. }
  67. return $this->handleInfo($info);
  68. }
  69. # 下单
  70. public function pay_commit(){}
  71. public function pay($info)
  72. {
  73. $order = array();
  74. $order['uid'] = Place::$uid;
  75. $order['name'] = $info['name'];
  76. $prefix = strtoupper(substr($this->app, 0, 2));
  77. if ($this->type == 3) {
  78. $order['cash'] = $info['value'];
  79. } else {
  80. $order['cash'] = ($info['value']/Place::$info['score_per']);
  81. }
  82. $order['order_num'] = Dever::load('util', 'api')->createNumber($prefix, 'place/order');
  83. $order['notify'] = 'place/callback.resource|' . Dever::input('p') . '|' . $order['order_num'] . '|' . $this->type;
  84. $data['pay'] = Dever::load('account', 'place')->pay($order);
  85. if ($data['pay'] && empty($data['pay']['link'])) {
  86. $order['status'] = 1;
  87. $order['type'] = $this->type;
  88. $order['type_id'] = $info['id'];
  89. $order['num'] = $info['num'];
  90. $data['order_id'] = Dever::db('order', 'place')->insert($order);
  91. $data['order_num'] = $order['order_num'];
  92. if (isset($info['detail'])) {
  93. $detail['order_id'] = $data['order_id'];
  94. foreach ($info['detail'] as $k => $v) {
  95. $detail['type'] = $this->type;
  96. $detail['type_id'] = $v['id'];
  97. if (isset($v['sku_id'])) {
  98. $detail['sku_id'] = $v['sku_id'];
  99. }
  100. if (isset($v['sku_name'])) {
  101. $detail['sku_name'] = $v['sku_name'];
  102. }
  103. $detail['pic'] = $v['pic'];
  104. $detail['cash'] = $v['cash'];
  105. $detail['num'] = $v['num'] ?? $order['num'];
  106. $detail['name'] = $v['name'];
  107. $detail['pic'] = $v['pic'];
  108. Dever::db('order_detail', 'place')->insert($detail);
  109. }
  110. }
  111. }
  112. return $data;
  113. }
  114. # 同步资源,关联资源
  115. public function relation($place_id, $data, $relation)
  116. {
  117. $where['channel_place_id'] = $place_id;
  118. $where['channel_type'] = $data['type'];
  119. $where['channel_type_id'] = $data['type_id'];
  120. $info = Dever::db('resource_relation', 'place')->find($where);
  121. if ($info) {
  122. if ($info['status'] == 2) {
  123. Dever::db('resource_relation', 'place')->update($info['id'], array('status' => 1));
  124. $update['source'] = 2;
  125. Dever::db('info', $this->app, false)->update($info['type_id'], $update);
  126. # 同步sku
  127. $this->relationSku($info['type_id'], $relation);
  128. }
  129. } else {
  130. $parent_id = $child_id = 0;
  131. foreach ($relation['cate'] as $k => $v) {
  132. unset($v['id']);
  133. unset($v['cdate']);
  134. if (!$parent_id) {
  135. $v['parent_id'] = 0;
  136. $parent_id = $this->createCate($v);
  137. } else {
  138. $v['parent_id'] = $parent_id;
  139. $child_id = $this->createCate($v);
  140. }
  141. }
  142. $cate[] = $relation['info']['cate_parent_id'] = $parent_id;
  143. if ($child_id) {
  144. $cate[] = $relation['info']['cate_child_id'] = $child_id;
  145. }
  146. $relation['info']['cate'] = implode(',', $cate);
  147. $relation['info']['source'] = 2;
  148. unset($relation['info']['id']);
  149. $info_id = Dever::db('info', $this->app, false)->insert($relation['info']);
  150. if ($info_id) {
  151. # 同步sku
  152. $this->relationSku($info_id, $relation);
  153. $where['type'] = $data['type'];
  154. $where['type_id'] = $info_id;
  155. $where['channel_price'] = $data['price'];
  156. $where['status'] = 1;
  157. Dever::db('resource_relation', 'place')->insert($where);
  158. }
  159. }
  160. }
  161. # 同步sku
  162. public function relationSku($info_id, $relation)
  163. {
  164. if (isset($relation['sku']) && $relation['sku']) {
  165. $spec_value = array();
  166. $spec_db = Dever::db('spec', $this->app, false);
  167. $spec_value_db = Dever::db('spec_value', $this->app, false);
  168. $sku_db = Dever::db('sku', $this->app, false);
  169. $spec_db->delete(array('info_id' => $info_id));
  170. $spec_value_db->delete(array('info_id' => $info_id));
  171. $sku_db->delete(array('info_id' => $info_id));
  172. foreach ($relation['spec'] as $v) {
  173. unset($v['id']);
  174. unset($v['cdate']);
  175. $v['info_id'] = $info_id;
  176. $spec_id = $spec_db->insert($v);
  177. foreach ($v['value'] as $v1) {
  178. $id = $v1['id'];
  179. unset($v1['id']);
  180. unset($v1['cdate']);
  181. $v1['info_id'] = $info_id;
  182. $v1['spec_id'] = $spec_id;
  183. $value_id = $spec_value_db->insert($v1);
  184. $spec_value[$id] = $value_id;
  185. }
  186. }
  187. foreach ($relation['sku'] as $v) {
  188. unset($v['id']);
  189. unset($v['cdate']);
  190. $key = explode(',', $v['key']);
  191. $v['key'] = array();
  192. foreach ($key as $v1) {
  193. if (isset($spec_value[$v1])) {
  194. $v['key'][] = $spec_value[$v1];
  195. }
  196. }
  197. $v['key'] = implode(',', $v['key']);
  198. $sku_db->insert($v);
  199. }
  200. }
  201. }
  202. # 删除关联关系
  203. public function relationDel($place_id, $data)
  204. {
  205. $where['channel_place_id'] = $place_id;
  206. $where['channel_type'] = $data['type'];
  207. $where['channel_type_id'] = $data['type_id'];
  208. $where['status'] = 1;
  209. $info = Dever::db('resource_relation', 'place')->find($where);
  210. if ($info) {
  211. Dever::db('resource_relation', 'place')->update($info['id'], array('status' => 2));
  212. Dever::db('info', $this->app, false)->update($info['type_id'], array('source' => 1));
  213. }
  214. }
  215. # 创建分类
  216. private function createCate($v)
  217. {
  218. $db = Dever::db('cate', $this->app, false);
  219. $info = $db->find(array('key' => $v['key']));
  220. if (!$info) {
  221. $id = $db->insert($v);
  222. } else {
  223. $id = $info['id'];
  224. }
  225. return $id;
  226. }
  227. private function handleInfo($info)
  228. {
  229. $info = Dever::load('info', $this->app)->getInfo($info);
  230. $info['pic'] = explode(',', $info['pic']);
  231. $info['cdate_str'] = date('Y-m-d H:i:s', $info['cdate']);
  232. if (Place::$uid && Place::$user['client_id'] > 0) {
  233. $info['client'] = Dever::load('client', 'place')->get($info, $this->app, $this->type, '进货');
  234. }
  235. $info['price'] = Dever::load('price', 'place')->get($info, $this->app, $this->type, '下载');
  236. if (isset($info['content'])) {
  237. if (isset($info['password'])) {
  238. unset($info['password']);
  239. }
  240. # 查看详情信息
  241. if (Place::$uid) {
  242. $act = new Act('collect');
  243. $info['collect'] = 2;
  244. $collect = $act->getInfo();
  245. if ($collect) {
  246. $info['collect'] = 1;
  247. }
  248. }
  249. $info['content'] = htmlspecialchars_decode($info['content']);
  250. } else {
  251. $info['pic'] = $info['pic'][0] ?? '';
  252. }
  253. return $info;
  254. }
  255. }