Torder.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace app\admin\controller\order;
  3. use app\common\controller\Backend;
  4. use app\common\lib\WxPay;
  5. use think\Db;
  6. use think\Exception;
  7. use think\exception\PDOException;
  8. use think\exception\ValidateException;
  9. /**
  10. * 订单管理
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class Torder extends Backend
  15. {
  16. /**
  17. * Torder模型对象
  18. * @var \app\admin\model\order\Torder
  19. */
  20. protected $model = null;
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new \app\admin\model\order\Torder;
  25. }
  26. /**
  27. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  28. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  29. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  30. */
  31. /**
  32. * 查看
  33. */
  34. public function index()
  35. {
  36. //当前是否为关联查询
  37. $this->relationSearch = true;
  38. //设置过滤方法
  39. $this->request->filter(['strip_tags', 'trim']);
  40. if ($this->request->isAjax())
  41. {
  42. //如果发送的来源是Selectpage,则转发到Selectpage
  43. if ($this->request->request('keyField'))
  44. {
  45. return $this->selectpage();
  46. }
  47. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  48. $total = $this->model
  49. ->with(['orderaddress','users'])
  50. ->where($where)
  51. ->where('state',4)
  52. ->order($sort, $order)
  53. ->count();
  54. $list = $this->model
  55. ->with(['orderaddress','users'])
  56. ->where($where)
  57. ->where('state',4)
  58. ->order($sort, $order)
  59. ->limit($offset, $limit)
  60. ->select();
  61. foreach ($list as $row) {
  62. $row->visible(['o_id','money','freight','number','create_time','pay_time','f_time','whitebeon','state','type','tuikuan_state']);
  63. $row->visible(['orderaddress']);
  64. $row->getRelation('orderaddress')->visible(['a_name','a_tel','a_city','a_area']);
  65. $row->visible(['users']);
  66. $row->getRelation('users')->visible(['user_nickname']);
  67. }
  68. $list = collection($list)->toArray();
  69. $result = array("total" => $total, "rows" => $list);
  70. return json($result);
  71. }
  72. return $this->view->fetch();
  73. }
  74. /**
  75. * 编辑
  76. */
  77. public function edit($ids = null)
  78. {
  79. $row = $this->model->get($ids);
  80. if (!$row) {
  81. $this->error(__('No Results were found'));
  82. }
  83. $adminIds = $this->getDataLimitAdminIds();
  84. if (is_array($adminIds)) {
  85. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  86. $this->error(__('You have no permission'));
  87. }
  88. }
  89. if ($this->request->isPost()) {
  90. $params = $this->request->post("row/a");
  91. if ($params) {
  92. $params = $this->preExcludeFields($params);
  93. if ($params['tuikuan_state'] == 1) {
  94. $order = Db::name('order')->where('o_id',$ids)->find();
  95. if ($order['type'] == 1) {
  96. // 申请微信退款
  97. $out_trade_no = $order['number'];
  98. $out_refund_no = $order['number'];
  99. $total_fee = $order['money'];
  100. $refund_fee = $order['money'];
  101. $notify_url = config('site.httpurl')."/api/order/notify_refund";
  102. $wxpay = new WxPay();
  103. $res = $wxpay->refund($out_trade_no,$out_refund_no,$total_fee,$refund_fee,$notify_url);
  104. if ($res['return_code'] == "SUCCESS" ) {
  105. if ($res['result_code'] != 'SUCCESS')
  106. return json(['code' => 100, 'msg' => $res['err_code_des']]);
  107. } else {
  108. return json(['code' => 100, 'msg' => '请求微信退失败']);
  109. }
  110. }
  111. if ($order['type'] == 0) {
  112. //处理余额退款
  113. $updMOney = Db::name('users')->where('user_id',$order['user_id'])->setInc('user_money',$order['money']);
  114. if (!$updMOney) {
  115. return json(['code' => 100, 'msg' => '处理失败']);
  116. }
  117. }
  118. }
  119. $result = false;
  120. Db::startTrans();
  121. try {
  122. //是否采用模型验证
  123. if ($this->modelValidate) {
  124. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  125. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  126. $row->validateFailException(true)->validate($validate);
  127. }
  128. $result = $row->allowField(true)->save($params);
  129. Db::commit();
  130. } catch (ValidateException $e) {
  131. Db::rollback();
  132. $this->error($e->getMessage());
  133. } catch (PDOException $e) {
  134. Db::rollback();
  135. $this->error($e->getMessage());
  136. } catch (Exception $e) {
  137. Db::rollback();
  138. $this->error($e->getMessage());
  139. }
  140. if ($result !== false) {
  141. $this->success();
  142. } else {
  143. $this->error(__('No rows were updated'));
  144. }
  145. }
  146. $this->error(__('Parameter %s can not be empty', ''));
  147. }
  148. $this->view->assign("row", $row);
  149. return $this->view->fetch();
  150. }
  151. //查看退款详情
  152. public function torderinfo($ids)
  153. {
  154. $data = Db::name('t_order')->where('o_id',$ids)->find();
  155. $data['images'] = explode(',',$data['images']);
  156. return $this->fetch('torderinfo', ['data' => $data]);
  157. }
  158. }