User.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2021 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: https://thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // | 免费声明 ( https://thinkadmin.top/disclaimer )
  11. // +----------------------------------------------------------------------
  12. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  13. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  14. // +----------------------------------------------------------------------
  15. namespace app\admin\controller;
  16. use app\admin\model\SystemBase;
  17. use app\admin\model\SystemUser;
  18. use think\admin\Controller;
  19. use think\admin\helper\QueryHelper;
  20. use think\admin\service\AdminService;
  21. /**
  22. * 系统用户管理
  23. * Class User
  24. * @package app\admin\controller
  25. */
  26. class User extends Controller
  27. {
  28. /**
  29. * 绑定数据表
  30. * @var string
  31. */
  32. private $table = 'SystemUser';
  33. /**
  34. * 超级用户名称
  35. * @var string
  36. */
  37. protected $superName;
  38. /**
  39. * 控制器初始化
  40. */
  41. protected function initialize()
  42. {
  43. // 超级用户名称
  44. $this->superName = AdminService::instance()->getSuperName();
  45. }
  46. /**
  47. * 系统用户管理
  48. * @auth true
  49. * @menu true
  50. * @throws \think\db\exception\DataNotFoundException
  51. * @throws \think\db\exception\DbException
  52. * @throws \think\db\exception\ModelNotFoundException
  53. */
  54. public function index()
  55. {
  56. $this->type = input('type', 'index');
  57. $this->_query(SystemUser::class)->layTable(function () {
  58. $this->title = '系统用户管理';
  59. $this->bases = (new SystemBase)->items('身份权限');
  60. }, function (QueryHelper $query) {
  61. // 加载对应数据列表
  62. if ($this->type === 'index') {
  63. $query->where(['is_deleted' => 0, 'status' => 1]);
  64. } elseif ($this->type = 'recycle') {
  65. $query->where(['is_deleted' => 0, 'status' => 0]);
  66. }
  67. // 数据列表搜索过滤
  68. $query->equal('status,usertype')->dateBetween('login_at,create_at');
  69. $query->like('username,nickname,contact_phone#phone,contact_mail#mail');
  70. });
  71. }
  72. /**
  73. * 数据列表处理
  74. * @param array $data
  75. */
  76. protected function _page_filter(array &$data)
  77. {
  78. (new SystemBase)->items('身份权限', $data, 'usertype', 'userinfo');
  79. }
  80. /**
  81. * 添加系统用户
  82. * @auth true
  83. * @throws \think\db\exception\DataNotFoundException
  84. * @throws \think\db\exception\DbException
  85. * @throws \think\db\exception\ModelNotFoundException
  86. */
  87. public function add()
  88. {
  89. $this->_applyFormToken();
  90. $this->_form($this->table, 'form');
  91. }
  92. /**
  93. * 编辑系统用户
  94. * @auth true
  95. * @throws \think\db\exception\DataNotFoundException
  96. * @throws \think\db\exception\DbException
  97. * @throws \think\db\exception\ModelNotFoundException
  98. */
  99. public function edit()
  100. {
  101. $this->_applyFormToken();
  102. $this->_form($this->table, 'form');
  103. }
  104. /**
  105. * 修改用户密码
  106. * @auth true
  107. * @throws \think\db\exception\DataNotFoundException
  108. * @throws \think\db\exception\DbException
  109. * @throws \think\db\exception\ModelNotFoundException
  110. */
  111. public function pass()
  112. {
  113. $this->_applyFormToken();
  114. if ($this->request->isGet()) {
  115. $this->verify = false;
  116. $this->_form($this->table, 'pass');
  117. } else {
  118. $data = $this->_vali([
  119. 'id.require' => '用户ID不能为空!',
  120. 'password.require' => '登录密码不能为空!',
  121. 'repassword.require' => '重复密码不能为空!',
  122. 'repassword.confirm:password' => '两次输入的密码不一致!',
  123. ]);
  124. if (data_save($this->table, ['id' => $data['id'], 'password' => md5($data['password'])])) {
  125. sysoplog('系统用户管理', "修改用户[{$data['id']}]密码成功");
  126. $this->success('密码修改成功,请使用新密码登录!', '');
  127. } else {
  128. $this->error('密码修改失败,请稍候再试!');
  129. }
  130. }
  131. }
  132. /**
  133. * 表单数据处理
  134. * @param array $data
  135. * @throws \think\db\exception\DataNotFoundException
  136. * @throws \think\db\exception\DbException
  137. * @throws \think\db\exception\ModelNotFoundException
  138. */
  139. protected function _form_filter(array &$data)
  140. {
  141. if ($this->request->isPost()) {
  142. // 账号权限绑定处理
  143. $data['authorize'] = arr2str($data['authorize'] ?? []);
  144. if (isset($data['id']) && $data['id'] > 0) {
  145. unset($data['username']);
  146. } else {
  147. // 检查登录账号是否出现重复
  148. if (empty($data['username'])) $this->error('登录账号不能为空!');
  149. $where = ['username' => $data['username'], 'is_deleted' => 0];
  150. if ($this->app->db->name($this->table)->where($where)->count() > 0) {
  151. $this->error("账号已经存在,请使用其它账号!");
  152. }
  153. // 新添加的用户密码与账号相同
  154. $data['password'] = md5($data['username']);
  155. }
  156. } else {
  157. // 用户身份数据
  158. $this->bases = (new SystemBase)->items('身份权限');
  159. // 权限绑定处理
  160. $data['authorize'] = str2arr($data['authorize'] ?? '');
  161. // 用户权限管理
  162. $query = $this->app->db->name('SystemAuth')->where(['status' => 1]);
  163. $this->authorizes = $query->order('sort desc,id desc')->select()->toArray();
  164. }
  165. }
  166. /**
  167. * 修改用户状态
  168. * @auth true
  169. * @throws \think\db\exception\DbException
  170. */
  171. public function state()
  172. {
  173. $this->_checkInput();
  174. $this->_save($this->table, $this->_vali([
  175. 'status.in:0,1' => '状态值范围异常!',
  176. 'status.require' => '状态值不能为空!',
  177. ]));
  178. }
  179. /**
  180. * 删除系统用户
  181. * @auth true
  182. * @throws \think\db\exception\DbException
  183. */
  184. public function remove()
  185. {
  186. $this->_checkInput();
  187. $this->_delete($this->table);
  188. }
  189. /**
  190. * 检查输入变量
  191. */
  192. private function _checkInput()
  193. {
  194. if (in_array('10000', str2arr(input('id', '')))) {
  195. $this->error('系统超级账号禁止删除!');
  196. }
  197. }
  198. /**
  199. * 表单结果处理
  200. * @param bool $result
  201. */
  202. protected function _add_form_result(bool $result)
  203. {
  204. if ($result) {
  205. $id = $this->app->db->name($this->table)->getLastInsID();
  206. sysoplog('系统用户管理', "添加系统用户[{$id}]成功");
  207. }
  208. }
  209. /**
  210. * 表单结果处理
  211. * @param boolean $result
  212. */
  213. protected function _edit_form_result(bool $result)
  214. {
  215. if ($result) {
  216. $id = input('id') ?: 0;
  217. sysoplog('系统用户管理', "修改系统用户[{$id}]成功");
  218. if ($id == AdminService::instance()->getUserId()) {
  219. $this->success('用户资料修改成功!', 'javascript:location.reload()');
  220. } else {
  221. $this->success('用户资料修改成功!');
  222. }
  223. }
  224. }
  225. /**
  226. * 状态结果处理
  227. * @param boolean $result
  228. */
  229. protected function _state_save_result(bool $result)
  230. {
  231. if ($result) {
  232. [$id, $state] = [input('id'), input('status')];
  233. sysoplog('系统用户管理', ($state ? '激活' : '禁用') . "系统用户[{$id}]成功");
  234. }
  235. }
  236. /**
  237. * 删除结果处理
  238. * @param boolean $result
  239. */
  240. protected function _remove_delete_result(bool $result)
  241. {
  242. if ($result) {
  243. $id = input('id') ?: 0;
  244. sysoplog('系统用户管理', "删除系统用户[{$id}]成功");
  245. }
  246. }
  247. }