User.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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 fast\Random;
  7. use think\Config;
  8. use think\Validate;
  9. use app\common\library\Token;
  10. use think\Db;
  11. /**
  12. * 会员接口
  13. */
  14. class User extends Api
  15. {
  16. protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'resetpwd', 'changeemail', 'changemobile', 'third'];
  17. protected $noNeedRight = '*';
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. if (!Config::get('fastadmin.usercenter')) {
  22. $this->error(__('User center already closed'));
  23. }
  24. }
  25. /**
  26. * 会员中心
  27. */
  28. public function index()
  29. {
  30. $this->success('', ['welcome' => $this->auth->nickname]);
  31. }
  32. /**
  33. * 会员登录
  34. *
  35. * @ApiMethod (POST)
  36. * @param string $account 账号
  37. * @param string $password 密码
  38. */
  39. public function login()
  40. {
  41. $account = $this->request->post('account');
  42. $password = $this->request->post('password');
  43. if (!$account || !$password) {
  44. $this->error(__('Invalid parameters'));
  45. }
  46. $ret = $this->auth->login($account, $password);
  47. if ($ret) {
  48. $data = $this->userInfo('return');
  49. $this->success(__('Logged in successful'), $data);
  50. } else {
  51. $this->error($this->auth->getError());
  52. }
  53. }
  54. /**
  55. * 手机验证码登录
  56. *
  57. * @ApiMethod (POST)
  58. * @param string $mobile 手机号
  59. * @param string $captcha 验证码
  60. */
  61. public function mobilelogin()
  62. {
  63. $mobile = $this->request->post('mobile');
  64. $captcha = $this->request->post('captcha');
  65. if (!$mobile || !$captcha) {
  66. $this->error(__('Invalid parameters'));
  67. }
  68. if (!Validate::regex($mobile, "^1\d{10}$")) {
  69. $this->error(__('Mobile is incorrect'));
  70. }
  71. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  72. $this->error(__('Captcha is incorrect'));
  73. }
  74. $user = \app\common\model\User::getByMobile($mobile);
  75. if ($user) {
  76. if ($user->status != 1) {
  77. $this->error(__('Account is locked'));
  78. }
  79. //如果已经有账号则直接登录
  80. $ret = $this->auth->direct($user->id);
  81. } else {
  82. $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, []);
  83. }
  84. if ($ret) {
  85. Sms::flush($mobile, 'mobilelogin');
  86. $data = $this->userInfo('return');
  87. $this->success(__('Logged in successful'), $data);
  88. } else {
  89. $this->error($this->auth->getError());
  90. }
  91. }
  92. /**
  93. * 注册会员
  94. *
  95. * @ApiMethod (POST)
  96. * @param string $username 用户名
  97. * @param string $password 密码
  98. * @param string $email 邮箱
  99. * @param string $mobile 手机号
  100. * @param string $code 验证码
  101. */
  102. public function register()
  103. {
  104. $username = $this->request->post('username');
  105. $password = $this->request->post('password');
  106. $email = $this->request->post('email');
  107. $mobile = $this->request->post('mobile');
  108. $code = $this->request->post('code');
  109. if (!$username || !$password) {
  110. $this->error(__('Invalid parameters'));
  111. }
  112. if ($email && !Validate::is($email, "email")) {
  113. $this->error(__('Email is incorrect'));
  114. }
  115. if ($mobile && !Validate::regex($mobile, "^1\d{10}$")) {
  116. $this->error(__('Mobile is incorrect'));
  117. }
  118. $ret = Sms::check($mobile, $code, 'register');
  119. if (!$ret) {
  120. $this->error(__('Captcha is incorrect'));
  121. }
  122. $ret = $this->auth->register($username, $password, $email, $mobile, []);
  123. if ($ret) {
  124. $data = $this->userInfo('return');
  125. $this->success(__('Sign up successful'), $data);
  126. } else {
  127. $this->error($this->auth->getError());
  128. }
  129. }
  130. //用户详细资料
  131. public function userInfo($type = 1){
  132. $info = [
  133. 'id' => $this->auth->id,
  134. 'username' => $this->auth->username,
  135. 'truename' => $this->auth->truename,
  136. 'nickname' => $this->auth->nickname,
  137. //'group_id' => $this->auth->group_id,
  138. 'idcard_status' => $this->auth->idcard_status,
  139. 'bio' => $this->auth->bio,
  140. 'birthday' => $this->auth->birthday,
  141. // 'address' => $this->auth->address,
  142. 'mobile' => $this->auth->mobile,
  143. 'avatar' => $this->auth->avatar,
  144. 'gender' => $this->auth->gender,
  145. 'email' => $this->auth->email,
  146. 'mobile' => $this->auth->mobile,
  147. // 'cover_image' => $this->auth->cover_image,
  148. // 'photo_images' => $this->auth->photo_images,
  149. // 'vip_endtime' => $this->auth->vip_endtime,
  150. ];
  151. //$info['is_vip'] = intval($info['vip_endtime']) - time() > 0 ? 1 : 0;
  152. $info['age'] = birthtime_to_age($info['birthday']);
  153. //$info = info_domain_image($info,['avatar','cover_image','photo_images']);
  154. if($type == 'return'){
  155. $info = array_merge($info, Token::get($this->auth->getToken())); //token 等多个字段
  156. //$info['token'] = $this->auth->getToken();
  157. return $info;
  158. }
  159. $this->success(__('success'),$info);
  160. }
  161. //实名认证信息
  162. public function idcard_confirm_info(){
  163. $check = Db::name('user_idconfirm')->where('user_id',$this->auth->id)->find();
  164. $this->success('success',$check);
  165. }
  166. //申请实名认证
  167. public function idcard_confirm(){
  168. $truename = input('truename','');
  169. $idcard = input('idcard','');
  170. $idcard_images = input('idcard_images','');
  171. if(empty($truename) || empty($idcard) || empty($idcard_images)){
  172. $this->error('实名认证信息必填');
  173. }
  174. if($this->auth->idcardstatus == 1){
  175. $this->error('您已经完成实名认证');
  176. }
  177. if($this->auth->idcardstatus == 0){
  178. $this->error('您已经提交实名认证,请等待审核');
  179. }
  180. Db::startTrans();
  181. $check = Db::name('user_idconfirm')->where('user_id',$this->auth->id)->lock(true)->find();
  182. if(!empty($check)){
  183. if($check['status'] == 0){
  184. Db::rollback();
  185. $this->error('您已经提交实名认证,请等待审核');
  186. }
  187. if($check['status'] == 1){
  188. Db::rollback();
  189. $this->error('您已经完成实名认证');
  190. }
  191. }
  192. $data = [
  193. 'user_id' => $this->auth->id,
  194. 'truename' => $truename,
  195. 'idcard' => $idcard,
  196. 'idcard_images' => $idcard_images,
  197. 'status' => 0,
  198. 'createtime' => time(),
  199. 'updatetime' => time(),
  200. ];
  201. //更新
  202. if(!empty($check)){
  203. $update_rs = Db::name('user')->where('id',$this->auth->id)->update(['idcardstatus'=>0]);
  204. $rs = Db::name('user_idconfirm')->where('id',$check['id'])->update($data);
  205. }else{
  206. $update_rs = Db::name('user')->where('id',$this->auth->id)->update(['idcardstatus'=>0]);
  207. $rs = Db::name('user_idconfirm')->insertGetId($data);
  208. }
  209. if(!$rs || !$update_rs){
  210. Db::rollback();
  211. $this->error('提交失败');
  212. }
  213. Db::commit();
  214. $this->success('提交成功,请等待审核');
  215. }
  216. /**
  217. * 退出登录
  218. * @ApiMethod (POST)
  219. */
  220. public function logout()
  221. {
  222. if (!$this->request->isPost()) {
  223. $this->error(__('Invalid parameters'));
  224. }
  225. $this->auth->logout();
  226. $this->success(__('Logout successful'));
  227. }
  228. /**
  229. * 修改会员个人信息
  230. *
  231. * @ApiMethod (POST)
  232. * @param string $avatar 头像地址
  233. * @param string $username 用户名
  234. * @param string $nickname 昵称
  235. * @param string $bio 个人简介
  236. */
  237. public function profile()
  238. {
  239. $user = $this->auth->getUser();
  240. $username = $this->request->post('username');
  241. $nickname = $this->request->post('nickname');
  242. $bio = $this->request->post('bio');
  243. $avatar = $this->request->post('avatar', '', 'trim,strip_tags,htmlspecialchars');
  244. if ($username) {
  245. $exists = \app\common\model\User::where('username', $username)->where('id', '<>', $this->auth->id)->find();
  246. if ($exists) {
  247. $this->error(__('Username already exists'));
  248. }
  249. $user->username = $username;
  250. }
  251. if ($nickname) {
  252. $exists = \app\common\model\User::where('nickname', $nickname)->where('id', '<>', $this->auth->id)->find();
  253. if ($exists) {
  254. $this->error(__('Nickname already exists'));
  255. }
  256. $user->nickname = $nickname;
  257. }
  258. $user->bio = $bio;
  259. $user->avatar = $avatar;
  260. $user->save();
  261. $this->success();
  262. }
  263. /**
  264. * 修改邮箱
  265. *
  266. * @ApiMethod (POST)
  267. * @param string $email 邮箱
  268. * @param string $captcha 验证码
  269. */
  270. public function changeemail()
  271. {
  272. $user = $this->auth->getUser();
  273. $email = $this->request->post('email');
  274. $captcha = $this->request->post('captcha');
  275. if (!$email || !$captcha) {
  276. $this->error(__('Invalid parameters'));
  277. }
  278. if (!Validate::is($email, "email")) {
  279. $this->error(__('Email is incorrect'));
  280. }
  281. if (\app\common\model\User::where('email', $email)->where('id', '<>', $user->id)->find()) {
  282. $this->error(__('Email already exists'));
  283. }
  284. $result = Ems::check($email, $captcha, 'changeemail');
  285. if (!$result) {
  286. $this->error(__('Captcha is incorrect'));
  287. }
  288. $verification = $user->verification;
  289. $verification->email = 1;
  290. $user->verification = $verification;
  291. $user->email = $email;
  292. $user->save();
  293. Ems::flush($email, 'changeemail');
  294. $this->success();
  295. }
  296. /**
  297. * 修改手机号
  298. *
  299. * @ApiMethod (POST)
  300. * @param string $mobile 手机号
  301. * @param string $captcha 验证码
  302. */
  303. public function changemobile()
  304. {
  305. $user = $this->auth->getUser();
  306. $mobile = $this->request->post('mobile');
  307. $captcha = $this->request->post('captcha');
  308. if (!$mobile || !$captcha) {
  309. $this->error(__('Invalid parameters'));
  310. }
  311. if (!Validate::regex($mobile, "^1\d{10}$")) {
  312. $this->error(__('Mobile is incorrect'));
  313. }
  314. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
  315. $this->error(__('Mobile already exists'));
  316. }
  317. $result = Sms::check($mobile, $captcha, 'changemobile');
  318. if (!$result) {
  319. $this->error(__('Captcha is incorrect'));
  320. }
  321. $verification = $user->verification;
  322. $verification->mobile = 1;
  323. $user->verification = $verification;
  324. $user->mobile = $mobile;
  325. $user->save();
  326. Sms::flush($mobile, 'changemobile');
  327. $this->success();
  328. }
  329. /**
  330. * 第三方登录
  331. *
  332. * @ApiMethod (POST)
  333. * @param string $platform 平台名称
  334. * @param string $code Code码
  335. */
  336. public function third()
  337. {
  338. $url = url('user/index');
  339. $platform = $this->request->post("platform");
  340. $code = $this->request->post("code");
  341. $config = get_addon_config('third');
  342. if (!$config || !isset($config[$platform])) {
  343. $this->error(__('Invalid parameters'));
  344. }
  345. $app = new \addons\third\library\Application($config);
  346. //通过code换access_token和绑定会员
  347. $result = $app->{$platform}->getUserInfo(['code' => $code]);
  348. if ($result) {
  349. $loginret = \addons\third\library\Service::connect($platform, $result);
  350. if ($loginret) {
  351. $data = [
  352. 'userinfo' => $this->auth->getUserinfo(),
  353. 'thirdinfo' => $result
  354. ];
  355. $this->success(__('Logged in successful'), $data);
  356. }
  357. }
  358. $this->error(__('Operation failed'), $url);
  359. }
  360. /**
  361. * 重置密码
  362. *
  363. * @ApiMethod (POST)
  364. * @param string $mobile 手机号
  365. * @param string $newpassword 新密码
  366. * @param string $captcha 验证码
  367. */
  368. public function resetpwd()
  369. {
  370. $type = $this->request->post("type");
  371. $mobile = $this->request->post("mobile");
  372. $email = $this->request->post("email");
  373. $newpassword = $this->request->post("newpassword");
  374. $captcha = $this->request->post("captcha");
  375. if (!$newpassword || !$captcha) {
  376. $this->error(__('Invalid parameters'));
  377. }
  378. if ($type == 'mobile') {
  379. if (!Validate::regex($mobile, "^1\d{10}$")) {
  380. $this->error(__('Mobile is incorrect'));
  381. }
  382. $user = \app\common\model\User::getByMobile($mobile);
  383. if (!$user) {
  384. $this->error(__('User not found'));
  385. }
  386. $ret = Sms::check($mobile, $captcha, 'resetpwd');
  387. if (!$ret) {
  388. $this->error(__('Captcha is incorrect'));
  389. }
  390. Sms::flush($mobile, 'resetpwd');
  391. } else {
  392. if (!Validate::is($email, "email")) {
  393. $this->error(__('Email is incorrect'));
  394. }
  395. $user = \app\common\model\User::getByEmail($email);
  396. if (!$user) {
  397. $this->error(__('User not found'));
  398. }
  399. $ret = Ems::check($email, $captcha, 'resetpwd');
  400. if (!$ret) {
  401. $this->error(__('Captcha is incorrect'));
  402. }
  403. Ems::flush($email, 'resetpwd');
  404. }
  405. //模拟一次登录
  406. $this->auth->direct($user->id);
  407. $ret = $this->auth->changepwd($newpassword, '', true);
  408. if ($ret) {
  409. $this->success(__('Reset password successful'));
  410. } else {
  411. $this->error($this->auth->getError());
  412. }
  413. }
  414. }