Device.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. namespace app\admin\controller\dict;
  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 Device extends Backend
  14. {
  15. /**
  16. * Device模型对象
  17. * @var \app\admin\model\dict\Device
  18. */
  19. protected $model = null;
  20. /**
  21. * 快速搜索时执行查找的字段
  22. */
  23. protected $searchFields = ['name'];
  24. /**
  25. * 无需鉴权的方法,但需要登录
  26. * @var array
  27. */
  28. protected $noNeedRight = ['classselectlist','examclassselectlist'];
  29. public function _initialize()
  30. {
  31. parent::_initialize();
  32. $this->model = new \app\admin\model\dict\Device;
  33. }
  34. /**
  35. * 查看
  36. */
  37. public function index()
  38. {
  39. //设置过滤方法
  40. $this->request->filter(['strip_tags']);
  41. if ($this->request->isAjax()) {
  42. //如果发送的来源是Selectpage,则转发到Selectpage
  43. if ($this->request->request('keyField')) {
  44. return $this->selectpage();
  45. }
  46. list($where, $sort, $order, $offset, $limit) = $this->buildparams($this->searchFields, true);
  47. $join = [
  48. ['constant c','d.exam_class_id = c.id','LEFT']
  49. ];
  50. $field = ['d.*','c.constant_value as exam_class'];
  51. $total = $this->model->alias('d')
  52. ->join($join)
  53. ->where($where)
  54. ->order($sort, $order)
  55. ->count();
  56. $list = $this->model->alias('d')
  57. ->join($join)
  58. ->where($where)
  59. ->order($sort, $order)
  60. ->limit($offset, $limit)
  61. ->field($field)
  62. ->select();
  63. $list = collection($list)->toArray();
  64. $result = array("total" => $total, "rows" => $list);
  65. return json($result);
  66. }
  67. return $this->view->fetch();
  68. }
  69. /**
  70. * 添加
  71. */
  72. public function add()
  73. {
  74. if ($this->request->isPost()) {
  75. $params = $this->request->post("row/a");
  76. if ($params) {
  77. $params = $this->preExcludeFields($params);
  78. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  79. $params[$this->dataLimitField] = $this->auth->id;
  80. }
  81. $result = false;
  82. Db::startTrans();
  83. try {
  84. //是否采用模型验证
  85. if ($this->modelValidate) {
  86. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  87. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  88. $this->model->validateFailException(true)->validate($validate);
  89. }
  90. $params['id'] = makeNew16Uid();
  91. $result = $this->model->allowField(true)->save($params);
  92. Db::commit();
  93. } catch (ValidateException $e) {
  94. Db::rollback();
  95. $this->error($e->getMessage());
  96. } catch (PDOException $e) {
  97. Db::rollback();
  98. $this->error($e->getMessage());
  99. } catch (Exception $e) {
  100. Db::rollback();
  101. $this->error($e->getMessage());
  102. }
  103. if ($result !== false) {
  104. $this->success();
  105. } else {
  106. $this->error(__('No rows were inserted'));
  107. }
  108. }
  109. $this->error(__('Parameter %s can not be empty', ''));
  110. }
  111. return $this->view->fetch();
  112. }
  113. /**
  114. * 编辑
  115. */
  116. public function edit($ids = null)
  117. {
  118. $row = $this->model->get($ids);
  119. if (!$row) {
  120. $this->error(__('No Results were found'));
  121. }
  122. $adminIds = $this->getDataLimitAdminIds();
  123. if (is_array($adminIds)) {
  124. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  125. $this->error(__('You have no permission'));
  126. }
  127. }
  128. if ($this->request->isPost()) {
  129. $params = $this->request->post("row/a");
  130. if ($params) {
  131. $params = $this->preExcludeFields($params);
  132. $result = false;
  133. Db::startTrans();
  134. try {
  135. //是否采用模型验证
  136. if ($this->modelValidate) {
  137. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  138. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  139. $row->validateFailException(true)->validate($validate);
  140. }
  141. $result = $row->allowField(true)->save($params);
  142. Db::commit();
  143. } catch (ValidateException $e) {
  144. Db::rollback();
  145. $this->error($e->getMessage());
  146. } catch (PDOException $e) {
  147. Db::rollback();
  148. $this->error($e->getMessage());
  149. } catch (Exception $e) {
  150. Db::rollback();
  151. $this->error($e->getMessage());
  152. }
  153. if ($result !== false) {
  154. $this->success();
  155. } else {
  156. $this->error(__('No rows were updated'));
  157. }
  158. }
  159. $this->error(__('Parameter %s can not be empty', ''));
  160. }
  161. $this->view->assign("row", $row);
  162. return $this->view->fetch();
  163. }
  164. /**
  165. * 检查类型下拉
  166. * @author matielong
  167. */
  168. public function classSelectList($save = false)
  169. {
  170. $data = Db::table('constant')
  171. ->where('parent_id','exam_class')
  172. ->field('id,constant_value as name')
  173. ->select();
  174. if($save){
  175. $result = array("rows" => $data);
  176. return json($result);
  177. }
  178. $data = ['searchlist' => $data];
  179. $this->success('success', null, $data);
  180. }
  181. public function examClassSelectList()
  182. {
  183. try{
  184. // 获取列表
  185. $total = Db::table('constant')
  186. ->where('parent_id','exam_class')
  187. ->count();
  188. $list = Db::table('constant')
  189. ->where('parent_id','exam_class')
  190. ->field('id,constant_value as name')
  191. ->select();
  192. // 格式化
  193. return json(array("total" => $total, "rows" => $list));
  194. } catch ( Exception $exception){
  195. $this->error($exception->getMessage());
  196. }
  197. }
  198. }