UserInfo.php 18 KB

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