Order.php 29 KB

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