UserInfo.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\controller;
  4. use app\BaseController;
  5. use app\model\Account;use app\model\AccountCompany;use think\App;
  6. use think\facade\Db;use think\facade\Validate;
  7. class UserInfo extends BaseController
  8. {
  9. public function __construct(App $app) {
  10. parent::__construct($app);
  11. }
  12. /**
  13. * @param string $nickname
  14. * @param string $username
  15. * @param int $status
  16. * @param array $uid
  17. * @param array $nuid
  18. * @param int $page
  19. * @param int $size
  20. * @param string $nickname
  21. * @return \think\response\Json
  22. * @throws \think\db\exception\DbException
  23. */
  24. public function UserList()
  25. {
  26. $post=$this->request->only(["nickname"=>"","username"=>"","status"=>"","uid"=>[],"nuid"=>[],"page"=>1,"size"=>10],"post");
  27. $condition = [["a.is_del","=",0]];
  28. isset($post['nickname'])&& $post['nickname']!="" ? $condition[]=["nickname","like","%{$post['nickname']}%"] : "";
  29. isset($post['username'])&& $post['username']!="" ? $condition[]=["username","like","%{$post['username']}%"] : "";
  30. isset($post['status'])&& $post['status']!=="" ? $condition[]=["a.status","=",$post['status']] : "";
  31. isset($post['uid'])&& $post['uid']!=="" ? $condition[]=["a.id","in",$post['uid']] : "";
  32. isset($post['nuid'])&& $post['nuid']!=="" ? $condition[]=["a.id","not in",$post['nuid']] : "";
  33. $page = isset($post['page'])&& $post['page']!=="" ? intval($post['page']) : 1;
  34. $size = isset($post['size'])&& $post['size']!=="" ? intval($post['size']) : 10;
  35. $count = Db::name("account")->alias("a")
  36. ->leftJoin("user b","a.id=b.account_id and b.status=1")
  37. ->where($condition)->count();
  38. $total =intval(ceil($count/$size)) ;
  39. $page = $total>=$page? $page:$total;
  40. $list = Db::name("account")->alias("a")
  41. ->leftJoin("user b","a.id=b.account_id and b.status=1")
  42. ->where($condition)->page($page,$size)
  43. ->field("a.id,a.username,a.mobile,a.source,a.status,b.nickname,b.sex,b.email,a.addtime,a.updatetime")
  44. ->cursor();
  45. $data=[];
  46. foreach ($list as $item){
  47. $item['plat']= Db::name("account_plat")->alias("a")
  48. ->leftJoin("platform b","a.plat_code=b.plat_code and b.is_del=0 and b.status=1")
  49. ->where(["a.status"=>1,"a.is_del"=>0,"a.account_id"=>$item['id']])->column("a.plat_code,plat_name");
  50. $item['company_relaton'] = Db::name("account_company")->where(["account_id"=>$item['id'],"is_del"=>0,"status"=>1])
  51. ->column("companyCode,companyName,company_type,is_main,status");
  52. $data[]=$item;
  53. }
  54. return json_show(0,"获取成功",["list"=>$data,"count"=>$count]);
  55. }
  56. /** @param int $id 账户id
  57. * @return \think\response\Json
  58. */
  59. public function info()
  60. {
  61. $post=$this->request->only(["id"=>""],"post","intval");
  62. if($post['id']==""){
  63. return json_show(1003,"参数 id 不能为空");
  64. }
  65. $list = Db::name("account")->alias("a")
  66. ->leftJoin("user b","a.id=b.account_id and b.status=1")
  67. ->where(["a.id"=>$post['id'],"a.is_del"=>0])
  68. ->field("a.id,a.username,a.mobile,a.source,a.status,b.nickname,b.sex,b.email,a.addtime,a.updatetime")
  69. ->findOrEmpty();
  70. if(empty($list)){
  71. return json_show(1004,"未找到用户信息");
  72. }
  73. $list['plat']= Db::name("account_plat")->alias("a")
  74. ->leftJoin("platform b","a.plat_code=b.plat_code and b.status=1")
  75. ->where(["a.status"=>1,"a.is_del"=>0,"a.account_id"=>$list['id']])->column("a.plat_code,plat_name");
  76. $list['company_relaton'] = Db::name("account_company")->where(["account_id"=>$list['id'],"is_del"=>0,"status"=>1])
  77. ->column("companyCode,companyName,company_type,is_main,status");
  78. return json_show(0,"获取成功",$list);
  79. }
  80. /**
  81. * @return \think\response\Json|void
  82. * @throws \think\db\exception\DataNotFoundException
  83. * @throws \think\db\exception\DbException
  84. * @throws \think\db\exception\ModelNotFoundException
  85. */
  86. public function PassSet()
  87. {
  88. $post=$this->request->only(["id"=>"","password"=>""],"post","trim");
  89. $validate=Validate::rule([
  90. 'id|账户ID' => 'require|number',
  91. 'password|密码' => 'require|min:6|max:200',
  92. ]);
  93. if($validate->check($post)==false) return json_show(1004,$validate->getError());
  94. $account=Db::name("account")->where("id","=",$post['id'])->find();
  95. if(empty($account)){
  96. return json_show(1003,"账户不存在");
  97. }
  98. $salt=makeSalt();
  99. $password = sha1($post['password'].$salt);
  100. $account['password']=$password;
  101. $account['salt']=$salt;
  102. $account['is_pass']=1;
  103. $account['updatetime']=date("Y-m-d H:i:s");
  104. $up = Db::name("account")->save($account);
  105. return $up?json_show(0,"密码修改成功"):json_show(1005,"密码修改失败");
  106. }
  107. /**@param int $id
  108. *@param array $company
  109. * @return \think\response\Json
  110. */
  111. public function setCompany(){
  112. $post = $this->request->only(["id"=>"","company"=>[]],"post");
  113. $validate=Validate::rule([
  114. 'id|账户ID' => 'require|number|gt:0',
  115. 'company|业务公司' => 'require|array',
  116. ]);
  117. if($validate->check($post)==false) return json_show(1004,$validate->getError());
  118. $company_insert=[];
  119. $acount =new AccountCompany();
  120. foreach ($post['company'] as $company){
  121. $ist=$acount->where(["account_id"=>$post['id'],"companyCode"=>$company['companyCode']])->find();
  122. if($ist!=false)$company['id']=$ist['id'];
  123. $company_insert[]=[
  124. "id"=>$company['id']??null,
  125. "account_id"=>$post['id'],
  126. "companyCode"=>$company['companyCode'],
  127. "companyName"=>$company['companyName'],
  128. "company_type"=>$company['company_type'],
  129. "is_main"=>$company['is_main'],
  130. "status"=>1,
  131. "is_del"=>$company['is_del']??0,
  132. "addtime"=>date("Y-m-d H:i:s"),
  133. "updatetime"=>date("Y-m-d H:i:s"),
  134. ];
  135. }
  136. $inser =$acount->saveAll($company_insert);
  137. return $inser?json_show(0,"关联企业设置成功"):json_show(1005,"关联企业设置失败");
  138. }
  139. /**
  140. * @param int $id
  141. * @param int $status
  142. * @return \think\response\Json
  143. * @throws \think\exception\DbException
  144. */
  145. public function setCompanyStatus(){
  146. $post = $this->request->only(["id"=>"","status"=>""],"post","intval");
  147. $validate=Validate::rule([
  148. 'id|主键ID' => 'require|number|gt:0',
  149. 'status|状态' => 'require|number|in:0,1',
  150. ]);
  151. if($validate->check($post)==false) return json_show(1004,$validate->getError());
  152. $info=AccountCompany::where(["id"=>$post['id']])->find();
  153. if($info==false){
  154. return json_show(1004,"未找到数据");
  155. }
  156. if($info['status']==$post['status']){
  157. return json_show(1004,"数据已更新");
  158. }
  159. $info['status']=$post['status'];
  160. $info['updatetime']=date("Y-m-d H:i:s");
  161. $inser=AccountCompany::update($info->toArray());
  162. return $inser?json_show(0,"关联企业状态设置成功"):json_show(1005,"关联企业状态设置失败");
  163. }
  164. /**
  165. * @param int $id
  166. * @param string $nickname
  167. * @param int $mobile
  168. * @param string $email
  169. * @param string $portrait
  170. * @param int $sex
  171. * @return \think\response\Json
  172. */
  173. public function UserSave()
  174. {
  175. $post = $this->request->only([
  176. "id"=>"",
  177. "nickname"=>"",
  178. "mobile"=>"",
  179. "email"=>"",
  180. "portrait"=>"",
  181. "sex"=>"",
  182. ],"post");
  183. $validate=Validate::rule([
  184. 'id|主键ID' => 'require|number|gt:0',
  185. 'nickname|名称' => 'require|max:255',
  186. 'mobile|手机号' => 'require|number|length:11|mobile',
  187. 'email|名称' => 'email',
  188. 'sex|性别' => 'number|in:0,1,2',
  189. ]);
  190. if($validate->check($post)==false) return json_show(1004,$validate->getError());
  191. $account=Db::name("account")->where([["id","=",$post['id']],["is_del","=",0]])->findOrEmpty();
  192. if(empty($account)){
  193. return json_show(1003,"账户不存在");
  194. }
  195. $accountinfo=Db::name("user")->where([["account_id","=",$post['id']]])->findOrEmpty();
  196. if(empty($accountinfo)){
  197. return json_show(1003,"账户信息不存在");
  198. }
  199. Db::startTrans();
  200. try{
  201. $userinfo=[
  202. "nickname"=>$post['nickname'],
  203. "mobile"=>$post['mobile'],
  204. "email"=>$post['email'],
  205. "portrait"=>$post['portrait'],
  206. "sex"=>$post['sex'],
  207. "updatetime"=>date("Y-m-d H:i:s")
  208. ];
  209. $dat=Db::name("user")->where($accountinfo)->update($userinfo);
  210. if($dat==false){
  211. Db::rollback();
  212. return json_show(1004,"信息修改失败");
  213. }
  214. $acc= [
  215. "id"=>$post['id'],
  216. "mobile"=>$post['mobile'],
  217. "username"=>$post['mobile'],
  218. "updatetime"=>date("Y-m-d H:i:s"),
  219. ];
  220. $nu = Db::name("account")->save($acc);
  221. if($nu){
  222. Db::commit();
  223. return json_show(0,"信息修改成功");
  224. }else{
  225. Db::rollback();
  226. return json_show(1004,"账户信息修改失败");
  227. }
  228. }catch (\Exception $e){
  229. Db::rollback();
  230. return json_show(1005,$e->getMessage());
  231. }
  232. }
  233. /**
  234. * @param int $id
  235. * @param int $status
  236. * @return \think\response\Json
  237. * @throws \think\exception\DbException
  238. */
  239. public function UserStatus()
  240. {
  241. $post = $this->request->only(["id"=>"","status"=>""],"post","trim");
  242. $validate=Validate::rule([
  243. 'id|主键ID' => 'require|number|gt:0',
  244. 'status|状态' => 'require|number|in:0,1',
  245. ]);
  246. if($validate->check($post)==false) return json_show(1004,$validate->getError());
  247. $account=Account::where("id",$post['id'])->findOrEmpty();
  248. if(empty($account)){
  249. return json_show(1003,"账户不存在");
  250. }
  251. if($account['status']==$post['status']){
  252. return json_show(1004,"数据已更新");
  253. }
  254. $message = $post['status']==1?"启用":"禁用";
  255. $result= Db::name("account")->where("id","=",$post['id'])->save(['status'=>$post['status'],"updatetime"=>date("Y-m-d
  256. H:i:s")]);
  257. return $result?json_show(0,"账户{$message}成功") : json_show(1005,"账户{$message}失败");
  258. }
  259. public function UserListByCompany()
  260. {
  261. $post=$this->request->only(["nickname"=>"","username"=>"","status"=>"","uid"=>[],"nuid"=>[],"companyNo"=>"","page"=>1, "size"=>10],"post");
  262. $condition = [["a.is_del","=",0]];
  263. $whereor=[];
  264. isset($post['nickname'])&& $post['nickname']!="" ? $condition[]=["nickname","like","%{$post['nickname']}%"] : "";
  265. isset($post['username'])&& $post['username']!="" ? $condition[]=["username","like","%{$post['username']}%"] : "";
  266. isset($post['status'])&& $post['status']!=="" ? $condition[]=["a.status","=",$post['status']] : "";
  267. isset($post['uid'])&& $post['uid']!==""&&!empty($post['uid']) ? $condition[]=["a.id","in",$post['uid']] : "";
  268. isset($post['nuid'])&& $post['nuid']!==""&&!empty($post['nuid']) ? $condition[]=["a.id","not in",$post['nuid']] : "";
  269. isset($post['companyNo'])&& $post['companyNo']!=="" ? $condition[]=["c.companyCode","=",$post['companyNo']]
  270. :$whereor[]=["c.companyCode","=",null];
  271. $page = isset($post['page'])&& $post['page']!=="" ? intval($post['page']) : 1;
  272. $size = isset($post['size'])&& $post['size']!=="" ? intval($post['size']) : 10;
  273. $count = Db::name("account")->alias("a")
  274. ->leftJoin("user b","a.id=b.account_id and b.status=1")
  275. ->leftJoin("account_company c","a.id=c.account_id and c.status=1 and c.is_del=0")
  276. ->where($condition)->whereOr($whereor)->count();
  277. $total =intval(ceil($count/$size)) ;
  278. $page = $total>=$page? $page:$total;
  279. $list = Db::name("account")->alias("a")
  280. ->leftJoin("user b","a.id=b.account_id and b.status=1")
  281. ->leftJoin("account_company c","a.id=c.account_id and c.is_del=0")
  282. ->where($condition)->whereOr($whereor)->page($page,$size)->order("c.addtime desc,a.addtime desc")
  283. ->field("a.id,a.username,a.mobile,a.source,a.status,b.nickname,b.sex,b.email,a.addtime,a.updatetime,c.companyCode,c.companyName,c.is_main,c.status as com_status")
  284. ->cursor();
  285. $data=[];
  286. foreach ($list as $item){
  287. $item['plat']= Db::name("account_plat")->alias("a")
  288. ->leftJoin("platform b","a.plat_code=b.plat_code and b.is_del=0 and b.status=1")
  289. ->where(["a.status"=>1,"a.is_del"=>0,"a.account_id"=>$item['id']])->column("a.plat_code,plat_name");
  290. $item['company_relaton'] = Db::name("account_company")->where(["account_id"=>$item['id'],"is_del"=>0])
  291. ->column("companyCode,companyName,company_type,is_main,status");
  292. $data[]=$item;
  293. }
  294. return json_show(0,"获取成功",["list"=>$data,"count"=>$count]);
  295. }
  296. /**
  297. * @return \think\response\Json
  298. * @throws \think\db\exception\DataNotFoundException
  299. * @throws \think\db\exception\DbException
  300. * @throws \think\db\exception\ModelNotFoundException
  301. */
  302. public function userAdd(){
  303. $post = $this->request->only(["nickname"=>"","mobile"=>"","email"=>"","companyArr"=>[]],"post","trim");
  304. $validate=Validate::rule([
  305. 'nickname|真实姓名' => 'require|min:2|max:200',
  306. 'mobile|手机号' => 'require|number|length:11|mobile',
  307. 'email|邮箱' => 'email',
  308. 'companyArr|关联业务公司' => 'array',
  309. ]);
  310. if($validate->check($post)==false) return json_show(1004,$validate->getError());
  311. Db::startTrans();
  312. $uiq = Db::table("sys_account")->where(["mobile"=>$post['mobile']])->find();
  313. if($uiq){
  314. return json_show(1002,"手机号已注册!");
  315. }
  316. try {
  317. $salt =makeSalt();
  318. $password = sha1("dingding123".$salt);
  319. $data = [
  320. 'username'=>$post['mobile'],
  321. "password"=>$password,
  322. "salt"=>$salt,
  323. "mobile"=>$post['mobile'],
  324. "source"=>"paltadd",
  325. "status"=>1,
  326. "addtime"=>date("Y-m-d H:i:s"),
  327. "updatetime"=>date("Y-m-d H:i:s")
  328. ];
  329. $reuslt = Db::table('sys_account')->insert($data,true);
  330. if($reuslt){
  331. $data=[
  332. "nickname"=>$post['nickname'],
  333. "mobile"=>$post['mobile'],
  334. "email"=>$post['email'],
  335. "portrait"=>"",
  336. "sex"=>1,
  337. "post"=>"",
  338. "department"=>"",
  339. "account_id"=>$reuslt,
  340. "status"=>1,
  341. "addtime"=>date("Y-m-d H:i:s"),
  342. "updatetime"=>date("Y-m-d H:i:s")
  343. ];
  344. $user=Db::table("sys_user")->insert($data);
  345. if($user!=false){
  346. if(!empty($post['companyArr'])){
  347. $company_insert=[];
  348. $acount =new AccountCompany();
  349. foreach ($post['companyArr'] as $company){
  350. $company_insert[]=[
  351. "account_id"=>$reuslt,
  352. "companyCode"=>$company['companyCode'],
  353. "companyName"=>$company['companyName'],
  354. "company_type"=>$company['company_type'],
  355. "is_main"=>$company['is_main'],
  356. "status"=>1,
  357. "is_del"=>0,
  358. "addtime"=>date("Y-m-d H:i:s"),
  359. "updatetime"=>date("Y-m-d H:i:s"),
  360. ];
  361. }
  362. $acount->saveAll($company_insert);
  363. }
  364. Db::commit();
  365. return json_show(0,"账户注册成功");
  366. }
  367. }
  368. Db::rollback();
  369. return json_show(1002,"账户注册失败");
  370. }catch (\Exception $e){
  371. Db::rollback();
  372. return json_show(1002,"账户注册失败".$e->getMessage());
  373. }
  374. }
  375. }