Order.php 29 KB

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