request->only(["nickname"=>"","username"=>"","status"=>"","companyNo"=>"","uid"=>[],"nuid"=>[], "page"=>1,"size"=>10],"post"); $condition = [["a.is_del","=",0]]; isset($post['nickname'])&& $post['nickname']!="" ? $condition[]=["nickname","like","%{$post['nickname']}%"] : ""; isset($post['username'])&& $post['username']!="" ? $condition[]=["username","like","%{$post['username']}%"] : ""; isset($post['status'])&& $post['status']!=="" ? $condition[]=["a.status","=",$post['status']] : ""; isset($post['uid'])&& !empty($post['uid']) ? $condition[]=["a.id","in",$post['uid']] : ""; isset($post['nuid'])&& !empty($post['nuid']) ? $condition[]=["a.id","not in",$post['nuid']] : ""; if ($post['companyNo']!=""){ $uid =Db::name("account_company")->where(["companyCode"=>$post['companyNo'],"is_del"=>0])->column("account_id"); $condition[]=["a.id","in",$uid]; } $page = isset($post['page'])&& $post['page']!=="" ? intval($post['page']) : 1; $size = isset($post['size'])&& $post['size']!=="" ? intval($post['size']) : 10; $count = Db::name("account")->alias("a") ->leftJoin("user b","a.id=b.account_id and b.status=1") ->where($condition)->count(); $total =intval(ceil($count/$size)) ; $page = $total>=$page? $page:$total; $list = Db::name("account")->alias("a") ->leftJoin("user b","a.id=b.account_id and b.status=1") ->append(['plat', 'company_relaton']) ->withAttr('plat', function ($val, $da) { return Db::name("account_plat") ->alias("a") ->leftJoin("platform b", "a.plat_code=b.plat_code and b.is_del=0 and b.status=1") ->where(["a.status" => 1, "a.is_del" => 0, "a.account_id" => $da['id']]) ->field("a.plat_code,plat_name") ->select() ->toArray(); }) ->withAttr('company_relaton', function ($val, $da) { return Db::name("account_company") ->where(["account_id" => $da['id'], "is_del" => 0]) ->field("companyCode,companyName,company_type,is_main,status") ->select() ->toArray(); }) ->where($condition)->page($page,$size)->order("a.id desc") ->field("a.id,a.username,a.mobile,a.source,a.status,b.nickname,b.sex,b.email,a.addtime,a.updatetime") ->select()->toArray(); return json_show(0,"获取成功",["list"=>$list,"count"=>$count]); } /** @param int $id 账户id * @return \think\response\Json */ public function info() { $post=$this->request->only(["id"=>""],"post","intval"); if($post['id']==""){ return json_show(1003,"参数 id 不能为空"); } $list = Db::name("account")->alias("a") ->leftJoin("user b","a.id=b.account_id and b.status=1") ->where(["a.id"=>$post['id'],"a.is_del"=>0]) ->field("a.id,a.username,a.mobile,a.source,a.status,b.nickname,b.sex,b.email,a.addtime,a.updatetime") ->findOrEmpty(); if(empty($list)){ return json_show(1004,"未找到用户信息"); } $list['plat']= Db::name("account_plat")->alias("a") ->leftJoin("platform b","a.plat_code=b.plat_code and b.status=1") ->where(["a.status"=>1,"a.is_del"=>0,"a.account_id"=>$list['id']])->column("a.plat_code,plat_name"); $list['company_relaton'] = Db::name("account_company")->where(["account_id"=>$list['id'],"is_del"=>0,"status"=>1]) ->column("companyCode,companyName,company_type,is_main,status"); return json_show(0,"获取成功",$list); } /** * @return \think\response\Json|void * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function PassSet() { $post=$this->request->only(["id"=>"","password"=>""],"post","trim"); $validate=Validate::rule([ 'id|账户ID' => 'require|number', 'password|密码' => 'require|min:6|max:200', ]); if($validate->check($post)==false) return json_show(1004,$validate->getError()); $account=Db::name("account")->where("id","=",$post['id'])->find(); if(empty($account)){ return json_show(1003,"账户不存在"); } $salt=makeSalt(); $password = sha1($post['password'].$salt); $account['password']=$password; $account['salt']=$salt; $account['is_pass']=1; $account['updatetime']=date("Y-m-d H:i:s"); $up = Db::name("account")->save($account); return $up?json_show(0,"密码修改成功"):json_show(1005,"密码修改失败"); } /**@param int $id *@param array $company * @return \think\response\Json */ public function setCompany(){ $post = $this->request->only(["id"=>"","company"=>[]],"post"); $validate=Validate::rule([ 'id|账户ID' => 'require|number|gt:0', 'company|业务公司' => 'require|array', ]); if($validate->check($post)==false) return json_show(1004,$validate->getError()); $company_insert=[]; $acount =new AccountCompany(); foreach ($post['company'] as $company){ $ist=$acount->where(["account_id"=>$post['id'],"companyCode"=>$company['companyCode']])->find(); if($ist!=false)$company['id']=$ist['id']; $company_insert[]=[ "id"=>$company['id']??null, "account_id"=>$post['id'], "companyCode"=>$company['companyCode'], "companyName"=>$company['companyName'], "company_type"=>$company['company_type'], "is_main"=>$company['is_main'], "status"=>1, "is_del"=>$company['is_del']??0, "addtime"=>date("Y-m-d H:i:s"), "updatetime"=>date("Y-m-d H:i:s"), ]; } $inser =$acount->saveAll($company_insert); return $inser?json_show(0,"关联企业设置成功"):json_show(1005,"关联企业设置失败"); } /** * @param int $id * @param int $status * @return \think\response\Json * @throws \think\exception\DbException */ public function setCompanyStatus(){ $post = $this->request->only(["id"=>"","status"=>""],"post","intval"); $validate=Validate::rule([ 'id|主键ID' => 'require|number|gt:0', 'status|状态' => 'require|number|in:0,1', ]); if($validate->check($post)==false) return json_show(1004,$validate->getError()); $info=AccountCompany::where(["id"=>$post['id']])->find(); if($info==false){ return json_show(1004,"未找到数据"); } if($info['status']==$post['status']){ return json_show(1004,"数据已更新"); } $info['status']=$post['status']; $info['updatetime']=date("Y-m-d H:i:s"); $inser=AccountCompany::update($info->toArray()); return $inser?json_show(0,"关联企业状态设置成功"):json_show(1005,"关联企业状态设置失败"); } /** * @param int $id * @param string $nickname * @param int $mobile * @param string $email * @param string $portrait * @param int $sex * @return \think\response\Json */ public function UserSave() { $post = $this->request->only([ "id"=>"", "nickname"=>"", "mobile"=>"", "email"=>"", "portrait"=>"", "sex"=>"", ],"post"); $validate=Validate::rule([ 'id|主键ID' => 'require|number|gt:0', 'nickname|名称' => 'require|max:255', 'mobile|手机号' => 'require|number|length:11|mobile', 'email|名称' => 'email', 'sex|性别' => 'number|in:0,1,2', ]); if($validate->check($post)==false) return json_show(1004,$validate->getError()); $account=Db::name("account")->where([["id","=",$post['id']],["is_del","=",0]])->findOrEmpty(); if(empty($account)){ return json_show(1003,"账户不存在"); } $accountinfo=Db::name("user")->where([["account_id","=",$post['id']]])->findOrEmpty(); if(empty($accountinfo)){ return json_show(1003,"账户信息不存在"); } $uiq = Db::table("sys_account")->where([["mobile","=",$post['mobile']],["id","<>",$post['id']],["is_del","=",0]])->find(); if($uiq){ return json_show(1002,"手机号已存在!"); } Db::startTrans(); try{ $userinfo=[ "nickname"=>$post['nickname'], "mobile"=>$post['mobile'], "email"=>$post['email'], "portrait"=>$post['portrait'], "sex"=>$post['sex'], "updatetime"=>date("Y-m-d H:i:s") ]; $dat=Db::name("user")->where($accountinfo)->update($userinfo); if($dat==false){ Db::rollback(); return json_show(1004,"信息修改失败"); } $acc= [ "id"=>$post['id'], "mobile"=>$post['mobile'], "username"=>$post['mobile'], "updatetime"=>date("Y-m-d H:i:s"), ]; $nu = Db::name("account")->save($acc); if($nu){ Db::commit(); return json_show(0,"信息修改成功"); }else{ Db::rollback(); return json_show(1004,"账户信息修改失败"); } }catch (\Exception $e){ Db::rollback(); return json_show(1005,$e->getMessage()); } } /** * @param int $id * @param int $status * @return \think\response\Json * @throws \think\exception\DbException */ public function UserStatus() { $post = $this->request->only(["id"=>"","status"=>""],"post","trim"); $validate=Validate::rule([ 'id|主键ID' => 'require|number|gt:0', 'status|状态' => 'require|number|in:0,1', ]); if($validate->check($post)==false) return json_show(1004,$validate->getError()); $account=Account::where("id",$post['id'])->findOrEmpty(); if(empty($account)){ return json_show(1003,"账户不存在"); } if($account['status']==$post['status']){ return json_show(1004,"数据已更新"); } $message = $post['status']==1?"启用":"禁用"; $result= Db::name("account")->where("id","=",$post['id'])->save(['status'=>$post['status'],"updatetime"=>date("Y-m-d H:i:s")]); return $result?json_show(0,"账户{$message}成功") : json_show(1005,"账户{$message}失败"); } //根据业务公司获取用户数据 public function UserListByCompany() { $post = $this->request->only(["nickname" => "", "username" => "", "status" => "", "uid" => [], "nuid" => [], "companyNo" => "", "page" => 1, "size" => 10], "post"); $condition = [["a.is_del", "=", 0]]; $whereor = []; isset($post['nickname']) && $post['nickname'] != "" ? $condition[] = ["nickname", "like", "%{$post['nickname']}%"] : ""; isset($post['username']) && $post['username'] != "" ? $condition[] = ["username", "like", "%{$post['username']}%"] : ""; isset($post['status']) && $post['status'] !== "" ? $condition[] = ["a.status", "=", $post['status']] : ""; isset($post['uid']) && !empty($post['uid'])&& !empty($post['uid']) ? $condition[] = ["a.id", "in", $post['uid']] : ""; isset($post['nuid']) &&!empty($post['nuid']) && !empty($post['nuid']) ? $condition[] = ["a.id", "not in", $post['nuid']] : ""; isset($post['companyNo']) && $post['companyNo'] !== "" ? $condition[] = ["c.companyCode", "=", $post['companyNo']] : $whereor[] = ["c.companyCode", "=", null]; $page = isset($post['page']) && $post['page'] !== "" ? intval($post['page']) : 1; $size = isset($post['size']) && $post['size'] !== "" ? intval($post['size']) : 10; $count = Db::name("account") ->alias("a") ->leftJoin("user b", "a.id=b.account_id and b.status=1") ->leftJoin("account_company c", "a.id=c.account_id and c.status=1 and c.is_del=0") ->where($condition) ->whereOr($whereor) ->count(); $total = intval(ceil($count / $size)); $page = $total >= $page ? $page : $total; $list = Db::name("account") ->alias("a") ->field("a.id,a.username,a.mobile,a.source,a.status,b.nickname,b.sex,b.email,a.addtime,a.updatetime,companyCode,companyName,company_type,is_main,c.status as com_status") ->leftJoin("user b", "a.id=b.account_id and b.status=1") ->leftJoin("account_company c", "a.id=c.account_id and c.is_del=0") ->where($condition) ->whereOr($whereor) ->page($page, $size) ->append(['plat', 'company_relaton']) ->withAttr('plat', function ($val, $da) { return Db::name("account_plat") ->alias("a") ->leftJoin("platform b", "a.plat_code=b.plat_code and b.is_del=0 and b.status=1") ->where(["a.status" => 1, "a.is_del" => 0, "a.account_id" => $da['id']]) ->field("a.plat_code,plat_name") ->select() ->toArray(); }) ->withAttr('company_relaton', function ($val, $da) { return Db::name("account_company") ->where(["account_id" => $da['id'], "is_del" => 0]) ->field("companyCode,companyName,company_type,is_main,status") ->select() ->toArray(); }) ->order("a.addtime desc") ->select() ->toArray(); return json_show(0, "获取成功", ["list" => $list, "count" => $count]); } /** * @return \think\response\Json * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function userAdd(){ $post = $this->request->only(["nickname"=>"","mobile"=>"","email"=>"","companyArr"=>[]],"post","trim"); $validate=Validate::rule([ 'nickname|真实姓名' => 'require|min:2|max:200', 'mobile|手机号' => 'require|number|length:11|mobile', 'email|邮箱' => 'email', 'companyArr|关联业务公司' => 'array', ]); if($validate->check($post)==false) return json_show(1004,$validate->getError()); Db::startTrans(); $uiq = Db::table("sys_account")->where(["mobile"=>$post['mobile']])->find(); if($uiq){ return json_show(1002,"手机号已注册!"); } try { $salt =makeSalt(); $password = sha1("dingding123".$salt); $data = [ 'username'=>$post['mobile'], "password"=>$password, "salt"=>$salt, "mobile"=>$post['mobile'], "source"=>"paltadd", "status"=>1, "addtime"=>date("Y-m-d H:i:s"), "updatetime"=>date("Y-m-d H:i:s") ]; $reuslt = Db::table('sys_account')->insert($data,true); if($reuslt){ $data=[ "nickname"=>$post['nickname'], "mobile"=>$post['mobile'], "email"=>$post['email'], "portrait"=>"", "sex"=>1, "post"=>"", "department"=>"", "account_id"=>$reuslt, "status"=>1, "addtime"=>date("Y-m-d H:i:s"), "updatetime"=>date("Y-m-d H:i:s") ]; $user=Db::table("sys_user")->insert($data); if($user!=false){ $acount =new AccountCompany(); if(!empty($post['companyArr'])){ $company_insert=[]; foreach ($post['companyArr'] as $company){ $company_insert[]=[ "account_id"=>$reuslt, "companyCode"=>$company['companyCode'], "companyName"=>$company['companyName'], "company_type"=>$company['company_type'], "is_main"=>$company['is_main'], "status"=>1, "is_del"=>0, "addtime"=>date("Y-m-d H:i:s"), "updatetime"=>date("Y-m-d H:i:s"), ]; } $u= $acount->saveAll($company_insert); }else{ $company_insert=[ "account_id"=>$reuslt, "companyCode"=>'', "companyName"=>'', "company_type"=>'0', "is_main"=>1, "status"=>1, "is_del"=>0, "addtime"=>date("Y-m-d H:i:s"), "updatetime"=>date("Y-m-d H:i:s"), ]; $u= $acount->save($company_insert); } if($u==false)throw new Exception("账户新建失败"); Db::commit(); return json_show(0,"账户注册成功",["userid"=>$reuslt,"nickname"=>$post['nickname']]); } } Db::rollback(); return json_show(1002,"账户注册失败"); }catch (\Exception $e){ Db::rollback(); return json_show(1002,"账户注册失败".$e->getMessage()); } } }