User.php 15 KB

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