Info.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. namespace app\admin\controller\car;
  3. use app\admin\model\User;
  4. use app\common\controller\Backend;
  5. use app\common\model\Category;
  6. use think\Db;
  7. use think\exception\PDOException;
  8. use think\exception\ValidateException;
  9. /**
  10. * 车型信息
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class Info extends Backend
  15. {
  16. /**
  17. * Info模型对象
  18. * @var \app\admin\model\car\Info
  19. */
  20. protected $model = null;
  21. protected $searchFields = ['code','name','user.nickname'];
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = new \app\admin\model\car\Info;
  26. }
  27. /**
  28. * 查看
  29. */
  30. public function index()
  31. {
  32. //设置过滤方法
  33. $this->request->filter(['strip_tags', 'trim']);
  34. if ($this->request->isAjax()) {
  35. //如果发送的来源是Selectpage,则转发到Selectpage
  36. if ($this->request->request('keyField')) {
  37. return $this->selectpage();
  38. }
  39. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  40. $list = $this->model->with('user')
  41. ->where($where)
  42. ->order($sort, $order)
  43. ->paginate($limit);
  44. foreach ($list as $row) {
  45. // $row->user_id = User::where(['id' => $row->user_id])->value('nickname') ?? '-';
  46. $row->p_category_id = Category::where(['id' => $row['p_category_id']])->value('name') ?? '-';
  47. $row->category_id = Category::where(['id' => $row['category_id']])->value('name') ?? '-';
  48. $row->e_category_id = Category::where(['id' => $row['e_category_id']])->value('name') ?? '-';
  49. }
  50. $result = array("total" => $list->total(), "rows" => $list->items());
  51. return json($result);
  52. }
  53. return $this->view->fetch();
  54. }
  55. /**
  56. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  57. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  58. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  59. */
  60. /**
  61. * 添加
  62. */
  63. public function add()
  64. {
  65. if ($this->request->isPost()) {
  66. $params = $this->request->post("row/a");
  67. if ($params) {
  68. $params = $this->preExcludeFields($params);
  69. $name = '';
  70. // 拼接名称
  71. $p_category = Category::where(['id' => $params['p_category_id']])->find();
  72. if ($p_category) {
  73. $name = $p_category['name'];
  74. }
  75. $category = Category::where(['id' => $params['category_id']])->find();
  76. if ($category) {
  77. $name = $name . $category['name'];
  78. }
  79. $e_category = Category::where(['id' => $params['e_category_id']])->find();
  80. if ($e_category) {
  81. $name = $name. $e_category['name'];
  82. }
  83. $params['name'] = $name;
  84. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  85. $params[$this->dataLimitField] = $this->auth->id;
  86. }
  87. $result = false;
  88. Db::startTrans();
  89. try {
  90. //是否采用模型验证
  91. if ($this->modelValidate) {
  92. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  93. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  94. $this->model->validateFailException(true)->validate($validate);
  95. }
  96. $result = $this->model->allowField(true)->save($params);
  97. Db::commit();
  98. } catch (ValidateException $e) {
  99. Db::rollback();
  100. $this->error($e->getMessage());
  101. } catch (PDOException $e) {
  102. Db::rollback();
  103. $this->error($e->getMessage());
  104. } catch (Exception $e) {
  105. Db::rollback();
  106. $this->error($e->getMessage());
  107. }
  108. if ($result !== false) {
  109. $this->success();
  110. } else {
  111. $this->error(__('No rows were inserted'));
  112. }
  113. }
  114. $this->error(__('Parameter %s can not be empty', ''));
  115. }
  116. return $this->view->fetch();
  117. }
  118. /**
  119. * 编辑
  120. */
  121. public function edit($ids = null)
  122. {
  123. $row = $this->model->get($ids);
  124. if (!$row) {
  125. $this->error(__('No Results were found'));
  126. }
  127. $adminIds = $this->getDataLimitAdminIds();
  128. if (is_array($adminIds)) {
  129. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  130. $this->error(__('You have no permission'));
  131. }
  132. }
  133. if ($this->request->isPost()) {
  134. $params = $this->request->post("row/a");
  135. $name = '';
  136. // 拼接名称
  137. $p_category = Category::where(['id' => $params['p_category_id']])->find();
  138. if ($p_category) {
  139. $name = $p_category['name'];
  140. }
  141. $category = Category::where(['id' => $params['category_id']])->find();
  142. if ($category) {
  143. $name = $name . $category['name'];
  144. }
  145. $e_category = Category::where(['id' => $params['e_category_id']])->find();
  146. if ($e_category) {
  147. $name = $name. $e_category['name'];
  148. }
  149. $params['name'] = $name;
  150. if ($params) {
  151. $params = $this->preExcludeFields($params);
  152. $result = false;
  153. Db::startTrans();
  154. try {
  155. //是否采用模型验证
  156. if ($this->modelValidate) {
  157. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  158. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  159. $row->validateFailException(true)->validate($validate);
  160. }
  161. $result = $row->allowField(true)->save($params);
  162. Db::commit();
  163. } catch (ValidateException $e) {
  164. Db::rollback();
  165. $this->error($e->getMessage());
  166. } catch (PDOException $e) {
  167. Db::rollback();
  168. $this->error($e->getMessage());
  169. } catch (Exception $e) {
  170. Db::rollback();
  171. $this->error($e->getMessage());
  172. }
  173. if ($result !== false) {
  174. $this->success();
  175. } else {
  176. $this->error(__('No rows were updated'));
  177. }
  178. }
  179. $this->error(__('Parameter %s can not be empty', ''));
  180. }
  181. $this->view->assign("row", $row);
  182. return $this->view->fetch();
  183. }
  184. /**
  185. * 获取分类
  186. *
  187. * @return void
  188. * @throws \think\db\exception\DataNotFoundException
  189. * @throws \think\db\exception\ModelNotFoundException
  190. * @throws \think\exception\DbException
  191. */
  192. public function category()
  193. {
  194. $pid = $this->request->get('pid', '');
  195. $pid = intval($pid);
  196. $type = $this->request->get('p', '');
  197. if ($type > 1 && $pid == 0) {
  198. $this->success('', '', []);
  199. return ;
  200. }
  201. $where = ['pid' => $pid];
  202. $list = Db::name('category')
  203. ->field('id as value,name')
  204. ->where($where)
  205. ->select();
  206. $this->success('', '', $list);
  207. }
  208. }