SystemUserController.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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.phone' => 'phone',
  54. 'sa_system_user.email' => 'email',
  55. 'sa_system_user.status' => 'status',
  56. 'sa_system_user.dept_id' => 'dept_id',
  57. 'sa_system_user.create_time' => 'create_time',
  58. 'sa_system_dept.name' => 'dept_name',
  59. 'GROUP_CONCAT(sa_system_user_role.role_id)' => 'role_id',
  60. 'GROUP_CONCAT(sa_system_role.name)' => 'role_name'
  61. ]);
  62. $query->leftJoin('sa_system_user_role', 'sa_system_user.id = sa_system_user_role.user_id');
  63. $query->leftJoin('sa_system_dept', 'sa_system_user.dept_id = sa_system_dept.id');
  64. $query->leftJoin('sa_system_role', 'sa_system_user_role.role_id = sa_system_role.id');
  65. $query->group('sa_system_user.id');
  66. $data = $this->logic->getList($query);
  67. return $this->success($data);
  68. }
  69. /**
  70. * 修改状态
  71. * @param Request $request
  72. * @return Response
  73. */
  74. public function changeStatus(Request $request) : Response
  75. {
  76. $id = $request->input('id', '');
  77. $status = $request->input('status', 1);
  78. $model = $this->logic->findOrEmpty($id);
  79. if ($model->isEmpty()) {
  80. return $this->fail('未查找到信息');
  81. }
  82. $result = $model->save(['status' => $status]);
  83. if ($result) {
  84. $this->afterChange('changeStatus', $model);
  85. return $this->success('操作成功');
  86. } else {
  87. return $this->fail('操作失败');
  88. }
  89. }
  90. /**
  91. * 更新资料
  92. * @param Request $request
  93. * @return Response
  94. */
  95. public function updateInfo(Request $request) : Response
  96. {
  97. $data = $request->post();
  98. unset($data['deptList']);
  99. unset($data['postList']);
  100. unset($data['roleList']);
  101. $result = $this->logic->update($data, ['id' => $this->adminId], ['nickname', 'phone', 'signed', 'email', 'avatar', 'backend_setting']);
  102. if ($result) {
  103. $userInfoCache = new UserInfoCache($this->adminId);
  104. $userInfoCache->clearUserInfo();
  105. $userAuthCache = new UserAuthCache($this->adminId);
  106. $userAuthCache->clearUserCache();
  107. return $this->success('操作成功');
  108. } else {
  109. return $this->fail('操作失败');
  110. }
  111. }
  112. /**
  113. * 修改密码
  114. * @param Request $request
  115. * @return Response
  116. */
  117. public function modifyPassword(Request $request) : Response
  118. {
  119. $oldPassword = $request->input('oldPassword');
  120. $newPassword = $request->input('newPassword');
  121. $this->logic->modifyPassword($this->adminId, $oldPassword, $newPassword);
  122. $userInfoCache = new UserInfoCache($this->adminId);
  123. $userInfoCache->clearUserInfo();
  124. $userAuthCache = new UserAuthCache($this->adminId);
  125. $userAuthCache->clearUserCache();
  126. return $this->success('修改成功');
  127. }
  128. /**
  129. * 清理用户缓存
  130. * @param Request $request
  131. * @return Response
  132. */
  133. public function clearCache(Request $request) : Response
  134. {
  135. $id = $request->post('id', '');
  136. $userInfoCache = new UserInfoCache($id);
  137. $userInfoCache->clearUserInfo();
  138. $userAuthCache = new UserAuthCache($id);
  139. $userAuthCache->clearUserCache();
  140. return $this->success('操作成功');
  141. }
  142. /**
  143. * 重置密码
  144. * @param Request $request
  145. * @return Response
  146. */
  147. public function initUserPassword(Request $request) : Response
  148. {
  149. $id = $request->post('id', '');
  150. if ($id == 1) {
  151. return $this->fail('超级管理员不允许重置密码');
  152. }
  153. $data = ['password' => password_hash('sai123456', PASSWORD_DEFAULT)];
  154. $this->logic->authEdit($id, $data);
  155. $userInfoCache = new UserInfoCache($id);
  156. $userInfoCache->clearUserInfo();
  157. $userAuthCache = new UserAuthCache($id);
  158. $userAuthCache->clearUserCache();
  159. return $this->success('操作成功');
  160. }
  161. /**
  162. * 设置首页
  163. * @param Request $request
  164. * @return Response
  165. */
  166. public function setHomePage(Request $request) : Response
  167. {
  168. $id = $request->post('id', '');
  169. $dashboard = $request->post('dashboard', '');
  170. $data = ['dashboard' => $dashboard];
  171. $this->logic->authEdit($id, $data);
  172. $userInfoCache = new UserInfoCache($id);
  173. $userInfoCache->clearUserInfo();
  174. $userAuthCache = new UserAuthCache($id);
  175. $userAuthCache->clearUserCache();
  176. return $this->success('操作成功');
  177. }
  178. /**
  179. * 设置用户权限
  180. * @param Request $request
  181. * @return Response
  182. */
  183. public function setUserPermission(Request $request) : Response
  184. {
  185. $id = $request->post('id');
  186. $game_list = $request->post('game_list');
  187. $normal_game_list = $request->post('normal_game_list');
  188. $ad_permission = $request->post('ad_permission');
  189. if ($game_list) {
  190. $data['game_list'] = $game_list;
  191. }
  192. if ($normal_game_list) {
  193. $data['normal_game_list'] = $normal_game_list;
  194. }
  195. if ($ad_permission) {
  196. $data['ad_permission'] = $ad_permission;
  197. }
  198. $this->logic->authEdit($id, $data);
  199. $userInfoCache = new UserInfoCache($id);
  200. $userInfoCache->clearUserInfo();
  201. $userAuthCache = new UserAuthCache($id);
  202. $userAuthCache->clearUserCache();
  203. return $this->success('操作成功');
  204. }
  205. }