User.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. $this->success(__('Logged in successful'), $this->auth->getUserinfo());
  54. } else {
  55. $this->error($this->auth->getError());
  56. }
  57. }
  58. /**
  59. * 修改手机号
  60. *
  61. * @ApiMethod (POST)
  62. * @param string $mobile 手机号
  63. * @param string $captcha 验证码
  64. */
  65. public function changemobile()
  66. {
  67. $user = $this->auth->getUser();
  68. $mobile = $this->request->post('mobile');
  69. $captcha = $this->request->post('captcha');
  70. if (!$mobile || !$captcha) {
  71. $this->error(__('Invalid parameters'));
  72. }
  73. if (!Validate::regex($mobile, "^1\d{10}$")) {
  74. $this->error(__('Mobile is incorrect'));
  75. }
  76. if (\app\common\model\User::where('mobile', $mobile)->where('id', '<>', $user->id)->find()) {
  77. $this->error(__('Mobile already exists'));
  78. }
  79. $result = Sms::check($mobile, $captcha, 'changemobile');
  80. if (!$result) {
  81. $this->error(__('Captcha is incorrect'));
  82. }
  83. $user->mobile = $mobile;
  84. $user->save();
  85. Sms::flush($mobile, 'changemobile');
  86. $this->success();
  87. }
  88. /**
  89. * 退出登录
  90. * @ApiMethod (POST)
  91. */
  92. public function logout()
  93. {
  94. if (!$this->request->isPost()) {
  95. $this->error(__('Invalid parameters'));
  96. }
  97. $this->auth->logout();
  98. $this->success(__('Logout successful'));
  99. }
  100. /**
  101. * 修改会员个人信息
  102. *
  103. * @ApiMethod (POST)
  104. * @param string $avatar 头像地址
  105. * @param string $nickname 昵称
  106. */
  107. public function profile()
  108. {
  109. $avatar = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  110. $nickname = input('nickname', '');
  111. //修改用户
  112. $data = [];
  113. if(!empty($avatar))
  114. {
  115. $data['avatar'] = $avatar;
  116. }
  117. if(!empty($nickname))
  118. {
  119. $data['nickname'] = $nickname;
  120. }
  121. if(!empty($data)){
  122. $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  123. }
  124. $this->success();
  125. }
  126. //用户详细资料
  127. public function getuserinfo(){
  128. $info = $this->auth->getUserinfo();
  129. $this->success(__('success'),$info);
  130. }
  131. }