Delivery.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace app\admin\controller\trade;
  3. use app\admin\model\Post;
  4. use app\common\controller\Backend;
  5. use think\Db;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. /**
  9. * 发货订单管理
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class Delivery extends Backend
  14. {
  15. protected $noNeedRight = 'selectpage';
  16. /**
  17. * Delivery模型对象
  18. * @var \app\admin\model\trade\Delivery
  19. */
  20. protected $model = null;
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new \app\admin\model\trade\Delivery;
  25. $this->view->assign("statusList", $this->model->getStatusList());
  26. }
  27. public function import()
  28. {
  29. parent::import();
  30. }
  31. /**
  32. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  33. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  34. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  35. */
  36. public function index()
  37. {
  38. //设置过滤方法
  39. $this->request->filter(['strip_tags', 'trim']);
  40. if ($this->request->isAjax()) {
  41. //如果发送的来源是Selectpage,则转发到Selectpage
  42. if ($this->request->request('keyField')) {
  43. return $this->selectpage();
  44. }
  45. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  46. $list = $this->model->alias('delivery')
  47. ->field('delivery.id,delivery.out_trade_no,delivery.delivery_order_no,delivery.prize_id,delivery.goods_name,delivery.goods_image,delivery.username,delivery.mobile,delivery.address,delivery.post_name,delivery.post_code,delivery.delivery_number,delivery.status,delivery.create_time,delivery.delivery_time,delivery.receive_time')
  48. ->field('user.nickname,user.avatar,delivery.mobile')
  49. ->join('user user', 'user.id = delivery.user_id')
  50. ->where($where)
  51. ->where('delivery.status','neq','unpay')
  52. ->order($sort, $order)
  53. ->paginate($limit)
  54. ->each(function ($item) {
  55. $item->avatar = $item->avatar ? cdnurl($item->avatar, true) : letter_avatar($item->nickname);
  56. });
  57. // 清空对应角标
  58. $filter = json_decode(input('filter'), true);
  59. if ($filter && !$list->isEmpty()) {
  60. \app\admin\model\trade\Delivery::where('backend_read', 0)->where('status', $list[0]->status)->update(['backend_read' => 1]);
  61. }
  62. $result = array("total" => $list->total(), "rows" => $list->items());
  63. return json($result);
  64. }
  65. // 渲染角标数字
  66. $sidebar = [
  67. 'all' => \app\admin\model\trade\Delivery::where('backend_read', 0)->where('status','neq','unpay')->count(),
  68. 'undelivered' => \app\admin\model\trade\Delivery::where('backend_read', 0)->where('status', 'undelivered')->count(),
  69. 'unreceived' => \app\admin\model\trade\Delivery::where('backend_read', 0)->where('status', 'unreceived')->count(),
  70. 'finished' => \app\admin\model\trade\Delivery::where('backend_read', 0)->where('status', 'finished')->count(),
  71. ];
  72. $this->assignconfig('sidebar_number', $sidebar);
  73. $this->assign('sidebar_number', $sidebar);
  74. return $this->view->fetch();
  75. }
  76. public function edit($ids = null)
  77. {
  78. $row = $this->model->alias('delivery')
  79. ->field('delivery.id,delivery.out_trade_no,delivery.prize_id,delivery.goods_name,delivery.goods_image,delivery.username,delivery.mobile,delivery.address,delivery.post_name,delivery.post_code,delivery.delivery_number,delivery.status,delivery.create_time')
  80. ->field('user.nickname,user.avatar,user.mobile')
  81. ->join('user user', 'user.id = delivery.user_id')
  82. ->where('delivery.id', $ids)
  83. ->find();
  84. if (!$row) {
  85. $this->error(__('No Results were found'));
  86. }
  87. $adminIds = $this->getDataLimitAdminIds();
  88. if (is_array($adminIds)) {
  89. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  90. $this->error(__('You have no permission'));
  91. }
  92. }
  93. if ($this->request->isPost()) {
  94. $params = $this->request->post("row/a");
  95. if ($params) {
  96. $params = $this->preExcludeFields($params);
  97. if ('finished' == $row->status) {
  98. $this->error('不能修改已完成订单');
  99. }
  100. if (!in_array($params['status'], ['undelivered', 'unreceived', 'finished'])) {
  101. $this->error('订单状态有误');
  102. }
  103. if ('undelivered' == $row->status && 'unreceived' == $params['status']) {
  104. $params['delivery_time'] = time();
  105. }
  106. if (empty($params['post_code'])) {
  107. $this->error('快递公司名称不能为空');
  108. }
  109. if (empty($params['delivery_number'])) {
  110. $this->error('快递单号不能为空');
  111. }
  112. // 检查快递公司
  113. $checkPostName = Post::where('post_code', $params['post_code'])->value('post_name');
  114. if (empty($checkPostName)) {
  115. $this->error('快递公司名称有误');
  116. }
  117. $params['post_name'] = $checkPostName;
  118. $result = false;
  119. Db::startTrans();
  120. try {
  121. //是否采用模型验证
  122. if ($this->modelValidate) {
  123. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  124. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  125. $row->validateFailException(true)->validate($validate);
  126. }
  127. $result = $row->allowField('post_name,post_code,delivery_number,status,delivery_time')->save($params);
  128. Db::commit();
  129. } catch (ValidateException $e) {
  130. Db::rollback();
  131. $this->error($e->getMessage());
  132. } catch (PDOException $e) {
  133. Db::rollback();
  134. $this->error($e->getMessage());
  135. } catch (\Exception $e) {
  136. Db::rollback();
  137. $this->error($e->getMessage());
  138. }
  139. if ($result !== false) {
  140. $this->success();
  141. } else {
  142. $this->error(__('No rows were updated'));
  143. }
  144. }
  145. $this->error(__('Parameter %s can not be empty', ''));
  146. }
  147. $this->view->assign("row", $row);
  148. return $this->view->fetch();
  149. }
  150. public function selectpage()
  151. {
  152. $search = [];
  153. if (!empty($name = $this->request->request('q_word/a'))) {
  154. $search = ['post_name' => ['like', '%' . $name[0] . '%']];
  155. }
  156. $list = Post::field('post_name,post_code')->where($search)->select();
  157. $ret = [
  158. 'list' => $list,
  159. 'total' => count($list)
  160. ];
  161. return json($ret);
  162. }
  163. }