User.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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 app\common\library\Wechat;
  7. use fast\Random;
  8. use think\Config;
  9. use think\Validate;
  10. use think\Db;
  11. /**
  12. * 会员接口
  13. */
  14. class User extends Api
  15. {
  16. protected $noNeedLogin = ['wxmini_openid_login'];
  17. protected $noNeedRight = '*';
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. }
  22. //用户详细资料
  23. public function getuserinfo(){
  24. $info = $this->auth->getUserinfo();
  25. $this->success(__('success'),$info);
  26. }
  27. /**
  28. * 退出登录
  29. * @ApiMethod (POST)
  30. */
  31. public function logout()
  32. {
  33. if (!$this->request->isPost()) {
  34. $this->error(__('Invalid parameters'));
  35. }
  36. $this->auth->logout();
  37. $this->success(__('Logout successful'));
  38. }
  39. /**
  40. * 修改会员个人信息
  41. *
  42. * @ApiMethod (POST)
  43. * @param string $avatar 头像地址
  44. * @param string $username 用户名
  45. * @param string $nickname 昵称
  46. * @param string $bio 个人简介
  47. */
  48. public function profile()
  49. {
  50. $avatar = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  51. $nickname = input('nickname', '');
  52. $mobile = input('mobile', '');
  53. $gender = input('gender', '1');
  54. $birthday = input('birthday', '');
  55. $height = input('height', '');
  56. $weight = input('weight', '');
  57. //修改用户
  58. $data = [];
  59. if(!empty($avatar))
  60. {
  61. $data['avatar'] = $avatar;
  62. }
  63. if(!empty($nickname))
  64. {
  65. $data['nickname'] = $nickname;
  66. }
  67. if(!empty($mobile))
  68. {
  69. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $this->auth->id)->find()) {
  70. $this->error('手机号已被其他人使用');
  71. }
  72. $data['mobile'] = $mobile;
  73. }
  74. if($gender != $this->auth->gender)
  75. {
  76. $data['gender'] = $gender;
  77. }
  78. if(!empty($birthday))
  79. {
  80. $data['birthday'] = strtotime($birthday);
  81. }
  82. if(!empty($height))
  83. {
  84. $data['height'] = $height;
  85. }
  86. if (!empty($weight))
  87. {
  88. $data['weight'] = $weight;
  89. }
  90. if(!empty($data)){
  91. $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  92. if($update_rs === false){
  93. $this->error('修改资料失败');
  94. }
  95. }
  96. $this->success();
  97. }
  98. /**
  99. * 微信小程序登录+注册
  100. * code得到openid
  101. * 没用到
  102. */
  103. public function wxmini_openid_login() {
  104. $code = input('code');
  105. if (!$code) {
  106. $this->error(__('Invalid parameters'));
  107. }
  108. $config = config('wxMiniProgram');
  109. $getopenid = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$config['appid'].'&secret='.$config['secret'].'&js_code='.$code.'&grant_type=authorization_code';
  110. $openidInfo = $this->getJson($getopenid);
  111. if(!isset($openidInfo['openid'])) {
  112. $this->error('用户openid获取失败',$openidInfo);
  113. }
  114. $openid = $openidInfo['openid'];
  115. if (!$openid) {
  116. $this->error('用户openid获取失败');
  117. }
  118. //用户信息
  119. $userInfo = Db::name('user')->where(['wxmini_openid'=>$openid])->find();
  120. if($userInfo) {
  121. if ($userInfo['status'] == 0) {
  122. $this->error('账号已被禁用');
  123. }
  124. if ($userInfo['status'] == -1) {
  125. $this->error('账号已被注销');
  126. }
  127. //如果已经有账号则直接登录
  128. $res = $this->auth->direct($userInfo['id']);
  129. } else {
  130. $extend = [
  131. 'wxmini_openid' => $openid
  132. ];
  133. $res = $this->auth->register('','','','',$extend);
  134. }
  135. if($res) {
  136. $this->success("登录成功!",$this->auth->getUserinfo());
  137. } else {
  138. $this->error($this->auth->getError());
  139. }
  140. }
  141. /**
  142. * json 请求
  143. * @param $url
  144. * @return mixed
  145. */
  146. private function getJson($url){
  147. $ch = curl_init();
  148. curl_setopt($ch, CURLOPT_URL, $url);
  149. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  150. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  151. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  152. $output = curl_exec($ch);
  153. curl_close($ch);
  154. return json_decode($output, true);
  155. }
  156. /**
  157. * 微信小程序登录+注册
  158. * code得到注册手机号,此手机号登录+注册
  159. */
  160. public function wxmini_regmobile_login(){
  161. $code = input('code');
  162. $opencode = input('opencode');
  163. if (!$code || !$opencode) {
  164. $this->error(__('Invalid parameters'));
  165. }
  166. $config = config('wxMiniProgram');
  167. $wechat = new Wechat($config['appid'],$config['secret']);
  168. $getuserphonenumber = $wechat->getuserphonenumber($code);
  169. if(!isset($getuserphonenumber['phone_info']['purePhoneNumber'])){
  170. $this->error('授权获取手机号失败');
  171. }
  172. //获取openid
  173. $getopenid = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$config['appid'].'&secret='.$config['secret'].'&js_code='.$opencode.'&grant_type=authorization_code';
  174. $openidInfo = $this->getJson($getopenid);
  175. if(!isset($openidInfo['openid'])) {
  176. $this->error('用户openid获取失败',$openidInfo);
  177. }
  178. $openid = $openidInfo['openid'];
  179. if (!$openid) {
  180. $this->error('用户openid获取失败');
  181. }
  182. $mobile = $getuserphonenumber['phone_info']['purePhoneNumber'];
  183. $userInfo = Db::name('user')->where('mobile',$mobile)->find();
  184. // 判断用户是否已经存在
  185. if($userInfo) { // 登录
  186. if ($userInfo['status'] != 1) {
  187. $this->error(__('Account is locked'));
  188. }
  189. //如果已经有账号则直接登录
  190. $res = $this->auth->direct($userInfo['id']);
  191. } else {
  192. $extend = ['wxmini_openid'=>$openid];
  193. $res = $this->auth->register('', '', '',$mobile, $extend);
  194. }
  195. if($res) {
  196. $this->success("登录成功!",$this->auth->getUserinfo());
  197. } else {
  198. $this->error($this->auth->getError());
  199. }
  200. }
  201. ////////////////////////////////下面的都没用到///////////////////////////////
  202. //绑定邀请人
  203. public function bindintro(){
  204. $introcode = input('introcode','');
  205. $data = [];
  206. if(!empty($introcode)){
  207. $intro_user = Db::name('user')->where('username',$introcode)->field('id,intro_uid,group_id')->find();
  208. //邀请人不为空 && 我没有上级 && 邀请人是推广组 && 邀请人不能是我 && 邀请人的上级不能是我
  209. if(!empty($intro_user) && empty($this->auth->intro_uid) && $intro_user['group_id'] == 2
  210. && $intro_user['id'] != $this->auth->id && $intro_user['intro_uid'] != $this->auth->id){
  211. $data['intro_uid'] = $intro_user['id'];
  212. }
  213. }
  214. if(empty($data)){
  215. $this->success();
  216. }
  217. $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  218. $this->success();
  219. }
  220. /**
  221. * 会员中心
  222. */
  223. public function index()
  224. {
  225. $this->success('', ['welcome' => $this->auth->nickname]);
  226. }
  227. /**
  228. * 会员登录
  229. *
  230. * @ApiMethod (POST)
  231. * @param string $account 账号
  232. * @param string $password 密码
  233. */
  234. public function login()
  235. {
  236. $account = $this->request->post('account');
  237. $password = $this->request->post('password');
  238. if (!$account || !$password) {
  239. $this->error(__('Invalid parameters'));
  240. }
  241. $ret = $this->auth->login($account, $password);
  242. if ($ret) {
  243. $data = ['userinfo' => $this->auth->getUserinfo()];
  244. $this->success(__('Logged in successful'), $data);
  245. } else {
  246. $this->error($this->auth->getError());
  247. }
  248. }
  249. /**
  250. * 手机验证码登录
  251. *
  252. * @ApiMethod (POST)
  253. * @param string $mobile 手机号
  254. * @param string $captcha 验证码
  255. */
  256. public function mobilelogin()
  257. {
  258. $mobile = $this->request->post('mobile');
  259. $captcha = $this->request->post('captcha');
  260. if (!$mobile || !$captcha) {
  261. $this->error(__('Invalid parameters'));
  262. }
  263. if (!Validate::regex($mobile, "^1\d{10}$")) {
  264. $this->error(__('Mobile is incorrect'));
  265. }
  266. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  267. $this->error(__('Captcha is incorrect'));
  268. }
  269. $user = \app\common\model\User::getByMobile($mobile);
  270. if ($user) {
  271. if ($user->status != 'normal') {
  272. $this->error(__('Account is locked'));
  273. }
  274. //如果已经有账号则直接登录
  275. $ret = $this->auth->direct($user->id);
  276. } else {
  277. $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, []);
  278. }
  279. if ($ret) {
  280. Sms::flush($mobile, 'mobilelogin');
  281. $data = ['userinfo' => $this->auth->getUserinfo()];
  282. $this->success(__('Logged in successful'), $data);
  283. } else {
  284. $this->error($this->auth->getError());
  285. }
  286. }
  287. /**
  288. * 注册会员
  289. *
  290. * @ApiMethod (POST)
  291. * @param string $username 用户名
  292. * @param string $password 密码
  293. * @param string $email 邮箱
  294. * @param string $mobile 手机号
  295. * @param string $code 验证码
  296. */
  297. public function register()
  298. {
  299. $username = $this->request->post('username');
  300. $password = $this->request->post('password');
  301. $email = $this->request->post('email');
  302. $mobile = $this->request->post('mobile');
  303. $code = $this->request->post('code');
  304. if (!$username || !$password) {
  305. $this->error(__('Invalid parameters'));
  306. }
  307. if ($email && !Validate::is($email, "email")) {
  308. $this->error(__('Email is incorrect'));
  309. }
  310. if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
  311. $this->error(__('Mobile is incorrect'));
  312. }
  313. $ret = Sms::check($mobile, $code, 'register');
  314. if (!$ret) {
  315. $this->error(__('Captcha is incorrect'));
  316. }
  317. $ret = $this->auth->register($username, $password, $email, $mobile, []);
  318. if ($ret) {
  319. $data = ['userinfo' => $this->auth->getUserinfo()];
  320. $this->success(__('Sign up successful'), $data);
  321. } else {
  322. $this->error($this->auth->getError());
  323. }
  324. }
  325. /**
  326. * 修改邮箱
  327. *
  328. * @ApiMethod (POST)
  329. * @param string $email 邮箱
  330. * @param string $captcha 验证码
  331. */
  332. public function changeemail()
  333. {
  334. $user = $this->auth->getUser();
  335. $email = $this->request->post('email');
  336. $captcha = $this->request->post('captcha');
  337. if (!$email || !$captcha) {
  338. $this->error(__('Invalid parameters'));
  339. }
  340. if (!Validate::is($email, "email")) {
  341. $this->error(__('Email is incorrect'));
  342. }
  343. if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) {
  344. $this->error(__('Email already exists'));
  345. }
  346. $result = Ems::check($email, $captcha, 'changeemail');
  347. if (!$result) {
  348. $this->error(__('Captcha is incorrect'));
  349. }
  350. $verification = $user->verification;
  351. $verification->email = 1;
  352. $user->verification = $verification;
  353. $user->email = $email;
  354. $user->save();
  355. Ems::flush($email, 'changeemail');
  356. $this->success();
  357. }
  358. /**
  359. * 修改手机号
  360. *
  361. * @ApiMethod (POST)
  362. * @param string $mobile 手机号
  363. * @param string $captcha 验证码
  364. */
  365. public function changemobile()
  366. {
  367. $user = $this->auth->getUser();
  368. $mobile = $this->request->post('mobile');
  369. $captcha = $this->request->post('captcha');
  370. if (!$mobile || !$captcha) {
  371. $this->error(__('Invalid parameters'));
  372. }
  373. if (!Validate::regex($mobile, "^1\d{10}$")) {
  374. $this->error(__('Mobile is incorrect'));
  375. }
  376. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
  377. $this->error(__('Mobile already exists'));
  378. }
  379. $result = Sms::check($mobile, $captcha, 'changemobile');
  380. if (!$result) {
  381. $this->error(__('Captcha is incorrect'));
  382. }
  383. $verification = $user->verification;
  384. $verification->mobile = 1;
  385. $user->verification = $verification;
  386. $user->mobile = $mobile;
  387. $user->save();
  388. Sms::flush($mobile, 'changemobile');
  389. $this->success();
  390. }
  391. /**
  392. * 第三方登录
  393. *
  394. * @ApiMethod (POST)
  395. * @param string $platform 平台名称
  396. * @param string $code Code码
  397. */
  398. public function third()
  399. {
  400. $url = url('user/index');
  401. $platform = $this->request->post("platform");
  402. $code = $this->request->post("code");
  403. $config = get_addon_config('third');
  404. if (!$config || !isset($config[$platform])) {
  405. $this->error(__('Invalid parameters'));
  406. }
  407. $app = new \addons\third\library\Application($config);
  408. //通过code换access_token和绑定会员
  409. $result = $app->{$platform}->getUserInfo(['code' => $code]);
  410. if ($result) {
  411. $loginret = \addons\third\library\Service::connect($platform, $result);
  412. if ($loginret) {
  413. $data = [
  414. 'userinfo' => $this->auth->getUserinfo(),
  415. 'thirdinfo' => $result
  416. ];
  417. $this->success(__('Logged in successful'), $data);
  418. }
  419. }
  420. $this->error(__('Operation failed'), $url);
  421. }
  422. /**
  423. * 重置密码
  424. *
  425. * @ApiMethod (POST)
  426. * @param string $mobile 手机号
  427. * @param string $newpassword 新密码
  428. * @param string $captcha 验证码
  429. */
  430. public function resetpwd()
  431. {
  432. $type = $this->request->post("type", "mobile");
  433. $mobile = $this->request->post("mobile");
  434. $email = $this->request->post("email");
  435. $newpassword = $this->request->post("newpassword");
  436. $captcha = $this->request->post("captcha");
  437. if (!$newpassword || !$captcha) {
  438. $this->error(__('Invalid parameters'));
  439. }
  440. //验证Token
  441. if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
  442. $this->error(__('Password must be 6 to 30 characters'));
  443. }
  444. if ($type == 'mobile') {
  445. if (!Validate::regex($mobile, "^1\d{10}$")) {
  446. $this->error(__('Mobile is incorrect'));
  447. }
  448. $user = \app\common\model\User::getByMobile($mobile);
  449. if (!$user) {
  450. $this->error(__('User not found'));
  451. }
  452. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  453. if (!$ret) {
  454. $this->error(__('Captcha is incorrect'));
  455. }
  456. Sms::flush($mobile, 'resetpwd');
  457. } else {
  458. if (!Validate::is($email, "email")) {
  459. $this->error(__('Email is incorrect'));
  460. }
  461. $user = \app\common\model\User::getByEmail($email);
  462. if (!$user) {
  463. $this->error(__('User not found'));
  464. }
  465. $ret = Ems::check($email, $captcha, 'resetpwd');
  466. if (!$ret) {
  467. $this->error(__('Captcha is incorrect'));
  468. }
  469. Ems::flush($email, 'resetpwd');
  470. }
  471. //模拟一次登录
  472. $this->auth->direct($user->id);
  473. $ret = $this->auth->changepwd($newpassword, '', true);
  474. if ($ret) {
  475. $this->success(__('Reset password successful'));
  476. } else {
  477. $this->error($this->auth->getError());
  478. }
  479. }
  480. }