SystemUserController.php 7.1 KB

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