User.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace app\admin\controller\bi;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. /**
  6. *
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class User extends Backend
  11. {
  12. protected $noNeedRight = ['*'];
  13. protected $noNeedLogin = ['*'];
  14. /**
  15. * User模型对象
  16. * @var \app\admin\model\bi\User
  17. */
  18. protected $model = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = new \app\admin\model\bi\User;
  23. }
  24. /**
  25. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  26. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  27. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  28. */
  29. /**
  30. * 添加
  31. */
  32. public function add()
  33. {
  34. if ($this->request->isPost()) {
  35. $params = $this->request->post("row/a");
  36. if ($params) {
  37. $params = $this->preExcludeFields($params);
  38. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  39. $params[$this->dataLimitField] = $this->auth->id;
  40. }
  41. $result = false;
  42. Db::startTrans();
  43. try {
  44. //是否采用模型验证
  45. if ($this->modelValidate) {
  46. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  47. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  48. $this->model->validateFailException(true)->validate($validate);
  49. }
  50. $params['institution_name'] = Db::table('bi_institution')->where('id',$params['institution_id'])->value('name');
  51. $result = $this->model->allowField(true)->save($params);
  52. Db::commit();
  53. } catch (ValidateException $e) {
  54. Db::rollback();
  55. $this->error($e->getMessage());
  56. } catch (PDOException $e) {
  57. Db::rollback();
  58. $this->error($e->getMessage());
  59. } catch (Exception $e) {
  60. Db::rollback();
  61. $this->error($e->getMessage());
  62. }
  63. if ($result !== false) {
  64. $this->success();
  65. } else {
  66. $this->error(__('No rows were inserted'));
  67. }
  68. }
  69. $this->error(__('Parameter %s can not be empty', ''));
  70. }
  71. return $this->view->fetch();
  72. }
  73. /**
  74. * 编辑
  75. */
  76. public function edit($ids = null)
  77. {
  78. $row = $this->model->get($ids);
  79. if (!$row) {
  80. $this->error(__('No Results were found'));
  81. }
  82. $adminIds = $this->getDataLimitAdminIds();
  83. if (is_array($adminIds)) {
  84. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  85. $this->error(__('You have no permission'));
  86. }
  87. }
  88. if ($this->request->isPost()) {
  89. $params = $this->request->post("row/a");
  90. if ($params) {
  91. $params = $this->preExcludeFields($params);
  92. $result = false;
  93. Db::startTrans();
  94. try {
  95. //是否采用模型验证
  96. if ($this->modelValidate) {
  97. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  98. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  99. $row->validateFailException(true)->validate($validate);
  100. }
  101. if(empty($params['password']))
  102. {
  103. unset($params['password']);
  104. }
  105. $params['institution_name'] = Db::table('bi_institution')->where('id',$params['institution_id'])->value('name');
  106. $result = $row->allowField(true)->save($params);
  107. Db::commit();
  108. } catch (ValidateException $e) {
  109. Db::rollback();
  110. $this->error($e->getMessage());
  111. } catch (PDOException $e) {
  112. Db::rollback();
  113. $this->error($e->getMessage());
  114. } catch (Exception $e) {
  115. Db::rollback();
  116. $this->error($e->getMessage());
  117. }
  118. if ($result !== false) {
  119. $this->success();
  120. } else {
  121. $this->error(__('No rows were updated'));
  122. }
  123. }
  124. $this->error(__('Parameter %s can not be empty', ''));
  125. }
  126. $this->view->assign("row", $row);
  127. return $this->view->fetch();
  128. }
  129. public function login()
  130. {
  131. $param = $this->request->post();
  132. $user = Db::table('bi_user')->where('username',$param['name'])->where('password',$param['pwd'])->find();
  133. if(empty($user))
  134. {
  135. return json(['code'=>'200','msg'=>'账号或密码错误','data'=>null]);
  136. }
  137. unset($user['password']);
  138. return json(['code'=>'0','msg'=>'登陆成功','data'=>['id'=>$user['id'],'oid'=>$user['institution_id'],'name'=>$user['realname'],'oname'=>$user['institution_name'],'level'=>$user['admin_level'],'key'=>$user['key']]]);
  139. }
  140. public function loginNoPass()
  141. {
  142. $param = $this->request->get();
  143. $user = Db::table('bi_user')->where('key',$param['key'])->find();
  144. if(empty($user))
  145. {
  146. return json(['code'=>'200','msg'=>'账号或密码错误','data'=>null]);
  147. }
  148. unset($user['password']);
  149. return json(['code'=>'0','msg'=>'登陆成功','data'=>['id'=>$user['id'],'oid'=>$user['institution_id'],'name'=>$user['realname'],'oname'=>$user['institution_name'],'level'=>$user['admin_level'],'key'=>$user['key']]]);
  150. }
  151. public function userSelectList()
  152. {
  153. try{
  154. // 获取列表
  155. $total = $this->model
  156. ->count();
  157. $list = $this->model
  158. ->field('id,username as name')
  159. ->select();
  160. // 格式化
  161. return json(array("total" => $total, "rows" => $list));
  162. } catch ( Exception $exception){
  163. $this->error($exception->getMessage());
  164. }
  165. }
  166. public function getUrlList()
  167. {
  168. $param = $this->request->get();
  169. $user = Db::table('bi_user')->where('key',$param['key'])->find();
  170. if(empty($user))
  171. {
  172. return json(['code'=>'300','msg'=>'错误的标识','data'=>null]);
  173. }
  174. $data = Db::table('bi_screen_manage')->where('institution_id',$user['institution_id'])->where('status',1)->field('name,url,file,ext')->order('weight desc')->select();
  175. return json(['code'=>'0','msg'=>'获取成功','data'=>$data]);
  176. }
  177. public function getUrlListByIns()
  178. {
  179. $param = $this->request->get();
  180. $data = Db::table('bi_screen_manage')->where('institution_id',$param['id'])->where('status',1)->order('weight desc')->field('name,url,file,ext')->select();
  181. return json(['code'=>'0','msg'=>'获取成功','data'=>$data]);
  182. }
  183. }