123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750 |
- <?php
- declare (strict_types = 1);
- namespace app\admin\controller;
- use think\App;use think\Exception;use think\facade\Db;use think\facade\Validate;
- class User extends Base{
- 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,'level'=>''],"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){
- switch ($value['level']) {
- case 1:
- $role_arr_where = ["uid" => $value['id'], "is_del" => 0];
- break;
- case 2:
- $role_arr_where = ["uid" => $value['id'], "a.companyNo" => $value['companyCode'], "is_del" => 0];
- break;
- case 3:
- $role_arr_where = ["uid" => $value['id'], "a.companyNo" => [$value['companyCode'] , ''], "is_del" => 0];
- break;
- }
- $roleArr = Db::name("user_role")
- ->alias("a")
- ->field("a.id,role_name,roleid,a.status,b.level")
- ->leftJoin("role b", "a.roleid=b.id")
- ->where($role_arr_where)
- ->order(['a.companyNo' => 'desc'])
- ->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,'level'=>''],"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")
- ->field('id,status,level')
- ->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, "超管级别角色不允许设置");
- }
- //查询该账户的详情
- $userinfo = GetInfoById($post['token'],['id'=>$post['id']]);
- if (!isset($userinfo['code']) || $userinfo['code'] != 0) return error_show($userinfo['code'], $userinfo['message'], $userinfo['data']);
- $level = $userinfo['data']['level'];
- if ($level == 1) return error_show(1004, '超管账号不允许设置角色');
- // if ($level == 1) {
- $companyNo = isset($post['companyNo']) && $post['companyNo'] !== '' ? trim($post['companyNo']) : "";
- // } else {
- // $companyNo = isset($post['relaComNo']) && $post['relaComNo'] !== '' ? trim($post['relaComNo']) : "";
- // }
- $company_type = 0;
- if ($level== 2) {
- if ($companyNo == "") return error_show(1004, "业务公司编号不能为空");
- $company = Db::name("company_info")->field('id,companyNo code,company_name name')->where(["companyNo" => $companyNo])->find();
- if ($company == false) return error_show(1004, "业务公司不存在");
- $company_type = 1;
- }elseif ($level==3){
- if ($companyNo == "") return error_show(1004, "供应商编号不能为空");
- $company = Db::name("supplier_info")->field('id,code,type,name')->where(["code" => $companyNo])->find();
- if ($company == false) return error_show(1004, "供应商公司不存在");
- $company_type = 2;
- }
- // $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_where = ["uid" => $post['id'], "is_del" => 0, 'companyNo' => $companyNo];
- // if ($level == 2) $userrole_where['companyNo'] = $companyNo;
- $userrole = Db::name("user_role")
- ->field('id')
- ->where($userrole_where)
- ->find();
- $ismain = Db::name("user_role")->field('id')->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,
- "is_main" => $ismain ? 0 : 1,
- "status" => 1,
- "addtime" => date("Y-m-d H:i:s"),
- "updatetime" => date("Y-m-d H:i:s")
- ];
- //level 为1,3时,角色不和公司关联,所以不需要操作
- if ($level == 2) {
- $companyarr = [
- [
- "companyCode" => $company["code"],
- "companyName" => $company["name"],
- "company_type" => $company_type,
- "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;
- $userrole['updatetime'] = date("Y-m-d H:i:s");
- $userrole['status'] = 1;
- $up = Db::name("user_role")->where(["id" => $userrole['id'], "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(["uid" => 0, "companyCode" => "", "companyName" => "", "page" => 1, "size" => 20], "post", "trim");
- if ($post['uid'] == 0) $post['uid'] = $this->uid;
- $rs = get_company_list($post);
- // return app_show($rs['code'], $rs['message'], $rs['data']);
- // $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']))
- // ->select()
- // ->toArray();
- foreach ($rs['data']['list'] as &$value) {
- switch ($value['level']) {
- case 1:
- $role_arr_where = ["uid" => $value['account_id'], "is_del" => 0];
- break;
- case 2:
- $role_arr_where = ["uid" => $value['account_id'], "a.companyNo" => $value['companyCode'], "is_del" => 0];
- break;
- case 3:
- $role_arr_where = ["uid" => $value['account_id'], "a.companyNo" => [$value['companyCode'] . ''], "is_del" => 0];
- break;
- }
- $value['role_name'] = Db::name("user_role")
- ->alias("a")
- ->leftJoin("role b", "a.roleid=b.id")
- ->where($role_arr_where)
- ->order(['a.companyNo' => 'desc'])
- ->value('b.role_name', '');
- }
- return app_show(0, '获取成功', $rs['data']);
- }
- //密码设置
- 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" => "", 'level'], "post", "trim");
- $validate = Validate::rule([
- 'id|账户ID' => 'number',
- 'nickname|真实姓名' => 'require|min:2|max:200',
- 'mobile|手机号' => 'require|mobile',
- 'email|邮箱' => 'email',
- 'level|账号等级' => 'require|number|in:1,2,3',
- 'companyArr|关联公司' => 'requireIf:level,2|requireIf:level,3|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'] != "") {
- if ($post['level'] == 1) {
- $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 {
- 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,
- ];
- }
- }
- $nam = addacount($post);
- if (!isset($nam['code']) || $nam['code'] != 0) {
- return error_show(1004, $nam['message']);
- }
- $roleid = 0;
- if ($post['level'] == 3) {
- //如果是供应商账号,在结算添加以后,要在采销也添加
- Db::connect('mysql_wsm3.0')
- ->table('wsm_user_role')
- ->insert([
- "uid" => $nam['data']["userid"],
- "nickname" => $nam['data']["nickname"],
- "roleid" => env('default_supplier_addcount_role_id', 90),//需要专门为供应商账号指定一个默认的角色id,采销和结算的id要保持一致
- "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"),
- ]);
- $roleid = env('default_supplier_addcount_role_id', 90);//需要专门为供应商账号指定一个默认的角色id,要同步到采销;,
- } elseif ($post['level'] == 1) $roleid = 1;//超管
- $userrole=[];
- switch ($post['level']){
- case 1:
- //超管账号不和公司关联,给一个默认的角色记录
- $userrole = [[
- "uid" => $nam['data']["userid"],
- "nickname" => $nam['data']["nickname"],
- "roleid" => $roleid,
- "companyNo" => '',
- "company_type" => '',
- "status" => 0,
- "is_main" => 1,
- "is_del" => 0,
- "addtime" => date("Y-m-d H:i:s"),
- "updatetime" => date("Y-m-d H:i:s"),
- ]];
- break;
- default:
- //其他的都需要和公司关联
- foreach ($post['companyArr'] as $val) {
- $userrole[]=[
- "uid" => $nam['data']["userid"],
- "nickname" => $nam['data']["nickname"],
- "roleid" => $roleid,
- "companyNo" => $val['companyCode'],
- "company_type" => $val['company_type'],
- "status" =>1,
- "is_main" => $val['is_main'],
- "is_del" => 0,
- "addtime" => date("Y-m-d H:i:s"),
- "updatetime" => date("Y-m-d H:i:s"),
- ];
- }
- }
- if ($userrole) {
- $up = Db::name("user_role")->insertAll($userrole);
- if ($up == false) return error_show(1004, '账户角色分配失败');
- }
- // if (!empty($userrole)) {
- // $userrole['uid'] = $nam['data']["userid"];
- // $userrole['nickname'] = $nam['data']["nickname"];
- // }
- }
- return $post['id'] == "" ? app_show(0, "账户新建成功") : app_show(0, "账户编辑成功");
- }
- }
|