|
@@ -3,7 +3,9 @@ declare (strict_types = 1);
|
|
|
|
|
|
namespace app\user\controller;
|
|
|
|
|
|
-use app\user\model\Account;use think\App;use think\facade\Validate;
|
|
|
+use app\user\model\Account;
|
|
|
+use app\user\model\AccountCompany;use app\user\model\Headquarters;use think\App;
|
|
|
+use think\Exception;use think\facade\Validate;use think\helper\Str;
|
|
|
|
|
|
class User extends Base
|
|
|
{
|
|
@@ -12,6 +14,136 @@ class User extends Base
|
|
|
$this->model= new Account();
|
|
|
}
|
|
|
|
|
|
+ public function create(){
|
|
|
+ $param =$this->request->param(['nickname'=>'','username'=>'','level'=>'','company_relaton'=>[],'field_deny'=>[]],
|
|
|
+ 'post','trim');
|
|
|
+ $valid = Validate::rule([
|
|
|
+ 'field|账户字段'=>'require|array',
|
|
|
+ 'nickname|账户名称'=>'require|max:255',
|
|
|
+ 'username|账户手机号'=>'require|mobile|unique:app\user\model\Account,username^is_del',
|
|
|
+ 'level|账户级别'=>'require|number|in:1,2,3',
|
|
|
+ 'company_relaton|账户关联公司'=>'require|array',
|
|
|
+ 'field_deny|可查看字段'=>'require|array',
|
|
|
+ ]);
|
|
|
+ if($valid->check($param)==false)return error($valid->getError());
|
|
|
+ $salt=Str::random(8,1);
|
|
|
+ $password = Str::substr($param['username'], 6, 6);
|
|
|
+ $user=[
|
|
|
+ "username"=>$param['username'],
|
|
|
+ "password"=>sha1($password.$salt),
|
|
|
+ "salt"=>$salt,
|
|
|
+ "mobile"=>$param['username'],
|
|
|
+ "level"=>$param['level'],
|
|
|
+ "status"=>1,
|
|
|
+ "source"=>"register"
|
|
|
+ ];
|
|
|
+ $relation=[];
|
|
|
+ if(!empty($param['company_relaton'])){
|
|
|
+ $codeArr = array_column($param['company_relaton'],"companyCode");
|
|
|
+ $comp = Headquarters::where('code',$codeArr)->column("name,type","code");
|
|
|
+ foreach ($param['company_relaton'] 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 success($e->getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ public function save(){
|
|
|
+ $param =$this->request->param(["id"=>"",'nickname'=>'','password'=>'','username'=>'','level'=>'','company_relaton'=>[],
|
|
|
+ 'field_deny'=>[]],'post','trim');
|
|
|
+ $valid = Validate::rule([
|
|
|
+ 'id|账户ID'=>'require|number|gt:0',
|
|
|
+ 'field|账户字段'=>'require|array',
|
|
|
+ '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',
|
|
|
+ 'company_relaton|账户关联公司'=>'array',
|
|
|
+ 'field_deny|可查看字段'=>'require|array',
|
|
|
+ ]);
|
|
|
+ if($valid->check($param)==false)return error($valid->getError());
|
|
|
+ $Account =Account::with(["userInfo"])->findOrEmpty($param['id']);
|
|
|
+ $salt=Str::random(8,1);
|
|
|
+ $password = Str::substr($param['username'], 6, 6);
|
|
|
+ $Account->username=$param['username'];
|
|
|
+ $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['company_relaton'])){
|
|
|
+ $codeArr = array_column($param['company_relaton'],'companyCode');
|
|
|
+ $comp = Headquarters::where('code',$codeArr)->column('name,type','code');
|
|
|
+ foreach ($param['company_relaton'] 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) 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 success($e->getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
public function userList(){
|
|
|
$post =$this->request->param(['page'=>1,'size'=>10,'nickname'=>'','username'=>'','level'=>'','itemid'=>'','status'=>''],"post","trim");
|
|
|
$where=[['is_del','=',0]];
|