User.php 16 KB

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