123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656 |
- <?php
- declare (strict_types = 1);
- namespace app\admin\controller;
- use app\admin\BaseController;use think\App;use think\Exception;use think\facade\Db;use think\facade\Validate;
- class User extends BaseController{
- public function __construct(App $app) {parent::__construct($app);}
- /**
- * 设置供应商信息
- * @return \think\response\Json|void
- */
- public function EditUserCompany(){
- $post = $this->request->only(["token"=>"","nickname"=>"","id"=>"","mobile"=>"","companyArr"=>[]],"post");
- $valide = $validate=Validate::rule([
- 'nickname|真实姓名' => 'require|max:255|min:2',
- 'id|账户ID' => 'require|number|gt:0',
- 'mobile|手机号' => 'require|number|mobile',
- 'companyArr|业务公司' => 'require|array',
- ]);
- if($valide->check($post)==false) return error_show(1004,$valide->getError());
- $isSuper = Db::name("user_role")->alias("a")
- ->leftJoin("role b","a.roleid=b.id")
- ->where(["a.uid"=>$post['id'],"a.is_del"=>0,"b.level"=>1])
- ->findOrEmpty();
- if(!empty($isSuper) && !empty($post['companyArr']))return error_show(1002,"超管账户不允许设置关联业务公司");
- Db::startTrans();
- try{
- $saveinfo =[
- "id"=>$post['id'],
- "nickname"=>$post['nickname'],
- "mobile"=>$post['mobile'],
- "token"=>$post['token']
- ];
- $saev= resetinfo($saveinfo);
- if(!isset($saev['code'])||$saev['code']!=0){
- throw new Exception($saev['message']);
- }
- if(!empty($post['companyArr'])){
- $CompArr=[];
- $is_delArr= array_column($post['companyArr'],"is_del");
- foreach ($post['companyArr'] as &$item){
- $company =Db::name("supplier_info")->where(["code"=>$item['companyCode']])->find();
- $item['company_type'] = $company["type"]==3?2:1;
- $res = Db::name("user_role")->where(["uid"=>$post['id'],"is_del"=>0,"companyNo"=>$item['companyCode']])->findOrEmpty();
- if(!empty($res)){
- if($item['is_del']!=$res['is_del'] || $item['is_main']!=$res['is_main'])
- {
- $CompArr[]=$item;
- if($item['is_del']==1){
- if($item['is_main']==1 && in_array(0,$is_delArr)) throw new Exception("默认企业不可删除");
- $re = Db::name("user_role")->where(["uid"=>$post['id'],"is_del"=>0,"companyNo"=>$item['companyCode']])
- ->update(["is_del"=>1,"is_main"=>0,"updatetime"=>date("Y-m-d H:i:s")]);
- }else{
- $re = Db::name("user_role")->where(["uid"=>$post['id'],"is_del"=>0,"companyNo"=>$item['companyCode']])
- ->update(["nickname"=>$post['nickname'],"is_main"=>$item['is_main'],"updatetime"=>date("Y-m-d H:i:s")]);
- }
- if($re==false)throw new Exception("关联企业保存失败");
- }
- }else{
- $CompArr[]=$item;
- $data=[
- "uid"=>$post['id'],
- "nickname"=>$post['nickname'],
- "roleid"=>0,
- "companyNo"=>$item['companyCode'],
- "company_type"=>$item['company_type'],
- "status"=>1,
- "is_main"=>$item['is_main']
- ];
- $instr=Db::name("user_role")->insert($data);
- if($instr==false) throw new Exception("关联企业保存失败");
- }
- }
- if(!empty($CompArr)){
- $companyinfo = ["id"=>$post['id'],"company"=>$CompArr,"token"=>$post['token']];
- $companyset =setUserCompany($companyinfo);
- if(!isset($companyset['code'])||$companyset['code']!=0){
- throw new Exception($companyset['message']);
- }
- }
- }
- Db::commit();
- return app_show(0,"设置成功");
- }catch (Exception $e){
- Db::rollback();
- return error_show(1005,$e->getMessage());
- }
- }
- /**
- * 用户列表
- */
- public function userList(){
- $post=$this->request->only(["token"=>"","nickname"=>"","relaComNo"=>"","islevel"=>"","username"=>"","companyNo"=>"","status"=>"","page"=>1,"size"=>10],"post");
- if($this->level==1){
- $companyNo = $post['companyNo']??"";
- }else{
- $companyNo = $post['relaComNo']??"";
- $post['companyNo'] = $post['relaComNo']??"";
- }
- $where=[["a.is_del","=",0]];
- unset($post['relaComNo']);
- if($post['companyNo']!=""){
- $where[]=["a.companyNo","=",$companyNo];
- }
- if($post['status']!==''){
- $uid =Db::name("user_role")->alias("a")->where(["status"=>1,"is_del"=>0])->where($where)->column("uid");
- if($post['status']==1){
- $where[]=["a.status","=",1];
- $post['uid']= $uid;
- }else{
- $post['nuid']=$uid;
- }
- unset($post['status']);
- }
- if($post["islevel"]!=""){
- $uid =Db::name("user_role")->alias("a")
- ->leftJoin("role b","a.roleid=b.id")->where([["b.level","=",1],["a.is_del","=",0]])->where($where)->column
- ("uid");
- if($post['islevel']==1){
- $post['uid']= $uid;
- }else{
- $post['nuid']=$uid;
- }
- unset($post["islevel"]);
- }
- // var_dump($post);
- $userinfo=GetUserList($post);
- if(empty($userinfo)||$userinfo['code']!=0){
- return app_show($userinfo['code'],$userinfo['msg']);
- }
- $data = $userinfo['data']['list'];
- $list=[];
- foreach ($data as $value){
- $roleArr = Db::name("user_role")->alias("a")
- ->leftJoin("role b","a.roleid=b.id")
- ->where(["uid"=>$value['id'],"a.companyNo"=>$value['companyCode'],"is_del"=>0])
- ->field("role_name,roleid,a.status,b.level")->findOrEmpty();
- $value['roleid']=$roleArr['roleid']??"";
- $value['status']=$value['status']==1?($roleArr['status']??0) :0;
- $value["role_name"] =$roleArr['role_name']??"";
- $value["role_level"] =$roleArr['level']??"";
- $list[]=$value;
- }
- return app_show(0,"获取成功",["list"=>$list,"count"=>$userinfo['data']["count"]]);
- }
- /**
- * 用户列表
- */
- public function userSingleList(){
- $post=$this->request->only(["token"=>"","nickname"=>"","relaComNo"=>"","islevel"=>"","username"=>"","companyNo"=>"","status"=>"","page"=>1,"size"=>10],"post");
- if($this->level==1){
- $companyNo = $post['companyNo']??"";
- }else{
- $companyNo = $post['relaComNo']??"";
- $post['companyNo'] = $post['relaComNo']??"";
- }
- $where=[["a.is_del","=",0]];
- unset($post['relaComNo']);
- if($post['companyNo']!=""){
- $where[]=["a.companyNo","=",$companyNo];
- }
- if($post["islevel"]!=""){
- $uid =Db::name("user_role")->alias("a")
- ->leftJoin("role b","a.roleid=b.id")->where([["b.level","=",1],["a.is_del","=",0]])->where($where)->column
- ("uid");
- if($post['islevel']==1){
- $post['uid']= $uid;
- }else{
- $post['nuid']=$uid;
- }
- unset($post["islevel"]);
- }
- $userinfo=GetList($post);
- if(empty($userinfo)||$userinfo['code']!=0){
- return app_show($userinfo['code'],$userinfo['msg']);
- }
- $data = $userinfo['data']['list'];
- $list=[];
- foreach ($data as $value){
- if(isset($value['company_relaton'])&& !empty($value['company_relaton'])){
- foreach ($value['company_relaton'] as &$item){
- $roleArr = Db::name("user_role")->alias("a")
- ->leftJoin("role b","a.roleid=b.id")
- ->where(["uid"=>$value['id'],"a.companyNo"=>$item['companyCode'],"is_del"=>0])
- ->field("role_name,roleid,a.status,b.level")->findOrEmpty();
- $item['roleid']=$roleArr['roleid']??"";
- $item['status']=$roleArr['status']??0;
- $item["role_name"] =$roleArr['role_name']??"";
- $item["role_level"] =$roleArr['level']??"";
- }
- }
- $value['roleid']=$roleArr['roleid']??"";
- $value['status']=$value['status']??0;
- $value["role_name"] =$roleArr['role_name']??"";
- $value["role_level"] =$roleArr['level']??"";
- $list[]=$value;
- }
- return app_show(0,"获取成功",["list"=>$list,"count"=>$userinfo['data']["count"]]);
- }
- /**获取当前访问用户的信息
- * @param token
- * @return \think\response\Json
- * @throws \think\exception\DbException
- */
- public function userInfo(){
- $post =$this->request->only(["token"=>"","relaComNo"=>""],"post","trim");
- $userinfo=GetUserInfo($post['token']);
- if(empty($userinfo)||$userinfo['code']!=0){
- return app_show(1002,"员工信息不存在");
- }
- $data = $userinfo['data'];
- $where=[["uid","=", $data['id']],["is_del","=",0]];
- if($post['relaComNo']!=""){
- $where[]=["companyNo","=",$post['relaComNo']];
- }else{
- $where[]=["is_main","=",1];
- }
- $role =Db::name("user_role")->where($where)->findOrEmpty();
- if(!empty($role)){
- $company_name = Db::name("supplier_info")->where(["code"=>$role['companyNo']])->value("name","");
- $roleinfo = Db::name("role")->where(["id"=>$role['roleid']])->field("role_name,level")->find();
- }else{
- return error_show(1004,'未开通登录权限');
- }
- if(!empty($data['company_relaton'])){
- $roleArr = Db::name("user_role")->alias("a")
- ->leftJoin("role b","a.roleid=b.id")
- ->where(["a.is_del"=>0,"a.uid"=>$data['id']])
- ->column("role_name,roleid,a.status,b.level","a.companyNo");
- foreach ($data['company_relaton'] as &$item){
- $item['roleid'] =isset($roleArr[$item['companyCode']])?$roleArr[$item['companyCode']]['roleid']:"";
- $item['role_name'] =isset($roleArr[$item['companyCode']])?$roleArr[$item['companyCode']]['role_name']:"";
- $item['role_status'] =isset($roleArr[$item['companyCode']])?$roleArr[$item['companyCode']]['status']:"";
- $item['role_level'] =isset($roleArr[$item['companyCode']])?$roleArr[$item['companyCode']]['level']:"";
- }
- }
- $data['role_name']=$roleinfo['role_name']??'';
- $data['company_name']=$company_name??'';
- $data['role_level']=$roleinfo['level']??'';
- $data['role_id']=$role['roleid']??'';
- $data['companyNo']=$role['companyNo']??'';
- $data['company_type']=$role['company_type']??'';
- return app_show(0,"获取成功",$data);
- }
- /**@param id int 账户id
- /**@param status int 状态 0/1
- * @return \think\response\Json|void
- */
- public function setStatus(){
- $post =$this->request->only(["id"=>"","status"=>"","token"=>""],"post");
- $valide =Validate::rule([
- 'id|账户ID' => 'require|number|gt:0',
- 'status|状态' => 'require|number|in:0,1',
- ]);
- if($valide->check($post)==false) return error_show(1004,$valide->getError());
- $message = $post['status']==1?"启用":"禁用";
- if($post['id']==$this->uid && $post['status']==0){
- return error_show(1004,'不可禁用当前账户');
- }
- Db::startTrans();
- try{
- $account = Db::name("user_role")->where(["uid"=>$post['id'],"is_del"=>0])->update(["status"=>$post['status'],"updatetime"=>date("Y-m-d H:i:s")]);
- if($account==false){
- throw new Exception("账户{$message}失败,账户未在系统中开通权限");
- }
- $condition=["id"=>$post['id'],"status"=>$post['status'],"token"=>$post['token']];
- $ex=setStatus($condition);
- if(!isset($ex['code'])||$ex['code']!=0){
- throw new Exception($ex['message']);
- }
- Db::commit();
- return app_show(0,"{$message}成功");
- }catch (\Exception $e){
- Db::rollback();
- return error_show(1006,$e->getMessage());
- }
- }
- public function setCompanyStatus(){
- $post =$this->request->only(["id"=>"","status"=>"","relaComNo"=>"","companyNo"=>"","token"=>""],"post");
- $valide =Validate::rule([
- 'id|账户ID' => 'require|number|gt:0',
- 'status|状态' => 'require|number|in:0,1',
- 'relaComNo|业务公司' => 'max:255',
- 'companyNo|公司编号' => 'require|max:255',
- ]);
- if($valide->check($post)==false) return error_show(1004,$valide->getError());
- $message = $post['status']==1?"启用":"禁用";
- if($post['id']==$this->uid &&$post['companyNo']==$post['relaComNo']&& $post['status']==0){
- return error_show(1004,'不可禁用当前账户');
- }
- Db::startTrans();
- try{
- $userinfo =Db::name("user_role")->where(["uid"=>$post['id'],"companyNo"=>$post['companyNo'],"is_del"=>0])->findOrEmpty();
- if(!empty($userinfo)){
- // if($userinfo['is_main']==1) throw new Exception("默认账户不可禁用!");
- $userinfo['status']=$post['status'];
- $userinfo['updatetime']=date("Y-m-d H:i:s");
- $sa=Db::name("user_role")->save($userinfo);
- if($sa==false) throw new Exception("状态更新失败!");
- }
- $condition=["account_id"=>$post['id'],"companyCode"=>$post['companyNo'],"status"=>$post['status'],"token"=>$post['token']];
- $effect=setCompanyStatus($condition);
- if(!isset($effect['code'])||$effect['code']!=0)throw new Exception($effect['message']);
- Db::commit();
- return app_show(0,"{$message}成功");
- }catch (\Exception $e){
- Db::rollback();
- return error_show(1004,$e->getMessage());
- }
- }
- //获取所有用户的信息
- public function userAll(){
- $post=$this->request->only(['relaComNo'=>"","companyNo"=>"","nickname"=>"","roleid"=>"","status"=>""],"post","trim");
- $condition = [["is_del","=",0]];
- $rolecond=" level in (1,2,3)";
- if($post['relaComNo']!=""){
- $condition[]=["companyNo","=",$post['relaComNo']];
- $rolecond = "level=1 or (level in (2,3) and companyNo ='{$post['relaComNo']}' )";
- }
- if($post['companyNo']!=""){
- $condition[]=["companyNo","=",$post['companyNo']];
- $rolecond = "level=1 or (level in (2,3) and companyNo ='{$post['companyNo']}' )";
- }
- if($post['roleid']!=""){
- $condition[]=["roleid","=",$post['roleid']];
- $rolecond = "id={$post['roleid']}";
- }
- if($post['status']!="")$condition[]=["status","=",$post['status']];
- if($post['nickname']!="")$condition[]=["nickname","like","%{$post['nickname']}%"];
- $list=Db::name("user_role")->where($condition)->select()->toArray();
- $role = Db::name("role")->whereRaw($rolecond)->column("role_name,level","id");
- foreach ($list as &$value){
- $value['role_name']= $role[$value['roleid']]['role_name']??"";
- $value['role_level']= $role[$value['roleid']]['level']??"";
- $value['companyName']= Db::name("supplier_info")->where(["code"=>$value['companyNo']])->value("name",'');
- }
- return app_show(0,"获取成功",$list);
- }
- /**
- * 修改用户基础信息
- */
- public function setRole(){
- $post=$this->request->only(["id"=>"","nickname"=>"","mobile"=>"","relaComNo"=>"","companyNo"=>"","roleid"=>"",
- "token"=>""],"post","trim");
- $valide=Validate::rule([
- "id|账户ID"=>"require|number|gt:0",
- "nickname|名称"=>"require|max:255",
- "mobile|手机号"=>"require|mobile",
- "relaComNo|业务公司"=>"max:255",
- "companyNo|业务公司"=>"max:255",
- "roleid|角色ID"=>"require|number",
- ]);
- if($valide->check($post)==false) return error_show(1004,$valide->getError());
- if($post['roleid']!=0){
- $roleinfo =Db::name("role")->where("id","=",$post['roleid'])->find();
- if(empty($roleinfo)){
- return error_show(1002,"角色不存在");
- }
- if($roleinfo['status']==0){
- return error_show(1002,"角色已禁用");
- }
- if($roleinfo['level']==1){
- return error_show(1002,"超管级别角色不允许设置");
- }
- }
- if($this->level==1){
- $companyNo = isset($post['companyNo'])&&$post['companyNo']!=='' ? trim($post['companyNo']) :"";
- }else{
- $companyNo = isset($post['relaComNo'])&&$post['relaComNo']!=='' ? trim($post['relaComNo']) :"";
- }
- if($companyNo==""){
- return error_show(1004,"业务公司编号不能为空");
- }
- $company =Db::name("supplier_info")->where(["code"=>$companyNo])->find();
- if($company==false){
- return error_show(1004,"业务公司不存在");
- }
- $isSuper = Db::name("user_role")->alias("a")
- ->leftJoin("role b","a.roleid=b.id")
- ->where(["a.uid"=>$post['id'],"a.is_del"=>0,"b.level"=>1])
- ->findOrEmpty();
- if(!empty($isSuper))return error_show(1002,"超管账户不允许设置其他角色");
- $userrole = Db::name("user_role")
- ->where(["uid"=>$post['id'],"companyNo"=>$companyNo,"is_del"=>0])
- ->find();
- $ismain =Db::name("user_role")->where(["uid"=>$post['id'],"is_main"=>1,"is_del"=>0])->find();
- Db::startTrans();
- try{
- if(empty($userrole)){
- $roledata =[
- "uid"=>$post['id'],
- "roleid"=>$post['roleid'],
- "nickname"=>$post['nickname'],
- "companyNo"=>$companyNo,
- "company_type"=>$company["type"]==3?2:1,
- "is_main"=>$ismain?0:1,
- "status"=>1,
- "addtime"=>date("Y-m-d H:i:s"),
- "updatetime"=>date("Y-m-d H:i:s")
- ];
- $companyarr=[
- [
- "companyCode"=>$company["code"],
- "companyName"=>$company["name"],
- "company_type"=>$company["type"]==3?2:1,
- "is_main"=>$ismain?0:1
- ]
- ];
- $companyinfo = ["id"=>$post['id'],"company"=>$companyarr,"token"=>$post['token']];
- $companyset =setUserCompany($companyinfo);
- if(!isset($companyset['code'])||$companyset['code']!=0){
- return error_show(1004,$companyset['message']);
- }
- $up= Db::name("user_role")->insert($roledata);
- }else{
- $userrole['roleid'] = $post['roleid'];
- $userrole['nickname'] = $post['nickname'];
- $userrole['companyNo'] = $companyNo;
- $userrole['company_type'] =$company["type"]==3?2:1;
- $userrole['updatetime'] = date("Y-m-d H:i:s");
- $up=Db::name("user_role")->where(["uid"=>$post['id'],"companyNo"=>$companyNo,"is_del"=>0])->save($userrole);
- }
- $saveinfo =[
- "id"=>$post['id'],
- "nickname"=>$post['nickname'],
- "mobile"=>$post['mobile'],
- "token"=>$post['token']
- ];
- $saev= resetinfo($saveinfo);
- if(!isset($saev['code'])||$saev['code']!=0){
- return error_show(1004,$saev['message']);
- }
- if($up==false){
- Db::rollback();
- return error_show(1004,'用户信息更新失败');
- }
- Db::commit();
- return app_show(0,'用户信息更新成功');
- }catch (\Exception $e){
- Db::rollback();
- return error_show(1004,$e->getMessage());
- }
- }
- /**获取用户关联业务公司
- * @return \think\response\Json|void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function GetCompany(){
- $post=$this->request->only(["token"=>"","uid"=>"","companyName"=>"","page"=>1,"size"=>20],"post","trim");
- $where=[['a.is_del',"=",0]];
- if($post['uid']!=""){
- $where[]=["a.uid","=",$post['uid']];
- }else{
- $where[]=["a.uid","=",$this->uid];
- }
- if($post['companyName']!=""){
- $where[]=["b.name","like","%{$post['companyName']}%"];
- }
- $count = Db::name("user_role")->alias("a")
- ->leftJoin("supplier_info b","a.companyNo=b.code")
- ->where($where)
- ->count();
- $total =ceil($count/$post['size']);
- $page = $post['page']>$total ? intval($total):intval($post['page']);
- $list = Db::name("user_role")->alias("a")
- ->leftJoin("supplier_info b","a.companyNo=b.code")
- ->where($where)
- ->field("a.uid,a.nickname,roleid,companyNo,b.name as companyName,a.status,a.company_type")
- ->page($page,intval($post['size']))
- ->order("a.is_main desc")
- ->select()->toArray();
- foreach ($list as &$value){
- $value['role_name'] = Db::name("role")->where(["id"=>$value["roleid"]])->value("role_name","");
- }
- return app_show(0,'获取成功',["list"=>$list,"count"=>$count]);
- }
- //密码设置
- public function setPwd(){
- $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 error_show(1004,$validate->getError());
- $data = resetpwd($post);
- if(empty($data)||$data['code']!=0){
- return error_show($data['code'],$data['message']);
- }else{
- return app_show(0,"密码修改成功");
- }
- }
- public function usersave(){
- $post=$this->request->only(["id"=>"","nickname"=>"","mobile"=>"","email"=>"","relaComNo"=>"","companyArr"=>[],"token"=>""],"post","trim");
- $validate=Validate::rule([
- 'id|账户ID' => 'number',
- 'nickname|真实姓名' => 'require|min:2|max:200',
- 'mobile|手机号' => 'require|number|length:11|mobile',
- 'email|邮箱' => 'email',
- 'companyArr|关联账户' => 'array',
- ]);
- if($validate->check($post)==false) return error_show(1004,$validate->getError());
- if(!empty($post['companyArr'])){
- foreach ($post['companyArr'] as &$item){
- $company =Db::name("supplier_info")->where(["code"=>$item['companyCode']])->find();
- $item['company_type'] = $company["type"]==3?2:1;
- }
- }
- if($post['id']!=""){
- $isSuper = Db::name("user_role")->alias("a")
- ->leftJoin("role b","a.roleid=b.id")
- ->where(["a.uid"=>$post['id'],"a.is_del"=>0,"b.level"=>1])
- ->findOrEmpty();
- if(!empty($isSuper) && !empty($post['companyArr']))return error_show(1002,"超管账户不允许设置关联业务公司");
- $saveinfo =[
- "id"=>$post['id'],
- "nickname"=>$post['nickname'],
- "mobile"=>$post['mobile'],
- "token"=>$post['token']
- ];
- $saev= resetinfo($saveinfo);
- if(!isset($saev['code'])||$saev['code']!=0){
- return error_show(1004,$saev['message']);
- }
- if(!empty($post['companyArr'])){
- $companyinfo = ["id"=>$post['id'],"company"=>$post['companyArr'],"token"=>$post['token']];
- $companyset =setUserCompany($companyinfo);
- if(!isset($companyset['code'])||$companyset['code']!=0){
- return error_show(1004,$companyset['message']);
- }
- }
- }else{
- $userrole=[];
- if($post['relaComNo']!=""){
- $company =Db::name("supplier_info")->where(["code"=>$post['relaComNo']])->find();
- if($company){
- $temp = [
- "companyCode"=>$company["code"],
- "companyName"=>$company["name"],
- "company_type"=>$company["type"]==3?2:1,
- "is_main"=>1,
- ];
- $post['companyArr'][]=$temp;
- }
- }else{
- if(!empty($post['companyArr'])){
- foreach ($post['companyArr'] as &$item){
- $company =Db::name("supplier_info")->where(["code"=>$item['companyCode']])->find();
- $item['company_type'] = $company["type"]==3?2:1;
- }
- }else{
- $post['companyArr'][]= [
- "companyCode"=>'',
- "companyName"=>'',
- "company_type"=>0,
- "is_main"=>1,
- ];
- $userrole=[
- "uid"=>0,
- "nickname"=>'',
- "roleid"=>1,
- "companyNo"=>'',
- "company_type"=>'',
- "status"=>1,
- "is_main"=>1,
- "is_del"=>0,
- "addtime"=>date("Y-m-d H:i:s"),
- "updatetime"=>date("Y-m-d H:i:s"),
- ];
- }
- }
- $nam = addacount($post);
- if(!isset($nam['code'])||$nam['code']!=0){
- return error_show(1004,$nam['message']);
- }
- if (!empty($userrole)){
- $userrole['uid'] = $nam['data']["userid"];
- $userrole['nickname'] = $nam['data']["nickname"];
- $up =Db::name("user_role")->insert($userrole);
- if($up==false){
- return error_show(1004,'账户超管角色分配失败');
- }
- }
- }
- return $post['id']==""?app_show(0,"账户新建成功"):app_show(0,"账户编辑成功");
- }
- //添加超管账号
- public function adminAccountAdd()
- {
- $post = $this->request->only(['nickname', 'mobile', 'email' => '', 'relaComNo' => ''], 'post', 'trim');
- $validate = Validate::rule([
- 'nickname|真实姓名' => 'require|min:2|max:200',
- 'mobile|手机号' => 'require|mobile',
- 'email|邮箱' => 'email|max:255',
- 'companyArr|关联账户' => 'array|max:100',
- ]);
- if ($validate->check($post) == false) return error_show(1004, $validate->getError());
- $post['companyArr'][] = [
- "companyCode" => '',
- "companyName" => '',
- "company_type" => 0,
- "is_main" => 1,
- ];
- $rs = add_admin_acount($post);
- if (!isset($rs['code']) || $rs['code'] != 0) return error_show(1004, $rs['message']);
- $up = Db::name("user_role")->insert([
- "uid" => $rs['data']["userid"],
- "nickname" => $rs['data']["nickname"],
- "roleid" => 1,
- "companyNo" => '',
- "company_type" => '',
- "status" => 1,
- "is_main" => 1,
- "is_del" => 0,
- "addtime" => date("Y-m-d H:i:s"),
- "updatetime" => date("Y-m-d H:i:s"),
- ]);
- if ($up == false) return error_show(1004, '超管账户分配角色失败');
- return app_show(0, '添加超管账号成功');
- }
- }
|