Buy.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. <?php
  2. # 支付
  3. namespace Card\Lib;
  4. use Dever;
  5. class Buy
  6. {
  7. # 1是列表,2是详情
  8. public $view = 1;
  9. # 获取配置
  10. public $config = array();
  11. # table
  12. public $table = 'card/order';
  13. public function __construct()
  14. {
  15. $this->config = Dever::db($this->table)->config;
  16. }
  17. # 设置订单的类型
  18. public function set($view)
  19. {
  20. $this->view = $view;
  21. return $this;
  22. }
  23. # 获取公共的where
  24. public function where($id)
  25. {
  26. if (!$id) {
  27. Dever::alert('参数错误');
  28. }
  29. $where = array();
  30. $where['uid'] = $id;
  31. return $where;
  32. }
  33. # 获取订单列表
  34. public function getList($id)
  35. {
  36. $result = array();
  37. $where = $this->where($id);
  38. $status = Dever::input('status');
  39. if ($status) {
  40. $where['status'] = $status;
  41. }
  42. $order_num = Dever::input('order_num');
  43. if ($order_num) {
  44. $where['order_num'] = $order_num;
  45. }
  46. $result['search_value'] = $where;
  47. $result['search_value']['day'] = $day = Dever::input('day');
  48. if ($day) {
  49. $where['start'] = Dever::maketime($day . ' 00:00:00');
  50. $where['end'] = Dever::maketime($day . ' 23:59:59');
  51. }
  52. $result['search_value']['start'] = $start = Dever::input('start');
  53. $result['search_value']['end'] = $end = Dever::input('end');
  54. if ($start && $end) {
  55. $where['start'] = Dever::maketime($start);
  56. $where['end'] = Dever::maketime($end);
  57. }
  58. $excel = Dever::input('excel', 1);
  59. if ($excel == 2) {
  60. $result['order'] = Dever::db($this->table)->getData($where);
  61. } else {
  62. $result['order'] = Dever::db($this->table)->getAll($where);
  63. }
  64. if ($result['order']) {
  65. foreach ($result['order'] as $k => $v) {
  66. $result['order'][$k] = $this->getInfo($v);
  67. }
  68. }
  69. $result['search'] = array();
  70. $result['search']['status'] = $this->config['status'];
  71. $config = Dever::db('main/sell_config')->find();
  72. $result['card_name'] = $config['card_name'];
  73. return $result;
  74. }
  75. # 查看详情
  76. public function getView($id, $order_id, $show = true)
  77. {
  78. $where = $this->where($id);
  79. $where['id'] = $order_id;
  80. $result = Dever::db($this->table)->find($where);
  81. if (!$result) {
  82. Dever::alert('订单不存在');
  83. }
  84. if ($show) {
  85. $result = $this->getInfo($result, true);
  86. $config = Dever::db('main/sell_config')->find();
  87. $result['card_name'] = $config['card_name'];
  88. }
  89. return $result;
  90. }
  91. # 获取订单详细信息
  92. public function getInfo($info, $view = false)
  93. {
  94. if ($info['status'] == 1) {
  95. # 15分钟内支付,900秒
  96. $m = 900;
  97. # 支付倒计时
  98. $info['time'] = time() - $info['cdate'];
  99. if ($info['time'] >= $m) {
  100. # 已过期,自动取消
  101. $info['time'] = -1;
  102. Dever::db($this->table)->update(array('where_id' => $info['id'], 'status' => 5));
  103. $info['status'] = 5;
  104. } else {
  105. $info['time'] = $m - $info['time'];
  106. }
  107. }
  108. $info['status_name'] = $this->config['status'][$info['status']];
  109. $info['card'] = Dever::db('card/order_card')->select(array('order_id' => $info['id']));
  110. $info['cdate'] = date('Y-m-d H:i', $info['cdate']);
  111. foreach ($info['card'] as $k => $v) {
  112. $info['card'][$k]['info'] = Dever::db('card/info')->find($v['card_id']);
  113. $info['card'][$k]['type'] = Dever::db('card/type')->find($info['card'][$k]['info']['type_id']);
  114. }
  115. # 配送信息
  116. $info['ps_info'] = Dever::db('card/order_ps')->find(array('order_id' => $info['id']));
  117. if ($info['ps_info']) {
  118. $info['ps_info']['service_name'] = '平台配送';
  119. $info['ps_info']['cdate'] = date('Y-m-d H:i', $info['ps_info']['cdate']);
  120. if ($info['ps_info']['qu_date']) {
  121. $info['ps_info']['qu_date'] = date('Y-m-d H:i', $info['ps_info']['qu_date']);
  122. }
  123. if ($info['ps_info']['qs_date']) {
  124. $info['ps_info']['qs_date'] = date('Y-m-d H:i', $info['ps_info']['qs_date']);
  125. }
  126. $status = Dever::db('card/order_ps')->config['status'];
  127. $info['ps_info']['status_name'] = $status[$info['ps_info']['status']];
  128. if ($info['ps_info']['service_id'] > 0) {
  129. $service = Dever::db('shop/service')->find($info['ps_info']['service_id']);
  130. $info['ps_info']['service_name'] = $service['name'];
  131. }
  132. }
  133. if ($info['address_id']) {
  134. $info['address'] = Dever::db('passport/address')->find($info['address_id']);
  135. }
  136. return $info;
  137. }
  138. public function getCard(&$data)
  139. {
  140. $card_id = Dever::input('card_id');
  141. if (!$card_id) {
  142. Dever::alert('请传入礼品卡');
  143. }
  144. $card_id = explode(',', $card_id);
  145. $num = Dever::input('num');
  146. if (!$num) {
  147. Dever::alert('请传入数量');
  148. }
  149. $num = explode(',', $num);
  150. $city = Dever::input('city');
  151. if (!$city) {
  152. Dever::alert('请传入城市');
  153. }
  154. $city = explode(',', $city);
  155. $data['price'] = 0;
  156. $data['num'] = 0;
  157. $data['name'] = array();
  158. $data['type'] = $data['list'] = array();
  159. # 计算总价格
  160. foreach ($card_id as $k => $v) {
  161. $c = isset($city[$k]) ? $city[$k] : -1;
  162. $n = isset($num[$k]) ? $num[$k] : 1;
  163. $info = Dever::db('card/info')->find($v);
  164. if (!$info) {
  165. continue;
  166. }
  167. $type = Dever::db('card/type')->find($info['type_id']);
  168. if (!isset($data['type'][$info['type_id']])) {
  169. $data['type'][$info['type_id']] = $type;
  170. }
  171. $data['type'][$info['type_id']]['list'][$k] = $info;
  172. $data['type'][$info['type_id']]['list'][$k]['buy_num'] = $n;
  173. $data['type'][$info['type_id']]['list'][$k]['city'] = $c;
  174. $data['list'][$k] = $info;
  175. $data['list'][$k]['buy_num'] = $n;
  176. $data['list'][$k]['city'] = $c;
  177. $data['num'] += $n;
  178. $data['price'] += $data['list'][$k]['price'] * $n;
  179. $data['name'][] = $data['list'][$k]['name'];
  180. }
  181. $data['name'] = implode(',', $data['name']);
  182. $config = Dever::db('main/sell_config')->find();
  183. $data['card_name'] = $config['card_name'];
  184. }
  185. # 发起支付
  186. public function pay($user, $shop, $name, $num, $card, $price, $address_id, $system_source = 5, $refer = '')
  187. {
  188. if (!$user) {
  189. Dever::alert('错误的用户信息');
  190. }
  191. $uid = $user['id'];
  192. if ($uid > 0) {
  193. $wechat = Dever::db('passport/wechat')->one(array('uid' => $uid, 'type' => 1, 'system_id' => 1));
  194. if (!$wechat) {
  195. Dever::alert('错误的用户信息');
  196. }
  197. }
  198. $order_data['shop_id'] = $shop['id'];
  199. $order_data['uid'] = $uid;
  200. $order_data['mobile'] = $user['mobile'];
  201. $order_data['address_id'] = $address_id;
  202. $order_data['name'] = $name;
  203. $order_data['num'] = $num;
  204. $order_data['price'] = $price;
  205. $order_data['order_num'] = $this->getOrderId();
  206. $id = Dever::db('card/order')->insert($order_data);
  207. if (!$id) {
  208. Dever::alert('支付失败');
  209. }
  210. foreach($card as $k => $v) {
  211. $data['uid'] = $uid;
  212. $data['order_id'] = $id;
  213. $data['card_id'] = $v['id'];
  214. $data['city'] = $v['city'];
  215. $data['price'] = $v['price'];
  216. $data['num'] = $v['buy_num'];
  217. Dever::db('card/order_card')->insert($data);
  218. }
  219. $sell_config = Dever::db('main/sell_config')->one();
  220. $param = array
  221. (
  222. 'project_id' => 3,
  223. 'channel_id' => 1,
  224. 'system_source' => $system_source,
  225. 'uid' => $uid,
  226. 'name' => $order_data['name'],
  227. 'cash' => $price,
  228. 'product_id' => $id,
  229. 'order_id' => $order_data['order_num'],
  230. 'refer' => $refer,
  231. );
  232. if ($uid > 0 && isset($wechat) && $wechat) {
  233. $param['openid'] = $wechat['openid'];
  234. }
  235. $receipt = Dever::input('receipt');
  236. if ($receipt) {
  237. $param['receipt'] = $receipt;
  238. }
  239. //$result = Dever::load('pay/api.pay', $param);
  240. $result['type'] = 'test';
  241. $result['order'] = '';
  242. $result['order_num'] = $order_data['order_num'];
  243. $result['order_id'] = $id;
  244. return $result;
  245. }
  246. # 再次发起支付
  247. public function rpay($id, $system_source = 5, $refer = '')
  248. {
  249. $order = Dever::db('card/order')->find($id);
  250. if (!$order) {
  251. Dever::alert('订单信息错误');
  252. }
  253. if ($order['status'] > 1) {
  254. Dever::alert('订单已支付或已取消');
  255. }
  256. $param = array
  257. (
  258. 'project_id' => 3,
  259. 'channel_id' => 1,
  260. 'system_source' => $system_source,
  261. 'uid' => $order['uid'],
  262. 'name' => $order['price'],
  263. 'cash' => $order['price'],
  264. 'product_id' => $id,
  265. 'order_id' => $order['order_num'],
  266. 'refer' => $refer,
  267. );
  268. $receipt = Dever::input('receipt');
  269. if ($receipt) {
  270. $param['receipt'] = $receipt;
  271. }
  272. $result = Dever::load('pay/api.pay', $param);
  273. $result['order_num'] = $order['order_num'];
  274. $result['order_id'] = $id;
  275. return $result;
  276. }
  277. # 查询是否支付
  278. public function cpay($id, $system_source = 5, $refer = '')
  279. {
  280. $order = Dever::db('card/order')->find($id);
  281. if (!$order) {
  282. Dever::alert('订单信息错误');
  283. }
  284. $result['order_num'] = $order['order_num'];
  285. $result['order_id'] = $id;
  286. if ($order['status'] > 1 && $order['status'] <= 6) {
  287. # 已支付
  288. $result['status'] = 1;
  289. } else {
  290. # 待支付
  291. $result['status'] = 2;
  292. }
  293. return $result;
  294. }
  295. # 支付成功回调 安全加密 设置token
  296. public function success_secure_api_token()
  297. {
  298. $project_id = Dever::input('pay_project_id');
  299. $info = Dever::db('pay/project')->one($project_id);
  300. if ($info) {
  301. return $info['key'];
  302. }
  303. return 'card_buy_dever_2020';
  304. }
  305. # 支付成功回调 安全加密
  306. public function success_secure_api($param = array())
  307. {
  308. $this->success($param);
  309. }
  310. # 支付成功回调
  311. public function success($param = array())
  312. {
  313. $send = $param ? $param : Dever::preInput('pay_');
  314. $product_id = $send['pay_product_id'];
  315. $order_id = $send['pay_order_id'];
  316. $status = $send['pay_status'];
  317. $cash = $send['pay_cash'];
  318. $msg = $send['pay_msg'];
  319. $order = Dever::db('card/order')->one(array('id' => $product_id, 'time' => time()));
  320. if ($order && $order['status'] == 1 && $status == 2) {
  321. $update['status'] = 2;
  322. $update['where_id'] = $order['id'];
  323. Dever::db('card/order')->update($update);
  324. # 生成卡号,从现有的卡池里获取,如果没有则要生成
  325. Dever::load('card/lib/code')->assign($order['id']);
  326. }
  327. return 'ok';
  328. }
  329. # 生成订单号
  330. public function getOrderId()
  331. {
  332. $where['order_num'] = Dever::order('D');
  333. $state = Dever::db('card/order')->one($where);
  334. if (!$state) {
  335. return $where['order_num'];
  336. } else {
  337. return $this->getOrderId();
  338. }
  339. }
  340. # 添加配送信息
  341. public function updatePs($order, $status = 1)
  342. {
  343. Dever::config('base')->hook = true;
  344. $where['order_id'] = $order['id'];
  345. $data = Dever::db('card/order_ps')->find($where);
  346. if ($data) {
  347. $update['where_id'] = $data['id'];
  348. $update['status'] = $status;
  349. if ($status == 2) {
  350. $update['qu_date'] = time();
  351. }
  352. if ($status == 3) {
  353. $update['qs_date'] = time();
  354. }
  355. $state = Dever::db('card/order_ps')->update($update);
  356. }
  357. }
  358. # 确认收货
  359. public function finish($id, $order_id)
  360. {
  361. $data = $this->getView($id, $order_id, false);
  362. if ($data['status'] <= 3) {
  363. $status = 4;
  364. $state = Dever::db('card/order')->update(array('where_id' => $data['id'], 'status' => $status, 'fdate' => time()));
  365. if ($state) {
  366. $this->updatePs($data, 3);
  367. }
  368. return 'ok';
  369. } else {
  370. Dever::alert('您没有权限');
  371. }
  372. }
  373. # 取消订单
  374. public function cancel($id, $order_id)
  375. {
  376. $data = $this->getView($id, $order_id, false);
  377. if ($data['status'] == 1) {
  378. $state = Dever::db('card/order')->update(array('where_id' => $data['id'], 'status' => 5, 'operdate' => time()));
  379. return 'ok';
  380. } else {
  381. Dever::alert('当前订单状态不允许取消');
  382. }
  383. }
  384. # 查看订单状态
  385. public function orderStatus($id)
  386. {
  387. $config = Dever::db('card/order')->config;
  388. $info = Dever::db('card/order')->one($id);
  389. $status = $config['status'][$info['status']];
  390. return $status;
  391. }
  392. # 收货
  393. public function send()
  394. {
  395. $order_id = Dever::input('order_id');
  396. $order = Dever::db('card/order')->find($order_id);
  397. if (!$order || ($order && $order['status'] != 3)) {
  398. Dever::alert('当前订单状态无法完成收货');
  399. }
  400. $state = Dever::db('card/order')->update(array('where_id' => $order['id'], 'status' => 4, 'fdate' => time()));
  401. if ($state) {
  402. $this->updatePs($order, 3);
  403. }
  404. return 'reload';
  405. }
  406. # 展示订单详情
  407. public function show()
  408. {
  409. $id = Dever::input('order_id');
  410. $config = Dever::db('card/order')->config;
  411. $info = Dever::db('card/order')->one($id);
  412. $status = $config['status'][$info['status']];
  413. $user = Dever::db('passport/user')->find($info['uid']);
  414. $cdate = date('Y-m-d H:i', $info['cdate']);
  415. if ($info['operdate']) {
  416. $opertime = date('Y-m-d H:i', $info['operdate']);
  417. } else {
  418. $opertime = '';
  419. }
  420. if ($info['fdate']) {
  421. $fdate = date('Y-m-d H:i', $info['fdate']);
  422. } else {
  423. $fdate = '';
  424. }
  425. if (!$user) {
  426. $user['username'] = '无';
  427. }
  428. if (!isset($user['truename'])) {
  429. $user['truename'] = $user['username'];
  430. }
  431. $time = array();
  432. $time[] = array('下单时间', $cdate);
  433. if ($opertime) {
  434. $time[] = array('操作时间', $opertime);
  435. }
  436. if ($fdate) {
  437. $time[] = array('完成时间', $fdate);
  438. }
  439. $result = array();
  440. $result[$info['order_num']] = array
  441. (
  442. 'type' => 'info',
  443. 'content' => array
  444. (
  445. array
  446. (
  447. array('下单用户', $user['username']),
  448. array('联系电话', $info['mobile']),
  449. array('订单状态', $status),
  450. ),
  451. $time,
  452. )
  453. );
  454. if ($info['address_id'] > 0) {
  455. $address = Dever::load('passport/address')->getOne($info['uid'], $info['address_id']);
  456. if ($address) {
  457. $result['收货地址'] = array
  458. (
  459. 'type' => 'info',
  460. 'content' => array
  461. (
  462. array
  463. (
  464. array('联系人', $address['contact']),
  465. array('联系电话', $address['mobile']),
  466. ),
  467. array
  468. (
  469. array('收货地址', $address['address']),
  470. array('门牌号', $address['house_number']),
  471. ),
  472. ),
  473. );
  474. }
  475. }
  476. $info['ps_info'] = Dever::db('card/order_ps')->find(array('order_id' => $info['id']));
  477. if ($info['ps_info']) {
  478. $info['ps_info']['service_name'] = '平台配送';
  479. $info['ps_info']['cdate'] = date('Y-m-d H:i', $info['ps_info']['cdate']);
  480. if ($info['ps_info']['qu_date']) {
  481. $info['ps_info']['qu_date'] = date('Y-m-d H:i', $info['ps_info']['qu_date']);
  482. } else {
  483. $info['ps_info']['qu_date'] = '无';
  484. }
  485. if ($info['ps_info']['qs_date']) {
  486. $info['ps_info']['qs_date'] = date('Y-m-d H:i', $info['ps_info']['qs_date']);
  487. } else {
  488. $info['ps_info']['qs_date'] = '无';
  489. }
  490. $status = Dever::db('card/order_ps')->config['status'];
  491. $info['ps_info']['status_name'] = $status[$info['ps_info']['status']];
  492. if ($info['ps_info']['service_id'] > 0) {
  493. $service = Dever::db('shop/service')->find($info['ps_info']['service_id']);
  494. $info['ps_info']['service_name'] = $service['name'];
  495. }
  496. $info['ps_info']['order_num'] = str_replace("\r\n", ',', $info['ps_info']['order_num']);
  497. $info['ps_info']['order_num'] .= '(<a href="https://www.kuaidi100.com/?from=openv" target="_blank">点此手动查单)</a>';
  498. $result['物流信息'] = array
  499. (
  500. 'type' => 'info',
  501. 'content' => array
  502. (
  503. array
  504. (
  505. array('名称', $info['ps_info']['service_name']),
  506. array('单号', $info['ps_info']['order_num']),
  507. array('费用', $info['ps_info']['price']),
  508. ),
  509. array
  510. (
  511. array('发货时间', $info['ps_info']['cdate']),
  512. array('取件时间', $info['ps_info']['qu_date']),
  513. array('签收时间', $info['ps_info']['qs_date']),
  514. ),
  515. ),
  516. );
  517. }
  518. $body = array();
  519. $body_total = array();
  520. $body_total['price'] = 0;
  521. $body_total['num'] = 0;
  522. $order_card = Dever::db('card/order_card')->select(array('order_id' => $info['id']));
  523. $order_card_status = Dever::db('card/order_card')->config['status'];
  524. foreach ($order_card as $k => $v) {
  525. $card = Dever::db('card/info')->find($v['card_id']);
  526. $card_type = Dever::db('card/type')->find($card['type_id']);
  527. $status = $order_card_status[$v['status']];
  528. $d = array
  529. (
  530. 'name' => $card_type['name'] . ' / ' . $card['name'],
  531. 'sku' => $v['cards'],
  532. 'price' => $v['price'],
  533. 'num' => $v['num'],
  534. 'status' => $status,
  535. );
  536. $body[] = $d;
  537. $price = $v['price']*$v['num'];
  538. $body_total['price'] += $price;
  539. $body_total['num'] += $v['num'];
  540. }
  541. if ($body) {
  542. $result['礼品卡清单'] = array
  543. (
  544. 'type' => 'list',
  545. 'content' => $body,
  546. 'total' => $body_total,
  547. );
  548. }
  549. $button = array();
  550. if ($info['status'] == 2) {
  551. $url = Dever::load('manage/database')->url('edit', $info['id'], 'order_ps&where_id='.$info['id'].'&search_option_order_id='.$info['id'].'&oper_save_jump=order&oper_table=order&oper_parent=order');
  552. $button[] = array
  553. (
  554. 'type' => 'edit',
  555. 'link' => $url,
  556. 'name' => '发货',
  557. );
  558. } elseif ($info['status'] == 3) {
  559. $button[] = array
  560. (
  561. 'type' => 'action',
  562. 'link' => Dever::url('lib/manage.send?order_id=' . $info['id'], 'card'),
  563. 'name' => '确认收货',
  564. );
  565. }
  566. $head = array
  567. (
  568. 'name' => '基本信息',
  569. 'btn' => $button,
  570. );
  571. $html = Dever::show($head, $result);
  572. return $html;
  573. }
  574. }