User.php 16 KB

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