User.php 16 KB

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