Order.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace app\admin\controller\dispatch;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. /**
  6. * 派单订单表
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Order extends Backend
  11. {
  12. /**
  13. * Order模型对象
  14. * @var \app\admin\model\dispatch\Order
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new \app\admin\model\dispatch\Order;
  21. $this->view->assign("isCommentList", $this->model->getIsCommentList());
  22. $this->view->assign("statusList", $this->model->getStatusList());
  23. }
  24. public function import()
  25. {
  26. parent::import();
  27. }
  28. /**
  29. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  30. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  31. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  32. */
  33. /**
  34. * 查看
  35. */
  36. public function index()
  37. {
  38. //当前是否为关联查询
  39. $this->relationSearch = true;
  40. //设置过滤方法
  41. $this->request->filter(['strip_tags', 'trim']);
  42. if ($this->request->isAjax()) {
  43. //如果发送的来源是Selectpage,则转发到Selectpage
  44. if ($this->request->request('keyField')) {
  45. return $this->selectpage();
  46. }
  47. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  48. $list = $this->model
  49. ->with(['user','recive'])
  50. ->where($where)
  51. ->order($sort, $order)
  52. ->paginate($limit);
  53. foreach ($list as $row) {
  54. $row->visible(['id','order_no','price','num','skill_image','skill_name','skill_unit','describe','make_time', 'is_comment','status','updatetime','createtime']);
  55. $row->visible(['user']);
  56. $row->getRelation('user')->visible(['u_id','nickname']);
  57. $row->visible(['recive']);
  58. $row->getRelation('recive')->visible(['u_id','nickname']);
  59. }
  60. $result = array("total" => $list->total(), "rows" => $list->items());
  61. return json($result);
  62. }
  63. return $this->view->fetch();
  64. }
  65. /**
  66. * 添加
  67. */
  68. public function add()
  69. {
  70. if ($this->request->isPost()) {
  71. $params = $this->request->post("row/a");
  72. if ($params) {
  73. $params = $this->preExcludeFields($params);
  74. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  75. $params[$this->dataLimitField] = $this->auth->id;
  76. }
  77. $result = false;
  78. Db::startTrans();
  79. try {
  80. //是否采用模型验证
  81. if ($this->modelValidate) {
  82. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  83. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  84. $this->model->validateFailException(true)->validate($validate);
  85. }
  86. $result = $this->model->allowField(true)->save($params);
  87. Db::commit();
  88. } catch (ValidateException $e) {
  89. Db::rollback();
  90. $this->error($e->getMessage());
  91. } catch (PDOException $e) {
  92. Db::rollback();
  93. $this->error($e->getMessage());
  94. } catch (Exception $e) {
  95. Db::rollback();
  96. $this->error($e->getMessage());
  97. }
  98. if ($result !== false) {
  99. $this->success();
  100. } else {
  101. $this->error(__('No rows were inserted'));
  102. }
  103. }
  104. $this->error(__('Parameter %s can not be empty', ''));
  105. }
  106. return $this->view->fetch();
  107. }
  108. /**
  109. * 编辑
  110. */
  111. public function edit($ids = null)
  112. {
  113. $row = $this->model->get($ids);
  114. if (!$row) {
  115. $this->error(__('No Results were found'));
  116. }
  117. $adminIds = $this->getDataLimitAdminIds();
  118. if (is_array($adminIds)) {
  119. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  120. $this->error(__('You have no permission'));
  121. }
  122. }
  123. if ($this->request->isPost()) {
  124. $params = $this->request->post("row/a");
  125. if ($params) {
  126. $params = $this->preExcludeFields($params);
  127. $params["make_time"] && $params["make_time"] = strtotime($params["make_time"]);
  128. $result = false;
  129. Db::startTrans();
  130. try {
  131. //是否采用模型验证
  132. if ($this->modelValidate) {
  133. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  134. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  135. $row->validateFailException(true)->validate($validate);
  136. }
  137. $result = $row->allowField(true)->save($params);
  138. Db::commit();
  139. } catch (ValidateException $e) {
  140. Db::rollback();
  141. $this->error($e->getMessage());
  142. } catch (PDOException $e) {
  143. Db::rollback();
  144. $this->error($e->getMessage());
  145. } catch (Exception $e) {
  146. Db::rollback();
  147. $this->error($e->getMessage());
  148. }
  149. if ($result !== false) {
  150. $this->success();
  151. } else {
  152. $this->error(__('No rows were updated'));
  153. }
  154. }
  155. $this->error(__('Parameter %s can not be empty', ''));
  156. }
  157. $this->view->assign("row", $row);
  158. return $this->view->fetch();
  159. }
  160. }