Resource.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. if ($v) {
  51. $result[] = $v;
  52. }
  53. }
  54. return $result;
  55. }
  56. public function getInfo($id, $state = true)
  57. {
  58. if (!$id) {
  59. Dever::error('资源不存在');
  60. }
  61. $this->where['id'] = $id;
  62. $info = $this->db->find($this->where);
  63. if (!$info) {
  64. if ($state) {
  65. Dever::error('资源不存在');
  66. }
  67. return false;
  68. }
  69. return $this->handleInfo($info);
  70. }
  71. # 下单
  72. public function pay_commit(){}
  73. public function pay($info)
  74. {
  75. if (isset($info['stock']) && $info['num'] > $info['stock']) {
  76. Dever::error('库存不足');
  77. }
  78. $order = array();
  79. $order['uid'] = Place::$uid;
  80. $order['name'] = $info['name'];
  81. $prefix = strtoupper(substr($this->app, 0, 2));
  82. if ($this->type == 3) {
  83. $order['cash'] = $info['value'];
  84. $order['address_id'] = Dever::input('address_id', 'is_numeric', '收货地址');
  85. } else {
  86. $order['cash'] = ($info['value']/Place::$info['score_per']);
  87. }
  88. $order['order_num'] = Dever::load('util', 'api')->createNumber($prefix, $this->app . '/order');
  89. $order['notify'] = 'place/callback.resource|' . Dever::input('p') . '|' . $order['order_num'] . '|' . $this->app;
  90. $data['pay'] = Dever::load('account', 'place')->pay($order);
  91. #if ($data['pay'] && empty($data['pay']['link'])) {
  92. if ($data['pay']) {
  93. $order['status'] = 2;
  94. $order['info_id'] = $info['id'];
  95. $order['num'] = $info['num'];
  96. $data['order_id'] = Dever::db('order', $this->app)->insert($order);
  97. $data['order_num'] = $order['order_num'];
  98. if (isset($info['detail'])) {
  99. Dever::load('log', $this->app)->up(1, $order['uid'], $data['order_id'], '下单成功');
  100. $detail['order_id'] = $data['order_id'];
  101. foreach ($info['detail'] as $k => $v) {
  102. $detail['info_id'] = $v['id'];
  103. if (isset($v['sku_id'])) {
  104. $detail['sku_id'] = $v['sku_id'];
  105. }
  106. if (isset($v['sku_name'])) {
  107. $detail['sku_name'] = $v['sku_name'];
  108. }
  109. $detail['pic'] = $v['pic'];
  110. $detail['cash'] = $v['cash'];
  111. $detail['num'] = $v['num'] ?? $order['num'];
  112. $detail['name'] = $v['name'];
  113. $detail['pic'] = $v['pic'];
  114. Dever::db('order_detail', $this->app)->insert($detail);
  115. if (isset($v['stock'])) {
  116. $state = Dever::load('stock', 'goods')->sell($detail['info_id'], $detail['sku_id'], $detail['num']);
  117. if (!$state) {
  118. Dever::error('库存不足');
  119. }
  120. }
  121. if (isset($v['cart_id']) && $v['cart_id']) {
  122. Dever::db('cart', $this->app)->delete(array('id' => $v['cart_id'], 'uid' => Place::$uid));
  123. }
  124. }
  125. }
  126. }
  127. return $data;
  128. }
  129. # 获取订单列表
  130. public function getOrderList()
  131. {
  132. $where['uid'] = Place::$uid;
  133. $set['num'] = 10;
  134. $set['col'] = 'id,order_num,info_id,name,cash,num,cdate,status';
  135. $status = Dever::input('status');
  136. if ($status) {
  137. if ($status == 10) {
  138. $where['or'] = array
  139. (
  140. 'refund_status' => '1',
  141. 'status' => '8',
  142. );
  143. $where = ' uid = ' . $where['uid'] . ' and (refund_status = 1 or status = 8)';
  144. } else {
  145. $where['status'] = array('in', $status);
  146. }
  147. }
  148. $data = Dever::db('order', $this->app)->select($where, $set);
  149. $result = array();
  150. if ($data) {
  151. foreach ($data as $k => $v) {
  152. $order = $this->getOrderInfo($v);
  153. if ($order) {
  154. $result[] = $order;
  155. }
  156. }
  157. }
  158. return $result;
  159. }
  160. # 获取订单详情
  161. public function getOrderView()
  162. {
  163. $where['uid'] = Place::$uid;
  164. $where['id'] = Dever::input('id', 'is_numeric', '订单');
  165. $order = Dever::db('order', $this->app)->find($where);
  166. if ($order) {
  167. $order = $this->getOrderInfo($order, true);
  168. } else {
  169. Dever::error('订单不存在');
  170. }
  171. return $order;
  172. }
  173. # 更新地址
  174. public function upOrderAddress()
  175. {
  176. $where['uid'] = Place::$uid;
  177. $where['id'] = Dever::input('id', 'is_numeric', '订单');
  178. $order = Dever::db('order', $this->app)->find($where);
  179. if ($order && $order['status'] <= 2) {
  180. $province = Dever::input('province', 'is_numeric', '省份');
  181. $city = Dever::input('city', 'is_numeric', '城市');
  182. $county = Dever::input('county', 'is_numeric', '区县');
  183. $address = Dever::input('address', 'is_string', '地址');
  184. $name = Dever::input('name', 'is_string', '联系人');
  185. $phone = Dever::input('phone', 'is_string', '联系方式');
  186. Dever::load('address', $this->app)->up($order['id'], $order['uid'], $order['address_id'], $name, $phone, $province . ',' . $city . ',' . $county, $address);
  187. } else {
  188. Dever::error('无法修改订单收货地址');
  189. }
  190. return $order;
  191. }
  192. # 取消订单
  193. public function upOrderCancel()
  194. {
  195. $where['uid'] = Place::$uid;
  196. $where['id'] = Dever::input('id', 'is_numeric', '订单');
  197. $order = Dever::db('order', $this->app)->find($where);
  198. if ($order && ($order['status'] == 1)) {
  199. Dever::load('order', $this->app)->cancel($order);
  200. } else {
  201. Dever::error('无法取消订单');
  202. }
  203. return $order;
  204. }
  205. # 确认收货
  206. public function upOrderFinish()
  207. {
  208. $where['uid'] = Place::$uid;
  209. $where['id'] = Dever::input('id', 'is_numeric', '订单');
  210. $order = Dever::db('order', $this->app)->find($where);
  211. if ($order && ($order['status'] == 3 || $order['status'] == 4)) {
  212. Dever::load('order', $this->app)->finish(1, $order);
  213. } else {
  214. Dever::error('无法确认收货');
  215. }
  216. return $order;
  217. }
  218. private function getOrderInfo($order, $view = false)
  219. {
  220. if ($order) {
  221. $info = Dever::db('info', $this->app)->find($order['info_id'], array('col' => 'id,name,pic,info'));
  222. $info['pic'] = explode(',', $info['pic']);
  223. $order['pic'] = $info['pic'][0] ?? '';
  224. $order['status_name'] = Dever::db('order', $this->app)->value('status', $order['status']);
  225. $order['cdate_str'] = date('Y-m-d H:i:s', $order['cdate']);
  226. if (isset($order['pdate']) && $order['pdate']) {
  227. $order['pdate_str'] = date('Y-m-d H:i:s', $order['pdate']);
  228. }
  229. if (isset($order['ddate']) && $order['ddate']) {
  230. $order['ddate_str'] = date('Y-m-d H:i:s', $order['ddate']);
  231. }
  232. if (isset($order['qdate']) && $order['qdate']) {
  233. $order['qdate_str'] = date('Y-m-d H:i:s', $order['qdate']);
  234. }
  235. if (isset($order['fdate']) && $order['fdate']) {
  236. $order['fdate_str'] = date('Y-m-d H:i:s', $order['fdate']);
  237. }
  238. $order = Dever::load('order', $this->app)->getInfo($order, $view);
  239. }
  240. return $order;
  241. }
  242. # 同步资源,关联资源
  243. public function relation($place_id, $data, $relation)
  244. {
  245. $where['channel_place_id'] = $place_id;
  246. $where['channel_type'] = $data['type'];
  247. $where['channel_type_id'] = $data['type_id'];
  248. $info = Dever::db('resource_relation', 'place')->find($where);
  249. if ($info) {
  250. if ($info['status'] == 2) {
  251. Dever::db('resource_relation', 'place')->update($info['id'], array('status' => 1));
  252. $update['source'] = 2;
  253. Dever::db('info', $this->app, false)->update($info['type_id'], $update);
  254. # 同步sku
  255. $this->relationSku($info['type_id'], $relation);
  256. }
  257. } else {
  258. $parent_id = $child_id = 0;
  259. foreach ($relation['cate'] as $k => $v) {
  260. unset($v['id']);
  261. unset($v['cdate']);
  262. if (!$parent_id) {
  263. $v['parent_id'] = 0;
  264. $parent_id = $this->createCate($v);
  265. } else {
  266. $v['parent_id'] = $parent_id;
  267. $child_id = $this->createCate($v);
  268. }
  269. }
  270. $cate[] = $relation['info']['cate_parent_id'] = $parent_id;
  271. if ($child_id) {
  272. $cate[] = $relation['info']['cate_child_id'] = $child_id;
  273. }
  274. $relation['info']['pic'] = implode(',', $relation['info']['pic']);
  275. $relation['info']['cate'] = implode(',', $cate);
  276. $relation['info']['source'] = 2;
  277. unset($relation['info']['id']);
  278. $info_id = Dever::db('info', $this->app, false)->insert($relation['info']);
  279. if ($info_id) {
  280. # 同步sku
  281. $this->relationSku($info_id, $relation);
  282. $where['type'] = $data['type'];
  283. $where['type_id'] = $info_id;
  284. $where['channel_price'] = $data['price'];
  285. $where['status'] = 1;
  286. Dever::db('resource_relation', 'place')->insert($where);
  287. }
  288. }
  289. }
  290. # 同步sku
  291. public function relationSku($info_id, $relation)
  292. {
  293. if (isset($relation['sku']) && $relation['sku']) {
  294. $spec_value = array();
  295. $spec_db = Dever::db('spec', $this->app, false);
  296. $spec_value_db = Dever::db('spec_value', $this->app, false);
  297. $sku_db = Dever::db('sku', $this->app, false);
  298. $spec_db->delete(array('info_id' => $info_id));
  299. $spec_value_db->delete(array('info_id' => $info_id));
  300. $sku_db->delete(array('info_id' => $info_id));
  301. foreach ($relation['spec'] as $v) {
  302. unset($v['id']);
  303. unset($v['cdate']);
  304. $v['info_id'] = $info_id;
  305. $spec_id = $spec_db->insert($v);
  306. foreach ($v['value'] as $v1) {
  307. $id = $v1['id'];
  308. unset($v1['id']);
  309. unset($v1['cdate']);
  310. $v1['info_id'] = $info_id;
  311. $v1['spec_id'] = $spec_id;
  312. $value_id = $spec_value_db->insert($v1);
  313. $spec_value[$id] = $value_id;
  314. }
  315. }
  316. foreach ($relation['sku'] as $v) {
  317. unset($v['id']);
  318. unset($v['cdate']);
  319. $key = explode(',', $v['key']);
  320. $v['key'] = array();
  321. foreach ($key as $v1) {
  322. if (isset($spec_value[$v1])) {
  323. $v['key'][] = $spec_value[$v1];
  324. }
  325. }
  326. $v['key'] = implode(',', $v['key']);
  327. $sku_db->insert($v);
  328. }
  329. }
  330. }
  331. # 删除关联关系
  332. public function relationDel($place_id, $data)
  333. {
  334. $where['channel_place_id'] = $place_id;
  335. $where['channel_type'] = $data['type'];
  336. $where['channel_type_id'] = $data['type_id'];
  337. $where['status'] = 1;
  338. $info = Dever::db('resource_relation', 'place')->find($where);
  339. if ($info) {
  340. Dever::db('resource_relation', 'place')->update($info['id'], array('status' => 2));
  341. Dever::db('info', $this->app, false)->update($info['type_id'], array('source' => 1));
  342. }
  343. }
  344. # 创建分类
  345. private function createCate($v)
  346. {
  347. $db = Dever::db('cate', $this->app, false);
  348. $info = $db->find(array('name' => $v['name']));
  349. if (!$info) {
  350. $id = $db->insert($v);
  351. } else {
  352. $id = $info['id'];
  353. }
  354. return $id;
  355. }
  356. private function handleInfo($info)
  357. {
  358. $info = Dever::load('info', $this->app)->getInfo($info);
  359. if (!$info) {
  360. return $info;
  361. }
  362. $info['pic'] = explode(',', $info['pic']);
  363. $info['cdate_str'] = date('Y-m-d H:i:s', $info['cdate']);
  364. if (Place::$uid && Place::$user['client_id'] > 0) {
  365. $info['client'] = Dever::load('client', 'place')->get($info, $this->app, $this->type, '进货');
  366. }
  367. $info['price'] = Dever::load('price', 'place')->get($info, $this->app, $this->type, '下载');
  368. if (isset($info['content'])) {
  369. if (isset($info['password'])) {
  370. unset($info['password']);
  371. }
  372. # 查看详情信息
  373. if (Place::$uid) {
  374. $act = new Act('collect', $this->type, $info['id']);
  375. $info['collect'] = 2;
  376. $collect = $act->getInfo();
  377. if ($collect) {
  378. $info['collect'] = 1;
  379. }
  380. }
  381. $info['content'] = htmlspecialchars_decode($info['content']);
  382. } else {
  383. $info['pic'] = $info['pic'][0] ?? '';
  384. }
  385. return $info;
  386. }
  387. }