User.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\user\controller;
  4. use app\user\model\Account;
  5. use app\user\model\AccountCompany;use app\user\model\Headquarters;use think\App;
  6. use think\Exception;use think\facade\Validate;use think\helper\Str;
  7. class User extends Base
  8. {
  9. public function __construct(App $app) {
  10. parent::__construct($app);
  11. $this->model= new Account();
  12. }
  13. public function create(){
  14. $param =$this->request->param(['nickname'=>'','username'=>'','level'=>'','companyArr'=>[],'field_deny'=>[]],
  15. 'post','trim');
  16. $valid = Validate::rule([
  17. 'nickname|账户名称'=>'require|max:255',
  18. 'username|账户手机号'=>'require|mobile|unique:app\user\model\Account,username^is_del',
  19. 'level|账户级别'=>'require|number|in:1,2,3',
  20. 'companyArr|账户关联公司'=>'require|array',
  21. 'field_deny|可查看字段'=>'require|array',
  22. ]);
  23. if($valid->check($param)==false)return error($valid->getError());
  24. $salt=Str::random(8,1);
  25. $password = Str::substr($param['username'], 6, 6);
  26. $user=[
  27. "username"=>$param['username'],
  28. "password"=>sha1($password.$salt),
  29. "salt"=>$salt,
  30. "mobile"=>$param['username'],
  31. "level"=>$param['level'],
  32. "status"=>1,
  33. "source"=>"register"
  34. ];
  35. $relation=[];
  36. if(!empty($param['companyArr'])){
  37. $codeArr = array_column($param['companyArr'],"companyCode");
  38. $comp = Headquarters::where('code',$codeArr)->column("name,type","code");
  39. foreach ($param['companyArr'] as $item){
  40. $temp=[];
  41. $temp['companyName']=$comp[$item['companyCode']]['name']??"";
  42. $temp['companyCode']=$item['companyCode'];
  43. $temp['is_main']=$item['is_main'];
  44. $temp['company_type']=$comp[$item['companyCode']]['type']==1?2:1;
  45. $relation[]=$temp;
  46. }
  47. }
  48. $this->model->startTrans();
  49. try{
  50. $user = Account::create($user);
  51. if($user->isEmpty()) throw new Exception("账户创建失败");
  52. $info = [
  53. "nickname"=>$param['nickname'],
  54. "mobile"=>$param['username'],
  55. "field_deny"=>$param['field_deny'],
  56. "account_id"=>$user->id
  57. ];
  58. $userInsert= \app\user\model\User::create($info);
  59. if($userInsert->isEmpty()) throw new Exception('账户信息创建失败');
  60. if(!empty($relation)){
  61. $uid=$user->id;
  62. array_walk($relation,function (&$v)use($uid){
  63. $v['account_id']= $uid;
  64. });
  65. $relationInsert=(new AccountCompany())->saveAll($relation);
  66. if($relationInsert->isEmpty())throw new Exception('账户关联公司创建失败');
  67. }
  68. $this->model->commit();
  69. return success("账户创建成功");
  70. }catch (\Exception $e){
  71. $this->model->rollback();
  72. return error($e->getMessage());
  73. }
  74. }
  75. public function save(){
  76. $param =$this->request->param(["id"=>"",'nickname'=>'','password'=>'','username'=>'','level'=>'','companyArr'=>[],
  77. 'field_deny'=>[]],'post','trim');
  78. $valid = Validate::rule([
  79. 'id|账户ID'=>'require|number|gt:0',
  80. 'nickname|账户名称'=>'require|max:255',
  81. 'password|密码'=>'max:255',
  82. 'username|账户手机号'=>'require|mobile|unique:app\user\model\Account,username^is_del',
  83. 'level|账户级别'=>'require|number|in:1,2,3',
  84. 'companyArr|账户关联公司'=>'array',
  85. 'field_deny|可查看字段'=>'require|array',
  86. ]);
  87. if($valid->check($param)==false)return error($valid->getError());
  88. $Account =Account::with(["userInfo"])->findOrEmpty($param['id']);
  89. $salt=Str::random(8,1);
  90. $password = Str::substr($param['username'], 6, 6);
  91. $Account->username=$param['username'];
  92. $Account->password=sha1($param['password']??$password.$salt);
  93. $Account->salt=$salt;
  94. $Account->mobile=$param['username'];
  95. $Account->level=$param['level'];
  96. $Account->userInfo->mobile=$param['username'];
  97. $Account->userInfo->field_deny=$param['field_deny'];
  98. $Account->userInfo->nickname=$param['nickname'];
  99. $relation=[];
  100. if(!empty($param['companyArr'])){
  101. $codeArr = array_column($param['companyArr'],'companyCode');
  102. $comp = Headquarters::whereIn('code',$codeArr)->column('name,type','code');
  103. foreach ($param['companyArr'] as $item){
  104. $temp=[];
  105. $temp['companyName']=$comp[$item['companyCode']]['name']??'';
  106. $temp['companyCode']=$item['companyCode'];
  107. $temp['is_main']=$item['is_main'];
  108. $temp['id']=$item['id']??null;
  109. $temp['is_del']=$item['is_del']??0;
  110. $temp['company_type']=$comp[$item['companyCode']]['type']==1?2:1;
  111. $relation[]=$temp;
  112. }
  113. }
  114. $this->model->startTrans();
  115. try{
  116. $user = $Account->save();
  117. if($user==false) throw new Exception('账户编辑失败');
  118. $userInsert= $Account->userInfo->save();
  119. if($userInsert==false) throw new Exception('账户信息编辑失败');
  120. if(!empty($relation)){
  121. $uid=$Account->id;
  122. array_walk($relation,function (&$v)use($uid){
  123. $v['account_id']= $uid;
  124. });
  125. $relationInsert=(new AccountCompany())->saveAll($relation);
  126. if($relationInsert->isEmpty())throw new Exception('账户关联公司编辑失败');
  127. }
  128. $this->model->commit();
  129. return success('账户编辑成功');
  130. }catch (\Exception $e){
  131. $this->model->rollback();
  132. return error($e->getMessage());
  133. }
  134. }
  135. public function userList(){
  136. $post =$this->request->param(['page'=>1,'size'=>10,'nickname'=>'','username'=>'','level'=>'','itemid'=>'','status'=>''],"post","trim");
  137. $where=[['is_del','=',0]];
  138. if($post['nickname']!='') $where[]=['userInfo.nickname','like',"%{$post['nickname']}%"];
  139. if($post['username']!='') $where[]=['username','like',"%{$post['username']}%"];
  140. if($post['status']!=='')$where[]=['status','=',$post['status']];
  141. if($post['level']!=0) $where[]=['level','=',$post['level']];
  142. if($post['itemid']!=0) $where[]=['accountItem.itemid','=',$post['itemid']];
  143. $list=$this->model->with(['accountItem'=>['itemName'],'company_relaton'])
  144. ->withJoin(['userInfo',"account_item"],'left')
  145. ->where($where)->order('account.id asc')
  146. ->paginate(['list_rows'=>$post['size'],'page'=>$post['page']]);
  147. $list->hidden(['userInfo','password','salt','account_item','accountItem']);
  148. return success('获取成功',['list'=>$list->items(),'count'=>$list->total()]);
  149. }
  150. public function userInfo(){
  151. $uid =$this->request->post("id/d");
  152. $info=$this->model->with(['accountItem'=>['itemName'],'company_relaton','userInfo'])->findOrEmpty($uid);
  153. $info->hidden(['userInfo','password','salt','accountItem']);
  154. return success('获取成功',$info);
  155. }
  156. //修改账户字段
  157. public function userChange(){
  158. $param =$this->request->param([
  159. 'id'=>"",
  160. 'nickname',
  161. 'mobile',
  162. 'level',
  163. 'status'],'post','trim');
  164. $valid = Validate::rule([
  165. "id|账户id"=>"require|number|gt:0",
  166. "nickname|账户名称"=>"max:255",
  167. "mobile|手机号"=>"mobile",
  168. "level|账户级别"=>"number|in:1,2,3",
  169. "status|账户状态"=>"number|in:0,1",
  170. ]);
  171. if($valid->check($param)==false)return error($valid->getError());
  172. $info=$this->model->with(['userInfo'])->findOrEmpty($param['id']);
  173. if($info->isEmpty())return error("账户数据不存在");
  174. if(key_exists("nickname",$param)){
  175. $info->userInfo->nickname= $param['nickname'];
  176. }
  177. if(key_exists('mobile',$param)){
  178. $info->userInfo->mobile= $param['mobile'];
  179. $info->mobile= $param['mobile'];
  180. $info->username= $param['mobile'];
  181. }
  182. if(key_exists('status',$param)){
  183. $info->status= $param['status'];
  184. }
  185. if(key_exists('level',$param)){
  186. $info->level= $param['level'];
  187. }
  188. $info->save();
  189. $info->userInfo->save();
  190. return success('修改成功',$info);
  191. }
  192. }