whereIn('role_name', ['管理员', '销售'])->lists('user_id'); $admin = Admin::whereIn('id', $ids)->get(); foreach($admin as $item){ #获取团队 $team = DB::table('teams')->select('name')->where('id', $item->team_id)->first(); $item->team_name = isset($team->name) ? $team->name : ''; #获取角色名称 $roleName = DB::table('admin_role')->where('user_id',$item->id)->pluck('role_name'); $item->role_name = $roleName; } return view('admin/adminall', ['admin' => $admin]); } /** * @return \Illuminate\View\View */ public function teamindex() { $team = DB::table('teams')->get(); $team = json_decode(json_encode($team),true); foreach($team as $k=>&$v){ if($v['type'] == 1){ $v['type'] = '销售团队'; }elseif($v['type'] == 2){ $v['type'] = '分销团队'; }else{ $v['type'] = '其他'; } } return view('admin/teamindex', ['team' => $team]); } /** * @return \Illuminate\View\View */ public function create() { $roles = Role::select('id','name')->where('is_show', 1)->where('name', '!=', '超级管理员')->where('name', '!=', '售后管理员')->groupBy('name')->get(); $team = DB::table('teams')->get(); $team = json_decode(json_encode($team),true); return view('admin/admincreate', ['roles' => $roles, 'team'=>$team]); } public function del($id){ if($id > 0 ){ Admin::where('id', $id)->update(['is_use'=> 0]); // exit('1'); echo '1';die; } // exit('0'); echo '0';die; } public function up($id){ if($id > 0 ){ Admin::where('id', $id)->update(['is_use'=> 1]); // exit('1'); echo '1';die; } // exit('0'); echo '0';die; } /** * @param Request $request * @return \Illuminate\Http\RedirectResponse */ public function store(Request $request) { $this->validate($request, [ 'username' => 'required|unique:admin', 'password' => 'required|confirmed', 'password_confirmation' => 'required', 'roles' => 'required', 'phone' => 'regex:/^1[3456789]\d{9}$/', ], [ 'username.required' => '登录名称不能为空', 'username.unique' => '登录名称已存在', 'password.required' => '密码不能为空', 'password_confirmation.required' => '再次输入密码为空', 'password.confirmed' => '两次密码不一致', 'roles.required' => '请至少选择一个角色', 'phone.regex' => '手机号格式有误', ]); $admin = new Admin(); $admin->username = $request->input('username'); $admin->password = bcrypt($request->input('password')); $admin->realname = $request->input('realname'); $admin->team_id = $request->input('team_id'); $admin->wx_id = $request->input('wx_id'); $admin->phone = $request->input('phone'); $admin->is_use = 1; //图片上传 阿里云oss if ($request->hasFile('qrcode') && $request->file('qrcode')->isValid()) { $file = $request->file('qrcode'); $ossClient=new oss(); // 上传阿里云 $file = $ossClient->upload($file->getClientOriginalExtension(), $file->getRealPath(), 'upload/seafoodPic'.date("Y-m-d",time()).'/'.date('His')); $qrcode=$file['oss-request-url']; $admin->qrcode=str_replace("kx-youhuiquan.oss-cn-beijing.aliyuncs.com","imgs.726p.com",$qrcode); } $admin->save(); $user_id = $admin->id; $roles = $request->input('roles'); foreach($roles as $v){ $admin_role = new AdminRole(); $admin_role->user_id = $user_id; $admin_role->role_name = $v; $admin_role->create_time = time(); $admin_role->save(); } return redirect('/admin/admin/all')->with('info', '添加管理员成功'); } /** * @param $id * @return \Illuminate\View\View */ public function edit($id) { $admin = Admin::findOrFail($id);//帐号密码信息 $admin_roles = AdminRole::select('role_name')->where('user_id', '=', $id)->get();//角色信息 $roles = Role::select('id','name')->where('is_show', 1)->where('name', '!=', '超级管理员')->where('name', '!=', '售后管理员')->groupBy('name')->get(); $team = DB::table('teams')->get(); $team = json_decode(json_encode($team),true); return view('admin/adminedit', ['roles' => $roles, 'admin' => $admin, 'admin_roles' => $admin_roles, 'team'=>$team]); } /** * @param Request $request * @return \Illuminate\Http\RedirectResponse */ public function update(Request $request) { $this->validate($request, [ 'password' => 'confirmed', 'roles' => 'required', 'phone' => 'regex:/^1[3456789]\d{9}$/', ], [ 'password.confirmed' => '两次密码不一致', 'roles.required' => '请至少选择一个角色', 'phone.regex' => '手机号格式有误', ]); $password = $request->input('password'); $user_id = $request->input('id'); $admin = Admin::findOrFail($user_id); $admin->realname = $request->input('realname'); $admin->team_id = $request->input('team_id'); $admin->wx_id = $request->input('wx_id'); $admin->phone = $request->input('phone'); if($password){ $admin->password = bcrypt($password); } //图片上传 阿里云oss if ($request->hasFile('qrcode') && $request->file('qrcode')->isValid()) { $file = $request->file('qrcode'); $ossClient=new oss(); // 上传阿里云 $file = $ossClient->upload($file->getClientOriginalExtension(), $file->getRealPath(), 'upload/seafoodPic'.date("Y-m-d",time()).'/'.date('His')); $qrcode=$file['oss-request-url']; $admin->qrcode=str_replace("kx-youhuiquan.oss-cn-beijing.aliyuncs.com","imgs.726p.com",$qrcode); } $admin->save(); $roles = $request->input('roles'); $admin_role = new AdminRole(); $admin_role->where('user_id', '=', $user_id)->delete(); foreach($roles as $v){ $admin_role = new AdminRole(); $admin_role->user_id = $user_id; $admin_role->role_name = $v; $admin_role->create_time = time(); $admin_role->save(); } return redirect('/admin/admin/all')->with('info', '修改管理员成功'); } /** * @return \Illuminate\View\View */ public function teamcreate() { return view('admin/teamcreate'); } /** * @param Request $request * @return \Illuminate\Http\RedirectResponse */ public function teamstore(Request $request) { $this->validate($request, [ 'name' => 'required|unique:teams', ], [ 'name.required' => '团队名称不能为空', 'name.unique' => '团队名称已存在', ]); $data = array(); $data['name'] = $request->input('name'); $data['type'] = (int)$request->input('type'); $res = DB::table('teams')->insert($data); return redirect('/admin/admin/teamindex')->with('info', '添加成功'); } /** * @param $id * @return \Illuminate\View\View */ public function teamedit($id) { $team = DB::table('teams')->where('id', $id)->first(); $team = json_decode(json_encode($team),true); return view('admin/teamedit', ['team' => $team]); } /** * @param Request $request * @return \Illuminate\Http\RedirectResponse */ public function teamupdate(Request $request) { $id = (int)$request->input('id'); $this->validate($request, [ 'name' => 'required|unique:teams,name,'.$id.',id', 'id' => 'required' ], [ 'name.required' => '团队名称不能为空', 'name.unique' => '团队名称已存在', ]); $data = array(); $data['name'] = $request->input('name'); $data['type'] = (int)$request->input('type'); $res = DB::table('teams')->where('id', $id)->update($data); return redirect('/admin/admin/teamindex')->with('info', '修改成功'); } /** * 销售上传二维码 */ public function uploadQrcodeEdit(Request $request){ $admin_id = session('admin_id'); $qrcode = Admin::where('id', $admin_id)->pluck('qrcode'); return view('admin/uploadqrcode', ['qrcode'=>$qrcode, 'id'=>$admin_id]); } /** * 销售上传二维码 */ public function uploadQrcode(Request $request){ $admin_id = session('admin_id'); //图片上传 阿里云oss $qrcode = null; if ($request->hasFile('img') && $request->file('img')->isValid()) { $file = $request->file('img'); $ossClient=new oss(); // 上传阿里云 $file = $ossClient->upload($file->getClientOriginalExtension(), $file->getRealPath(), 'upload/seafoodPic'.date("Y-m-d",time()).'/'.date('His')); $img=$file['oss-request-url']; $qrcode=str_replace("kx-youhuiquan.oss-cn-beijing.aliyuncs.com","imgs.726p.com",$img); } if($qrcode){ $res = Admin::where('id', $admin_id)->update(['qrcode'=>$qrcode]); return redirect('/admin/admin/uploadQrcodeEdit')->with('info', '上传成功'); } return redirect('/admin/admin/uploadQrcodeEdit')->with('info', '未做修改'); } /** * 管理自己团队的成员 */ public function teamSalers(Request $request) { $self_role = session('role_name'); if($self_role == '超级管理员'){ $admin = Admin::get(); }else{ $admin_id = session('admin_id'); $team_id = Admin::where('id', $admin_id)->pluck('team_id'); $admin = Admin::where('team_id', $team_id)->get(); } return view('admin/teamSalers', ['admin' => $admin]); } public function qrcode_del($id){ if($id > 0 ){ Admin::where('id', $id)->update(['is_qrcode_use'=> 0]); // exit('1'); echo '1';die; } // exit('0'); echo '0';die; } public function qrcode_up($id){ if($id > 0 ){ Admin::where('id', $id)->update(['is_qrcode_use'=> 1]); // exit('1'); echo '1';die; } // exit('0'); echo '0';die; } }