User.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 think\Db;
  10. /**
  11. * 会员接口
  12. */
  13. class User extends Api
  14. {
  15. protected $noNeedLogin = ['mobilelogin'];
  16. protected $noNeedRight = '*';
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. }
  21. /**
  22. * 手机验证码登录
  23. *
  24. * @ApiMethod (POST)
  25. * @param string $mobile 手机号
  26. * @param string $captcha 验证码
  27. */
  28. public function mobilelogin()
  29. {
  30. $mobile = $this->request->post('mobile');
  31. $captcha = $this->request->post('captcha');
  32. if (!$mobile || !$captcha) {
  33. $this->error(__('Invalid parameters'));
  34. }
  35. if (!Validate::regex($mobile, "^1\d{10}$")) {
  36. $this->error(__('Mobile is incorrect'));
  37. }
  38. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  39. $this->error(__('Captcha is incorrect'));
  40. }
  41. $user = \app\common\model\User::getByMobile($mobile);
  42. if ($user) {
  43. if ($user->status != 1) {
  44. $this->error(__('Account is locked'));
  45. }
  46. //如果已经有账号则直接登录
  47. $ret = $this->auth->direct($user->id);
  48. } else {
  49. $ret = $this->auth->register('', '', '', $mobile, []);
  50. }
  51. if ($ret) {
  52. Sms::flush($mobile, 'mobilelogin');
  53. $data = ['userinfo' => $this->auth->getUserinfo()];
  54. $this->success(__('Logged in successful'), $data);
  55. } else {
  56. $this->error($this->auth->getError());
  57. }
  58. }
  59. /**
  60. * 修改手机号
  61. *
  62. * @ApiMethod (POST)
  63. * @param string $mobile 手机号
  64. * @param string $captcha 验证码
  65. */
  66. public function changemobile()
  67. {
  68. $user = $this->auth->getUser();
  69. $mobile = $this->request->post('mobile');
  70. $captcha = $this->request->post('captcha');
  71. if (!$mobile || !$captcha) {
  72. $this->error(__('Invalid parameters'));
  73. }
  74. if (!Validate::regex($mobile, "^1\d{10}$")) {
  75. $this->error(__('Mobile is incorrect'));
  76. }
  77. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
  78. $this->error(__('Mobile already exists'));
  79. }
  80. $result = Sms::check($mobile, $captcha, 'changemobile');
  81. if (!$result) {
  82. $this->error(__('Captcha is incorrect'));
  83. }
  84. $user->mobile = $mobile;
  85. $user->save();
  86. Sms::flush($mobile, 'changemobile');
  87. $this->success();
  88. }
  89. /**
  90. * 退出登录
  91. * @ApiMethod (POST)
  92. */
  93. public function logout()
  94. {
  95. if (!$this->request->isPost()) {
  96. $this->error(__('Invalid parameters'));
  97. }
  98. $this->auth->logout();
  99. $this->success(__('Logout successful'));
  100. }
  101. /**
  102. * 修改会员个人信息
  103. *
  104. * @ApiMethod (POST)
  105. * @param string $avatar 头像地址
  106. * @param string $nickname 昵称
  107. */
  108. public function profile()
  109. {
  110. $avatar = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  111. $nickname = input('nickname', '');
  112. //修改用户
  113. $data = [];
  114. if(!empty($avatar))
  115. {
  116. $data['avatar'] = $avatar;
  117. }
  118. if(!empty($nickname))
  119. {
  120. $data['nickname'] = $nickname;
  121. }
  122. if(!empty($data)){
  123. $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  124. }
  125. $this->success();
  126. }
  127. //用户详细资料
  128. public function getuserinfo(){
  129. $info = $this->auth->getUserinfo();
  130. $this->success(__('success'),$info);
  131. }
  132. }