Remote.php 6.5 KB

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