User.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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','','trim,intval');
  28. $captcha = $this->request->post('captcha','','trim,intval');
  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(!$this->apiLimit(60,1,'60_'.$mobile)){
  36. $this->error('您的手机号登录频繁,请一分钟后再试');
  37. };
  38. if(!$this->apiLimit(3600,10,'3600_'.$mobile)){
  39. $this->error('您的手机号登录频繁,请一小时后再试!');
  40. };
  41. if(!$this->apiLimit(60,10,request()->ip())){
  42. $this->error('当前登录人数过多,请稍后再试');
  43. };
  44. if (!Sms::check($mobile, $captcha, 'mobilelogin')) {
  45. $this->error(__('Captcha is incorrect'));
  46. }
  47. $user = \app\common\model\User::getByMobile($mobile);
  48. if ($user) {
  49. if ($user->status != 1) {
  50. $this->error('网络开小差了请稍后重试');
  51. }
  52. //如果已经有账号则直接登录
  53. $ret = $this->auth->direct($user->id);
  54. } else {
  55. //$this->error('注册已截止');
  56. $ret = $this->auth->register('', '', '', $mobile, []);
  57. }
  58. if ($ret) {
  59. Sms::flush($mobile, 'mobilelogin');
  60. $this->success(__('Logged in successful'), $this->auth->getUserinfo());
  61. } else {
  62. $this->error($this->auth->getError());
  63. }
  64. }
  65. /**
  66. * 退出登录
  67. * @ApiMethod (POST)
  68. */
  69. public function logout()
  70. {
  71. if (!$this->request->isPost()) {
  72. $this->error(__('Invalid parameters'));
  73. }
  74. $this->auth->logout();
  75. $this->success(__('Logout successful'));
  76. }
  77. //备选头像
  78. public function enum_avatar(){
  79. $list = Db::name('enum_avatar')->order('id desc')->select();
  80. $list = list_domain_image($list,['avatar']);
  81. $this->success(1,$list);
  82. }
  83. /**
  84. * 修改会员个人信息
  85. *
  86. * @ApiMethod (POST)
  87. * @param string $avatar 头像地址
  88. * @param string $nickname 昵称
  89. */
  90. public function profile()
  91. {
  92. $avatar = input('avatar', '', 'trim,strip_tags,htmlspecialchars');
  93. $nickname = input('nickname', '', 'trim,htmlspecialchars');
  94. //修改用户
  95. $data = [];
  96. if(!empty($avatar))
  97. {
  98. $data['avatar'] = $avatar;
  99. }
  100. if(!empty($nickname))
  101. {
  102. $data['nickname'] = $nickname;
  103. }
  104. if(!empty($data)){
  105. $update_rs = Db::name('user')->where('id',$this->auth->id)->update($data);
  106. }
  107. $this->success();
  108. }
  109. //用户详细资料
  110. public function getuserinfo(){
  111. $info = $this->auth->getUserinfo();
  112. $this->success(__('success'),$info);
  113. }
  114. /**
  115. * 微信内H5-JSAPI支付
  116. */
  117. public function jssdkBuildConfig() {
  118. $this->success("获取成功!",'123456');
  119. $url = $this->request->request("url");
  120. $wechat = new \app\common\library\Wechatshare;
  121. $sign = $wechat->getSignPackage(urldecode($url));
  122. $this->success("获取成功!",$sign);
  123. }
  124. }