Resource.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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' => '9',
  142. );
  143. } else {
  144. $where['status'] = array('in', $status);
  145. }
  146. }
  147. $data = Dever::db('order', $this->app)->select($where, $set);
  148. $result = array();
  149. if ($data) {
  150. foreach ($data as $k => $v) {
  151. $order = $this->getOrderInfo($v);
  152. if ($order) {
  153. $result[] = $order;
  154. }
  155. }
  156. }
  157. return $result;
  158. }
  159. # 获取订单详情
  160. public function getOrderView()
  161. {
  162. $where['uid'] = Place::$uid;
  163. $where['id'] = Dever::input('id', 'is_numeric', '订单');
  164. $order = Dever::db('order', $this->app)->find($where);
  165. if ($order) {
  166. $order = $this->getOrderInfo($order, true);
  167. } else {
  168. Dever::error('订单不存在');
  169. }
  170. return $order;
  171. }
  172. # 更新地址
  173. public function upOrderAddress()
  174. {
  175. $where['uid'] = Place::$uid;
  176. $where['id'] = Dever::input('id', 'is_numeric', '订单');
  177. $order = Dever::db('order', $this->app)->find($where);
  178. if ($order && $order['status'] <= 2) {
  179. $province = Dever::input('province', 'is_numeric', '省份');
  180. $city = Dever::input('city', 'is_numeric', '城市');
  181. $county = Dever::input('county', 'is_numeric', '区县');
  182. $address = Dever::input('address', 'is_string', '地址');
  183. $name = Dever::input('name', 'is_string', '联系人');
  184. $phone = Dever::input('phone', 'is_string', '联系方式');
  185. Dever::load('address', $this->app)->up($order['id'], $order['uid'], $order['address_id'], $name, $phone, $province . ',' . $city . ',' . $county, $address);
  186. } else {
  187. Dever::error('无法修改订单收货地址');
  188. }
  189. return $order;
  190. }
  191. # 取消订单
  192. public function upOrderCancel()
  193. {
  194. $where['uid'] = Place::$uid;
  195. $where['id'] = Dever::input('id', 'is_numeric', '订单');
  196. $order = Dever::db('order', $this->app)->find($where);
  197. if ($order && ($order['status'] == 1)) {
  198. Dever::load('order', $this->app)->cancel($order);
  199. } else {
  200. Dever::error('无法取消订单');
  201. }
  202. return $order;
  203. }
  204. # 确认收货
  205. public function upOrderFinish()
  206. {
  207. $where['uid'] = Place::$uid;
  208. $where['id'] = Dever::input('id', 'is_numeric', '订单');
  209. $order = Dever::db('order', $this->app)->find($where);
  210. if ($order && ($order['status'] == 3 || $order['status'] == 4)) {
  211. Dever::load('order', $this->app)->finish(1, $order);
  212. } else {
  213. Dever::error('无法确认收货');
  214. }
  215. return $order;
  216. }
  217. private function getOrderInfo($order, $view = false)
  218. {
  219. if ($order) {
  220. $info = Dever::db('info', $this->app)->find($order['info_id'], array('col' => 'id,name,pic,info'));
  221. $info['pic'] = explode(',', $info['pic']);
  222. $order['pic'] = $info['pic'][0] ?? '';
  223. $order['status_name'] = Dever::db('order', $this->app)->value('status', $order['status']);
  224. $order['cdate_str'] = date('Y-m-d H:i:s', $order['cdate']);
  225. if (isset($order['pdate']) && $order['pdate']) {
  226. $order['pdate_str'] = date('Y-m-d H:i:s', $order['pdate']);
  227. }
  228. if (isset($order['ddate']) && $order['ddate']) {
  229. $order['ddate_str'] = date('Y-m-d H:i:s', $order['ddate']);
  230. }
  231. if (isset($order['qdate']) && $order['qdate']) {
  232. $order['qdate_str'] = date('Y-m-d H:i:s', $order['qdate']);
  233. }
  234. if (isset($order['fdate']) && $order['fdate']) {
  235. $order['fdate_str'] = date('Y-m-d H:i:s', $order['fdate']);
  236. }
  237. $order = Dever::load('order', $this->app)->getInfo($order, $view);
  238. }
  239. return $order;
  240. }
  241. # 同步资源,关联资源
  242. public function relation($place_id, $data, $relation)
  243. {
  244. $where['channel_place_id'] = $place_id;
  245. $where['channel_type'] = $data['type'];
  246. $where['channel_type_id'] = $data['type_id'];
  247. $info = Dever::db('resource_relation', 'place')->find($where);
  248. if ($info) {
  249. if ($info['status'] == 2) {
  250. Dever::db('resource_relation', 'place')->update($info['id'], array('status' => 1));
  251. $update['source'] = 2;
  252. Dever::db('info', $this->app, false)->update($info['type_id'], $update);
  253. # 同步sku
  254. $this->relationSku($info['type_id'], $relation);
  255. }
  256. } else {
  257. $parent_id = $child_id = 0;
  258. foreach ($relation['cate'] as $k => $v) {
  259. unset($v['id']);
  260. unset($v['cdate']);
  261. if (!$parent_id) {
  262. $v['parent_id'] = 0;
  263. $parent_id = $this->createCate($v);
  264. } else {
  265. $v['parent_id'] = $parent_id;
  266. $child_id = $this->createCate($v);
  267. }
  268. }
  269. $cate[] = $relation['info']['cate_parent_id'] = $parent_id;
  270. if ($child_id) {
  271. $cate[] = $relation['info']['cate_child_id'] = $child_id;
  272. }
  273. $relation['info']['cate'] = implode(',', $cate);
  274. $relation['info']['source'] = 2;
  275. unset($relation['info']['id']);
  276. $info_id = Dever::db('info', $this->app, false)->insert($relation['info']);
  277. if ($info_id) {
  278. # 同步sku
  279. $this->relationSku($info_id, $relation);
  280. $where['type'] = $data['type'];
  281. $where['type_id'] = $info_id;
  282. $where['channel_price'] = $data['price'];
  283. $where['status'] = 1;
  284. Dever::db('resource_relation', 'place')->insert($where);
  285. }
  286. }
  287. }
  288. # 同步sku
  289. public function relationSku($info_id, $relation)
  290. {
  291. if (isset($relation['sku']) && $relation['sku']) {
  292. $spec_value = array();
  293. $spec_db = Dever::db('spec', $this->app, false);
  294. $spec_value_db = Dever::db('spec_value', $this->app, false);
  295. $sku_db = Dever::db('sku', $this->app, false);
  296. $spec_db->delete(array('info_id' => $info_id));
  297. $spec_value_db->delete(array('info_id' => $info_id));
  298. $sku_db->delete(array('info_id' => $info_id));
  299. foreach ($relation['spec'] as $v) {
  300. unset($v['id']);
  301. unset($v['cdate']);
  302. $v['info_id'] = $info_id;
  303. $spec_id = $spec_db->insert($v);
  304. foreach ($v['value'] as $v1) {
  305. $id = $v1['id'];
  306. unset($v1['id']);
  307. unset($v1['cdate']);
  308. $v1['info_id'] = $info_id;
  309. $v1['spec_id'] = $spec_id;
  310. $value_id = $spec_value_db->insert($v1);
  311. $spec_value[$id] = $value_id;
  312. }
  313. }
  314. foreach ($relation['sku'] as $v) {
  315. unset($v['id']);
  316. unset($v['cdate']);
  317. $key = explode(',', $v['key']);
  318. $v['key'] = array();
  319. foreach ($key as $v1) {
  320. if (isset($spec_value[$v1])) {
  321. $v['key'][] = $spec_value[$v1];
  322. }
  323. }
  324. $v['key'] = implode(',', $v['key']);
  325. $sku_db->insert($v);
  326. }
  327. }
  328. }
  329. # 删除关联关系
  330. public function relationDel($place_id, $data)
  331. {
  332. $where['channel_place_id'] = $place_id;
  333. $where['channel_type'] = $data['type'];
  334. $where['channel_type_id'] = $data['type_id'];
  335. $where['status'] = 1;
  336. $info = Dever::db('resource_relation', 'place')->find($where);
  337. if ($info) {
  338. Dever::db('resource_relation', 'place')->update($info['id'], array('status' => 2));
  339. Dever::db('info', $this->app, false)->update($info['type_id'], array('source' => 1));
  340. }
  341. }
  342. # 创建分类
  343. private function createCate($v)
  344. {
  345. $db = Dever::db('cate', $this->app, false);
  346. $info = $db->find(array('key' => $v['key']));
  347. if (!$info) {
  348. $id = $db->insert($v);
  349. } else {
  350. $id = $info['id'];
  351. }
  352. return $id;
  353. }
  354. private function handleInfo($info)
  355. {
  356. $info = Dever::load('info', $this->app)->getInfo($info);
  357. if (!$info) {
  358. return $info;
  359. }
  360. $info['pic'] = explode(',', $info['pic']);
  361. $info['cdate_str'] = date('Y-m-d H:i:s', $info['cdate']);
  362. if (Place::$uid && Place::$user['client_id'] > 0) {
  363. $info['client'] = Dever::load('client', 'place')->get($info, $this->app, $this->type, '进货');
  364. }
  365. $info['price'] = Dever::load('price', 'place')->get($info, $this->app, $this->type, '下载');
  366. if (isset($info['content'])) {
  367. if (isset($info['password'])) {
  368. unset($info['password']);
  369. }
  370. # 查看详情信息
  371. if (Place::$uid) {
  372. $act = new Act('collect', $this->type, $info['id']);
  373. $info['collect'] = 2;
  374. $collect = $act->getInfo();
  375. if ($collect) {
  376. $info['collect'] = 1;
  377. }
  378. }
  379. $info['content'] = htmlspecialchars_decode($info['content']);
  380. } else {
  381. $info['pic'] = $info['pic'][0] ?? '';
  382. }
  383. return $info;
  384. }
  385. }