User.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <?php
  2. namespace app\agent\controller\auth;
  3. use app\common\controller\AgentController;
  4. use app\http\IResponse;
  5. use Lettered\Support\Auth as IAuth;
  6. use think\App;
  7. use think\Exception;
  8. class User extends AgentController
  9. {
  10. protected $model;
  11. public function __construct(App $app = null, IAuth $auth)
  12. {
  13. parent::__construct($app, $auth);
  14. $this->model = new \app\agent\model\auth\User();
  15. }
  16. /**
  17. * 登录个人信息
  18. *
  19. * @author 许祖兴 < zuxing.xu@lettered.cn>
  20. * @date 2020/3/16 14:39
  21. *
  22. * @return \think\response\Json
  23. * @throws \Lettered\Support\Exceptions\FailedException
  24. * @throws \think\db\exception\DataNotFoundException
  25. * @throws \think\db\exception\ModelNotFoundException
  26. * @throws \think\exception\DbException
  27. */
  28. public function person()
  29. {
  30. // 接受参数
  31. $params = $this->request->param();
  32. // 获取用户信息
  33. $user = $this->auth->user();
  34. // 修改密码
  35. if ($this->request->isPut() && isset($params['password']) && $params['password'] != ''){
  36. // 检查原始密码
  37. if (!password_verify($params['password'], $user->password)){
  38. return $this->ApiJson(-1,"原始密码不正确");
  39. }
  40. // 查改
  41. $user = $this->model->findBy($user->id);
  42. // 更新
  43. $user->allowField(true)->updateBy($user->id, ['password' => $params['npassword']]);
  44. return $this->JsonSuccess([],"密码修改成,请重新登录!");
  45. }
  46. // 非超级管理员
  47. if ($user->id != 1){
  48. $Permission = new \app\agent\model\auth\Permission();
  49. $PermissionRole = new PermissionRole();
  50. $Role = new \app\agent\model\auth\Role();
  51. // 获取用户角色
  52. $roles = [];
  53. // 角色的规则
  54. $permissions_r = [];
  55. foreach ( $this->enforcer::GetRolesForUser('user_id_' . $user->id) as $role){
  56. $roles[] = $Role->getBy(['name' => $role]);
  57. // 找啊找啊找朋友
  58. foreach ( $this->enforcer::GetPermissionsForUser($role) as $permissions_u){
  59. $permissions_r[] = $Permission->field('sort,status,created_at,updated_at,deleted_at',true)
  60. ->where(['url' => $permissions_u[1],'policy' => $permissions_u[2]])->find();
  61. }
  62. }
  63. // 角色菜单权限
  64. $permission_idx = $PermissionRole->where(['role_id' => $roles[0]['id']])->value('permission_idx');
  65. $permissions_m = [];
  66. foreach (str2arr($permission_idx) as $idx){
  67. $permissions_m[] = $Permission->field('sort,status,created_at,updated_at,deleted_at',true)
  68. ->find($idx);
  69. }
  70. $permissions = array_merge($permissions_r, $permissions_m);
  71. }else{ // 超级用户
  72. $Permission = new \app\agent\model\auth\Permission();
  73. $permissions = $Permission
  74. ->field('sort,status,created_at,updated_at,deleted_at',true)
  75. ->select()->toArray();
  76. $roles = ['super'];
  77. }
  78. // 直接返回客户端处理树形
  79. $user->permissions = $permissions;
  80. // 用户角色
  81. $user->roles = $roles;
  82. return IResponse::success($user);
  83. }
  84. /**
  85. * 获取用户的角色
  86. *
  87. * @author 许祖兴 < zuxing.xu@lettered.cn>
  88. * @date 2020/3/16 14:44
  89. *
  90. * @param $id
  91. * @return \think\response\Json
  92. */
  93. public function roles($id)
  94. {
  95. // 获取用户信息
  96. $user = $this->model->getBy($id);
  97. if ($user) {
  98. $Role = new \app\agent\model\auth\Role();
  99. $roles = [];
  100. foreach ( $this->enforcer::GetRolesForUser('user_id_' . $user->id) as $role){
  101. $roles[] = $Role->getBy(['name' => $role]);
  102. }
  103. return IResponse::success($roles);
  104. }
  105. return IResponse::failure('用户不存在!');
  106. }
  107. /**
  108. * 用户列表
  109. *
  110. * @author 许祖兴 < zuxing.xu@lettered.cn>
  111. * @date 2020/3/16 14:39
  112. *
  113. * @return \think\response\Json
  114. * @throws \think\exception\DbException
  115. */
  116. public function index()
  117. {
  118. $where = [];
  119. !empty($this->auth->user()['user_id']) && $where[]
  120. = ['user_id', '=', $this->auth->user()['user_id']];
  121. //组合搜索
  122. !empty(input('keyword')) && $where[]
  123. = ['email|username', 'like', '%' . input('keyword') . '%'];
  124. $users = $this->model;
  125. if (input('status') == 'trashed'){
  126. // ->withTrashed() 包括软删除的数据;
  127. // ->onlyTrashed() 只查询删除
  128. $users = $users->onlyTrashed();
  129. }else {
  130. $users = $users->withTrashed();
  131. (!empty(input('status')) || input('status') == '0' ) &&
  132. $where[] = ['status', 'eq', input('status')];
  133. }
  134. return IResponse::paginate($users->where($where)
  135. ->paginate(input('limit'),false));
  136. }
  137. /**
  138. * 新增角色
  139. *
  140. * @author 许祖兴 < zuxing.xu@lettered.cn>
  141. * @date 2020/3/16 14:24
  142. *
  143. */
  144. public function save()
  145. {
  146. // 接收数据
  147. $params = $this->request->param();
  148. // 数据校验
  149. $valid = $this->validate($params,[
  150. 'email|账号' => 'require|email|unique:\\app\\agent\\model\\auth\\User',
  151. 'username|用户名' => 'require|alpha|unique:\\app\\agent\\model\\auth\\User',
  152. 'password|密码' => 'require'
  153. ],[
  154. 'email.unique' => '账号已存在!',
  155. 'name.alpha' => '用户名名称仅支持英文!',
  156. 'name.unique' => '用户名名称已存在!'
  157. ]);
  158. (true !== $valid) && IResponse::failure($valid);
  159. $auth = $this->auth->user();
  160. $params['area_id'] = $auth['area_id'];
  161. $params['user_id'] = $auth['user_id'];
  162. $params['password'] = password_hash(input('password/s'), PASSWORD_BCRYPT);
  163. $result = false;
  164. $this->model->startTrans();
  165. try {
  166. // 保存数据
  167. $result = $this->model->storeBy($params);
  168. // 获取角色信息
  169. $Role = new \app\agent\model\auth\Role();
  170. $role = $Role->getBy($params['roles']);
  171. // 写入用户权限
  172. $this->enforcer::AddRoleForUser('user_id_' . $result,$role->name);
  173. $this->model->commit();
  174. }
  175. catch(Exception $e) {
  176. $this->model->rollback();
  177. }
  178. if ($result) {
  179. IResponse::success([],'新增用户成功');
  180. }
  181. IResponse::failure('新增用户异常');
  182. }
  183. /**
  184. * 更新数据
  185. *
  186. * @author 许祖兴 < zuxing.xu@lettered.cn>
  187. * @date 2020/3/16 14:24
  188. *
  189. * @param $id
  190. * @return \think\response\Json
  191. */
  192. public function update($id)
  193. {
  194. // 接收数据
  195. $params = $this->request->param();
  196. // 查询用户
  197. $user = $this->model->findBy($id);
  198. // 是否更改状态操作
  199. if (isset($params['status']) && $params['status'] != '') {
  200. $valid = $this->validate($params, [
  201. 'status|配置状态' => 'require|integer'
  202. ]);
  203. if ($params['id'] == $user->id && $params['status'] == 0)
  204. return IResponse::failure("连自己够搞,不太好吧");
  205. }else {
  206. // 数据校验
  207. $valid = $this->validate($params, [
  208. 'email|账号' => 'require|email',
  209. 'username|用户名' => 'require|alpha'
  210. ], [
  211. 'name.alpha' => '用户名称仅支持英文!',
  212. ]);
  213. }
  214. // 错误返回
  215. (true !== $valid) && IResponse::failure($valid);
  216. // 是否更改状态操作
  217. if (!isset($params['status'])) {
  218. // 密码处理
  219. if (isset($params['password']) && $params['password'] == '') {
  220. // 密码空则不变动
  221. unset($params['password']);
  222. }
  223. else {
  224. $params['password'] = password_hash(input('password/s'), PASSWORD_BCRYPT);
  225. }
  226. $Role = new \app\agent\model\auth\Role();
  227. // 原先的角色
  228. $user_role = $this->enforcer::GetRolesForUser('user_id_' . $user->id);
  229. // 获取角色信息
  230. $role = $Role->getBy($params['roles']);
  231. // 没有原先直接新增
  232. if (!empty($user_role)) {
  233. // 是否变更操作
  234. if ($user_role[0] != $role->name) {
  235. // 删除原先
  236. $this->enforcer::DeleteRoleForUser('user_id_' . $user->id, $user_role[0]);
  237. }
  238. }
  239. // 写入变更用户角色
  240. $this->enforcer::AddRoleForUser('user_id_' . $user->id, $role->name);
  241. }
  242. // 更新用户信息
  243. $user->updateBy($id, $params);
  244. return IResponse::success('更新用户信息成功');
  245. }
  246. /**
  247. * 删除角色
  248. *
  249. * @author 许祖兴 < zuxing.xu@lettered.cn>
  250. * @date 2020/3/16 14:22
  251. *
  252. * @param $id
  253. * @return \think\response\Json
  254. */
  255. public function delete($id)
  256. {
  257. $this->model->deleteBy($id);
  258. return IResponse::success([],'删除用户成功');
  259. }
  260. /**
  261. * 用户批量操作
  262. *
  263. * @author 许祖兴 < zuxing.xu@lettered.cn>
  264. * @date 2020/3/23 11:38
  265. *
  266. * @return mixed
  267. */
  268. public function plectron(){
  269. // 收参数
  270. $params = $this->request->param();
  271. foreach (str2arr($params['ids']) as $id){
  272. $user = $this->model->getBy($id);
  273. if ($this->request->isDelete()){
  274. $user->deleteBy($id);
  275. return IResponse::success([],'删除用户成功');
  276. }
  277. $user->allowField(true)->updateBy($id, $params);
  278. }
  279. return IResponse::success([],'操作成功');
  280. }
  281. /**
  282. * 恢复删除用户
  283. *
  284. * @author 许祖兴 < zuxing.xu@lettered.cn>
  285. * @date 2020/3/23 13:05
  286. *
  287. * @param $id
  288. * @return mixed
  289. * @throws \think\db\exception\DataNotFoundException
  290. * @throws \think\db\exception\ModelNotFoundException
  291. * @throws \think\exception\DbException
  292. */
  293. public function restore($id)
  294. {
  295. // 查询数据
  296. $user = $this->model->onlyTrashed()->find($id);
  297. if (!$user){
  298. return IResponse::failure('用户不存在!');
  299. }
  300. // 恢复
  301. return $user->restore() ? IResponse::success('恢复用户成功!')
  302. : IResponse::failure('恢复用户失败!');
  303. }
  304. }