User.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. namespace catchAdmin\permissions\controller;
  3. use catchAdmin\permissions\excel\UserExport;
  4. use catcher\base\CatchRequest as Request;
  5. use catchAdmin\permissions\model\Permissions;
  6. use catchAdmin\permissions\model\Roles;
  7. use catchAdmin\permissions\model\Users;
  8. use catchAdmin\permissions\request\CreateRequest;
  9. use catchAdmin\permissions\request\UpdateRequest;
  10. use catchAdmin\permissions\request\ProfileRequest;
  11. use catcher\base\CatchController;
  12. use catcher\CatchAuth;
  13. use catcher\CatchCacheKeys;
  14. use catcher\CatchResponse;
  15. use catcher\library\excel\Excel;
  16. use catcher\Tree;
  17. use catcher\Utils;
  18. use think\facade\Cache;
  19. class User extends CatchController
  20. {
  21. protected $user;
  22. public function __construct(Users $user)
  23. {
  24. $this->user = $user;
  25. }
  26. /**
  27. *
  28. * @time 2020年04月24日
  29. * @throws \think\db\exception\DbException
  30. * @return \think\response\Json
  31. */
  32. public function index()
  33. {
  34. return CatchResponse::paginate($this->user->getList());
  35. }
  36. /**
  37. * 获取用户信息
  38. *
  39. * @time 2020年01月07日
  40. * @param CatchAuth $auth
  41. * @throws \think\db\exception\DataNotFoundException
  42. * @throws \think\db\exception\DbException
  43. * @throws \think\db\exception\ModelNotFoundException
  44. * @return \think\response\Json
  45. */
  46. public function info(CatchAuth $auth)
  47. {
  48. $user = $auth->user();
  49. $roles = $user->getRoles()->column('identify');
  50. $permissionIds = $user->getPermissionsBy($user->id);
  51. // 缓存用户权限
  52. Cache::set(CatchCacheKeys::USER_PERMISSIONS . $user->id, $permissionIds);
  53. $user->permissions = Permissions::getCurrentUserPermissions($permissionIds);
  54. $user->roles = $roles;
  55. // 用户数据权限
  56. // $user->data_range = Roles::getDepartmentUserIdsBy($roles);
  57. return CatchResponse::success($user);
  58. }
  59. /**
  60. *
  61. * @param CreateRequest $request
  62. * @time 2019年12月06日
  63. * @return \think\response\Json
  64. */
  65. public function save(CreateRequest $request)
  66. {
  67. // $area_id=json_encode($request->param('area_id'));
  68. $params = $request->param();
  69. // $params['area_id']=json_encode($params['area_id']);
  70. $params['realname'] = $params['username'];
  71. $params['equ_password'] = strtoupper(substr(md5($params['password']), 8, 16));
  72. $this->user->storeBy($params);
  73. $this->user->attachRoles($request->param('roles'));
  74. if ($request->param('jobs')) {
  75. $this->user->attachJobs($request->param('jobs'));
  76. }
  77. $this->user->equUserUpdate($this->user);
  78. return CatchResponse::success('', '添加成功');
  79. }
  80. /**
  81. *
  82. * @time 2019年12月04日
  83. * @param $id
  84. * @return \think\response\Json
  85. */
  86. public function read($id)
  87. {
  88. $user = $this->user->findBy($id);
  89. $user->roles = $user->getRoles();
  90. $user->jobs = $user->getJobs();
  91. return CatchResponse::success($user);
  92. }
  93. /**
  94. *
  95. * @time 2019年12月04日
  96. * @param $id
  97. * @param UpdateRequest $request
  98. * @return \think\response\Json
  99. */
  100. public function update($id, UpdateRequest $request)
  101. {
  102. $params = $request->param();
  103. // $params['area_id']=json_encode($params['area_id']);
  104. if ($params['password'] == "") {
  105. unset($params['password']);
  106. } else {
  107. $params['equ_password'] = strtoupper(substr(md5($params['password']), 8, 16));
  108. }
  109. $params['realname'] = $params['username'];
  110. $this->user->updateBy($id, $params);
  111. $user = $this->user->findBy($id);
  112. $user->detachRoles();
  113. $user->detachJobs();
  114. if (!empty($request->param('roles'))) {
  115. $user->attachRoles($request->param('roles'));
  116. }
  117. if (!empty($request->param('jobs'))) {
  118. $user->attachJobs($request->param('jobs'));
  119. }
  120. $this->user->equUserUpdate($this->user->where('id', $id)->find());
  121. return CatchResponse::success();
  122. }
  123. /**
  124. *
  125. * @time 2019年12月04日
  126. * @param $id
  127. * @return \think\response\Json
  128. */
  129. public function delete($id)
  130. {
  131. $ids = Utils::stringToArrayBy($id);
  132. foreach ($ids as $_id) {
  133. $user = $this->user->findBy($_id);
  134. $array = $user;
  135. // 删除角色
  136. $user->detachRoles();
  137. // 删除岗位
  138. $user->detachJobs();
  139. $this->user->deleteBy($_id, true);
  140. $this->user->equUserUpdate($array);
  141. }
  142. return CatchResponse::success();
  143. }
  144. /**
  145. *
  146. * @time 2019年12月07日
  147. * @param $id
  148. * @return \think\response\Json
  149. */
  150. public function switchStatus($id): \think\response\Json
  151. {
  152. $ids = Utils::stringToArrayBy($id);
  153. foreach ($ids as $_id) {
  154. $user = $this->user->findBy($_id);
  155. $this->user->updateBy($_id, [
  156. 'status' => $user->status == Users::ENABLE ? Users::DISABLE : Users::ENABLE,
  157. ]);
  158. }
  159. return CatchResponse::success([], '操作成功');
  160. }
  161. /**
  162. *
  163. * @time 2019年12月07日
  164. * @param $id
  165. * @return \think\response\Json
  166. * @throws \think\db\exception\DbException
  167. * @throws \think\db\exception\ModelNotFoundException
  168. * @throws \think\db\exception\DataNotFoundException
  169. */
  170. public function recover($id): \think\response\Json
  171. {
  172. $trashedUser = $this->user->findBy($id, ['*'], true);
  173. if ($this->user->where('email', $trashedUser->email)->find()) {
  174. return CatchResponse::fail(sprintf('该恢复用户的邮箱 [%s] 已被占用', $trashedUser->email));
  175. }
  176. return CatchResponse::success($this->user->recover($id));
  177. }
  178. /**
  179. *
  180. * @time 2019年12月11日
  181. * @param Request $request
  182. * @param Roles $roles
  183. * @return \think\response\Json
  184. */
  185. public function getRoles(Request $request, Roles $roles): \think\response\Json
  186. {
  187. $roles = Tree::done($roles->getList());
  188. $roleIds = [];
  189. if ($request->param('uid')) {
  190. $userHasRoles = $this->user->findBy($request->param('uid'))->getRoles();
  191. foreach ($userHasRoles as $role) {
  192. $roleIds[] = $role->pivot->role_id;
  193. }
  194. }
  195. return CatchResponse::success([
  196. 'roles' => $roles,
  197. 'hasRoles' => $roleIds,
  198. ]);
  199. }
  200. /**
  201. * 导出
  202. *
  203. * @time 2020年09月08日
  204. * @param Excel $excel
  205. * @param UserExport $userExport
  206. * @throws \PhpOffice\PhpSpreadsheet\Exception
  207. * @return \think\response\Json
  208. */
  209. public function export(Excel $excel, UserExport $userExport)
  210. {
  211. return CatchResponse::success($excel->save($userExport, Utils::publicPath('export/users')));
  212. }
  213. /**
  214. * 更新个人信息
  215. *
  216. * @time 2020年09月20日
  217. * @param ProfileRequest $request
  218. * @return \think\response\Json
  219. */
  220. public function profile(ProfileRequest $request)
  221. {
  222. return CatchResponse::success($this->user->updateBy($request->user()->id, $request->param()));
  223. }
  224. /**
  225. * 获取管理区域
  226. */
  227. public function getAreaIdBy()
  228. {
  229. return CatchResponse::success($this->user->getAreaIdBy());
  230. }
  231. /**
  232. *
  233. * @time 2019年12月07日
  234. * @param $depart_id
  235. * @return \think\response\Json
  236. */
  237. public function getUserByDepart($depart_id): \think\response\Json
  238. {
  239. return CatchResponse::success($this->user->getUserByDepart($depart_id));
  240. }
  241. /**
  242. * 获取推送用户
  243. */
  244. public function getPushUserList()
  245. {
  246. return CatchResponse::success($this->user->getPushUserList());
  247. }
  248. }