User.php 16 KB

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