Buy.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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] >= 0 ? $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. /*
  380. # 退款涉及到了这些
  381. $order_card = Dever::db('card/order_card')->select(array('order_id' => $data['id']));
  382. if ($order_card) {
  383. foreach ($order_card as $k => $v) {
  384. Dever::db('card/order_card')->update(array('where_id' => $v['id'], 'status' => 3));
  385. }
  386. }
  387. $order_code = Dever::db('card/code')->select(array('order_id' => $data['id'], 'status' => 2));
  388. if ($order_code) {
  389. foreach ($order_code as $k => $v) {
  390. Dever::db('card/code')->update(array('where_id' => $v['id'], 'status' => 1, 'uid' => '0', 'order_id' => '', 'order_card_id' => '', 'bdate' => ''));
  391. }
  392. }
  393. */
  394. return 'ok';
  395. } else {
  396. Dever::alert('当前订单状态不允许取消');
  397. }
  398. }
  399. # 查看订单状态
  400. public function orderStatus($id)
  401. {
  402. $config = Dever::db('card/order')->config;
  403. $info = Dever::db('card/order')->one($id);
  404. $status = $config['status'][$info['status']];
  405. return $status;
  406. }
  407. # 收货
  408. public function send()
  409. {
  410. $order_id = Dever::input('order_id');
  411. $order = Dever::db('card/order')->find($order_id);
  412. if (!$order || ($order && $order['status'] != 3)) {
  413. Dever::alert('当前订单状态无法完成收货');
  414. }
  415. $state = Dever::db('card/order')->update(array('where_id' => $order['id'], 'status' => 4, 'fdate' => time()));
  416. if ($state) {
  417. $this->updatePs($order, 3);
  418. }
  419. return 'reload';
  420. }
  421. # 展示订单详情
  422. public function show()
  423. {
  424. $id = Dever::input('order_id');
  425. $config = Dever::db('card/order')->config;
  426. $info = Dever::db('card/order')->one($id);
  427. $status = $config['status'][$info['status']];
  428. $user = Dever::db('passport/user')->find($info['uid']);
  429. $cdate = date('Y-m-d H:i', $info['cdate']);
  430. if ($info['operdate']) {
  431. $opertime = date('Y-m-d H:i', $info['operdate']);
  432. } else {
  433. $opertime = '';
  434. }
  435. if ($info['fdate']) {
  436. $fdate = date('Y-m-d H:i', $info['fdate']);
  437. } else {
  438. $fdate = '';
  439. }
  440. if (!$user) {
  441. $user['username'] = '无';
  442. }
  443. if (!isset($user['truename'])) {
  444. $user['truename'] = $user['username'];
  445. }
  446. $time = array();
  447. $time[] = array('下单时间', $cdate);
  448. if ($opertime) {
  449. $time[] = array('操作时间', $opertime);
  450. }
  451. if ($fdate) {
  452. $time[] = array('完成时间', $fdate);
  453. }
  454. $result = array();
  455. $result[$info['order_num']] = array
  456. (
  457. 'type' => 'info',
  458. 'content' => array
  459. (
  460. array
  461. (
  462. array('下单用户', $user['username']),
  463. array('联系电话', $info['mobile']),
  464. array('订单状态', $status),
  465. ),
  466. $time,
  467. )
  468. );
  469. if ($info['address_id'] > 0) {
  470. $address = Dever::load('passport/address')->getOne($info['uid'], $info['address_id']);
  471. if ($address) {
  472. $result['收货地址'] = array
  473. (
  474. 'type' => 'info',
  475. 'content' => array
  476. (
  477. array
  478. (
  479. array('联系人', $address['contact']),
  480. array('联系电话', $address['mobile']),
  481. ),
  482. array
  483. (
  484. array('收货地址', $address['address']),
  485. array('门牌号', $address['house_number']),
  486. ),
  487. ),
  488. );
  489. }
  490. }
  491. $info['ps_info'] = Dever::db('card/order_ps')->find(array('order_id' => $info['id']));
  492. if ($info['ps_info']) {
  493. $info['ps_info']['service_name'] = '平台配送';
  494. $info['ps_info']['cdate'] = date('Y-m-d H:i', $info['ps_info']['cdate']);
  495. if ($info['ps_info']['qu_date']) {
  496. $info['ps_info']['qu_date'] = date('Y-m-d H:i', $info['ps_info']['qu_date']);
  497. } else {
  498. $info['ps_info']['qu_date'] = '无';
  499. }
  500. if ($info['ps_info']['qs_date']) {
  501. $info['ps_info']['qs_date'] = date('Y-m-d H:i', $info['ps_info']['qs_date']);
  502. } else {
  503. $info['ps_info']['qs_date'] = '无';
  504. }
  505. $status = Dever::db('card/order_ps')->config['status'];
  506. $info['ps_info']['status_name'] = $status[$info['ps_info']['status']];
  507. if ($info['ps_info']['service_id'] > 0) {
  508. $service = Dever::db('shop/service')->find($info['ps_info']['service_id']);
  509. $info['ps_info']['service_name'] = $service['name'];
  510. }
  511. $info['ps_info']['order_num'] = str_replace("\r\n", ',', $info['ps_info']['order_num']);
  512. $info['ps_info']['order_num'] .= '(<a href="https://www.kuaidi100.com/?from=openv" target="_blank">点此手动查单)</a>';
  513. $result['物流信息'] = array
  514. (
  515. 'type' => 'info',
  516. 'content' => array
  517. (
  518. array
  519. (
  520. array('名称', $info['ps_info']['service_name']),
  521. array('单号', $info['ps_info']['order_num']),
  522. array('费用', $info['ps_info']['price']),
  523. ),
  524. array
  525. (
  526. array('发货时间', $info['ps_info']['cdate']),
  527. array('取件时间', $info['ps_info']['qu_date']),
  528. array('签收时间', $info['ps_info']['qs_date']),
  529. ),
  530. ),
  531. );
  532. }
  533. $body = array();
  534. $body_total = array();
  535. $body_total['price'] = 0;
  536. $body_total['num'] = 0;
  537. $order_card = Dever::db('card/order_card')->select(array('order_id' => $info['id']));
  538. $order_card_status = Dever::db('card/order_card')->config['status'];
  539. foreach ($order_card as $k => $v) {
  540. $card = Dever::db('card/info')->find($v['card_id']);
  541. $card_type = Dever::db('card/type')->find($card['type_id']);
  542. $status = $order_card_status[$v['status']];
  543. $d = array
  544. (
  545. 'name' => $card_type['name'] . ' / ' . $card['name'],
  546. 'sku' => $v['cards'],
  547. 'price' => $v['price'],
  548. 'num' => $v['num'],
  549. 'status' => $status,
  550. );
  551. $body[] = $d;
  552. $price = $v['price']*$v['num'];
  553. $body_total['price'] += $price;
  554. $body_total['num'] += $v['num'];
  555. }
  556. if ($body) {
  557. $result['礼品卡清单'] = array
  558. (
  559. 'type' => 'list',
  560. 'content' => $body,
  561. 'total' => $body_total,
  562. );
  563. }
  564. $button = array();
  565. if ($info['status'] == 2) {
  566. $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');
  567. $button[] = array
  568. (
  569. 'type' => 'edit',
  570. 'link' => $url,
  571. 'name' => '发货',
  572. );
  573. } elseif ($info['status'] == 3) {
  574. $button[] = array
  575. (
  576. 'type' => 'action',
  577. 'link' => Dever::url('lib/manage.send?order_id=' . $info['id'], 'card'),
  578. 'name' => '确认收货',
  579. );
  580. }
  581. $head = array
  582. (
  583. 'name' => '基本信息',
  584. 'btn' => $button,
  585. );
  586. $html = Dever::show($head, $result);
  587. return $html;
  588. }
  589. }