User.php 4.2 KB

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