SystemUserController.php 7.2 KB

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