瀏覽代碼

登录注册

15954078560 2 年之前
父節點
當前提交
0197814265

+ 2 - 2
application/api/controller/Sms.php

@@ -47,7 +47,7 @@ class Sms extends Api
             } elseif (in_array($event, ['changemobile']) && $userinfo) {
                 //被占用
                 $this->error(__('已被占用'));
-            } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
+            } elseif (in_array($event, ['changepwd', 'resetpwd', 'mobilelogin']) && !$userinfo) {
                 //未注册
                 $this->error(__('未注册'));
             }
@@ -89,7 +89,7 @@ class Sms extends Api
             } elseif (in_array($event, ['changemobile']) && $userinfo) {
                 //被占用
                 $this->error(__('已被占用'));
-            } elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo) {
+            } elseif (in_array($event, ['changepwd', 'resetpwd', 'mobilelogin']) && !$userinfo) {
                 //未注册
                 $this->error(__('未注册'));
             }

+ 177 - 11
application/api/controller/User.php

@@ -15,7 +15,7 @@ use think\Db;
  */
 class User extends Api
 {
-    protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'registercheck', 'resetpwd', 'changeemail', 'changemobile', 'third'];
+    protected $noNeedLogin = ['login', 'mobilelogin', 'register', 'registercheck', 'resetpwd', 'changeemail', 'changemobile', 'third', 'getopenid', 'getagreement', 'wxlogin'];
     protected $noNeedRight = '*';
 
     public function _initialize()
@@ -80,15 +80,22 @@ class User extends Api
             $this->error(__('Captcha is incorrect'));
         }
         $user = \app\common\model\User::getByMobile($mobile);
-        if ($user) {
-            if ($user->status != 'normal') {
-                $this->error(__('Account is locked'));
-            }
-            //如果已经有账号则直接登录
-            $ret = $this->auth->direct($user->id);
-        } else {
-            $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, []);
+        if (!$user) {
+            $this->error('用户尚未注册');
         }
+        if ($user['status'] != 1) {
+            $this->error(__('Account is locked'));
+        }
+//        if ($user) {
+//            if ($user->status != 'normal') {
+//                $this->error(__('Account is locked'));
+//            }
+//            //如果已经有账号则直接登录
+//            $ret = $this->auth->direct($user->id);
+//        } else {
+//            $ret = $this->auth->register($mobile, Random::alnum(), '', $mobile, []);
+//        }
+        $ret = $this->auth->direct($user->id);
         if ($ret) {
             Sms::flush($mobile, 'mobilelogin');
             $data = ['userinfo' => $this->auth->getUserinfo()];
@@ -108,7 +115,7 @@ class User extends Api
      * @param string $mobile   手机号
      * @param string $code     验证码
      */
-    public function register()
+    /*public function register()
     {
         $mobile = $this->request->post('mobile', '', 'trim'); //手机号
         $code = $this->request->post('code', '', 'trim'); //验证码
@@ -241,7 +248,7 @@ class User extends Api
         } else {
             $this->error($this->auth->getError());
         }
-    }
+    }*/
 
     //注册第一步验证
     public function registercheck()
@@ -1097,5 +1104,164 @@ class User extends Api
         $this->success('个人账单明细', $data);
     }
 
+    //关于我们/免责协议/用户协议/隐私政策
+    public function getagreement()
+    {
+        $type = input('type', 0, 'intval');
+        if (!in_array($type, [1, 2, 3, 4])) {
+            $this->error('参数错误');
+        }
+
+        $info = Db::name('platform_info')->field('title, content')->where(['type' => $type])->find();
+
+        $this->success('协议', $info);
+    }
+
+    //获取openid
+    public function getopenid() {
+        //code
+        $code = $this->request->param('code', '', 'trim');// code值
+        if (!$code) {
+            $this->error(__('Invalid parameters'));
+        }
+
+        $config = config('wxchatpay');
+        $getopenid_url = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$config['app_id'].'&secret='.$config['app_secret'].'&js_code='.$code.'&grant_type=authorization_code';
+
+        $openidInfo = httpRequest($getopenid_url, 'GET');//$this->getJson($getopenid_url);
+        $openidInfo = json_decode($openidInfo,true);
+
+        if(!isset($openidInfo['openid'])) {
+            $this->error('用户openid获取失败', $openidInfo);
+        }
+
+        $this->success('success', $openidInfo);
+    }
+
+    //微信登录
+    public function wxlogin() {
+        $openid = input('openid', '', 'trim');
+        if (!$openid) {
+            $this->error('参数缺失');
+        }
 
+        $user = \app\common\model\User::getByOpenid($openid);
+        if (!$user) {
+            $this->error('用户尚未注册', [], 5);
+        }
+        if ($user['status'] != 1) {
+            $this->error(__('Account is locked'));
+        }
+        $ret = $this->auth->direct($user->id);
+        if ($ret) {
+            $data = ['userinfo' => $this->auth->getUserinfo()];
+            $this->success(__('Logged in successful'), $data);
+        } else {
+            $this->error($this->auth->getError());
+        }
+    }
+
+    //注册
+    public function register()
+    {
+        $mobile = $this->request->post('mobile', '', 'trim'); //手机号
+        $code = $this->request->post('code', '', 'trim'); //验证码
+        $birthday = $this->request->post('birthday', '', 'strtotime'); //生日
+        $gender = $this->request->post('gender', 0, 'intval'); //性别:1=男,2=女
+        $invite_no = $this->request->post('invite_no', '', 'trim'); //邀请码
+        $openid = $this->request->post('openid', '', 'trim'); //微信openid
+        $nickname = $this->request->post('nickname', '', 'trim'); //微信昵称
+        $avatar = $this->request->post('avatar', '', 'trim,strip_tags,htmlspecialchars'); //微信头像
+
+        $openidcount = Db::name('user')->where(['openid' => $openid])->count('id');
+        if ($openidcount) {
+            $this->error('微信已经注册,请直接登录');
+        }
+        if (!Validate::regex($mobile, "^1\d{10}$")) {
+            $this->error(__('Mobile is incorrect'));
+        }
+        $count = Db::name('user')->where(['mobile' => $mobile])->count('id');
+        if ($count) {
+            $this->error('手机号已被注册');
+        }
+        if (iconv_strlen($code, 'utf-8') != config('alisms.length')) {
+            $this->error(__('Captcha is incorrect'));
+        }
+        $ret = Sms::check($mobile, $code, 'register');
+        if (!$ret) {
+            $this->error(__('Captcha is incorrect'));
+        }
+        if (!$birthday || $birthday >= time()) {
+            $this->error('请选择正确生日');
+        }
+        if (!in_array($gender, [1, 2])) {
+            $this->error('请选择性别');
+        }
+        $invitecount = Db::name('user')->where(['invite_no' => $invite_no])->count('id');
+        if (!$invitecount) {
+            $this->error('邀请码不存在');
+        }
+        if (!$nickname || !$avatar) {
+            $this->error('参数缺失');
+        }
+        if (iconv_strlen($nickname, 'utf-8') > 30 || iconv_strlen($avatar, 'utf-8') > 255) {
+            $this->error('参数错误');
+        }
+
+        $ip = request()->ip();
+        $time = time();
+
+        $data = [
+            'nickname'  => $nickname,
+            'province' => $province,
+            'city' => $city,
+            'area' => $area,
+            'address' => $address,
+            'createtime'    => $time
+        ];
+        $params = array_merge($data, [
+            'mobile'   => $mobile,
+            'password' => $password,
+            'avatar'   => '/assets/img/avatar.png',
+            'salt'      => Random::alnum(),
+            'jointime'  => $time,
+            'joinip'    => $ip,
+            'logintime' => $time,
+            'loginip'   => $ip,
+            'prevtime'  => $time,
+            'is_auth' => 1
+        ]);
+        $params['password'] = md5(md5($password) . $params['salt']);
+
+        //开启事务
+        Db::startTrans();
+
+        $rs = Db::name('user')->insertGetId($params);
+        if (!$rs) {
+            Db::rollback();
+            $this->error('注册失败');
+        }
+
+        $data['user_id'] = $rs;
+        $data['idcard'] = $idcard;
+        $data['zimage'] = $zimage;
+        $data['fimage'] = $fimage;
+        $data['recommender'] = $recommender;
+        $data['recommender_mobile'] = $recommender_mobile;
+
+        $rt = Db::name('user_auth')->insertGetId($data);
+        if (!$rt) {
+            Db::rollback();
+            $this->error('注册失败');
+        }
+        Db::commit();
+
+        $ret = $this->auth->login($mobile, $password);
+        if ($ret) {
+            $data = ['userinfo' => $this->auth->getUserinfo()];
+            $this->success(__('Sign up successful'), $data);
+        } else {
+            $this->error($this->auth->getError());
+        }
+    }
 }

+ 60 - 0
application/common/controller/Api.php

@@ -163,6 +163,66 @@ class Api
         ];
 
         Db::name('user_log')->insert($user_log);
+
+        //检查会员等级, 发送成长值
+        if ($this->auth->id) {
+            //查询今日是否登录赠送过成长值
+            $time = strtotime(date('Y-m-d', time()));
+            $logingrowth = config('site.logingrowth') ? (int)config('site.logingrowth') : 0;//登录成长值
+            if ($logingrowth) {
+                $growth_log = Db::name('user_growth_log')->where(['user_id' => $this->auth->id, 'type' => 1])->order('id', 'desc')->find();
+                if (!$growth_log || ($growth_log['after'] == $this->auth->growthvalue && $growth_log['createtime'] < $time)) {
+                    $growth_data['user_id'] = $this->auth->id;
+                    $growth_data['growth'] = $logingrowth;
+                    $growth_data['before'] = $this->auth->growthvalue;
+                    $growth_data['after'] = $this->auth->growthvalue + $logingrowth;
+                    $growth_data['memo'] = '登录';
+                    $growth_data['createtime'] = time();
+
+                    Db::startTrans();
+                    $rt = Db::name('user_growth_log')->insertGetId($growth_data);
+                    $rs = Db::name('user')->where(['id' => $this->auth->id, 'growthvalue' => $this->auth->growthvalue])->setField('growthvalue', $growth_data['after']);
+                    if ($rt && $rs) {
+                        Db::commit();
+                    } else {
+                        Db::rollback();
+                    }
+                }
+            }
+            //检查更新会员等级
+            $growthvalue = Db::name('user')->where(['id' => $this->auth->id])->value('growthvalue');
+            $vip_info = Db::name('vip')->where(['growthvalue' => ['elt', $growthvalue]])->order('id', 'desc')->find();
+            $user_data = [];
+            if ($vip_info['id'] > $this->auth->growthlevel) {
+                $user_data['growthlevel'] = $vip_info['id'];
+                //当前会员信息
+                $last_vip_info = Db::name('vip')->find($vip_info['id']);
+                $user_data['freenumber'] = $this->auth->freenumber + $vip_info['free'] - $last_vip_info['free'];
+            }
+            //检查体验会员
+            if ($this->auth->experiencetime < time()) {
+                //体验会员到期
+                if ($vip_info['id'] > $this->auth->growthlevel) { //成长值会员等级更新
+                    $user_data['maxlevel'] = $vip_info['id'];
+                } elseif ($this->auth->maxlevel != $this->auth->growthlevel) {
+                    $user_data['maxlevel'] = $this->auth->growthlevel;
+                }
+            } else {
+                //体验会员没到期
+                if ($vip_info['id'] > $this->auth->maxlevel) {
+                    $user_data['maxlevel'] = $vip_info['id'];
+                }
+            }
+            if ($user_data) {
+                Db::startTrans();
+                $res = Db::name('user')->where(['id' => $this->auth->id])->setField($user_data);
+                if (!$res) {
+                    Db::rollback();
+                } else {
+                    Db::commit();
+                }
+            }
+        }
     }
 
     /**

+ 2 - 1
application/common/library/Auth.php

@@ -26,7 +26,8 @@ class Auth
     //默认配置
     protected $config = [];
     protected $options = [];
-    protected $allowFields = ['id', 'nickname', 'mobile', 'avatar', 'money', 'province', 'city', 'area', 'address', 'is_auth'];
+    protected $allowFields = ['id', 'username', 'nickname', 'mobile', 'avatar', 'money', 'realname', 'gender', 'birthday', 'idcard',
+        'passport', 'emergencycontact', 'contactmobile', 'outdoorduration', 'invite_no', 'maxlevel'];
 
     public function __construct($options = [])
     {