Order.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. <?php
  2. namespace app\admin\controller\unishop;
  3. use app\admin\model\unishop\Area;
  4. use app\admin\model\unishop\OrderRefund;
  5. use app\common\controller\Backend;
  6. use think\Db;
  7. use think\Exception;
  8. use think\exception\PDOException;
  9. use think\exception\ValidateException;
  10. use think\Hook;
  11. use addons\epay\library\Service;
  12. /**
  13. * 订单管理
  14. *
  15. * @icon fa fa-circle-o
  16. */
  17. class Order extends Backend
  18. {
  19. /**
  20. * 是否是关联查询
  21. */
  22. protected $relationSearch = true;
  23. /**
  24. * Order模型对象
  25. * @var \app\admin\model\unishop\Order
  26. */
  27. protected $model = null;
  28. public function _initialize()
  29. {
  30. parent::_initialize();
  31. $this->model = new \app\admin\model\unishop\Order;
  32. $this->view->assign("payTypeList", $this->model->getPayTypeList());
  33. $this->view->assign("statusList", $this->model->getStatusList());
  34. $this->view->assign("refundStatusList", $this->model->getRefundStatusList());
  35. }
  36. /**
  37. * 查看
  38. */
  39. public function index()
  40. {
  41. //设置过滤方法
  42. $this->request->filter(['strip_tags']);
  43. if ($this->request->isAjax()) {
  44. //如果发送的来源是Selectpage,则转发到Selectpage
  45. if ($this->request->request('keyField')) {
  46. return $this->selectpage();
  47. }
  48. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  49. $total = $this->model
  50. ->alias('order')
  51. ->join('user', 'user.id = order.user_id')
  52. ->where($where)
  53. ->count();
  54. $list = $this->model
  55. ->alias('order')
  56. ->join('user', 'user.id = order.user_id')
  57. ->where($where)
  58. ->order($sort, $order)
  59. ->limit($offset, $limit)
  60. ->field('order.*,user.username')
  61. ->select();
  62. $list = collection($list)->toArray();
  63. foreach ($list as &$item) {
  64. $item['id'] = (string)$item['id']; // 整形数字太大js会失准
  65. $item['user'] = [];
  66. $item['user']['username'] = $item['username'] ? $item['username'] : __('Tourist');
  67. $item['have_paid_status'] = $item['have_paid'];
  68. $item['have_delivered_status'] = $item['have_delivered'];
  69. $item['have_received_status'] = $item['have_received'];
  70. $item['have_commented_status'] = $item['have_commented'];
  71. }
  72. $result = array("total" => $total, "rows" => $list);
  73. return json($result);
  74. }
  75. return $this->view->fetch();
  76. }
  77. /**
  78. * 生成查询所需要的条件,排序方式
  79. * @param mixed $searchfields 快速查询的字段
  80. * @param boolean $relationSearch 是否关联查询
  81. * @return array
  82. */
  83. protected function buildparams($searchfields = null, $relationSearch = null)
  84. {
  85. $searchfields = is_null($searchfields) ? $this->searchFields : $searchfields;
  86. $relationSearch = is_null($relationSearch) ? $this->relationSearch : $relationSearch;
  87. $search = $this->request->get("search", '');
  88. $filter = $this->request->get("filter", '');
  89. $op = $this->request->get("op", '', 'trim');
  90. $sort = $this->request->get("sort", "id");
  91. $order = $this->request->get("order", "DESC");
  92. $offset = $this->request->get("offset", 0);
  93. $limit = $this->request->get("limit", 0);
  94. $filter = (array)json_decode($filter, true);
  95. $op = (array)json_decode($op, true);
  96. $filter = $filter ? $filter : [];
  97. $where = [];
  98. $tableName = '';
  99. if ($relationSearch) {
  100. if (!empty($this->model)) {
  101. $name = \think\Loader::parseName(basename(str_replace('\\', '/', get_class($this->model))));
  102. $tableName = '' . $name . '.';
  103. }
  104. $sortArr = explode(',', $sort);
  105. foreach ($sortArr as $index => & $item) {
  106. $item = stripos($item, ".") === false ? $tableName . trim($item) : $item;
  107. }
  108. unset($item);
  109. $sort = implode(',', $sortArr);
  110. }
  111. $adminIds = $this->getDataLimitAdminIds();
  112. if (is_array($adminIds)) {
  113. $where[] = [$tableName . $this->dataLimitField, 'in', $adminIds];
  114. }
  115. if ($search) {
  116. $searcharr = is_array($searchfields) ? $searchfields : explode(',', $searchfields);
  117. foreach ($searcharr as $k => &$v) {
  118. $v = stripos($v, ".") === false ? $tableName . $v : $v;
  119. }
  120. unset($v);
  121. $where[] = [implode("|", $searcharr), "LIKE", "%{$search}%"];
  122. }
  123. foreach ($filter as $k => $v) {
  124. // 搜索订单状态
  125. if (in_array($k, ['have_paid_status', 'have_delivered_status', 'have_received_status', 'have_commented_status'])) {
  126. switch ($k) {
  127. case 'have_paid_status':
  128. $k = 'have_paid';
  129. break;
  130. case 'have_delivered_status':
  131. $k = 'have_delivered';
  132. break;
  133. case 'have_received_status':
  134. $k = 'have_received';
  135. break;
  136. case 'have_commented_status':
  137. $k = 'have_commented';
  138. break;
  139. }
  140. $v == 0 ? ($op[$k] = '=') : ($op[$k] = '>');
  141. $v = 0;
  142. }
  143. $sym = isset($op[$k]) ? $op[$k] : '=';
  144. if (stripos($k, ".") === false) {
  145. $k = $tableName . $k;
  146. }
  147. $v = !is_array($v) ? trim($v) : $v;
  148. $sym = strtoupper(isset($op[$k]) ? $op[$k] : $sym);
  149. switch ($sym) {
  150. case '=':
  151. case '<>':
  152. $where[] = [$k, $sym, (string)$v];
  153. break;
  154. case 'LIKE':
  155. case 'NOT LIKE':
  156. case 'LIKE %...%':
  157. case 'NOT LIKE %...%':
  158. $where[] = [$k, trim(str_replace('%...%', '', $sym)), "%{$v}%"];
  159. break;
  160. case '>':
  161. case '>=':
  162. case '<':
  163. case '<=':
  164. $where[] = [$k, $sym, intval($v)];
  165. break;
  166. case 'FINDIN':
  167. case 'FINDINSET':
  168. case 'FIND_IN_SET':
  169. $where[] = "FIND_IN_SET('{$v}', " . ($relationSearch ? $k : '`' . str_replace('.', '`.`', $k) . '`') . ")";
  170. break;
  171. case 'IN':
  172. case 'IN(...)':
  173. case 'NOT IN':
  174. case 'NOT IN(...)':
  175. $where[] = [$k, str_replace('(...)', '', $sym), is_array($v) ? $v : explode(',', $v)];
  176. break;
  177. case 'BETWEEN':
  178. case 'NOT BETWEEN':
  179. $arr = array_slice(explode(',', $v), 0, 2);
  180. if (stripos($v, ',') === false || !array_filter($arr)) {
  181. continue 2;
  182. }
  183. //当出现一边为空时改变操作符
  184. if ($arr[0] === '') {
  185. $sym = $sym == 'BETWEEN' ? '<=' : '>';
  186. $arr = $arr[1];
  187. } elseif ($arr[1] === '') {
  188. $sym = $sym == 'BETWEEN' ? '>=' : '<';
  189. $arr = $arr[0];
  190. }
  191. $where[] = [$k, $sym, $arr];
  192. break;
  193. case 'RANGE':
  194. case 'NOT RANGE':
  195. $v = str_replace(' - ', ',', $v);
  196. $arr = array_slice(explode(',', $v), 0, 2);
  197. if (stripos($v, ',') === false || !array_filter($arr)) {
  198. continue 2;
  199. }
  200. //当出现一边为空时改变操作符
  201. if ($arr[0] === '') {
  202. $sym = $sym == 'RANGE' ? '<=' : '>';
  203. $arr = $arr[1];
  204. } elseif ($arr[1] === '') {
  205. $sym = $sym == 'RANGE' ? '>=' : '<';
  206. $arr = $arr[0];
  207. }
  208. $where[] = [$k, str_replace('RANGE', 'BETWEEN', $sym) . ' time', $arr];
  209. break;
  210. case 'LIKE':
  211. case 'LIKE %...%':
  212. $where[] = [$k, 'LIKE', "%{$v}%"];
  213. break;
  214. case 'NULL':
  215. case 'IS NULL':
  216. case 'NOT NULL':
  217. case 'IS NOT NULL':
  218. $where[] = [$k, strtolower(str_replace('IS ', '', $sym))];
  219. break;
  220. default:
  221. break;
  222. }
  223. }
  224. $where = function ($query) use ($where) {
  225. foreach ($where as $k => $v) {
  226. if (is_array($v)) {
  227. call_user_func_array([$query, 'where'], $v);
  228. } else {
  229. $query->where($v);
  230. }
  231. }
  232. };
  233. return [$where, $sort, $order, $offset, $limit];
  234. }
  235. /**
  236. * 编辑
  237. */
  238. public function edit($ids = null)
  239. {
  240. $row = $this->model->get($ids);
  241. if (!$row) {
  242. $this->error(__('No Results were found'));
  243. }
  244. $adminIds = $this->getDataLimitAdminIds();
  245. if (is_array($adminIds)) {
  246. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  247. $this->error(__('You have no permission'));
  248. }
  249. }
  250. if ($this->request->isPost()) {
  251. $params = $this->request->post("row/a");
  252. if ($params) {
  253. $params = $this->preExcludeFields($params);
  254. $result = false;
  255. Db::startTrans();
  256. try {
  257. //是否采用模型验证
  258. if ($this->modelValidate) {
  259. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  260. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  261. $row->validateFailException(true)->validate($validate);
  262. }
  263. $updatetime = $this->request->post('updatetime');
  264. // 乐观锁
  265. $result = $this->model->allowField(true)->save($params, ['id' => $ids, 'updatetime' => $updatetime]);
  266. if (!$result) {
  267. throw new Exception(__('Data had been update before saved, close windows and do it again'));
  268. }
  269. Db::commit();
  270. } catch (ValidateException $e) {
  271. Db::rollback();
  272. $this->error($e->getMessage());
  273. } catch (PDOException $e) {
  274. Db::rollback();
  275. $this->error($e->getMessage());
  276. } catch (Exception $e) {
  277. Db::rollback();
  278. $this->error($e->getMessage());
  279. }
  280. if ($result !== false) {
  281. $this->success();
  282. } else {
  283. $this->error(__('No rows were updated'));
  284. }
  285. }
  286. $this->error(__('Parameter %s can not be empty', ''));
  287. }
  288. $this->view->assign("row", $row);
  289. return $this->view->fetch();
  290. }
  291. /**
  292. * 物流管理
  293. */
  294. public function delivery($ids = null)
  295. {
  296. $row = $this->model->get($ids, ['extend']);
  297. if (!$row) {
  298. $this->error(__('No Results were found'));
  299. }
  300. $adminIds = $this->getDataLimitAdminIds();
  301. if (is_array($adminIds)) {
  302. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  303. $this->error(__('You have no permission'));
  304. }
  305. }
  306. if ($this->request->isPost()) {
  307. $result = false;
  308. Db::startTrans();
  309. try {
  310. $express_number = $this->request->post('express_number','');
  311. $express_company = $this->request->post('express_company','');
  312. // $have_delivered = $express_number ? time() : 0;
  313. $have_delivered = time();
  314. $res1 = $row->allowField(true)->save(['have_delivered' => $have_delivered]);
  315. $res2 = $row->extend->allowField(true)->save(['express_number' => $express_number, 'express_company' => $express_company]);
  316. if ($res1 !== false && $res2 !== false) {
  317. $result = true;
  318. } else {
  319. throw new Exception(__('No rows were updated'));
  320. }
  321. Db::commit();
  322. } catch (ValidateException $e) {
  323. Db::rollback();
  324. $this->error($e->getMessage());
  325. } catch (PDOException $e) {
  326. Db::rollback();
  327. $this->error($e->getMessage());
  328. } catch (Exception $e) {
  329. Db::rollback();
  330. $this->error($e->getMessage());
  331. }
  332. if ($result !== false) {
  333. $this->success();
  334. } else {
  335. $this->error(__('No rows were updated'));
  336. }
  337. $this->error(__('Parameter %s can not be empty', ''));
  338. }
  339. $address = json_decode($row->extend->address_json,true);
  340. if ($address) {
  341. // $area = (new Area)->whereIn('id',[$address['province_id'],$address['city_id'],$address['area_id']])->column('name', 'id');
  342. // $row['addressText'] = $area[$address['province_id']].$area[$address['city_id']].$area[$address['area_id']].' '.$address['address'];
  343. $row['addressText'] = $address['province_name'].$address['city_name'].$address['area_name'].$address['address'];
  344. $row['address'] = $address;
  345. }
  346. $this->view->assign("row", $row);
  347. // 快递公司
  348. if (!class_exists(\addons\expressquery\library\Expressquery::class)) {
  349. $expressInfo = array_merge(['' => '请先安装插件《物流信息接口》']);
  350. } else {
  351. $expressInfo = Db::name('expressquery')->column('name', 'express');
  352. $expressInfo = $expressInfo ?? [];
  353. $expressInfo = array_merge(['' => '请选择快递公司'], $expressInfo);
  354. }
  355. $this->view->assign('expressCompany', $expressInfo);
  356. return $this->view->fetch();
  357. }
  358. /**
  359. * 商品管理
  360. */
  361. public function product($ids = null)
  362. {
  363. if ($this->request->isPost()) {
  364. $this->success();
  365. }
  366. $row = $this->model->get($ids, ['product','evaluate','extend']);
  367. $this->view->assign('product', $row->product);
  368. $evaluate = [];
  369. foreach ($row->evaluate as $key => $item) {
  370. $evaluate[$item['product_id']] = $item;
  371. }
  372. //地址
  373. $row['addressText'] = '';
  374. $address = json_decode($row->extend->address_json,true);
  375. if ($address) {
  376. $row['addressText'] = $address['province_name'].$address['city_name'].$address['area_name'].$address['address'];
  377. $row['address'] = $address;
  378. }
  379. $this->view->assign('order', $row);
  380. $this->view->assign('evaluate', $evaluate);
  381. return $this->view->fetch();
  382. }
  383. /**
  384. * 退货管理
  385. */
  386. public function refund($ids = null)
  387. {
  388. $row = $this->model->get($ids, ['refund']);
  389. if ($row['status'] != \app\admin\model\unishop\Order::STATUS_REFUND) {
  390. $this->error(__('This order is not returned'));
  391. }
  392. if ($this->request->isPost()) {
  393. Db::startTrans();
  394. try {
  395. $updatetime = $this->request->post('updatetime');
  396. $refund_amount = $this->request->post('refund_amount');
  397. $refund_status = $this->request->post('refund_status');
  398. if ($refund_amount > $row['total_price']) {
  399. $this->error('退款金额不能大于实际支付金额');
  400. }
  401. // 退款
  402. $params = [
  403. 'refund_status' => $refund_status,
  404. 'updatetime' => time(),
  405. 'refund_amount' => $refund_amount,
  406. ];
  407. if($refund_status == 3) {
  408. $params['had_refund'] = time();
  409. }
  410. // 乐观锁
  411. $result = Db::name('unishop_order')->where(['id' => $ids, 'updatetime' => $updatetime])->update($params);
  412. if (!$result) {
  413. throw new Exception(__('Data had been update before saved, close windows and do it again'));
  414. }
  415. //修改退款金额
  416. $rs2 = Db::name('unishop_order_refund')->where('order_id',$ids)->update(['amount'=>$refund_amount]);
  417. if ($rs2 === false) {
  418. throw new Exception('操作失败');
  419. }
  420. //同意并执行退款
  421. if($refund_status == 3 && $refund_amount > 0){
  422. $order = Db::name('unishop_order')->where('id',$ids)->find();
  423. if($order['pay_type'] == 2){
  424. $wallet_rs = model('wallet')->lockChangeAccountRemain($order['user_id'],'money',$refund_amount,32,'商城售后订单退款'.$order['out_trade_no'],'unishop_order',$ids);
  425. if($wallet_rs['status'] === false){
  426. throw new Exception($wallet_rs['msg']);
  427. }
  428. }elseif($order['pay_type'] == 3 || $order['pay_type'] == 4){
  429. $refund_result = $this->old_refund($order,$refund_amount);
  430. if($refund_result !== true){
  431. throw new Exception($refund_result);
  432. }
  433. }
  434. }
  435. Db::commit();
  436. } catch (ValidateException $e) {
  437. Db::rollback();
  438. $this->error($e->getMessage());
  439. } catch (PDOException $e) {
  440. Db::rollback();
  441. $this->error($e->getMessage());
  442. } catch (Exception $e) {
  443. Db::rollback();
  444. $this->error($e->getMessage());
  445. }
  446. $this->success();
  447. }
  448. $products = $row->product;
  449. $refundProducts = $row->refundProduct;
  450. foreach ($products as &$product) {
  451. $product['choose'] = 0;
  452. foreach ($refundProducts as $refundProduct) {
  453. if ($product['id'] == $refundProduct['order_product_id']) {
  454. $product['choose'] = 1;
  455. }
  456. }
  457. }
  458. if ($row->refund) {
  459. $refund = $row->refund->append(['receiving_status_text', 'service_type_text'])->toArray();
  460. } else {
  461. $refund = [
  462. 'service_type' => 0,
  463. 'express_number' => -1,
  464. 'receiving_status_text' => -1,
  465. 'receiving_status' => -1,
  466. 'service_type_text' => -1,
  467. 'amount' => -1,
  468. 'reason_type' => -1,
  469. 'refund_explain' => -1,
  470. ];
  471. }
  472. $this->view->assign('row', $row);
  473. $this->view->assign('product', $products);
  474. $this->view->assign('refund', $refund);
  475. return $this->view->fetch();
  476. }
  477. /**
  478. * 已支付未发货退款
  479. */
  480. public function cancelrefund($ids = null)
  481. {
  482. $row = $this->model->get($ids);
  483. if ($row['status'] != \app\admin\model\unishop\Order::STATUS_CANCEL || $row['have_paid'] == 0 || $row['have_delivered'] != 0) {
  484. $this->error('订单状态错误');
  485. }
  486. if ($this->request->isPost()) {
  487. Db::startTrans();
  488. try {
  489. $updatetime = $this->request->post('updatetime');
  490. $refund_amount = $this->request->post('refund_amount');
  491. $refund_status = $this->request->post('refund_status');
  492. if ($refund_amount > $row['total_price']) {
  493. $this->error('退款金额不能大于实际支付金额');
  494. }
  495. // 退款
  496. $params = [
  497. 'refund_status' => $refund_status,
  498. 'updatetime' => time(),
  499. 'refund_amount' => $refund_amount,
  500. ];
  501. if($refund_status == 3) {
  502. $params['had_refund'] = time();
  503. }
  504. // 乐观锁
  505. $result = Db::name('unishop_order')->where(['id' => $ids, 'updatetime' => $updatetime])->update($params);
  506. if (!$result) {
  507. throw new Exception(__('Data had been update before saved, close windows and do it again'));
  508. }
  509. //修改退款金额
  510. /*$rs2 = Db::name('unishop_order_refund')->where('order_id',$ids)->update(['amount'=>$refund_amount]);
  511. if ($rs2 === false) {
  512. throw new Exception('操作失败');
  513. }*/
  514. //同意并执行退款
  515. if($refund_status == 3 && $refund_amount > 0){
  516. $order = Db::name('unishop_order')->where('id',$ids)->find();
  517. if($order['pay_type'] == 2){
  518. $wallet_rs = model('wallet')->lockChangeAccountRemain($order['user_id'],'money',$refund_amount,32,'商城取消订单退款'.$order['out_trade_no'],'unishop_order',$ids);
  519. if($wallet_rs['status'] === false){
  520. throw new Exception($wallet_rs['msg']);
  521. }
  522. }elseif($order['pay_type'] == 3 || $order['pay_type'] == 4){
  523. $refund_result = $this->old_refund($order,$refund_amount);
  524. if($refund_result !== true){
  525. throw new Exception($refund_result);
  526. }
  527. }
  528. }
  529. Db::commit();
  530. } catch (ValidateException $e) {
  531. Db::rollback();
  532. $this->error($e->getMessage());
  533. } catch (PDOException $e) {
  534. Db::rollback();
  535. $this->error($e->getMessage());
  536. } catch (Exception $e) {
  537. Db::rollback();
  538. $this->error($e->getMessage());
  539. }
  540. $this->success();
  541. }
  542. $this->view->assign('row', $row);
  543. return $this->view->fetch();
  544. }
  545. // 退款
  546. public function old_refund($order, $refund_price)
  547. {
  548. $table = 'unishop_order';
  549. $remark = '订单退款';
  550. if($order['pay_type'] == 3){
  551. $order['pay_type'] = 'wechat';
  552. }
  553. if($order['pay_type'] == 4){
  554. $order['pay_type'] = 'alipay';
  555. }
  556. // 生成退款单
  557. $refund_data = [
  558. 'order_id' => $order['id'],
  559. 'out_refund_no'=> createUniqueNo('R'),
  560. 'pay_fee' => $order['total_price'],
  561. 'refund_price' => $refund_price,
  562. 'pay_type' => $order['pay_type'],
  563. 'status' => 0,
  564. 'createtime' => time(),
  565. 'updatetime' => time(),
  566. 'table' => $table,
  567. 'table_id' => $order['id'],
  568. ];
  569. $refund_log_id = Db::name('pay_order_refund_log')->insertGetId($refund_data);
  570. if(!$refund_log_id){
  571. return '退款失败';
  572. }
  573. if ($order['pay_type'] == 'wechat' || $order['pay_type'] == 'alipay') {
  574. // 微信|支付宝退款
  575. // 退款数据
  576. $order_data = [
  577. 'out_trade_no' => $order['pay_out_trade_no']
  578. ];
  579. if ($order['pay_type'] == 'wechat') {
  580. $total_fee = $order['total_price'] * 100;
  581. $refund_fee = $refund_price * 100;
  582. $order_data = array_merge($order_data, [
  583. 'out_refund_no' => $refund_data['out_refund_no'],
  584. 'total_fee' => $total_fee,
  585. 'refund_fee' => $refund_fee,
  586. 'refund_desc' => $remark,
  587. ]);
  588. } else {
  589. $order_data = array_merge($order_data, [
  590. 'out_request_no' => $refund_data['out_refund_no'],
  591. 'refund_amount' => $refund_price,
  592. ]);
  593. }
  594. //
  595. if ($order['pay_type'] == 'wechat') {
  596. $wxpay = new \app\common\library\Wxpay;
  597. $result = $wxpay->WxPayRefund($order_data);
  598. // 微信通知回调 pay->notifyr
  599. if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
  600. Db::name('pay_order_refund_log')->where('id',$refund_log_id)->update(['status'=>1]);
  601. return true;
  602. } else {
  603. return $result['return_msg'];
  604. }
  605. } else {
  606. $result = Service::submitRefund($order['total_price'],$refund_price,$order['pay_out_trade_no'],$refund_data['out_refund_no'],$order['pay_type'],$remark,'');
  607. if($result['code'] == '10000'){
  608. Db::name('pay_order_refund_log')->where('id',$refund_log_id)->update(['status'=>1]);
  609. return true;
  610. }else{
  611. return $result['msg'];
  612. }
  613. /* return 'alipay wrong way';
  614. $alipay = new \app\common\library\AliPay;
  615. $result = $alipay->AliPayRefund($order_data);
  616. // 支付宝通知回调 pay->notifyx
  617. return $result;*/
  618. /*if ($result['code'] == "10000") {
  619. return true;
  620. } else {
  621. throw new \Exception($result['msg']);
  622. }*/
  623. }
  624. // { // 微信返回结果
  625. // "return_code":"SUCCESS",
  626. // "return_msg":"OK",
  627. // "appid":"wx39cd0799d4567dd0",
  628. // "mch_id":"1481069012",
  629. // "nonce_str":"huW9eIAb5BDPn0Ma",
  630. // "sign":"250316740B263FE53F5DFF50AF5A8FA1",
  631. // "result_code":"SUCCESS",
  632. // "transaction_id":"4200000497202004072822298902",
  633. // "out_trade_no":"202010300857029180027000",
  634. // "out_refund_no":"1586241595",
  635. // "refund_id":"50300603862020040700031444448",
  636. // "refund_channel":[],
  637. // "refund_fee":"1",
  638. // "coupon_refund_fee":"0",
  639. // "total_fee":"1",
  640. // "cash_fee":"1",
  641. // "coupon_refund_count":"0",
  642. // "cash_refund_fee":"1
  643. // }
  644. // { // 支付宝返回结果
  645. // "code": "10000",
  646. // "msg": "Success",
  647. // "buyer_logon_id": "157***@163.com",
  648. // "buyer_user_id": "2088902485164146",
  649. // "fund_change": "Y",
  650. // "gmt_refund_pay": "2020-08-15 16:11:45",
  651. // "out_trade_no": "202002460317545607015300",
  652. // "refund_fee": "0.01",
  653. // "send_back_fee": "0.00",
  654. // "trade_no": "2020081522001464141438570535"
  655. // }
  656. }
  657. return true;
  658. }
  659. /**
  660. * 回收站
  661. */
  662. public function recyclebin()
  663. {
  664. //设置过滤方法
  665. $this->request->filter(['strip_tags']);
  666. if ($this->request->isAjax()) {
  667. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  668. $total = $this->model
  669. ->onlyTrashed()
  670. ->alias('order')
  671. ->join('user', 'user.id = order.user_id')
  672. ->where($where)
  673. ->count();
  674. $list = $this->model
  675. ->onlyTrashed()
  676. ->alias('order')
  677. ->join('user', 'user.id = order.user_id')
  678. ->where($where)
  679. ->field('order.*,user.username')
  680. ->order($sort, $order)
  681. ->limit($offset, $limit)
  682. ->select();
  683. $list = collection($list)->toArray();
  684. foreach ($list as &$item) {
  685. $item['id'] = (string)$item['id'];
  686. $item['user'] = [];
  687. $item['user']['username'] = $item['username'] ? $item['username'] : __('Tourist');
  688. $item['have_paid_status'] = $item['have_paid'];
  689. $item['have_delivered_status'] = $item['have_delivered'];
  690. $item['have_received_status'] = $item['have_received'];
  691. $item['have_commented_status'] = $item['have_commented'];
  692. }
  693. $result = array("total" => $total, "rows" => $list);
  694. return json($result);
  695. }
  696. return $this->view->fetch();
  697. }
  698. }