User.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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'=>'','is_del'=>'0','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. if($param['password']!=''){
  101. $Account->password=sha1($param['password']??$password.$salt);
  102. $Account->salt=$salt;
  103. }
  104. $Account->mobile=$param['username'];
  105. $Account->level=$param['level'];
  106. $Account->userInfo->mobile=$param['username'];
  107. $Account->userInfo->field_deny=$param['field_deny'];
  108. $Account->userInfo->nickname=$param['nickname'];
  109. $relation=[];
  110. if(!empty($param['companyArr'])){
  111. $codeArr = array_column($param['companyArr'],'companyCode');
  112. $comp = Headquarters::whereIn('code',$codeArr)->column('name,type','code');
  113. foreach ($param['companyArr'] as $item){
  114. $temp=[];
  115. $temp['companyName']=$comp[$item['companyCode']]['name']??'';
  116. $temp['companyCode']=$item['companyCode'];
  117. $temp['is_main']=$item['is_main'];
  118. $temp['id']=$item['id']??null;
  119. $temp['is_del']=$item['is_del']??0;
  120. $temp['company_type']=$comp[$item['companyCode']]['type']==1?2:1;
  121. $relation[]=$temp;
  122. }
  123. }
  124. $this->model->startTrans();
  125. try{
  126. $user = $Account->save();
  127. if($user==false) throw new Exception('账户编辑失败');
  128. $userInsert= $Account->userInfo->save();
  129. if($userInsert==false) throw new Exception('账户信息编辑失败');
  130. if(!empty($relation)){
  131. $uid=$Account->id;
  132. array_walk($relation,function (&$v)use($uid){
  133. $v['account_id']= $uid;
  134. });
  135. $relationInsert=(new AccountCompany())->saveAll($relation);
  136. if($relationInsert->isEmpty())throw new Exception('账户关联公司编辑失败');
  137. }
  138. $this->model->commit();
  139. return success('账户编辑成功');
  140. }catch (\Exception $e){
  141. $this->model->rollback();
  142. return error($e->getMessage());
  143. }
  144. }
  145. public function userList(){
  146. $post =$this->request->param(['page'=>1,'size'=>10,'nickname'=>'','username'=>'','level'=>'','itemid'=>'','status'=>''],"post","trim");
  147. $where=[['is_del','=',0]];
  148. if($post['nickname']!='') $where[]=['userInfo.nickname','like',"%{$post['nickname']}%"];
  149. if($post['username']!='') $where[]=['username','like',"%{$post['username']}%"];
  150. if($post['status']!=='')$where[]=['status','=',$post['status']];
  151. if($post['level']!=0) $where[]=['level','=',$post['level']];
  152. if($post['itemid']!=0) $where[]=['accountItem.itemid','=',$post['itemid']];
  153. $list=$this->model->with(['accountItem'=>['itemName'],'company_relaton'])
  154. ->withJoin(['userInfo',"account_item"],'left')
  155. ->where($where)->order('account.id desc')
  156. ->paginate(['list_rows'=>$post['size'],'page'=>$post['page']]);
  157. $list->hidden(['userInfo','password','salt','account_item','accountItem']);
  158. return success('获取成功',['list'=>$list->items(),'count'=>$list->total()]);
  159. }
  160. public function userInfo(){
  161. $uid =$this->request->post("id/d");
  162. $info=$this->model->with(['accountItem'=>['itemName'],'company_relaton','userInfo'])->findOrEmpty($uid);
  163. $info->hidden(['userInfo','password','salt','accountItem']);
  164. return success('获取成功',$info);
  165. }
  166. public function GetUserQueryByCondition(){
  167. $post =$this->request->param(['nickname'=>'','username'=>'','level'=>'','itemid'=>'','size'=>100,
  168. 'status'=>''],'post','trim');
  169. $where=[['is_del','=',0]];
  170. if($post['nickname']!='') $where[]=['userInfo.nickname','like',"%{$post['nickname']}%"];
  171. if($post['username']!='') $where[]=['username','like',"%{$post['username']}%"];
  172. if($post['status']!=='')$where[]=['status','=',$post['status']];
  173. if($post['level']!=0) $where[]=['level','=',$post['level']];
  174. if($post['itemid']!=0) $where[]=['accountItem.itemid','=',$post['itemid']];
  175. $info=$this->model
  176. ->withJoin(['userInfo','account_item'],'left')
  177. ->where($where)
  178. ->field("length(nickname) nameL")
  179. ->limit(intval($post['size']))
  180. ->order('nameL asc')
  181. ->select();
  182. $info->hidden(['userInfo','password','salt','accountItem','account_item']);
  183. return success('获取成功',$info);
  184. }
  185. //修改账户字段
  186. public function userChange(){
  187. $param =$this->request->param([
  188. 'id'=>"",
  189. 'nickname',
  190. 'mobile',
  191. 'password',
  192. 'level',
  193. 'status'],'post','trim');
  194. $valid = Validate::rule([
  195. "id|账户id"=>"require|number|gt:0",
  196. "nickname|账户名称"=>"max:255",
  197. "mobile|手机号"=>"mobile",
  198. "password|密码"=>"max:255",
  199. "level|账户级别"=>"number|in:1,2,3",
  200. "status|账户状态"=>"number|in:0,1",
  201. ]);
  202. if($valid->check($param)==false)return error($valid->getError());
  203. $info=$this->model->with(['userInfo'])->findOrEmpty($param['id']);
  204. if($info->isEmpty())return error("账户数据不存在");
  205. if(key_exists("nickname",$param)){
  206. $info->userInfo->nickname= $param['nickname'];
  207. }
  208. if(key_exists('mobile',$param)){
  209. $info->userInfo->mobile= $param['mobile'];
  210. $info->mobile= $param['mobile'];
  211. $info->username= $param['mobile'];
  212. }
  213. if(key_exists('status',$param)){
  214. $info->status= $param['status'];
  215. }
  216. if(key_exists('level',$param)){
  217. $info->level= $param['level'];
  218. }
  219. if(key_exists('password',$param)){
  220. $salt= Str::random(8,1);
  221. $info->password= sha1($param['password'].$salt);
  222. $info->salt= $salt;
  223. }
  224. $info->save();
  225. $info->userInfo->save();
  226. return success('修改成功',$info);
  227. }
  228. }