Order.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace app\admin\controller\ddrive;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\Exception;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. /**
  9. * 订单管理
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class Order extends Backend
  14. {
  15. /**
  16. * Order模型对象
  17. * @var \addons\ddrive\model\Order
  18. */
  19. protected $model = null;
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = new \addons\ddrive\model\Order;
  24. $this->view->assign("statusList", $this->model->getStatusList());
  25. $this->view->assign("typeList", $this->model->getTypeList());
  26. $this->view->assign("commentList", $this->model->getCommentList());
  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. $total = $this->model
  49. ->with(['user','driver'])
  50. ->where($where)
  51. ->order($sort, $order)
  52. ->count();
  53. $list = $this->model
  54. ->with(['user','driver'])
  55. ->where($where)
  56. ->order($sort, $order)
  57. ->limit($offset, $limit)
  58. ->select();
  59. foreach ($list as $row) {
  60. }
  61. $list = collection($list)->toArray();
  62. $result = array("total" => $total, "rows" => $list);
  63. return json($result);
  64. }
  65. return $this->view->fetch();
  66. }
  67. /**
  68. * 添加
  69. */
  70. public function add()
  71. {
  72. if ($this->request->isPost()) {
  73. $params = $this->request->post("row/a");
  74. if ($params) {
  75. $params = $this->preExcludeFields($params);
  76. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  77. $params[$this->dataLimitField] = $this->auth->id;
  78. }
  79. $result = false;
  80. Db::startTrans();
  81. try {
  82. //是否采用模型验证
  83. if ($this->modelValidate) {
  84. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  85. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  86. $this->model->validateFailException(true)->validate($validate);
  87. }
  88. $params['mobile'] = Db::name('user')->where('id',$params['user_id'])->value('mobile');
  89. $params['appointment_time'] = strtotime($params['appointment_time']);
  90. $result = $this->model->allowField(true)->save($params);
  91. Db::commit();
  92. } catch (ValidateException $e) {
  93. Db::rollback();
  94. $this->error($e->getMessage());
  95. } catch (PDOException $e) {
  96. Db::rollback();
  97. $this->error($e->getMessage());
  98. } catch (Exception $e) {
  99. Db::rollback();
  100. $this->error($e->getMessage());
  101. }
  102. if ($result !== false) {
  103. $this->success();
  104. } else {
  105. $this->error(__('No rows were inserted'));
  106. }
  107. }
  108. $this->error(__('Parameter %s can not be empty', ''));
  109. }
  110. return $this->view->fetch();
  111. }
  112. /**
  113. * 编辑
  114. */
  115. public function edit($ids = null)
  116. {
  117. $row = $this->model->get($ids);
  118. if (!$row) {
  119. $this->error(__('No Results were found'));
  120. }
  121. $adminIds = $this->getDataLimitAdminIds();
  122. if (is_array($adminIds)) {
  123. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  124. $this->error(__('You have no permission'));
  125. }
  126. }
  127. if ($this->request->isPost()) {
  128. $params = $this->request->post("row/a");
  129. if ($params) {
  130. $params = $this->preExcludeFields($params);
  131. $result = false;
  132. Db::startTrans();
  133. try {
  134. //是否采用模型验证
  135. if ($this->modelValidate) {
  136. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  137. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  138. $row->validateFailException(true)->validate($validate);
  139. }
  140. $params['mobile'] = Db::name('user')->where('id',$row['user_id'])->value('mobile');
  141. $params['appointment_time'] = strtotime($params['appointment_time']);
  142. $result = $row->allowField(true)->save($params);
  143. Db::commit();
  144. } catch (ValidateException $e) {
  145. Db::rollback();
  146. $this->error($e->getMessage());
  147. } catch (PDOException $e) {
  148. Db::rollback();
  149. $this->error($e->getMessage());
  150. } catch (Exception $e) {
  151. Db::rollback();
  152. $this->error($e->getMessage());
  153. }
  154. if ($result !== false) {
  155. $this->success();
  156. } else {
  157. $this->error(__('No rows were updated'));
  158. }
  159. }
  160. $this->error(__('Parameter %s can not be empty', ''));
  161. }
  162. $this->view->assign("row", $row);
  163. return $this->view->fetch();
  164. }
  165. }