User.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. $this->user->storeBy($params);
  72. $this->user->attachRoles($request->param('roles'));
  73. if ($request->param('jobs')) {
  74. $this->user->attachJobs($request->param('jobs'));
  75. }
  76. return CatchResponse::success('', '添加成功');
  77. }
  78. /**
  79. *
  80. * @time 2019年12月04日
  81. * @param $id
  82. * @return \think\response\Json
  83. */
  84. public function read($id)
  85. {
  86. $user = $this->user->findBy($id);
  87. $user->roles = $user->getRoles();
  88. $user->jobs = $user->getJobs();
  89. return CatchResponse::success($user);
  90. }
  91. /**
  92. *
  93. * @time 2019年12月04日
  94. * @param $id
  95. * @param UpdateRequest $request
  96. * @return \think\response\Json
  97. */
  98. public function update($id, UpdateRequest $request)
  99. {
  100. $params=$request->param();
  101. // $params['area_id']=json_encode($params['area_id']);
  102. if($params['password']==""){
  103. unset($params['password']);
  104. }
  105. $params['realname']=$params['username'];
  106. $this->user->updateBy($id, $params);
  107. $user = $this->user->findBy($id);
  108. $user->detachRoles();
  109. $user->detachJobs();
  110. if (!empty($request->param('roles'))) {
  111. $user->attachRoles($request->param('roles'));
  112. }
  113. if (!empty($request->param('jobs'))) {
  114. $user->attachJobs($request->param('jobs'));
  115. }
  116. return CatchResponse::success();
  117. }
  118. /**
  119. *
  120. * @time 2019年12月04日
  121. * @param $id
  122. * @return \think\response\Json
  123. */
  124. public function delete($id)
  125. {
  126. $ids = Utils::stringToArrayBy($id);
  127. foreach ($ids as $_id) {
  128. $user = $this->user->findBy($_id);
  129. // 删除角色
  130. $user->detachRoles();
  131. // 删除岗位
  132. $user->detachJobs();
  133. $this->user->deleteBy($_id,true);
  134. }
  135. return CatchResponse::success();
  136. }
  137. /**
  138. *
  139. * @time 2019年12月07日
  140. * @param $id
  141. * @return \think\response\Json
  142. */
  143. public function switchStatus($id): \think\response\Json
  144. {
  145. $ids = Utils::stringToArrayBy($id);
  146. foreach ($ids as $_id) {
  147. $user = $this->user->findBy($_id);
  148. $this->user->updateBy($_id, [
  149. 'status' => $user->status == Users::ENABLE ? Users::DISABLE : Users::ENABLE,
  150. ]);
  151. }
  152. return CatchResponse::success([], '操作成功');
  153. }
  154. /**
  155. *
  156. * @time 2019年12月07日
  157. * @param $id
  158. * @return \think\response\Json
  159. * @throws \think\db\exception\DbException
  160. * @throws \think\db\exception\ModelNotFoundException
  161. * @throws \think\db\exception\DataNotFoundException
  162. */
  163. public function recover($id): \think\response\Json
  164. {
  165. $trashedUser = $this->user->findBy($id, ['*'], true);
  166. if ($this->user->where('email', $trashedUser->email)->find()) {
  167. return CatchResponse::fail(sprintf('该恢复用户的邮箱 [%s] 已被占用', $trashedUser->email));
  168. }
  169. return CatchResponse::success($this->user->recover($id));
  170. }
  171. /**
  172. *
  173. * @time 2019年12月11日
  174. * @param Request $request
  175. * @param Roles $roles
  176. * @return \think\response\Json
  177. */
  178. public function getRoles(Request $request, Roles $roles): \think\response\Json
  179. {
  180. $roles = Tree::done($roles->getList());
  181. $roleIds = [];
  182. if ($request->param('uid')) {
  183. $userHasRoles = $this->user->findBy($request->param('uid'))->getRoles();
  184. foreach ($userHasRoles as $role) {
  185. $roleIds[] = $role->pivot->role_id;
  186. }
  187. }
  188. return CatchResponse::success([
  189. 'roles' => $roles,
  190. 'hasRoles' => $roleIds,
  191. ]);
  192. }
  193. /**
  194. * 导出
  195. *
  196. * @time 2020年09月08日
  197. * @param Excel $excel
  198. * @param UserExport $userExport
  199. * @throws \PhpOffice\PhpSpreadsheet\Exception
  200. * @return \think\response\Json
  201. */
  202. public function export(Excel $excel, UserExport $userExport)
  203. {
  204. return CatchResponse::success($excel->save($userExport, Utils::publicPath('export/users')));
  205. }
  206. /**
  207. * 更新个人信息
  208. *
  209. * @time 2020年09月20日
  210. * @param ProfileRequest $request
  211. * @return \think\response\Json
  212. */
  213. public function profile(ProfileRequest $request)
  214. {
  215. return CatchResponse::success($this->user->updateBy($request->user()->id, $request->param()));
  216. }
  217. /**
  218. * 获取管理区域
  219. */
  220. public function getAreaIdBy()
  221. {
  222. return CatchResponse::success($this->user->getAreaIdBy());
  223. }
  224. /**
  225. *
  226. * @time 2019年12月07日
  227. * @param $depart_id
  228. * @return \think\response\Json
  229. */
  230. public function getUserByDepart($depart_id): \think\response\Json
  231. {
  232. return CatchResponse::success($this->user->getUserByDepart($depart_id));
  233. }
  234. /**
  235. * 获取推送用户
  236. */
  237. public function getPushUserList()
  238. {
  239. return CatchResponse::success($this->user->getPushUserList());
  240. }
  241. }