User.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\Api\controller;
  4. use think\facade\Db;
  5. use think\App;
  6. use think\facade\Env;
  7. use think\facade\Log;
  8. class User
  9. {
  10. /**
  11. * Request实例
  12. * @var \think\Request
  13. */
  14. protected $request;
  15. /**
  16. * 应用实例
  17. * @var \think\App
  18. */
  19. protected $app;
  20. /**
  21. * 显示资源列表
  22. *
  23. * @return \think\Response
  24. */
  25. public function __construct(App $app)
  26. {
  27. $this->app = $app;
  28. $this->request = $this->app->request;
  29. }
  30. public function register()
  31. {
  32. $post = $this->request->post();
  33. $username = isset($post['username']) ? trim($post['username']):"";
  34. if($username==""){
  35. return app_show(1002,"账户名不能为空!");
  36. }
  37. $password = isset($post['password']) ? trim($post['password']):"";
  38. if($password==""){
  39. return app_show(1002,"密码不能为空!");
  40. }
  41. $mobile = isset($post['mobile']) ? trim($post['mobile']):"";
  42. if($mobile==""){
  43. return app_show(1002,"手机号不能为空!");
  44. }
  45. if(checkMobile($mobile)==false){
  46. return app_show(1002,"手机号格式不正确!");
  47. }
  48. $source = isset($post['source']) ? trim($post['source']):"";
  49. $uiq = Db::table("sys_account")->where(["username"=>$username])->find();
  50. if($uiq){
  51. return app_show(1002,"账户名已存在!");
  52. }
  53. $uiq = Db::table("sys_account")->where(["mobile"=>$mobile])->find();
  54. if($uiq){
  55. return app_show(1002,"手机号已注册!");
  56. }
  57. Db::startTrans();
  58. try {
  59. $salt =makeSalt();
  60. $password = sha1($password.$salt);
  61. $data = ['username'=>$username,"password"=>$password,"salt"=>$salt,"mobile"=>$mobile,"source"=>$source,
  62. "status"=>1,"addtime"=>date("Y-m-d H:i:s"),"updatetime"=>date("Y-m-d H:i:s")];
  63. $reuslt = Db::table('sys_account')->insert($data,true);
  64. if($reuslt){
  65. $data=[
  66. "nickname"=>"",
  67. "mobile"=>$mobile,
  68. "email"=>"",
  69. "portrait"=>"",
  70. "sex"=>1,
  71. "post"=>"",
  72. "department"=>"",
  73. "status"=>1,
  74. "addtime"=>date("Y-m-d H:i:s"),
  75. "updatetime"=>date("Y-m-d H:i:s")
  76. ];
  77. $user=Db::table("sys_user")->insert($data,true);
  78. if($user){
  79. $table=[
  80. "accountid"=>$reuslt,
  81. "user_id"=>$user,
  82. "roleid"=>0,
  83. "updatetime"=>date("Y-m-d H:i:s")
  84. ];
  85. $rela = Db::table("sys_user_relation")->save($table);
  86. if($rela){
  87. Db::commit();
  88. return app_show(0,"账户注册成功");
  89. }
  90. Db::rollback();
  91. return error_show(1002,"用户信息管联失败");
  92. }
  93. Db::rollback();
  94. return error_show(1002,"用户信息注册失败");
  95. }
  96. Db::rollback();
  97. return error_show(1002,"账户注册失败");
  98. }catch (\Exception $e){
  99. Db::rollback();
  100. var_dump($e->getMessage());
  101. return error_show(1002,"账户注册失败");
  102. }
  103. }
  104. /**
  105. * 显示创建资源表单页.
  106. *
  107. * @return \think\Response
  108. */
  109. public function verify_code()
  110. {
  111. $post = $this->request->post();
  112. $code = make_verify();
  113. $mobile = isset($post['mobile'])&&checkMobile($post['mobile']) ? $post['mobile'] :"" ;
  114. if($mobile==""){
  115. return app_show(1001,"手机号格式不正确");
  116. }
  117. $mess =Db::name("send_message")->where(['mobile'=>$mobile,"status"=>0,"msg_type"=>1])->find();
  118. if($mess){
  119. if($mess['expire']>time()-60){
  120. return app_show(1001,"验证码发送中!");
  121. }
  122. $mess['status']=1;
  123. Db::name("send_message")->save($mess);
  124. }
  125. // $sendJson = sendMessage($mobile, $code);
  126. // $sendResult = json_decode($sendJson, TRUE);
  127. // if($sendResult['description'] != 'Success') {
  128. // return app_show(1002, '短信发送失败,请重试');
  129. // }
  130. $data=['code'=>$code,"mobile"=>$mobile,"status"=>0,"msg_type"=>1,"addtime"=>date("Y-m-d H:i:s"),
  131. "expire"=>time()];
  132. $result = Db::name("send_message")->insert($data);
  133. return $result ? app_show(0,"验证码已发送",$code): app_show(1001,"验证码发送失败");
  134. }
  135. /**
  136. * 保存新建的资源
  137. *
  138. * @param \think\Request $request
  139. * @return \think\Response
  140. */
  141. public function login()
  142. {
  143. $post = $this->request->post();
  144. $username = isset($post['username']) ? trim($post['username']):"";
  145. if($username==""){
  146. return app_show(1002,"账户名不能为空!");
  147. }
  148. $password = isset($post['password']) ? trim($post['password']):"";
  149. if($password==""){
  150. return app_show(1002,"密码不能为空!");
  151. }
  152. $acc= Db::table("sys_account")->where(['username'=>$username])->find();
  153. if($acc==false){
  154. return app_show(1003,'账户名不存在');
  155. }
  156. if($acc['status']==0){
  157. return app_show(1003,'账户名已禁用');
  158. }
  159. $sha1=sha1($password.$acc['salt']);
  160. if($sha1!=$acc['password']){
  161. return app_show(1003,'账户或密码错误');
  162. }
  163. $userinfo = Db::name("view_userinfo")->where("id","=", $acc['id'])->find();
  164. $token = makeToken($userinfo,time()+1800);
  165. if($token==""){
  166. return app_show(1003,'token生成失败');
  167. }
  168. $user=['userinfo'=>$userinfo ,"token"=>$token];
  169. // $data = [
  170. // "info"=>"钉钉登录",
  171. // "action"=>"/Api",
  172. // "url"=>"/login",
  173. // "param"=>"",
  174. // "name"=>$userinfo['nickname'],
  175. // "rolename"=>"",
  176. // "addtime"=>date("Y-m-d H:i:s")
  177. // ];
  178. // Db::name("system_log")->save($data);
  179. return app_show(0,"登录成功",$user);
  180. }
  181. /**
  182. * 显示指定的资源
  183. *
  184. * @param int $id
  185. * @return \think\Response
  186. */
  187. public function prefect()
  188. {
  189. $post=$this->request->post();
  190. $toke= isset($post['token'])? trim($post['token']):"";
  191. if ($toke==""){
  192. return app_show(100,"token不能为空");
  193. }
  194. $verify = VerifyToken($toke);
  195. if ($verify['code']!=0){
  196. return app_show($verify['code'],$verify['message']);
  197. }
  198. $userinfo = Db::name("view_userinfo")->where("id","=",$verify['user']['id'])->find();
  199. $nickname= isset($post['nickname'])? trim($post['nickname']):"";
  200. if ($nickname==""){
  201. return app_show(1001,"昵称不能为空");
  202. }
  203. $mobile= isset($post['mobile'])? trim($post['mobile']):"";
  204. if ($mobile==""){
  205. return app_show(1002,"手机号不能为空");
  206. }
  207. if (checkMobile($mobile)==false){
  208. return app_show(1002,"手机号格式不正确");
  209. }
  210. $email= isset($post['email'])? trim($post['email']):"";
  211. if ($email==""){
  212. return app_show(1003,"邮箱不能为空");
  213. }
  214. if (checkEmail($email)==false){
  215. return app_show(1003,"邮箱格式不正确");
  216. }
  217. $avatar = isset($post['portrait'])? trim($post['portrait']):"";
  218. $sex = isset($post['sex'])? trim($post['sex']):"";
  219. $post = isset($post['post'])? trim($post['post']):"";
  220. $department = isset($post['department'])? trim($post['department']):"";
  221. $userio=[
  222. "id"=>$userinfo['user_id'],
  223. "nickname"=>$nickname,
  224. "mobile"=>$mobile,
  225. "email"=>$email,
  226. "portrait"=>$avatar,
  227. "sex"=>$sex,
  228. "post"=>$post,
  229. "department"=>$department,
  230. "updatetime"=>date("Y-m-d H:i:s")
  231. ];
  232. Db::startTrans();
  233. $result=Db::name("account")->where("id","=",$userinfo['id'])->save(["mobile"=>$mobile,"username"=>$mobile,"updatetime"=>date("Y-m-d")]);
  234. if($result){
  235. $user= Db::name("user")->save($userio);
  236. if($user){
  237. Db::commit();
  238. return app_show(0,"信息修改成功");
  239. }
  240. }
  241. Db::rollback();
  242. return app_show(1004,"信息修改失败");
  243. }
  244. /**
  245. * @return \think\response\Json|void
  246. * @throws \think\db\exception\DataNotFoundException
  247. * @throws \think\db\exception\DbException
  248. * @throws \think\db\exception\ModelNotFoundException
  249. * @throws \think\exception\DbException
  250. */
  251. public function GetUserInfo()
  252. {
  253. $post=$this->request->post();
  254. $toke= isset($post['token'])? trim($post['token']):"";
  255. if ($toke==""){
  256. return app_show(100,"token不能为空");
  257. }
  258. $verify = VerifyToken($toke);
  259. if ($verify['code']!=0){
  260. return app_show($verify['code'],$verify['message']);
  261. }
  262. $userid= $verify['user']['id'];
  263. $userinfo = Db::name("view_userinfo")->where(['id'=>$userid])->find();
  264. if(!$userinfo){
  265. return app_show(106,"用户信息未完善");
  266. }
  267. return app_show(0,"信息获取成功",$userinfo);
  268. }
  269. /**
  270. * 保存更新的资源
  271. *
  272. * @param \think\Request $request
  273. * @param int $id
  274. * @return \think\Response
  275. */
  276. public function DingTalk()
  277. {
  278. $config= Config("app")['dingtalk'];
  279. $dingtalk =new \DingTalk($config);
  280. $code = $this->request->post("code");
  281. if($code==""){
  282. return error_show(106,"code不能为空");
  283. }
  284. $li = $dingtalk->getUserByCode($code);
  285. Log::record(var_export($li,true));
  286. if($li['errcode']!=0){
  287. return app_show(107,"授权失败",$li);
  288. }
  289. $list = $dingtalk->getUser($li['userid']);
  290. if($list['errcode']!=0){
  291. return app_show(107,"授权失败",$list);
  292. }
  293. $userinfo = Db::name("view_userinfo")->where(['DTuserid'=>$list['userid'],"unionid"=>$list['unionid']])->find();
  294. if(empty($userinfo)){
  295. $userinfo = $this->DingTalkRegister($list);
  296. }
  297. $token = makeToken($userinfo,time()+1800);
  298. $userinfo['token']=$token;
  299. // $data = [
  300. // "info"=>"钉钉登录",
  301. // "action"=>"/Api",
  302. // "url"=>"/login",
  303. // "param"=>"",
  304. // "name"=>$userinfo['nickname'],
  305. // "rolename"=>"",
  306. // "addtime"=>date("Y-m-d H:i:s")
  307. // ];
  308. // Db::name("system_log")->save($data);
  309. return app_show(0,"授权成功",$userinfo);
  310. }
  311. private function DingTalkRegister($Dingtalinfo){
  312. Db::startTrans();
  313. $verify = Db::name("user")->where("mobile","=",$Dingtalinfo['mobile'])->find();
  314. if(!empty($verify)){
  315. $verify['unionid']=$Dingtalinfo['unionid'];
  316. $verify['openId']=$Dingtalinfo['openId'];
  317. $verify['DTuserid']=$Dingtalinfo['userid'];
  318. $verify['mobile']=$Dingtalinfo['mobile'];
  319. isset($verify['portrait'])??$verify['portrait']=$Dingtalinfo['avatar'];
  320. isset($verify['email'])??$verify['email']=$Dingtalinfo['email'];
  321. $verify['updatetime']=date("Y-m-d H:i:s");
  322. $user =Db::table("sys_user")->save($verify);
  323. $uid = $verify["id"];
  324. }else{
  325. $data=[
  326. "nickname"=>$Dingtalinfo['name'],
  327. "mobile"=>$Dingtalinfo['mobile'],
  328. "email"=>$Dingtalinfo['email'],
  329. "portrait"=>$Dingtalinfo['avatar'],
  330. "sex"=>1,
  331. "post"=>"",
  332. "unionid"=>$Dingtalinfo['unionid'],
  333. "openId"=>$Dingtalinfo['openId'],
  334. "DTuserid"=>$Dingtalinfo['userid'],
  335. "department"=>"",
  336. "status"=>1,
  337. "addtime"=>date("Y-m-d H:i:s"),
  338. "updatetime"=>date("Y-m-d H:i:s")
  339. ];
  340. $uid =Db::table("sys_user")->insert($data,true);
  341. }
  342. if($uid<=0){
  343. Db::rollback();
  344. return [];
  345. }
  346. $relation = Db::table("sys_user_relation")->where("user_id","=",$uid)->find();
  347. if(empty($relation)){
  348. $salt=makeSalt();
  349. $data=[
  350. "username"=>$Dingtalinfo['mobile'],
  351. "password"=>sha1("dingding123".$salt),
  352. "salt"=>$salt,
  353. "status"=>1,
  354. "source"=>"dingtalk",
  355. "addtime"=>date("Y-m-d H:i:s"),
  356. "updatetime"=>date("Y-m-d H:i:s")
  357. ];
  358. $account = Db::table("sys_account")->insert($data,true);
  359. $rela =['accountid'=>$account,"user_id"=>$uid,"roleid"=>0,"updatetime"=>date("Y-m-d H:i:s")];
  360. $account_relation = Db::table("sys_user_relation")->save($rela);
  361. if(!$account_relation){
  362. Db::rollback();
  363. return [];
  364. }
  365. }
  366. Db::commit();
  367. $userinfo = Db::name("view_userinfo")->where("user_id","=",$uid)->find();
  368. return $userinfo;
  369. }
  370. /**
  371. *
  372. */
  373. public function verify_token(){
  374. $post=$this->request->post();
  375. $toke= isset($post['token'])? trim($post['token']):"";
  376. if ($toke==""){
  377. return app_show(100,"token不能为空");
  378. }
  379. $verify = VerifyToken($toke);
  380. return app_show($verify['code'],$verify['message'],["user"=>isset($verify['user'])?$verify['user']:""]);
  381. }
  382. /**
  383. * @return \think\response\Json|void
  384. * @throws \think\db\exception\DataNotFoundException
  385. * @throws \think\db\exception\DbException
  386. * @throws \think\db\exception\ModelNotFoundException
  387. * @throws \think\exception\DbException
  388. */
  389. public function reset_password(){
  390. $post=$this->request->post();
  391. $toke= isset($post['token'])? trim($post['token']):"";
  392. if ($toke==""){
  393. return app_show(100,"token不能为空");
  394. }
  395. $verify = VerifyToken($toke);
  396. if ($verify['code']!=0){
  397. return app_show($verify['code'],$verify['message']);
  398. }
  399. $userinfo = Db::name("account")->where("id","=",$verify['user']['id'])->find();
  400. $oldpwd = isset($post['oldpwd'])?trim($post['oldpwd']):"";
  401. if($oldpwd==""){
  402. return error_show(1002,"原密码不能为空");
  403. }
  404. if(sha1($oldpwd.$userinfo['salt'])!=$userinfo['password']){
  405. return error_show(1002,"原密码错误");
  406. }
  407. $newpwd = isset($post['newpwd'])?trim($post['newpwd']):"";
  408. if($newpwd==""){
  409. return error_show(1002,"新密码不能为空");
  410. }
  411. $salt=makeSalt();
  412. $pass=sha1($newpwd.$salt);
  413. $userinfo['salt']=$salt;
  414. $userinfo['password']=$pass;
  415. $userinfo['is_pass']=1;
  416. $result= Db::name("account")->save($userinfo);
  417. return $result ?app_show(0,"密码修改成功"):error_show(1003,"密码修改失败");
  418. }
  419. /**
  420. * @return \think\response\Json|void
  421. * @throws \think\db\exception\DataNotFoundException
  422. * @throws \think\db\exception\DbException
  423. * @throws \think\db\exception\ModelNotFoundException
  424. */
  425. public function reset_password_mobile(){
  426. $post=$this->request->post();
  427. $mobile = isset($post['mobile'])? trim($post['mobile']):"";
  428. if($mobile==""){
  429. return error_show(1001,"手机号不能为空");
  430. }
  431. if(checkMobile($mobile)==false){
  432. return error_show(1002,"手机号格式不正确!");
  433. }
  434. $code = isset($post['code'])? trim($post['code']):"";
  435. if($code==""){
  436. return error_show(1001,"验证码不能为空");
  437. }
  438. $username = isset($post['username'])?trim($post['username']):"";
  439. if($username==""){
  440. return error_show(1001,"参数username 不能为空");
  441. }
  442. $account = Db::name("account")->where("username","=",$username)->find();
  443. if($account['mobile']!=$mobile){
  444. return error_show(1004,"账户关联手机号不正确");
  445. }
  446. $password = isset($post['password'])?trim($post['password']):"";
  447. if($password==""){
  448. return error_show(1001,"新密码不能为空");
  449. }
  450. if(sha1($password.$account['salt'])==$account['password']){
  451. return error_show(1001,"新密码不能与原密码相同");
  452. }
  453. $codeinfo = Db::name("send_message")->where(["mobile"=>$mobile,"status"=>0,"msg_type"=>1])->find();
  454. if($code!=$codeinfo['code']){
  455. return error_show(1003,"验证码错误");
  456. }
  457. $codeinfo['status']=1;
  458. Db::name("send_message")->save($codeinfo);
  459. $account['salt']=makeSalt();
  460. $account['updatetime']=date("Y-m-d");
  461. $account['is_pass']=1;
  462. $account['password']=sha1($password.$account['salt']);
  463. $result=Db::name("account")->save($account);
  464. return $result?app_show(0,"密码修改成功"):app_show(1003,"密码修改失败");
  465. }
  466. /**
  467. * @return \think\response\Json|void
  468. * @throws \think\db\exception\DataNotFoundException
  469. * @throws \think\db\exception\DbException
  470. * @throws \think\db\exception\ModelNotFoundException
  471. */
  472. public function reset_mobile(){
  473. $post=$this->request->post();
  474. $toke= isset($post['token'])? trim($post['token']):"";
  475. if ($toke==""){
  476. return app_show(100,"token不能为空");
  477. }
  478. $verify = VerifyToken($toke);
  479. if ($verify['code']!=0){
  480. return app_show($verify['code'],$verify['message']);
  481. }
  482. $mobile = isset($post['mobile'])? trim($post['mobile']):"";
  483. if($mobile==""){
  484. return error_show(1001,"手机号不能为空");
  485. }
  486. if(checkMobile($mobile)==false){
  487. return error_show(1002,"手机号格式不正确!");
  488. }
  489. $code = isset($post['code'])? trim($post['code']):"";
  490. if($code==""){
  491. return error_show(1001,"验证码不能为空");
  492. }
  493. $account = Db::name("view_userinfo")->where("id","=",$verify['user']['id'])->find();
  494. if($account['mobile']==$mobile){
  495. return error_show(1004,"新手机号与原手机号相同");
  496. }
  497. $codeinfo = Db::name("send_message")->where(["mobile"=>$mobile,"status"=>0,"msg_type"=>1])->find();
  498. if(!$codeinfo||$code!=$codeinfo['code']){
  499. return error_show(1003,"验证码错误");
  500. }
  501. $codeinfo['status']=1;
  502. Db::name("send_message")->save($codeinfo);
  503. Db::startTrans();
  504. $result=Db::name("account")->where("id","=",$account['id'])->save(["mobile"=>$mobile,"username"=>$mobile,"updatetime"=>date("Y-m-d")]);
  505. if($result){
  506. $user= Db::name("user")->where("id","=",$account['user_id'])->save(["mobile"=>$mobile]);
  507. if($user){
  508. Db::commit();
  509. return app_show(0,"手机号修改成功");
  510. }
  511. }
  512. Db::rollback();
  513. return app_show(1003,"手机号修改失败");
  514. }
  515. /**
  516. * @return \think\response\Json|void
  517. * @throws \think\db\exception\DataNotFoundException
  518. * @throws \think\db\exception\DbException
  519. * @throws \think\db\exception\ModelNotFoundException
  520. * @throws \think\exception\DbException
  521. */
  522. public function GetUserlist(){
  523. $post=$this->request->post();
  524. $toke= isset($post['token'])? trim($post['token']):"";
  525. if ($toke==""){
  526. return app_show(100,"token不能为空");
  527. }
  528. $verify = VerifyToken($toke);
  529. if ($verify['code']!=0){
  530. return app_show($verify['code'],$verify['message']);
  531. }
  532. $condition = "1";
  533. $id = isset($post['id'])&&is_array($post['id'])? $post['id'] :[];
  534. if(!empty($id)){
  535. $condition .= " and id in (".implode(",",$id).")";
  536. }
  537. $roleid = isset($post['roleid'])? intval($post['roleid']) :"";
  538. if($roleid!=""){
  539. $condition .= " and roleid ={$roleid}";
  540. }
  541. $user = Db::name("view_userinfo")->where($condition)->select();
  542. return app_show(0,"获取成功",$user);
  543. }
  544. }