model= new Account(); } public function create(){ $param =$this->request->param(['nickname'=>'','username'=>'','level'=>'','companyArr'=>[],'field_deny'=>[]], 'post','trim'); $valid = Validate::rule([ 'nickname|账户名称'=>'require|max:255', 'username|账户手机号'=>'require|mobile|unique:app\user\model\Account,username^is_del', 'level|账户级别'=>'require|number|in:1,2,3', 'companyArr|账户关联公司'=>'array', 'field_deny|可查看字段'=>'require|array', ]); if($valid->check($param)==false)return error($valid->getError()); if(in_array($param['level'],[2,3])&& empty($param['companyArr'])){ return error("请选择关联公司"); } $salt=Str::random(8,1); $password = Str::substr($param['username'], 5, 6); $password = "dingding123"; $user=[ "username"=>$param['username'], "password"=>sha1($password.$salt), "salt"=>$salt, "mobile"=>$param['username'], "level"=>$param['level'], "status"=>1, "source"=>"register" ]; $relation=[]; if(!empty($param['companyArr'])){ $codeArr = array_column($param['companyArr'],"companyCode"); $comp = Headquarters::whereIn('code',$codeArr)->column("name,type","code"); foreach ($param['companyArr'] as $item){ $temp=[]; $temp['companyName']=$comp[$item['companyCode']]['name']??""; $temp['companyCode']=$item['companyCode']; $temp['is_main']=$item['is_main']; $temp['company_type']=$comp[$item['companyCode']]['type']==1?2:1; $relation[]=$temp; } } $this->model->startTrans(); try{ $user = Account::create($user); if($user->isEmpty()) throw new Exception("账户创建失败"); $info = [ "nickname"=>$param['nickname'], "mobile"=>$param['username'], "field_deny"=>$param['field_deny'], "account_id"=>$user->id ]; $userInsert= \app\user\model\User::create($info); if($userInsert->isEmpty()) throw new Exception('账户信息创建失败'); if(!empty($relation)){ $uid=$user->id; array_walk($relation,function (&$v)use($uid){ $v['account_id']= $uid; }); $relationInsert=(new AccountCompany())->saveAll($relation); if($relationInsert->isEmpty())throw new Exception('账户关联公司创建失败'); } $this->model->commit(); return success("账户创建成功"); }catch (\Exception $e){ $this->model->rollback(); return error($e->getMessage()); } } public function save(){ $param =$this->request->param(["id"=>"",'nickname'=>'','password'=>'','username'=>'','is_del'=>'0','level'=>'','companyArr'=>[], 'field_deny'=>[]],'post','trim'); $valid = Validate::rule([ 'id|账户ID'=>'require|number|gt:0', 'nickname|账户名称'=>'require|max:255', 'password|密码'=>'max:255', 'username|账户手机号'=>'require|mobile|unique:app\user\model\Account,username^is_del', 'level|账户级别'=>'require|number|in:1,2,3', 'companyArr|账户关联公司'=>'array', 'field_deny|可查看字段'=>'require|array', ]); if($valid->check($param)==false)return error($valid->getError()); $Account =Account::with(["userInfo"])->findOrEmpty($param['id']); if(in_array($param['level'],[2,3])&& empty($param['companyArr'])){ return error('请选择关联公司'); } $salt=Str::random(8,1); $password = Str::substr($param['username'], 5, 6); $password='dingding123'; $Account->username=$param['username']; if($param['password']!=''){ $Account->password=sha1($param['password']??$password.$salt); $Account->salt=$salt; } $Account->mobile=$param['username']; $Account->level=$param['level']; $Account->userInfo->mobile=$param['username']; $Account->userInfo->field_deny=$param['field_deny']; $Account->userInfo->nickname=$param['nickname']; $relation=[]; if(!empty($param['companyArr'])){ $codeArr = array_column($param['companyArr'],'companyCode'); $comp = Headquarters::whereIn('code',$codeArr)->column('name,type','code'); foreach ($param['companyArr'] as $item){ $temp=[]; $temp['companyName']=$comp[$item['companyCode']]['name']??''; $temp['companyCode']=$item['companyCode']; $temp['is_main']=$item['is_main']; $temp['id']=$item['id']??null; $temp['is_del']=$item['is_del']??0; $temp['company_type']=$comp[$item['companyCode']]['type']==1?2:1; $relation[]=$temp; } } $this->model->startTrans(); try{ $user = $Account->save(); if($user==false) throw new Exception('账户编辑失败'); $userInsert= $Account->userInfo->save(); if($userInsert==false) throw new Exception('账户信息编辑失败'); if(!empty($relation)){ $uid=$Account->id; array_walk($relation,function (&$v)use($uid){ $v['account_id']= $uid; }); $relationInsert=(new AccountCompany())->saveAll($relation); if($relationInsert->isEmpty())throw new Exception('账户关联公司编辑失败'); } $this->model->commit(); return success('账户编辑成功'); }catch (\Exception $e){ $this->model->rollback(); return error($e->getMessage()); } } public function userList(){ $post =$this->request->param(['page'=>1,'size'=>10,'nickname'=>'','username'=>'','level'=>'','itemid'=>'','status'=>''],"post","trim"); $where=[['is_del','=',0]]; if($post['nickname']!='') $where[]=['userInfo.nickname','like',"%{$post['nickname']}%"]; if($post['username']!='') $where[]=['username','like',"%{$post['username']}%"]; if($post['status']!=='')$where[]=['status','=',$post['status']]; if($post['level']!=0) $where[]=['level','=',$post['level']]; if($post['itemid']!=0) $where[]=['accountItem.itemid','=',$post['itemid']]; $list=$this->model->with(['accountItem'=>['itemName'],'company_relaton']) ->withJoin(['userInfo',"account_item"],'left') ->where($where)->order('account.id desc') ->paginate(['list_rows'=>$post['size'],'page'=>$post['page']]); $list->hidden(['userInfo','password','salt','account_item','accountItem']); return success('获取成功',['list'=>$list->items(),'count'=>$list->total()]); } public function userInfo(){ $uid =$this->request->post("id/d"); $info=$this->model->with(['accountItem'=>['itemName'],'company_relaton','userInfo'])->findOrEmpty($uid); $info->hidden(['userInfo','password','salt','accountItem']); return success('获取成功',$info); } public function GetUserQueryByCondition(){ $post =$this->request->param(['nickname'=>'','username'=>'','level'=>'','itemid'=>'','size'=>100, 'status'=>''],'post','trim'); $where=[['is_del','=',0]]; if($post['nickname']!='') $where[]=['userInfo.nickname','like',"%{$post['nickname']}%"]; if($post['username']!='') $where[]=['username','like',"%{$post['username']}%"]; if($post['status']!=='')$where[]=['status','=',$post['status']]; if($post['level']!=0) $where[]=['level','=',$post['level']]; if($post['itemid']!=0) $where[]=['accountItem.itemid','=',$post['itemid']]; $info=$this->model ->withJoin(['userInfo','account_item'],'left') ->where($where) ->field("length(nickname) nameL") ->limit(intval($post['size'])) ->order('nameL asc') ->select(); $info->hidden(['userInfo','password','salt','accountItem','account_item']); return success('获取成功',$info); } //修改账户字段 public function userChange(){ $param =$this->request->param([ 'id'=>"", 'nickname', 'mobile', 'password', 'level', 'status'],'post','trim'); $valid = Validate::rule([ "id|账户id"=>"require|number|gt:0", "nickname|账户名称"=>"max:255", "mobile|手机号"=>"mobile", "password|密码"=>"max:255", "level|账户级别"=>"number|in:1,2,3", "status|账户状态"=>"number|in:0,1", ]); if($valid->check($param)==false)return error($valid->getError()); $info=$this->model->with(['userInfo'])->findOrEmpty($param['id']); if($info->isEmpty())return error("账户数据不存在"); if(key_exists("nickname",$param)){ $info->userInfo->nickname= $param['nickname']; } if(key_exists('mobile',$param)){ $info->userInfo->mobile= $param['mobile']; $info->mobile= $param['mobile']; $info->username= $param['mobile']; } if(key_exists('status',$param)){ $info->status= $param['status']; } if(key_exists('level',$param)){ $info->level= $param['level']; } if(key_exists('password',$param)){ $salt= Str::random(8,1); $info->password= sha1($param['password'].$salt); $info->salt= $salt; } $info->save(); $info->userInfo->save(); return success('修改成功',$info); } }