Browse Source

登录注册

15954078560 2 years ago
parent
commit
f64608f9fd
1 changed files with 101 additions and 29 deletions
  1. 101 29
      application/api/controller/User.php

+ 101 - 29
application/api/controller/User.php

@@ -1197,9 +1197,11 @@ class User extends Api
         if (!in_array($gender, [1, 2])) {
             $this->error('请选择性别');
         }
-        $invitecount = Db::name('user')->where(['invite_no' => $invite_no])->count('id');
-        if (!$invitecount) {
-            $this->error('邀请码不存在');
+        if ($invite_no) {
+            $invite_info = Db::name('user')->where(['invite_no' => $invite_no])->find();
+            if (!$invite_info) {
+                $this->error('邀请码不存在');
+            }
         }
         if (!$nickname || !$avatar) {
             $this->error('参数缺失');
@@ -1213,50 +1215,92 @@ class User extends Api
 
         $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,
+            'mobile' => $mobile,
+            'avatar'   => $avatar,
             'joinip'    => $ip,
+            'jointime'  => $time,
             'logintime' => $time,
             'loginip'   => $ip,
             'prevtime'  => $time,
-            'is_auth' => 1
-        ]);
-        $params['password'] = md5(md5($password) . $params['salt']);
+            'createtime'    => $time,
+            'gender' => $gender,
+            'birthday' => $birthday,
+            'openid' => $openid,
+            'invite_no' => $this->myinvite(),
+            'pre_user_id' => $invite_info['id'],
+            'invite_time' => $time,
+        ];
 
         //开启事务
         Db::startTrans();
-
-        $rs = Db::name('user')->insertGetId($params);
+        $rs = Db::name('user')->insertGetId($data);
         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);
+        //生成uid
+        $username = $this->myuid($rs);
+        $rt = Db::name('user')->where(['id' => $rs])->setField('username', $username);
         if (!$rt) {
             Db::rollback();
             $this->error('注册失败');
         }
+        //给用户发放注册优惠券
+        $register_coupon = Db::name('coupon')->where(['purpose' => 1, 'status' => 1])->order('weigh desc, id desc')->find();
+        if ($register_coupon) {
+            $register_coupon_data = [
+                'user_id' => $rs,
+                'coupon_id' => $register_coupon['id'],
+                'title' => $register_coupon['title'],
+                'desc' => $register_coupon['desc'],
+                'type' => $register_coupon['type'],
+                'money' => $register_coupon['money'],
+                'minmoney' => $register_coupon['minmoney'],
+                'starttime' => time(),
+                'endtime' => time() + $register_coupon['effectiveday'] * 86400,
+                'createtime' => time()
+            ];
+
+            $register_coupon_rs = Db::name('user_coupon')->insertGetId($register_coupon_data);
+            if (!$register_coupon_rs) {
+                Db::rollback();
+                $this->error('注册失败');
+            }
+        }
+        //查询是否需要给上级发放优惠券
+        if ($invite_info) {
+            $invite_count = Db::name('user')->where(['pre_user_id' => $invite_info['id']])->count();
+            if ($invite_count > 0 && $invite_count % 10 == 0) {
+                //每邀请10人发放一次优惠券
+                //查询推广优惠券
+                $invite_coupon = Db::name('coupon')->where(['purpose' => 2, 'status' => 1])->order('weigh desc, id desc')->find();
+                if ($invite_coupon) {
+                    $invite_coupon_data = [
+                        'user_id' => $invite_info['id'],
+                        'coupon_id' => $invite_coupon['id'],
+                        'title' => $invite_coupon['title'],
+                        'desc' => $invite_coupon['desc'],
+                        'type' => $invite_coupon['type'],
+                        'money' => $invite_coupon['money'],
+                        'minmoney' => $invite_coupon['minmoney'],
+                        'starttime' => time(),
+                        'endtime' => time() + $invite_coupon['effectiveday'] * 86400,
+                        'createtime' => time()
+                    ];
+
+                    $invite_coupon_rs = Db::name('user_coupon')->insertGetId($invite_coupon_data);
+                    if (!$invite_coupon_rs) {
+                        Db::rollback();
+                        $this->error('注册失败');
+                    }
+                }
+            }
+        }
+
         Db::commit();
 
-        $ret = $this->auth->login($mobile, $password);
+        $ret = $this->auth->direct($rs);
         if ($ret) {
             $data = ['userinfo' => $this->auth->getUserinfo()];
             $this->success(__('Sign up successful'), $data);
@@ -1264,4 +1308,32 @@ class User extends Api
             $this->error($this->auth->getError());
         }
     }
+
+    //生成邀请码
+    public function myinvite() {
+        $invite = Random::alnum(7);
+        $count = Db::name('user')->where(['invite_no' => $invite])->count('id');
+        if ($count) {
+            $this->myinvite();
+        }
+
+        return $invite;
+    }
+
+    //生成uid
+    public function myuid($id = 0) {
+        if (strlen($id) < 8) {
+            $username_len = 8 - strlen($id);
+            $username = $id . Random::numeric($username_len);
+        } else {
+            $username = $id;
+        }
+
+        $count = Db::name('user')->where(['username' => $username])->count('id');
+        if ($count) {
+            $this->myuid($id);
+        }
+
+        return $username;
+    }
 }