User.php 14 KB

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