User.php 8.5 KB

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