SystemUserController.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | saiadmin [ saiadmin快速开发框架 ]
  4. // +----------------------------------------------------------------------
  5. // | Author: sai <1430792918@qq.com>
  6. // +----------------------------------------------------------------------
  7. namespace plugin\saiadmin\app\controller\system;
  8. use plugin\saiadmin\app\cache\UserAuthCache;
  9. use plugin\saiadmin\basic\BaseController;
  10. use plugin\saiadmin\app\cache\UserInfoCache;
  11. use plugin\saiadmin\app\logic\system\SystemUserLogic;
  12. use plugin\saiadmin\app\validate\system\SystemUserValidate;
  13. use support\Request;
  14. use support\Response;
  15. /**
  16. * 用户信息控制器
  17. */
  18. class SystemUserController extends BaseController
  19. {
  20. /**
  21. * 构造
  22. */
  23. public function __construct()
  24. {
  25. $this->logic = new SystemUserLogic();
  26. $this->validate = new SystemUserValidate;
  27. parent::__construct();
  28. }
  29. /**
  30. * 数据列表
  31. * @param Request $request
  32. * @return Response
  33. */
  34. public function index(Request $request) : Response
  35. {
  36. $where = $request->more([
  37. ['username', ''],
  38. ['phone', ''],
  39. ['email', ''],
  40. ['status', ''],
  41. ['dept_id', ''],
  42. ['create_time', ''],
  43. ]);
  44. $query = $this->logic->search($where);
  45. $query->auth([
  46. 'id' => $this->adminId,
  47. 'dept' => $this->adminInfo['deptList']
  48. ]);
  49. $query->field([
  50. 'sa_system_user.id' => 'id',
  51. 'sa_system_user.nickname' => 'nickname',
  52. 'sa_system_user.username' => 'username',
  53. 'sa_system_user.data_permission' => 'data_permission',
  54. 'sa_system_user.phone' => 'phone',
  55. 'sa_system_user.email' => 'email',
  56. 'sa_system_user.status' => 'status',
  57. 'sa_system_user.dept_id' => 'dept_id',
  58. 'sa_system_user.create_time' => 'create_time',
  59. 'sa_system_dept.name' => 'dept_name',
  60. 'GROUP_CONCAT(sa_system_user_role.role_id)' => 'role_id',
  61. 'GROUP_CONCAT(sa_system_role.name)' => 'role_name',
  62. 'sa_system_user.ad_permission' => 'ad_permission',
  63. 'sa_system_user.normal_game_list' => 'normal_game_list'
  64. ]);
  65. $query->leftJoin('sa_system_user_role', 'sa_system_user.id = sa_system_user_role.user_id');
  66. $query->leftJoin('sa_system_dept', 'sa_system_user.dept_id = sa_system_dept.id');
  67. $query->leftJoin('sa_system_role', 'sa_system_user_role.role_id = sa_system_role.id');
  68. $query->group('sa_system_user.id');
  69. $data = $this->logic->getList($query);
  70. return $this->success($data);
  71. }
  72. /**
  73. * 修改状态
  74. * @param Request $request
  75. * @return Response
  76. */
  77. public function changeStatus(Request $request) : Response
  78. {
  79. $id = $request->input('id', '');
  80. $status = $request->input('status', 1);
  81. $model = $this->logic->findOrEmpty($id);
  82. if ($model->isEmpty()) {
  83. return $this->fail('未查找到信息');
  84. }
  85. $result = $model->save(['status' => $status]);
  86. if ($result) {
  87. $this->afterChange('changeStatus', $model);
  88. return $this->success('操作成功');
  89. } else {
  90. return $this->fail('操作失败');
  91. }
  92. }
  93. /**
  94. * 更新资料
  95. * @param Request $request
  96. * @return Response
  97. */
  98. public function updateInfo(Request $request) : Response
  99. {
  100. $data = $request->post();
  101. unset($data['deptList']);
  102. unset($data['postList']);
  103. unset($data['roleList']);
  104. $result = $this->logic->update($data, ['id' => $this->adminId], ['nickname', 'phone', 'signed', 'email', 'avatar', 'backend_setting']);
  105. if ($result) {
  106. $userInfoCache = new UserInfoCache($this->adminId);
  107. $userInfoCache->clearUserInfo();
  108. $userAuthCache = new UserAuthCache($this->adminId);
  109. $userAuthCache->clearUserCache();
  110. return $this->success('操作成功');
  111. } else {
  112. return $this->fail('操作失败');
  113. }
  114. }
  115. /**
  116. * 修改密码
  117. * @param Request $request
  118. * @return Response
  119. */
  120. public function modifyPassword(Request $request) : Response
  121. {
  122. $oldPassword = $request->input('oldPassword');
  123. $newPassword = $request->input('newPassword');
  124. $this->logic->modifyPassword($this->adminId, $oldPassword, $newPassword);
  125. $userInfoCache = new UserInfoCache($this->adminId);
  126. $userInfoCache->clearUserInfo();
  127. $userAuthCache = new UserAuthCache($this->adminId);
  128. $userAuthCache->clearUserCache();
  129. return $this->success('修改成功');
  130. }
  131. /**
  132. * 清理用户缓存
  133. * @param Request $request
  134. * @return Response
  135. */
  136. public function clearCache(Request $request) : Response
  137. {
  138. $id = $request->post('id', '');
  139. $userInfoCache = new UserInfoCache($id);
  140. $userInfoCache->clearUserInfo();
  141. $userAuthCache = new UserAuthCache($id);
  142. $userAuthCache->clearUserCache();
  143. return $this->success('操作成功');
  144. }
  145. /**
  146. * 重置密码
  147. * @param Request $request
  148. * @return Response
  149. */
  150. public function initUserPassword(Request $request) : Response
  151. {
  152. $id = $request->post('id', '');
  153. if ($id == 1) {
  154. return $this->fail('超级管理员不允许重置密码');
  155. }
  156. $data = ['password' => password_hash('sai123456', PASSWORD_DEFAULT)];
  157. $this->logic->authEdit($id, $data);
  158. $userInfoCache = new UserInfoCache($id);
  159. $userInfoCache->clearUserInfo();
  160. $userAuthCache = new UserAuthCache($id);
  161. $userAuthCache->clearUserCache();
  162. return $this->success('操作成功');
  163. }
  164. /**
  165. * 设置首页
  166. * @param Request $request
  167. * @return Response
  168. */
  169. public function setHomePage(Request $request) : Response
  170. {
  171. $id = $request->post('id', '');
  172. $dashboard = $request->post('dashboard', '');
  173. $data = ['dashboard' => $dashboard];
  174. $this->logic->authEdit($id, $data);
  175. $userInfoCache = new UserInfoCache($id);
  176. $userInfoCache->clearUserInfo();
  177. $userAuthCache = new UserAuthCache($id);
  178. $userAuthCache->clearUserCache();
  179. return $this->success('操作成功');
  180. }
  181. /**
  182. * 设置用户权限
  183. * @param Request $request
  184. * @return Response
  185. */
  186. public function setUserPermission(Request $request) : Response
  187. {
  188. $params = $request->post();
  189. $id = $params['id'];
  190. unset($params['id']);
  191. $this->logic->authEdit($id, $params);
  192. $userInfoCache = new UserInfoCache($id);
  193. $userInfoCache->clearUserInfo();
  194. $userAuthCache = new UserAuthCache($id);
  195. $userAuthCache->clearUserCache();
  196. return $this->success('操作成功');
  197. }
  198. }