UserController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. <?php
  2. namespace App\Http\Controllers\AdminApi;
  3. use App\Models\Menu;
  4. use App\Models\Role;
  5. use App\Models\User;
  6. use App\Servers\MenuServer;
  7. use App\Servers\PassServer;
  8. use App\Servers\RedisDataServer;
  9. use Illuminate\Http\Request;
  10. use App\Http\Controllers\FrontController;
  11. class UserController extends FrontController
  12. {
  13. public function __construct()
  14. {
  15. parent::__construct();
  16. }
  17. /**
  18. * 后台账号列表
  19. * @return \Illuminate\Http\JsonResponse
  20. */
  21. function getList(){
  22. $id = request()->input('id', '');//搜索的内容
  23. $search = request()->input('search','');//获取搜索内容
  24. $status = request()->input('status','');//获取状态
  25. $role_id = request()->input('role_id','');//获取角色id
  26. $start = request()->input('start','');//获取开始时间
  27. $end = request()->input('end','');//获取结束时间
  28. //查询数据条件
  29. $where = [['is_del',0]];
  30. $sql = "(concat(name,phone) like '%" . $search . "%')";
  31. if($id) $where[] = ['id', '=', $id];
  32. if($status) $where[] = ['status', '=', $status];
  33. if($role_id) $where[] = ['roles_id', '=', $role_id];
  34. if($start) $where[] = ['created_at', '>=', $start];
  35. if($end) $where[] = ['created_at', '<=', $end];
  36. //获取数据
  37. $list = User::where($where)
  38. ->whereRaw($sql)
  39. ->where('id','<>',1)
  40. ->orderBy('id','desc')
  41. ->select(['id','name','phone','status','roles_id','created_at'])
  42. ->paginate(10);
  43. foreach ($list as $value){
  44. if($value['id'] == 1){
  45. $value['role_name'] = '超级管理员';
  46. }else{
  47. $value['role_name'] = Role::where('id',$value['roles_id'])->value('name');
  48. }
  49. }
  50. return $this->apiResponseSuccess('获取列表成功', [
  51. 'items' => $list->items(),
  52. 'total' => $list->total(),
  53. 'limit' => 10,
  54. ]);
  55. }
  56. /**
  57. * 获取用户详情
  58. * @return \Illuminate\Http\JsonResponse
  59. */
  60. function getUserInfo(){
  61. $id = request()->input('id', '');//记录id,更新时必须
  62. if (empty($id)) return $this->apiResponseError( '缺少必要参数');
  63. //查询数据
  64. $info = User::where('id',$id)->select(['id','phone','name','roles_id'])->first();
  65. if(empty($info)) return $this->apiResponseError('没有找到该记录');
  66. return $this->apiResponseSuccess('获取成功', $info);
  67. }
  68. /**
  69. * 更新&编辑后台账号
  70. * @return false|\Illuminate\Http\JsonResponse
  71. */
  72. function save(){
  73. $id = request()->input('id', '');//记录id,更新时必须
  74. $phone = request()->input('phone', '');//登录手机号
  75. $name = request()->input('name', '');//姓名
  76. $roles_id = request()->input('roles_id', 0);//角色id
  77. $password = request()->input('password', '');//密码
  78. $again_password = request()->input('again_password', '');//确认密码
  79. if (empty($phone)) return $this->apiResponseError( '登录手机号必填');
  80. if(!preg_match('/^1[3-9]\d{9}$/',$phone)){
  81. return $this->apiResponseError( '手机号不正确');
  82. }
  83. if ($roles_id < 0 ) {
  84. return $this->apiResponseError( '请选择角色');
  85. }
  86. $email = $phone.'@admin.com';
  87. if(empty($id)){
  88. if (empty($password) || empty($again_password)) {
  89. return $this->apiResponseError( '请输入新密码');
  90. }
  91. if($password != $again_password){
  92. return $this->apiResponseError( '两次密码不一致');
  93. }
  94. $count = User::where('phone', '=', $phone)->where('is_del',0)->count();
  95. if ($count > 0 ) {
  96. return $this->apiResponseError( '账号已注册');
  97. }
  98. //生成密码
  99. $pass_server = PassServer::creatServer($password);
  100. $new_pass = $pass_server->creatPassword();
  101. $res = User::create([
  102. 'name' => $name,
  103. 'email' => $email,
  104. 'phone' => $phone,
  105. 'password' => $new_pass['password'],
  106. 'roles_id' => $roles_id,
  107. 'encrypt' => $new_pass['encrypt']
  108. ]);
  109. }else{
  110. if (empty($id)) return $this->apiResponseError( '缺少必要参数');
  111. $info = User::where('id',$id)->where('is_del',0)->first();
  112. if(!$info) return $this->apiResponseError( '记录不存在');
  113. $count = User::where('phone', '=', $phone)->where('is_del',0)->where('id', '<>', $id)->count();
  114. if ($count > 0 ) {
  115. return $this->apiResponseError( '账号已注册');
  116. }
  117. if (!empty($password) ) {
  118. if($password != $again_password){
  119. return $this->apiResponseError( '两次密码不一致');
  120. }
  121. //生成密码
  122. $pass_server = PassServer::creatServer($password, $info->{'encrypt'});
  123. $new_pass = $pass_server->creatPassword();
  124. $update['password'] = $new_pass['password'];
  125. $update['encrypt'] = $new_pass['encrypt'];
  126. }
  127. if($id == 1){//超级管理员
  128. $roles_id = 0;
  129. }
  130. $update['email'] = $email;
  131. $update['phone'] = $phone;
  132. $update['name'] = $name;
  133. $update['roles_id'] = $roles_id;
  134. $res = $info->update($update);
  135. }
  136. if ($res) {
  137. return $this->apiResponseSuccess('成功');
  138. } else {
  139. return $this->apiResponseError('失败');
  140. }
  141. }
  142. /**
  143. * 快速更新是否启用
  144. * @return \Illuminate\Http\JsonResponse
  145. */
  146. function setStatus(){
  147. $id = request()->input('id','');//记录id
  148. $status = request()->input('status', 0);//是否显示
  149. if(empty($id) || !in_array($status,[1,2])){
  150. return $this->apiResponseError('缺少必要参数');
  151. }
  152. if($id == 1){
  153. return $this->apiResponseError('超级管理员不能关闭');
  154. }
  155. $info = User::where('id',$id)->where('is_del',0)->select(['id'])->first();
  156. if (empty($info)) {
  157. return $this->apiResponseError('没有找到该记录');
  158. }
  159. $res = $info->update(['status'=>$status]);
  160. if ($res) {
  161. return $this->apiResponseSuccess('操作成功');
  162. } else {
  163. return $this->apiResponseError('操作失败');
  164. }
  165. }
  166. /**
  167. * 重置密码
  168. * @return \Illuminate\Http\JsonResponse
  169. */
  170. function resetPassword(){
  171. $id = request()->input('id', '');//重置密码的记录id
  172. if(empty($id)) return $this->apiResponseError('缺少必要参数');
  173. $info = User::where('id',$id)->where('is_del',0)->select(['id','encrypt'])->first();
  174. if (empty($info)) {
  175. return $this->apiResponseError('没有找到该记录');
  176. }
  177. $password = '123456';
  178. //生成密码
  179. $pass_server = PassServer::creatServer($password, $info->{'encrypt'});
  180. $new_pass = $pass_server->creatPassword();
  181. $update['password'] = $new_pass['password'];
  182. $update['encrypt'] = $new_pass['encrypt'];
  183. $res = $info->update($update);
  184. if ($res) {
  185. return $this->apiResponseSuccess('密码已重置,新密码为'.$password);
  186. } else {
  187. return $this->apiResponseError('密码重置失败');
  188. }
  189. }
  190. /**
  191. * 删除账号
  192. * @return \Illuminate\Http\JsonResponse
  193. */
  194. function destroys(){
  195. $id = request()->input('id','');//获取需要删除的id
  196. if(empty($id)) return $this->apiResponseError('缺少必要参数');
  197. //数据条件
  198. // $user_ids = array_filter(explode(',', $ids));
  199. // if(in_array(1,$user_ids)){
  200. // return $this->apiResponseError('超级管理员不能删除');
  201. // }
  202. if($id == 1){
  203. return $this->apiResponseError('超级管理员不能删除');
  204. }
  205. $res = User::where('id',$id)->where('id','<>',1)->update(['is_del' => 1,'status'=>2]);
  206. if ($res) {
  207. return $this->apiResponseSuccess('删除成功');
  208. } else {
  209. return $this->apiResponseError('删除失败');
  210. }
  211. }
  212. /**
  213. * 获取角色列表
  214. * @return \Illuminate\Http\JsonResponse
  215. */
  216. function getRoleList(){
  217. $id = request()->input('id', '');//搜索的内容
  218. $search = request()->input('search','');//获取搜索内容
  219. $start = request()->input('start','');//获取开始时间
  220. $end = request()->input('end','');//获取结束时间
  221. //查询数据条件
  222. $where = [];
  223. if($search) $where[] = ['name', 'like', "%$search%"];
  224. if($start) $where[] = ['created_at', '>=', $start];
  225. if($end) $where[] = ['created_at', '<=', $end];
  226. if($id) $where[] = ['id', $id];
  227. //获取数据
  228. $list = Role::where($where)
  229. ->orderBy('id','desc')
  230. ->select(['id','name','created_at'])
  231. ->paginate(10);
  232. foreach ($list as $value){
  233. $value['count'] = User::where([['is_del',0], ['roles_id',$value['id']]])->count();
  234. }
  235. return $this->apiResponseSuccess('获取列表成功', [
  236. 'items' => $list->items(),
  237. 'total' => $list->total(),
  238. 'limit' => 10,
  239. ]);
  240. }
  241. /**
  242. * 删除角色
  243. * @return \Illuminate\Http\JsonResponse
  244. */
  245. function roleDestroys(){
  246. $id = request()->input('id','');//获取需要删除的id
  247. if(empty($id)) return $this->apiResponseError('缺少必要参数');
  248. //检查每一个分类下面是否有商家
  249. $where = [['is_del',0], ['roles_id',$id]];
  250. $result = User::where($where)->first();
  251. if($result){
  252. return $this->apiResponseError('角色ID['.$id.']下还有账户,不能删除');
  253. }
  254. $res = Role::where('id',$id)->delete();
  255. if ($res) {
  256. return $this->apiResponseSuccess('删除成功');
  257. } else {
  258. return $this->apiResponseError('删除失败');
  259. }
  260. }
  261. /**
  262. * 新增编辑权限角色
  263. * @return \Illuminate\Http\JsonResponse
  264. */
  265. function saveRole(){
  266. $id = request()->input('id', '');//更新时id
  267. $name = request()->input('name', '');//角色名称
  268. $permission_ids = request()->input('permission_ids', '');//权限节点
  269. $status = request()->input('status', 1);
  270. if (empty($permission_ids)) {
  271. return $this->apiResponseError( '请选择权限');
  272. }
  273. if (empty($name)) {
  274. return $this->apiResponseError( '请输入角色名称');
  275. }
  276. $permission_ids = trim($permission_ids,',');
  277. $data = compact('name','permission_ids','status');
  278. if(empty($id)){
  279. $count = Role::where('name', '=', $name)->count();
  280. if ($count > 0 ) {
  281. return $this->apiResponseError( '该角色已存在');
  282. }
  283. $res = Role::create($data);
  284. }else{
  285. $info = Role::where('id',$id)->first();
  286. if(!$info) return $this->apiResponseError( '记录不存在');
  287. $count = Role::where('name', '=', $name)->where('id', '<>', $id)->first();
  288. if ($count > 0 ) {
  289. return $this->apiResponseError( '该角色已存在');
  290. }
  291. $res = $info->update($data);
  292. //更新该角色redis节点信息
  293. RedisDataServer::creatServer()->delData('role_ids_' . $id);
  294. }
  295. if ($res) {
  296. return $this->apiResponseSuccess('成功');
  297. } else {
  298. return $this->apiResponseError('失败');
  299. }
  300. }
  301. /**
  302. * 获取后台菜单
  303. * @return \Illuminate\Http\JsonResponse
  304. */
  305. function getMenuList(){
  306. $role_id = request()->input('role_id', '');
  307. //获取该角色的权限节点
  308. $permission_ids = Role::where('id', $role_id)->value('permission_ids');
  309. $url_ids = explode(',', $permission_ids);
  310. //获取数据
  311. $list = Menu::where([ ['p_id',0], ['level',0]])
  312. ->select(['id','menu_name'])
  313. ->orderBy('sort','asc')
  314. ->get();
  315. foreach ($list as $value){
  316. $value['is_check'] = in_array($value['id'],$url_ids) ? 1 : 0;
  317. $value['children'] = Menu::where([['level',1], ['p_id',$value['id']]])->orderBy('sort', 'asc')->select(['id','menu_name'])->get();
  318. foreach ($value['children'] as $item){
  319. $item['is_check'] = in_array($item['id'],$url_ids) ? 1 : 0;
  320. $item['children'] = Menu::where([['level',2], ['p_id',$item['id']]])->orderBy('sort', 'asc')->select(['id','menu_name'])->get();
  321. foreach ($item['children'] as $three){
  322. $three['is_check'] = in_array($three['id'],$url_ids) ? 1 : 0;
  323. }
  324. }
  325. }
  326. return $this->apiResponseSuccess('获取列表成功', [
  327. 'items' => $list,
  328. ]);
  329. }
  330. /**
  331. * 前端登录后获取后台菜单权限id
  332. * @return \Illuminate\Http\JsonResponse
  333. */
  334. function getMenuId(){
  335. $role_id = Request()->admin_user['roles_id'];//获取当前用户权限id
  336. if($role_id < 0) return $this->apiResponseError('权限错误');
  337. //获取该角色的权限节点
  338. $permission_ids = Role::where('id', $role_id)->value('permission_ids');
  339. $url_ids = explode(',', $permission_ids);
  340. if (empty($url_ids)) {
  341. return $this->apiResponseError('信息不存在');
  342. }
  343. $data = Menu::where(function ($q) use ($role_id,$url_ids){
  344. if($role_id){
  345. $q->whereIn('id',$url_ids);
  346. }
  347. })
  348. ->where(function ($q){
  349. $q->orWhere('level',0);
  350. $q->orWhere('level',1);
  351. })
  352. ->orderBy('sort','asc')
  353. ->orderBy('id','asc')
  354. ->pluck('id')
  355. ->toArray();
  356. return $this->apiResponseSuccess('获取信息成功', $data);
  357. }
  358. }