SystemUserController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. $data = $this->logic->getList($query);
  50. return $this->success($data);
  51. }
  52. /**
  53. * 修改状态
  54. * @param Request $request
  55. * @return Response
  56. */
  57. public function changeStatus(Request $request) : Response
  58. {
  59. $id = $request->input('id', '');
  60. $status = $request->input('status', 1);
  61. $model = $this->logic->findOrEmpty($id);
  62. if ($model->isEmpty()) {
  63. return $this->fail('未查找到信息');
  64. }
  65. $result = $model->save(['status' => $status]);
  66. if ($result) {
  67. $this->afterChange('changeStatus', $model);
  68. return $this->success('操作成功');
  69. } else {
  70. return $this->fail('操作失败');
  71. }
  72. }
  73. /**
  74. * 更新资料
  75. * @param Request $request
  76. * @return Response
  77. */
  78. public function updateInfo(Request $request) : Response
  79. {
  80. $data = $request->post();
  81. unset($data['deptList']);
  82. unset($data['postList']);
  83. unset($data['roleList']);
  84. $result = $this->logic->update($data, ['id' => $this->adminId], ['nickname', 'phone', 'signed', 'email', 'avatar', 'backend_setting']);
  85. if ($result) {
  86. $userInfoCache = new UserInfoCache($this->adminId);
  87. $userInfoCache->clearUserInfo();
  88. $userAuthCache = new UserAuthCache($this->adminId);
  89. $userAuthCache->clearUserCache();
  90. return $this->success('操作成功');
  91. } else {
  92. return $this->fail('操作失败');
  93. }
  94. }
  95. /**
  96. * 修改密码
  97. * @param Request $request
  98. * @return Response
  99. */
  100. public function modifyPassword(Request $request) : Response
  101. {
  102. $oldPassword = $request->input('oldPassword');
  103. $newPassword = $request->input('newPassword');
  104. $this->logic->modifyPassword($this->adminId, $oldPassword, $newPassword);
  105. $userInfoCache = new UserInfoCache($this->adminId);
  106. $userInfoCache->clearUserInfo();
  107. $userAuthCache = new UserAuthCache($this->adminId);
  108. $userAuthCache->clearUserCache();
  109. return $this->success('修改成功');
  110. }
  111. /**
  112. * 清理用户缓存
  113. * @param Request $request
  114. * @return Response
  115. */
  116. public function clearCache(Request $request) : Response
  117. {
  118. $id = $request->post('id', '');
  119. $userInfoCache = new UserInfoCache($id);
  120. $userInfoCache->clearUserInfo();
  121. $userAuthCache = new UserAuthCache($id);
  122. $userAuthCache->clearUserCache();
  123. return $this->success('操作成功');
  124. }
  125. /**
  126. * 重置密码
  127. * @param Request $request
  128. * @return Response
  129. */
  130. public function initUserPassword(Request $request) : Response
  131. {
  132. $id = $request->post('id', '');
  133. if ($id == 1) {
  134. return $this->fail('超级管理员不允许重置密码');
  135. }
  136. $data = ['password' => password_hash('sai123456', PASSWORD_DEFAULT)];
  137. $this->logic->authEdit($id, $data);
  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 setHomePage(Request $request) : Response
  150. {
  151. $id = $request->post('id', '');
  152. $dashboard = $request->post('dashboard', '');
  153. $data = ['dashboard' => $dashboard];
  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. }