User.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\library\Ems;
  5. use app\common\library\Sms;
  6. use fast\Random;
  7. use think\Config;
  8. use think\Validate;
  9. use app\common\library\Keyworld;
  10. use think\Db;
  11. use app\common\library\Wechat;
  12. /**
  13. * 会员接口
  14. */
  15. class User extends Api
  16. {
  17. protected $noNeedLogin = ['login', 'mobilelogin', 'tvuser_login', 'wechatlogin', 'bindmobile','register', 'resetpwd', 'changeemail', 'changemobile', 'third'];
  18. protected $noNeedRight = '*';
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. if (!Config::get('fastadmin.usercenter')) {
  23. $this->error(__('User center already closed'));
  24. }
  25. }
  26. /**
  27. * 会员中心
  28. */
  29. public function index()
  30. {
  31. $this->success('', ['welcome' => $this->auth->nickname]);
  32. }
  33. /**
  34. * 手机验证码登录
  35. *
  36. * @ApiMethod (POST)
  37. * @param string $mobile 手机号
  38. * @param string $captcha 验证码
  39. */
  40. public function mobilelogin()
  41. {
  42. $mobile = input('mobile');
  43. $captcha = input('captcha');
  44. if (!$mobile || !$captcha) {
  45. $this->error(__('Invalid parameters'));
  46. }
  47. if (!Validate::regex($mobile, "^1\d{10}$")) {
  48. $this->error(__('Mobile is incorrect'));
  49. }
  50. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  51. $this->error(__('Captcha is incorrect'));
  52. }
  53. $user = \app\common\model\User::getByMobile($mobile);
  54. if ($user) {
  55. if ($user->status == -1) {
  56. $this->error('账号已注销');
  57. }
  58. if ($user->status != 1) {
  59. $this->error(__('Account is locked'));
  60. }
  61. //如果已经有账号则直接登录
  62. $ret = $this->auth->direct($user->id);
  63. } else {
  64. $ret = $this->auth->register('', '', '', $mobile, []);
  65. }
  66. if ($ret) {
  67. Sms::flush($mobile, 'mobilelogin');
  68. $this->success(__('Logged in successful'), $this->auth->getUserinfo_simple());
  69. } else {
  70. $this->error($this->auth->getError());
  71. }
  72. }
  73. //微信登录,预先假注册
  74. public function wechatlogin(){
  75. $code = input('code','');
  76. if(!$code){
  77. $this->error();
  78. }
  79. //微信
  80. $wechat = new Wechat();
  81. $wxuserinfo = $wechat->getAccessToken($code);
  82. if(!$wxuserinfo){
  83. $this->error('openid获取失败');
  84. }
  85. if(!is_array($wxuserinfo) || !isset($wxuserinfo['openid'])){
  86. $this->error('openid获取失败');
  87. }
  88. $openid = $wxuserinfo['openid'];
  89. //检查用户
  90. $user = Db::name('user')->where('wechat_openid',$openid)->find();
  91. if ($user) {
  92. if ($user['status'] == -1) {
  93. $this->error('账户已注销');
  94. }
  95. if ($user['status'] != 1) {
  96. $this->error(__('Account is locked'));
  97. }
  98. //如果已经有账号则直接登录
  99. $ret = $this->auth->direct($user['id']);
  100. if ($ret) {
  101. $userInfo = $this->auth->getUserinfo_simple();
  102. $userInfo['is_register'] = 0;
  103. $userInfo['code'] = $code;
  104. $this->success(__('Logged in successful'), $userInfo);
  105. } else {
  106. $this->error($this->auth->getError());
  107. }
  108. } else {
  109. //记录code和openid,绑定手机号的时候更新openid
  110. $wechatCodeData = [
  111. 'code' => $code,
  112. 'openid' => $openid,
  113. 'createtime' => time(),
  114. ];
  115. $wechatCode = Db::name('wechat_code')->where(['openid'=>$openid])->find();
  116. if (empty($wechatCode)) {
  117. Db::name('wechat_code')->insertGetId($wechatCodeData);
  118. } else {
  119. Db::name('wechat_code')->where(['openid'=>$openid])->update($wechatCodeData);
  120. }
  121. //直接返回
  122. $userInfo = [];
  123. $userInfo['is_register'] = 1;
  124. $userInfo['code'] = $code;
  125. $this->success('获取信息成功', $userInfo);
  126. }
  127. }
  128. /**
  129. * 微信注册来的,绑定手机号
  130. *
  131. * @ApiMethod (POST)
  132. * @param string $mobile 手机号
  133. * @param string $captcha 验证码
  134. */
  135. public function bindmobile()
  136. {
  137. $mobile = input('mobile');
  138. $captcha = input('captcha');
  139. $code = input('code');
  140. if (!$mobile || !$captcha || !$code) {
  141. $this->error(__('Invalid parameters'));
  142. }
  143. if (!Validate::regex($mobile, "^1\d{10}$")) {
  144. $this->error(__('Mobile is incorrect'));
  145. }
  146. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  147. $this->error(__('Captcha is incorrect'));
  148. }
  149. $wechatCodeWhere['code'] = $code;
  150. $wechatCode = Db::name('wechat_code')->where($wechatCodeWhere)->find();
  151. if (empty($wechatCode)) {
  152. $this->error('请先微信登录');
  153. }
  154. //检查appid绑定的用户
  155. $user = Db::name('user')->where('wechat_openid',$wechatCode['openid'])->find();
  156. if ($user) {
  157. if ($user['status'] == -1) {
  158. $this->error('账户已注销');
  159. }
  160. if ($user['status'] != 1) {
  161. $this->error(__('Account is locked'));
  162. }
  163. //如果已经有账号则直接登录
  164. $ret = $this->auth->direct($user['id']);
  165. $this->success(__('Logged in successful'), $this->auth->getUserinfo_simple());
  166. }
  167. //新的openid用户
  168. $where = [];
  169. $where['mobile'] = $mobile;
  170. $userData = Db::name('user')->where($where)->find();//老用户
  171. if (!empty($userData)) {
  172. if (empty($userData['wechat_openid'])) {
  173. Db::name('user')->where('id',$userData['id'])->update(['wechat_openid' => $wechatCode['openid']]);//老用户更新openid
  174. } else {
  175. if ($userData['wechat_openid'] != $wechatCode['openid']) {
  176. $this->error('该手机号已被其他用户绑定');
  177. }
  178. }
  179. $ret = $this->auth->direct($userData['id']);
  180. } else {
  181. $extend = [
  182. 'wechat_openid' => $wechatCode['openid'],
  183. ];
  184. $ret = $this->auth->register('', '','', $mobile, $extend);
  185. }
  186. if (!$ret) {
  187. $this->error($this->auth->getError());
  188. }
  189. $this->success(__('Logged in successful'), $this->auth->getUserinfo_simple());
  190. }
  191. //电视盒子用户登录。
  192. public function tvuser_login(){
  193. $tv_userid = input('tv_userid','');
  194. $tv_mobile = input('tv_mobile','');
  195. $tv_signtime = input('tv_signtime','');
  196. $tv_sign = input('tv_sign','');
  197. if(empty($tv_userid) || empty($tv_mobile) || empty($tv_signtime) || empty($tv_sign)){
  198. $this->error('登录参数缺失');
  199. }
  200. //验签
  201. $salt = 'be7bcf1499b0fec801406f6aafbd04c4';
  202. $get_sign = md5(md5($tv_userid) . $tv_signtime . $salt);
  203. if($tv_sign != $get_sign){
  204. $this->error('验签失败');
  205. }
  206. if(time() - $tv_signtime > 300){
  207. //测试临时屏蔽
  208. $this->error('验签过期');
  209. }
  210. //找到用户
  211. $user = Db::name('user')->where('tv_userid',$tv_userid)->find();
  212. if ($user) {
  213. /*if ($user['status'] == -1) {
  214. $this->error('账号已注销');
  215. }
  216. if ($user['status'] != 1) {
  217. $this->error(__('Account is locked'));
  218. }*/
  219. //如果已经有账号则直接登录
  220. $ret = $this->auth->direct($user['id']);
  221. } else {
  222. $extend = ['tv_mobile'=>$tv_mobile];
  223. $ret = $this->auth->tv_register($tv_userid,$extend);
  224. }
  225. if ($ret) {
  226. $this->success(__('Logged in successful'), $this->auth->getUserinfo_simple());
  227. } else {
  228. $this->error($this->auth->getError());
  229. }
  230. }
  231. //用户详细资料
  232. public function userInfo(){
  233. $info = $this->auth->getUserinfo();
  234. $this->success(__('success'),$info);
  235. }
  236. /**
  237. * 退出登录
  238. * @ApiMethod (POST)
  239. */
  240. public function logout()
  241. {
  242. if (!$this->request->isPost()) {
  243. $this->error(__('Invalid parameters'));
  244. }
  245. $this->auth->logout();
  246. $this->success(__('Logout successful'));
  247. }
  248. /**
  249. * 修改会员个人信息
  250. *
  251. * @ApiMethod (POST)
  252. * @param string $avatar 头像地址
  253. * @param string $username 用户名
  254. * @param string $nickname 昵称
  255. * @param string $bio 个人简介
  256. */
  257. public function profile()
  258. {
  259. $field_array = [
  260. 'avatar','nickname','gender',
  261. ];
  262. $data = [];
  263. foreach($field_array as $key => $field){
  264. //前端传不了post,改了
  265. /*if(!request()->has($field,'post')){
  266. continue;
  267. }*/
  268. if(!input('?'.$field)){
  269. continue;
  270. }
  271. $newone = input($field);
  272. if($field == 'avatar'){
  273. $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  274. }
  275. if($field == 'nickname'){
  276. $newone = Keyworld::sensitive($newone);
  277. }
  278. $data[$field] = $newone;
  279. }
  280. //
  281. if(isset($data['birthday'])){
  282. $data['birthday'] = strtotime($data['birthday']);
  283. }
  284. if(empty($data)){
  285. $this->success();
  286. }
  287. $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  288. $this->success();
  289. }
  290. /**
  291. * 修改手机号
  292. *
  293. * @ApiMethod (POST)
  294. * @param string $mobile 手机号
  295. * @param string $captcha 验证码
  296. */
  297. public function changemobile()
  298. {
  299. $user = $this->auth->getUser();
  300. $oldcaptcha = input('oldcaptcha');
  301. $mobile = input('mobile');
  302. $captcha = input('captcha');
  303. if (!$oldcaptcha || !$mobile || !$captcha) {
  304. $this->error(__('Invalid parameters'));
  305. }
  306. if (!Validate::regex($mobile, "^1\d{10}$")) {
  307. $this->error(__('Mobile is incorrect'));
  308. }
  309. if($user->mobile == $mobile){
  310. $this->error('新手机号不能与旧手机号相同');
  311. }
  312. if (\app\common\model\User::where('mobile', $mobile)->find()) {
  313. $this->error(__('Mobile already exist'));
  314. }
  315. $result = Sms::check($user->mobile, $oldcaptcha, 'changemobile');
  316. if (!$result) {
  317. $this->error('原手机号验证码错误');
  318. }
  319. $result = Sms::check($mobile, $captcha, 'changemobile');
  320. if (!$result) {
  321. $this->error('新手机号验证码错误');
  322. }
  323. Sms::flush($user->mobile, 'changemobile');
  324. Sms::flush($mobile, 'changemobile');
  325. $user->mobile = $mobile;
  326. $user->save();
  327. $this->success();
  328. }
  329. //假注销
  330. public function cancleUser(){
  331. /*$captcha = input('captcha','');
  332. if (!$captcha) {
  333. $this->error(__('Invalid parameters'));
  334. }
  335. if (!Sms::check($this->auth->mobile, $captcha, 'mobilelogin')) {
  336. $this->error(__('Captcha is incorrect'));
  337. }*/
  338. Db::name('user')->where('id',$this->auth->id)->update(['status'=>-1]);
  339. $this->auth->logout();
  340. $this->success('注销成功');
  341. }
  342. //绑定盒子手机号
  343. public function bind_tv(){
  344. $tv_mobile = input('tv_mobile','');
  345. if(empty($tv_mobile)){
  346. $this->error();
  347. }
  348. //跨数据库查询
  349. $tv_user = Db::connect('database_tv')->name('hu_user')->where('mobile',$tv_mobile)->find();
  350. if(empty($tv_user)){
  351. $this->error('没有找到该终端用户');
  352. }
  353. $update = [
  354. 'tv_userid'=>$tv_user['id'],
  355. 'tv_mobile'=>$tv_user['mobile'],
  356. ];
  357. $rs = Db::name('user')->where('id',$this->auth->id)->update($update);
  358. if($rs === false){
  359. $this->error('绑定失败');
  360. }
  361. $this->success('绑定成功');
  362. }
  363. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  364. /**
  365. * 修改邮箱
  366. *
  367. * @ApiMethod (POST)
  368. * @param string $email 邮箱
  369. * @param string $captcha 验证码
  370. */
  371. public function changeemail()
  372. {
  373. $user = $this->auth->getUser();
  374. $email = input('email');
  375. $captcha = input('captcha');
  376. if (!$email || !$captcha) {
  377. $this->error(__('Invalid parameters'));
  378. }
  379. if (!Validate::is($email, "email")) {
  380. $this->error(__('Email is incorrect'));
  381. }
  382. if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) {
  383. $this->error(__('Email already exists'));
  384. }
  385. $result = Ems::check($email, $captcha, 'changeemail');
  386. if (!$result) {
  387. $this->error(__('Captcha is incorrect'));
  388. }
  389. $verification = $user->verification;
  390. $verification->email = 1;
  391. $user->verification = $verification;
  392. $user->email = $email;
  393. $user->save();
  394. Ems::flush($email, 'changeemail');
  395. $this->success();
  396. }
  397. /**
  398. * 第三方登录
  399. *
  400. * @ApiMethod (POST)
  401. * @param string $platform 平台名称
  402. * @param string $code Code码
  403. */
  404. public function third()
  405. {
  406. $url = url('user/index');
  407. $platform = input("platform");
  408. $code = input("code");
  409. $config = get_addon_config('third');
  410. if (!$config || !isset($config[$platform])) {
  411. $this->error(__('Invalid parameters'));
  412. }
  413. $app = new \addons\third\library\Application($config);
  414. //通过code换access_token和绑定会员
  415. $result = $app->{$platform}->getUserInfo(['code' => $code]);
  416. if ($result) {
  417. $loginret = \addons\third\library\Service::connect($platform, $result);
  418. if ($loginret) {
  419. $data = [
  420. 'userinfo' => $this->auth->getUserinfo(),
  421. 'thirdinfo' => $result
  422. ];
  423. $this->success(__('Logged in successful'), $data);
  424. }
  425. }
  426. $this->error(__('Operation failed'), $url);
  427. }
  428. /**
  429. * 重置密码
  430. *
  431. * @ApiMethod (POST)
  432. * @param string $mobile 手机号
  433. * @param string $newpassword 新密码
  434. * @param string $captcha 验证码
  435. */
  436. public function resetpwd()
  437. {
  438. $type = input("type", "mobile");
  439. $mobile = input("mobile");
  440. $email = input("email");
  441. $newpassword = input("newpassword");
  442. $captcha = input("captcha");
  443. if (!$newpassword || !$captcha) {
  444. $this->error(__('Invalid parameters'));
  445. }
  446. //验证Token
  447. if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
  448. $this->error(__('Password must be 6 to 30 characters'));
  449. }
  450. if ($type == 'mobile') {
  451. if (!Validate::regex($mobile, "^1\d{10}$")) {
  452. $this->error(__('Mobile is incorrect'));
  453. }
  454. $user = \app\common\model\User::getByMobile($mobile);
  455. if (!$user) {
  456. $this->error(__('User not found'));
  457. }
  458. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  459. if (!$ret) {
  460. $this->error(__('Captcha is incorrect'));
  461. }
  462. Sms::flush($mobile, 'resetpwd');
  463. } else {
  464. if (!Validate::is($email, "email")) {
  465. $this->error(__('Email is incorrect'));
  466. }
  467. $user = \app\common\model\User::getByEmail($email);
  468. if (!$user) {
  469. $this->error(__('User not found'));
  470. }
  471. $ret = Ems::check($email, $captcha, 'resetpwd');
  472. if (!$ret) {
  473. $this->error(__('Captcha is incorrect'));
  474. }
  475. Ems::flush($email, 'resetpwd');
  476. }
  477. //模拟一次登录
  478. $this->auth->direct($user->id);
  479. $ret = $this->auth->changepwd($newpassword, '', true);
  480. if ($ret) {
  481. $this->success(__('Reset password successful'));
  482. } else {
  483. $this->error($this->auth->getError());
  484. }
  485. }
  486. /**
  487. * 会员登录
  488. *
  489. * @ApiMethod (POST)
  490. * @param string $account 账号
  491. * @param string $password 密码
  492. */
  493. public function login()
  494. {
  495. $account = input('account');
  496. $password = input('password');
  497. if (!$account || !$password) {
  498. $this->error(__('Invalid parameters'));
  499. }
  500. $ret = $this->auth->login($account, $password);
  501. if ($ret) {
  502. $data = ['userinfo' => $this->auth->getUserinfo()];
  503. $this->success(__('Logged in successful'), $data);
  504. } else {
  505. $this->error($this->auth->getError());
  506. }
  507. }
  508. /**
  509. * 注册会员
  510. *
  511. * @ApiMethod (POST)
  512. * @param string $username 用户名
  513. * @param string $password 密码
  514. * @param string $email 邮箱
  515. * @param string $mobile 手机号
  516. * @param string $code 验证码
  517. */
  518. public function register()
  519. {
  520. $username = input('username');
  521. $password = input('password');
  522. $email = input('email');
  523. $mobile = input('mobile');
  524. $code = input('code');
  525. if (!$username || !$password) {
  526. $this->error(__('Invalid parameters'));
  527. }
  528. if ($email && !Validate::is($email, "email")) {
  529. $this->error(__('Email is incorrect'));
  530. }
  531. if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
  532. $this->error(__('Mobile is incorrect'));
  533. }
  534. $ret = Sms::check($mobile, $code, 'register');
  535. if (!$ret) {
  536. $this->error(__('Captcha is incorrect'));
  537. }
  538. $ret = $this->auth->register($username, $password, $email, $mobile, []);
  539. if ($ret) {
  540. $data = ['userinfo' => $this->auth->getUserinfo()];
  541. $this->success(__('Sign up successful'), $data);
  542. } else {
  543. $this->error($this->auth->getError());
  544. }
  545. }
  546. }