|
@@ -0,0 +1,400 @@
|
|
|
|
|
+<?php
|
|
|
|
|
+
|
|
|
|
|
+namespace App\Http\Controllers\AdminApi;
|
|
|
|
|
+
|
|
|
|
|
+use App\Models\Menu;
|
|
|
|
|
+use App\Models\Role;
|
|
|
|
|
+use App\Models\User;
|
|
|
|
|
+use App\Servers\MenuServer;
|
|
|
|
|
+use App\Servers\PassServer;
|
|
|
|
|
+use App\Servers\RedisDataServer;
|
|
|
|
|
+use Illuminate\Http\Request;
|
|
|
|
|
+use App\Http\Controllers\FrontController;
|
|
|
|
|
+
|
|
|
|
|
+class UserController extends FrontController
|
|
|
|
|
+{
|
|
|
|
|
+ public function __construct()
|
|
|
|
|
+ {
|
|
|
|
|
+ parent::__construct();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 后台账号列表
|
|
|
|
|
+ * @return \Illuminate\Http\JsonResponse
|
|
|
|
|
+ */
|
|
|
|
|
+ function getList(){
|
|
|
|
|
+ $id = request()->input('id', '');//搜索的内容
|
|
|
|
|
+ $search = request()->input('search','');//获取搜索内容
|
|
|
|
|
+ $status = request()->input('status','');//获取状态
|
|
|
|
|
+ $role_id = request()->input('role_id','');//获取角色id
|
|
|
|
|
+ $start = request()->input('start','');//获取开始时间
|
|
|
|
|
+ $end = request()->input('end','');//获取结束时间
|
|
|
|
|
+
|
|
|
|
|
+ //查询数据条件
|
|
|
|
|
+ $where = [['is_del',0]];
|
|
|
|
|
+ $sql = "(concat(name,phone) like '%" . $search . "%')";
|
|
|
|
|
+
|
|
|
|
|
+ if($id) $where[] = ['id', '=', $id];
|
|
|
|
|
+ if($status) $where[] = ['status', '=', $status];
|
|
|
|
|
+ if($role_id) $where[] = ['roles_id', '=', $role_id];
|
|
|
|
|
+ if($start) $where[] = ['created_at', '>=', $start];
|
|
|
|
|
+ if($end) $where[] = ['created_at', '<=', $end];
|
|
|
|
|
+
|
|
|
|
|
+ //获取数据
|
|
|
|
|
+ $list = User::where($where)
|
|
|
|
|
+ ->whereRaw($sql)
|
|
|
|
|
+ ->where('id','<>',1)
|
|
|
|
|
+ ->orderBy('id','desc')
|
|
|
|
|
+ ->select(['id','name','phone','status','roles_id','created_at'])
|
|
|
|
|
+ ->paginate(10);
|
|
|
|
|
+ foreach ($list as $value){
|
|
|
|
|
+ if($value['id'] == 1){
|
|
|
|
|
+ $value['role_name'] = '超级管理员';
|
|
|
|
|
+ }else{
|
|
|
|
|
+ $value['role_name'] = Role::where('id',$value['roles_id'])->value('name');
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return $this->apiResponseSuccess('获取列表成功', [
|
|
|
|
|
+ 'items' => $list->items(),
|
|
|
|
|
+ 'total' => $list->total(),
|
|
|
|
|
+ 'limit' => 10,
|
|
|
|
|
+ ]);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取用户详情
|
|
|
|
|
+ * @return \Illuminate\Http\JsonResponse
|
|
|
|
|
+ */
|
|
|
|
|
+ function getUserInfo(){
|
|
|
|
|
+
|
|
|
|
|
+ $id = request()->input('id', '');//记录id,更新时必须
|
|
|
|
|
+ if (empty($id)) return $this->apiResponseError( '缺少必要参数');
|
|
|
|
|
+ //查询数据
|
|
|
|
|
+ $info = User::where('id',$id)->select(['id','phone','name','roles_id'])->first();
|
|
|
|
|
+
|
|
|
|
|
+ if(empty($info)) return $this->apiResponseError('没有找到该记录');
|
|
|
|
|
+
|
|
|
|
|
+ return $this->apiResponseSuccess('获取成功', $info);
|
|
|
|
|
+ }
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 更新&编辑后台账号
|
|
|
|
|
+ * @return false|\Illuminate\Http\JsonResponse
|
|
|
|
|
+ */
|
|
|
|
|
+ function save(){
|
|
|
|
|
+ $id = request()->input('id', '');//记录id,更新时必须
|
|
|
|
|
+ $phone = request()->input('phone', '');//登录手机号
|
|
|
|
|
+ $name = request()->input('name', '');//姓名
|
|
|
|
|
+ $roles_id = request()->input('roles_id', 0);//角色id
|
|
|
|
|
+ $password = request()->input('password', '');//密码
|
|
|
|
|
+ $again_password = request()->input('again_password', '');//确认密码
|
|
|
|
|
+ if (empty($phone)) return $this->apiResponseError( '登录手机号必填');
|
|
|
|
|
+
|
|
|
|
|
+ if(!preg_match('/^1[3-9]\d{9}$/',$phone)){
|
|
|
|
|
+ return $this->apiResponseError( '手机号不正确');
|
|
|
|
|
+ }
|
|
|
|
|
+ if ($roles_id < 0 ) {
|
|
|
|
|
+ return $this->apiResponseError( '请选择角色');
|
|
|
|
|
+ }
|
|
|
|
|
+ $email = $phone.'@admin.com';
|
|
|
|
|
+
|
|
|
|
|
+ if(empty($id)){
|
|
|
|
|
+ if (empty($password) || empty($again_password)) {
|
|
|
|
|
+ return $this->apiResponseError( '请输入新密码');
|
|
|
|
|
+ }
|
|
|
|
|
+ if($password != $again_password){
|
|
|
|
|
+ return $this->apiResponseError( '两次密码不一致');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $count = User::where('phone', '=', $phone)->where('is_del',0)->count();
|
|
|
|
|
+ if ($count > 0 ) {
|
|
|
|
|
+ return $this->apiResponseError( '账号已注册');
|
|
|
|
|
+ }
|
|
|
|
|
+ //生成密码
|
|
|
|
|
+ $pass_server = PassServer::creatServer($password);
|
|
|
|
|
+ $new_pass = $pass_server->creatPassword();
|
|
|
|
|
+
|
|
|
|
|
+ $res = User::create([
|
|
|
|
|
+ 'name' => $name,
|
|
|
|
|
+ 'email' => $email,
|
|
|
|
|
+ 'phone' => $phone,
|
|
|
|
|
+ 'password' => $new_pass['password'],
|
|
|
|
|
+ 'roles_id' => $roles_id,
|
|
|
|
|
+ 'encrypt' => $new_pass['encrypt']
|
|
|
|
|
+ ]);
|
|
|
|
|
+ }else{
|
|
|
|
|
+ if (empty($id)) return $this->apiResponseError( '缺少必要参数');
|
|
|
|
|
+
|
|
|
|
|
+ $info = User::where('id',$id)->where('is_del',0)->first();
|
|
|
|
|
+ if(!$info) return $this->apiResponseError( '记录不存在');
|
|
|
|
|
+
|
|
|
|
|
+ $count = User::where('phone', '=', $phone)->where('is_del',0)->where('id', '<>', $id)->count();
|
|
|
|
|
+ if ($count > 0 ) {
|
|
|
|
|
+ return $this->apiResponseError( '账号已注册');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (!empty($password) ) {
|
|
|
|
|
+ if($password != $again_password){
|
|
|
|
|
+ return $this->apiResponseError( '两次密码不一致');
|
|
|
|
|
+ }
|
|
|
|
|
+ //生成密码
|
|
|
|
|
+ $pass_server = PassServer::creatServer($password, $info->{'encrypt'});
|
|
|
|
|
+ $new_pass = $pass_server->creatPassword();
|
|
|
|
|
+ $update['password'] = $new_pass['password'];
|
|
|
|
|
+ $update['encrypt'] = $new_pass['encrypt'];
|
|
|
|
|
+ }
|
|
|
|
|
+ if($id == 1){//超级管理员
|
|
|
|
|
+ $roles_id = 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ $update['email'] = $email;
|
|
|
|
|
+ $update['phone'] = $phone;
|
|
|
|
|
+ $update['name'] = $name;
|
|
|
|
|
+ $update['roles_id'] = $roles_id;
|
|
|
|
|
+ $res = $info->update($update);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if ($res) {
|
|
|
|
|
+ return $this->apiResponseSuccess('成功');
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return $this->apiResponseError('失败');
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 快速更新是否启用
|
|
|
|
|
+ * @return \Illuminate\Http\JsonResponse
|
|
|
|
|
+ */
|
|
|
|
|
+ function setStatus(){
|
|
|
|
|
+ $id = request()->input('id','');//记录id
|
|
|
|
|
+ $status = request()->input('status', 0);//是否显示
|
|
|
|
|
+ if(empty($id) || !in_array($status,[1,2])){
|
|
|
|
|
+ return $this->apiResponseError('缺少必要参数');
|
|
|
|
|
+ }
|
|
|
|
|
+ if($id == 1){
|
|
|
|
|
+ return $this->apiResponseError('超级管理员不能关闭');
|
|
|
|
|
+ }
|
|
|
|
|
+ $info = User::where('id',$id)->where('is_del',0)->select(['id'])->first();
|
|
|
|
|
+ if (empty($info)) {
|
|
|
|
|
+ return $this->apiResponseError('没有找到该记录');
|
|
|
|
|
+ }
|
|
|
|
|
+ $res = $info->update(['status'=>$status]);
|
|
|
|
|
+ if ($res) {
|
|
|
|
|
+ return $this->apiResponseSuccess('操作成功');
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return $this->apiResponseError('操作失败');
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 重置密码
|
|
|
|
|
+ * @return \Illuminate\Http\JsonResponse
|
|
|
|
|
+ */
|
|
|
|
|
+ function resetPassword(){
|
|
|
|
|
+ $id = request()->input('id', '');//重置密码的记录id
|
|
|
|
|
+ if(empty($id)) return $this->apiResponseError('缺少必要参数');
|
|
|
|
|
+ $info = User::where('id',$id)->where('is_del',0)->select(['id','encrypt'])->first();
|
|
|
|
|
+ if (empty($info)) {
|
|
|
|
|
+ return $this->apiResponseError('没有找到该记录');
|
|
|
|
|
+ }
|
|
|
|
|
+ $password = '123456';
|
|
|
|
|
+ //生成密码
|
|
|
|
|
+ $pass_server = PassServer::creatServer($password, $info->{'encrypt'});
|
|
|
|
|
+ $new_pass = $pass_server->creatPassword();
|
|
|
|
|
+ $update['password'] = $new_pass['password'];
|
|
|
|
|
+ $update['encrypt'] = $new_pass['encrypt'];
|
|
|
|
|
+
|
|
|
|
|
+ $res = $info->update($update);
|
|
|
|
|
+ if ($res) {
|
|
|
|
|
+ return $this->apiResponseSuccess('密码已重置,新密码为'.$password);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return $this->apiResponseError('密码重置失败');
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 删除账号
|
|
|
|
|
+ * @return \Illuminate\Http\JsonResponse
|
|
|
|
|
+ */
|
|
|
|
|
+ function destroys(){
|
|
|
|
|
+ $id = request()->input('id','');//获取需要删除的id
|
|
|
|
|
+ if(empty($id)) return $this->apiResponseError('缺少必要参数');
|
|
|
|
|
+ //数据条件
|
|
|
|
|
+// $user_ids = array_filter(explode(',', $ids));
|
|
|
|
|
+// if(in_array(1,$user_ids)){
|
|
|
|
|
+// return $this->apiResponseError('超级管理员不能删除');
|
|
|
|
|
+// }
|
|
|
|
|
+ if($id == 1){
|
|
|
|
|
+ return $this->apiResponseError('超级管理员不能删除');
|
|
|
|
|
+ }
|
|
|
|
|
+ $res = User::where('id',$id)->where('id','<>',1)->update(['is_del' => 1,'status'=>2]);
|
|
|
|
|
+ if ($res) {
|
|
|
|
|
+ return $this->apiResponseSuccess('删除成功');
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return $this->apiResponseError('删除失败');
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取角色列表
|
|
|
|
|
+ * @return \Illuminate\Http\JsonResponse
|
|
|
|
|
+ */
|
|
|
|
|
+ function getRoleList(){
|
|
|
|
|
+ $id = request()->input('id', '');//搜索的内容
|
|
|
|
|
+ $search = request()->input('search','');//获取搜索内容
|
|
|
|
|
+ $start = request()->input('start','');//获取开始时间
|
|
|
|
|
+ $end = request()->input('end','');//获取结束时间
|
|
|
|
|
+ //查询数据条件
|
|
|
|
|
+ $where = [];
|
|
|
|
|
+ if($search) $where[] = ['name', 'like', "%$search%"];
|
|
|
|
|
+ if($start) $where[] = ['created_at', '>=', $start];
|
|
|
|
|
+ if($end) $where[] = ['created_at', '<=', $end];
|
|
|
|
|
+ if($id) $where[] = ['id', $id];
|
|
|
|
|
+
|
|
|
|
|
+ //获取数据
|
|
|
|
|
+ $list = Role::where($where)
|
|
|
|
|
+ ->orderBy('id','desc')
|
|
|
|
|
+ ->select(['id','name','created_at'])
|
|
|
|
|
+ ->paginate(10);
|
|
|
|
|
+ foreach ($list as $value){
|
|
|
|
|
+ $value['count'] = User::where([['is_del',0], ['roles_id',$value['id']]])->count();
|
|
|
|
|
+ }
|
|
|
|
|
+ return $this->apiResponseSuccess('获取列表成功', [
|
|
|
|
|
+ 'items' => $list->items(),
|
|
|
|
|
+ 'total' => $list->total(),
|
|
|
|
|
+ 'limit' => 10,
|
|
|
|
|
+ ]);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 删除角色
|
|
|
|
|
+ * @return \Illuminate\Http\JsonResponse
|
|
|
|
|
+ */
|
|
|
|
|
+ function roleDestroys(){
|
|
|
|
|
+ $id = request()->input('id','');//获取需要删除的id
|
|
|
|
|
+ if(empty($id)) return $this->apiResponseError('缺少必要参数');
|
|
|
|
|
+
|
|
|
|
|
+ //检查每一个分类下面是否有商家
|
|
|
|
|
+ $where = [['is_del',0], ['roles_id',$id]];
|
|
|
|
|
+ $result = User::where($where)->first();
|
|
|
|
|
+ if($result){
|
|
|
|
|
+ return $this->apiResponseError('角色ID['.$id.']下还有账户,不能删除');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $res = Role::where('id',$id)->delete();
|
|
|
|
|
+ if ($res) {
|
|
|
|
|
+ return $this->apiResponseSuccess('删除成功');
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return $this->apiResponseError('删除失败');
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 新增编辑权限角色
|
|
|
|
|
+ * @return \Illuminate\Http\JsonResponse
|
|
|
|
|
+ */
|
|
|
|
|
+ function saveRole(){
|
|
|
|
|
+ $id = request()->input('id', '');//更新时id
|
|
|
|
|
+ $name = request()->input('name', '');//角色名称
|
|
|
|
|
+ $permission_ids = request()->input('permission_ids', '');//权限节点
|
|
|
|
|
+ $status = request()->input('status', 1);
|
|
|
|
|
+ if (empty($permission_ids)) {
|
|
|
|
|
+ return $this->apiResponseError( '请选择权限');
|
|
|
|
|
+ }
|
|
|
|
|
+ if (empty($name)) {
|
|
|
|
|
+ return $this->apiResponseError( '请输入角色名称');
|
|
|
|
|
+ }
|
|
|
|
|
+ $permission_ids = trim($permission_ids,',');
|
|
|
|
|
+
|
|
|
|
|
+ $data = compact('name','permission_ids','status');
|
|
|
|
|
+
|
|
|
|
|
+ if(empty($id)){
|
|
|
|
|
+ $count = Role::where('name', '=', $name)->count();
|
|
|
|
|
+ if ($count > 0 ) {
|
|
|
|
|
+ return $this->apiResponseError( '该角色已存在');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $res = Role::create($data);
|
|
|
|
|
+ }else{
|
|
|
|
|
+ $info = Role::where('id',$id)->first();
|
|
|
|
|
+ if(!$info) return $this->apiResponseError( '记录不存在');
|
|
|
|
|
+
|
|
|
|
|
+ $count = Role::where('name', '=', $name)->where('id', '<>', $id)->first();
|
|
|
|
|
+ if ($count > 0 ) {
|
|
|
|
|
+ return $this->apiResponseError( '该角色已存在');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $res = $info->update($data);
|
|
|
|
|
+ //更新该角色redis节点信息
|
|
|
|
|
+ RedisDataServer::creatServer()->delData('role_ids_' . $id);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if ($res) {
|
|
|
|
|
+ return $this->apiResponseSuccess('成功');
|
|
|
|
|
+ } else {
|
|
|
|
|
+ return $this->apiResponseError('失败');
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取后台菜单
|
|
|
|
|
+ * @return \Illuminate\Http\JsonResponse
|
|
|
|
|
+ */
|
|
|
|
|
+ function getMenuList(){
|
|
|
|
|
+ $role_id = request()->input('role_id', '');
|
|
|
|
|
+
|
|
|
|
|
+ //获取该角色的权限节点
|
|
|
|
|
+ $permission_ids = Role::where('id', $role_id)->value('permission_ids');
|
|
|
|
|
+ $url_ids = explode(',', $permission_ids);
|
|
|
|
|
+ //获取数据
|
|
|
|
|
+ $list = Menu::where([ ['p_id',0], ['level',0]])
|
|
|
|
|
+ ->select(['id','menu_name'])
|
|
|
|
|
+ ->orderBy('sort','asc')
|
|
|
|
|
+ ->get();
|
|
|
|
|
+ foreach ($list as $value){
|
|
|
|
|
+ $value['is_check'] = in_array($value['id'],$url_ids) ? 1 : 0;
|
|
|
|
|
+ $value['children'] = Menu::where([['level',1], ['p_id',$value['id']]])->orderBy('sort', 'asc')->select(['id','menu_name'])->get();
|
|
|
|
|
+ foreach ($value['children'] as $item){
|
|
|
|
|
+ $item['is_check'] = in_array($item['id'],$url_ids) ? 1 : 0;
|
|
|
|
|
+ $item['children'] = Menu::where([['level',2], ['p_id',$item['id']]])->orderBy('sort', 'asc')->select(['id','menu_name'])->get();
|
|
|
|
|
+ foreach ($item['children'] as $three){
|
|
|
|
|
+ $three['is_check'] = in_array($three['id'],$url_ids) ? 1 : 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return $this->apiResponseSuccess('获取列表成功', [
|
|
|
|
|
+ 'items' => $list,
|
|
|
|
|
+ ]);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 前端登录后获取后台菜单权限id
|
|
|
|
|
+ * @return \Illuminate\Http\JsonResponse
|
|
|
|
|
+ */
|
|
|
|
|
+ function getMenuId(){
|
|
|
|
|
+ $role_id = Request()->admin_user['roles_id'];//获取当前用户权限id
|
|
|
|
|
+
|
|
|
|
|
+ if($role_id < 0) return $this->apiResponseError('权限错误');
|
|
|
|
|
+ //获取该角色的权限节点
|
|
|
|
|
+ $permission_ids = Role::where('id', $role_id)->value('permission_ids');
|
|
|
|
|
+ $url_ids = explode(',', $permission_ids);
|
|
|
|
|
+
|
|
|
|
|
+ if (empty($url_ids)) {
|
|
|
|
|
+ return $this->apiResponseError('信息不存在');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $data = Menu::where(function ($q) use ($role_id,$url_ids){
|
|
|
|
|
+ if($role_id){
|
|
|
|
|
+ $q->whereIn('id',$url_ids);
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ ->where(function ($q){
|
|
|
|
|
+ $q->orWhere('level',0);
|
|
|
|
|
+ $q->orWhere('level',1);
|
|
|
|
|
+ })
|
|
|
|
|
+ ->orderBy('sort','asc')
|
|
|
|
|
+ ->orderBy('id','asc')
|
|
|
|
|
+ ->pluck('id')
|
|
|
|
|
+ ->toArray();
|
|
|
|
|
+
|
|
|
|
|
+ return $this->apiResponseSuccess('获取信息成功', $data);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|