User.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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. $this->error('验签过期');
  208. }
  209. //找到用户
  210. $user = Db::name('user')->where('comefrom',2)->where('tv_userid',$tv_userid)->find();
  211. if ($user) {
  212. /*if ($user['status'] == -1) {
  213. $this->error('账号已注销');
  214. }
  215. if ($user['status'] != 1) {
  216. $this->error(__('Account is locked'));
  217. }*/
  218. //如果已经有账号则直接登录
  219. $ret = $this->auth->direct($user['id']);
  220. } else {
  221. $extend = ['tv_mobile'=>$tv_mobile];
  222. $ret = $this->auth->tv_register($tv_userid,$extend);
  223. }
  224. if ($ret) {
  225. $this->success(__('Logged in successful'), $this->auth->getUserinfo_simple());
  226. } else {
  227. $this->error($this->auth->getError());
  228. }
  229. }
  230. //用户详细资料
  231. public function userInfo(){
  232. $info = $this->auth->getUserinfo();
  233. $this->success(__('success'),$info);
  234. }
  235. /**
  236. * 退出登录
  237. * @ApiMethod (POST)
  238. */
  239. public function logout()
  240. {
  241. if (!$this->request->isPost()) {
  242. $this->error(__('Invalid parameters'));
  243. }
  244. $this->auth->logout();
  245. $this->success(__('Logout successful'));
  246. }
  247. /**
  248. * 修改会员个人信息
  249. *
  250. * @ApiMethod (POST)
  251. * @param string $avatar 头像地址
  252. * @param string $username 用户名
  253. * @param string $nickname 昵称
  254. * @param string $bio 个人简介
  255. */
  256. public function profile()
  257. {
  258. $field_array = [
  259. 'avatar','nickname','gender','tuijian_switch'
  260. ];
  261. $data = [];
  262. foreach($field_array as $key => $field){
  263. //前端传不了post,改了
  264. /*if(!request()->has($field,'post')){
  265. continue;
  266. }*/
  267. if(!input('?'.$field)){
  268. continue;
  269. }
  270. $newone = input($field);
  271. if($field == 'avatar'){
  272. $newone = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  273. }
  274. if($field == 'nickname'){
  275. $newone = Keyworld::sensitive($newone);
  276. }
  277. $data[$field] = $newone;
  278. }
  279. //
  280. if(isset($data['birthday'])){
  281. $data['birthday'] = strtotime($data['birthday']);
  282. }
  283. if(empty($data)){
  284. $this->success();
  285. }
  286. $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  287. $this->success();
  288. }
  289. /**
  290. * 修改手机号
  291. *
  292. * @ApiMethod (POST)
  293. * @param string $mobile 手机号
  294. * @param string $captcha 验证码
  295. */
  296. public function changemobile()
  297. {
  298. $user = $this->auth->getUser();
  299. $oldcaptcha = input('oldcaptcha');
  300. $mobile = input('mobile');
  301. $captcha = input('captcha');
  302. if (!$oldcaptcha || !$mobile || !$captcha) {
  303. $this->error(__('Invalid parameters'));
  304. }
  305. if (!Validate::regex($mobile, "^1\d{10}$")) {
  306. $this->error(__('Mobile is incorrect'));
  307. }
  308. if($user->mobile == $mobile){
  309. $this->error('新手机号不能与旧手机号相同');
  310. }
  311. if (\app\common\model\User::where('mobile', $mobile)->find()) {
  312. $this->error(__('Mobile already exist'));
  313. }
  314. $result = Sms::check($user->mobile, $oldcaptcha, 'changemobile');
  315. if (!$result) {
  316. $this->error('原手机号验证码错误');
  317. }
  318. $result = Sms::check($mobile, $captcha, 'changemobile');
  319. if (!$result) {
  320. $this->error('新手机号验证码错误');
  321. }
  322. Sms::flush($user->mobile, 'changemobile');
  323. Sms::flush($mobile, 'changemobile');
  324. $user->mobile = $mobile;
  325. $user->save();
  326. $this->success();
  327. }
  328. //假注销
  329. public function cancleUser(){
  330. /*$captcha = input('captcha','');
  331. if (!$captcha) {
  332. $this->error(__('Invalid parameters'));
  333. }
  334. if (!Sms::check($this->auth->mobile, $captcha, 'mobilelogin')) {
  335. $this->error(__('Captcha is incorrect'));
  336. }*/
  337. Db::name('user')->where('id',$this->auth->id)->update(['status'=>-1]);
  338. $this->auth->logout();
  339. $this->success('注销成功');
  340. }
  341. //绑定盒子手机号
  342. public function bind_tv(){
  343. $tv_mobile = input('tv_mobile','');
  344. if(empty($tv_mobile)){
  345. $this->error();
  346. }
  347. //跨数据库查询
  348. $tv_user = Db::connect('database_tv')->name('hu_user')->where('mobile',$tv_mobile)->find();
  349. if(empty($tv_user)){
  350. $this->error('没有找到该终端用户');
  351. }
  352. $update = [
  353. 'tv_userid'=>$tv_user['id'],
  354. 'tv_mobile'=>$tv_user['mobile'],
  355. ];
  356. $rs = Db::name('user')->where('id',$this->auth->id)->update($update);
  357. if($rs === false){
  358. $this->error('绑定失败');
  359. }
  360. $this->success('绑定成功');
  361. }
  362. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  363. /**
  364. * 修改邮箱
  365. *
  366. * @ApiMethod (POST)
  367. * @param string $email 邮箱
  368. * @param string $captcha 验证码
  369. */
  370. public function changeemail()
  371. {
  372. $user = $this->auth->getUser();
  373. $email = input('email');
  374. $captcha = input('captcha');
  375. if (!$email || !$captcha) {
  376. $this->error(__('Invalid parameters'));
  377. }
  378. if (!Validate::is($email, "email")) {
  379. $this->error(__('Email is incorrect'));
  380. }
  381. if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) {
  382. $this->error(__('Email already exists'));
  383. }
  384. $result = Ems::check($email, $captcha, 'changeemail');
  385. if (!$result) {
  386. $this->error(__('Captcha is incorrect'));
  387. }
  388. $verification = $user->verification;
  389. $verification->email = 1;
  390. $user->verification = $verification;
  391. $user->email = $email;
  392. $user->save();
  393. Ems::flush($email, 'changeemail');
  394. $this->success();
  395. }
  396. /**
  397. * 第三方登录
  398. *
  399. * @ApiMethod (POST)
  400. * @param string $platform 平台名称
  401. * @param string $code Code码
  402. */
  403. public function third()
  404. {
  405. $url = url('user/index');
  406. $platform = input("platform");
  407. $code = input("code");
  408. $config = get_addon_config('third');
  409. if (!$config || !isset($config[$platform])) {
  410. $this->error(__('Invalid parameters'));
  411. }
  412. $app = new \addons\third\library\Application($config);
  413. //通过code换access_token和绑定会员
  414. $result = $app->{$platform}->getUserInfo(['code' => $code]);
  415. if ($result) {
  416. $loginret = \addons\third\library\Service::connect($platform, $result);
  417. if ($loginret) {
  418. $data = [
  419. 'userinfo' => $this->auth->getUserinfo(),
  420. 'thirdinfo' => $result
  421. ];
  422. $this->success(__('Logged in successful'), $data);
  423. }
  424. }
  425. $this->error(__('Operation failed'), $url);
  426. }
  427. /**
  428. * 重置密码
  429. *
  430. * @ApiMethod (POST)
  431. * @param string $mobile 手机号
  432. * @param string $newpassword 新密码
  433. * @param string $captcha 验证码
  434. */
  435. public function resetpwd()
  436. {
  437. $type = input("type", "mobile");
  438. $mobile = input("mobile");
  439. $email = input("email");
  440. $newpassword = input("newpassword");
  441. $captcha = input("captcha");
  442. if (!$newpassword || !$captcha) {
  443. $this->error(__('Invalid parameters'));
  444. }
  445. //验证Token
  446. if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
  447. $this->error(__('Password must be 6 to 30 characters'));
  448. }
  449. if ($type == 'mobile') {
  450. if (!Validate::regex($mobile, "^1\d{10}$")) {
  451. $this->error(__('Mobile is incorrect'));
  452. }
  453. $user = \app\common\model\User::getByMobile($mobile);
  454. if (!$user) {
  455. $this->error(__('User not found'));
  456. }
  457. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  458. if (!$ret) {
  459. $this->error(__('Captcha is incorrect'));
  460. }
  461. Sms::flush($mobile, 'resetpwd');
  462. } else {
  463. if (!Validate::is($email, "email")) {
  464. $this->error(__('Email is incorrect'));
  465. }
  466. $user = \app\common\model\User::getByEmail($email);
  467. if (!$user) {
  468. $this->error(__('User not found'));
  469. }
  470. $ret = Ems::check($email, $captcha, 'resetpwd');
  471. if (!$ret) {
  472. $this->error(__('Captcha is incorrect'));
  473. }
  474. Ems::flush($email, 'resetpwd');
  475. }
  476. //模拟一次登录
  477. $this->auth->direct($user->id);
  478. $ret = $this->auth->changepwd($newpassword, '', true);
  479. if ($ret) {
  480. $this->success(__('Reset password successful'));
  481. } else {
  482. $this->error($this->auth->getError());
  483. }
  484. }
  485. /**
  486. * 会员登录
  487. *
  488. * @ApiMethod (POST)
  489. * @param string $account 账号
  490. * @param string $password 密码
  491. */
  492. public function login()
  493. {
  494. $account = input('account');
  495. $password = input('password');
  496. if (!$account || !$password) {
  497. $this->error(__('Invalid parameters'));
  498. }
  499. $ret = $this->auth->login($account, $password);
  500. if ($ret) {
  501. $data = ['userinfo' => $this->auth->getUserinfo()];
  502. $this->success(__('Logged in successful'), $data);
  503. } else {
  504. $this->error($this->auth->getError());
  505. }
  506. }
  507. /**
  508. * 注册会员
  509. *
  510. * @ApiMethod (POST)
  511. * @param string $username 用户名
  512. * @param string $password 密码
  513. * @param string $email 邮箱
  514. * @param string $mobile 手机号
  515. * @param string $code 验证码
  516. */
  517. public function register()
  518. {
  519. $username = input('username');
  520. $password = input('password');
  521. $email = input('email');
  522. $mobile = input('mobile');
  523. $code = input('code');
  524. if (!$username || !$password) {
  525. $this->error(__('Invalid parameters'));
  526. }
  527. if ($email && !Validate::is($email, "email")) {
  528. $this->error(__('Email is incorrect'));
  529. }
  530. if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
  531. $this->error(__('Mobile is incorrect'));
  532. }
  533. $ret = Sms::check($mobile, $code, 'register');
  534. if (!$ret) {
  535. $this->error(__('Captcha is incorrect'));
  536. }
  537. $ret = $this->auth->register($username, $password, $email, $mobile, []);
  538. if ($ret) {
  539. $data = ['userinfo' => $this->auth->getUserinfo()];
  540. $this->success(__('Sign up successful'), $data);
  541. } else {
  542. $this->error($this->auth->getError());
  543. }
  544. }
  545. }