User.php 9.6 KB

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