Order.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace app\admin\controller\offline;
  3. use app\common\controller\Backend;
  4. use Exception;
  5. use think\Db;
  6. use app\common\model\UserArea;
  7. use think\exception\DbException;
  8. use think\exception\ValidateException;
  9. /**
  10. * 订单管理
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class Order extends Backend
  15. {
  16. /**
  17. * Order模型对象
  18. * @var \app\admin\model\offline\Order
  19. */
  20. protected $model = null;
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new \app\common\model\ProductOrder();
  25. }
  26. /**
  27. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  28. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  29. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  30. */
  31. /**
  32. * 查看
  33. *
  34. * @return string|Json
  35. * @throws \think\Exception
  36. * @throws DbException
  37. */
  38. public function index()
  39. {
  40. //设置过滤方法
  41. $this->request->filter(['strip_tags', 'trim']);
  42. if (false === $this->request->isAjax()) {
  43. return $this->view->fetch();
  44. }
  45. //如果发送的来源是 Selectpage,则转发到 Selectpage
  46. if ($this->request->request('keyField')) {
  47. return $this->selectpage();
  48. }
  49. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  50. $list = $this->model->with('users,products,areas,address')
  51. ->where($where)
  52. ->order($sort, $order)
  53. ->paginate($limit);
  54. $result = ['total' => $list->total(), 'rows' => $list->items()];
  55. return json($result);
  56. }
  57. /**
  58. * 发货
  59. * @param $ids
  60. * @return string|void
  61. * @throws \think\Exception
  62. * @throws \think\exception\DbException
  63. */
  64. public function shipping($ids = null)
  65. {
  66. $row = $this->model->get($ids);
  67. if (!$row) {
  68. $this->error(__('No Results were found'));
  69. }
  70. $adminIds = $this->getDataLimitAdminIds();
  71. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  72. $this->error(__('You have no permission'));
  73. }
  74. if (false === $this->request->isPost()) {
  75. $area = UserArea::where('order_id', $ids)->find();
  76. $this->view->assign('row', $row);
  77. $this->view->assign('area', $area);
  78. return $this->view->fetch();
  79. }
  80. $params = $this->request->post('row/a');
  81. if (empty($params)) {
  82. $this->error(__('Parameter %s can not be empty', ''));
  83. }
  84. $params = $this->preExcludeFields($params);
  85. $result = false;
  86. Db::startTrans();
  87. try {
  88. //是否采用模型验证
  89. if ($this->modelValidate) {
  90. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  91. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  92. $row->validateFailException()->validate($validate);
  93. }
  94. if(empty($params['tracking_no'])) throw new ValidateException('请添加快递单号');
  95. $result = UserArea::where('id', $params['area_id'])->where('status', UserArea::Waiting)->update([
  96. 'status' => UserArea::Shipped,
  97. 'tracking_no' => $params['tracking_no']
  98. ]);
  99. Db::commit();
  100. } catch (\Exception $e) {
  101. Db::rollback();
  102. $this->error($e->getMessage());
  103. }
  104. if (false === $result) {
  105. $this->error(__('No rows were updated'));
  106. }
  107. $this->success();
  108. }
  109. }