Browse Source

绑定上级

afa 4 tháng trước cách đây
mục cha
commit
6249933586

+ 44 - 2
application/api/controller/Member.php

@@ -21,7 +21,7 @@ use function fast\e;
 class Member extends Api
 {   
     protected $model = null;
-    protected array $noNeedLogin = ['register', 'auth'];
+    protected array $noNeedLogin = ['register', 'auth','bind'];
 
     public function _initialize()
     {
@@ -122,7 +122,7 @@ class Member extends Api
             if($user['is_login']){
                 throw new Exception(__('Your account has been locked please contact customer service'));
             }
-            Token::marshal($user['id'], $user['address']);
+            Token::marshal($user['id']);
             $user['token'] = Token::getEncryptedToken($user['id']);
 
             Db::commit();
@@ -132,6 +132,48 @@ class Member extends Api
             $this->error( $e->getMessage());
         }
             $this->success($msg, $user);
+    }
+
+
+    /**
+     * 绑定上级
+     * @return void
+     */
+    public function bind()
+    {
+        $mobile    = $this->request->post('mobile');      // 手机号
+        $inviteCode = $this->request->post('inviteCode'); // 邀请码
+        if (empty($mobile) || empty($inviteCode)) $this->error(__('Invalid parameters'));
+        
+        $inviteCode = strtolower($inviteCode); // 转小写
+        $msg        = '';
+        Db::startTrans();
+        try {
+            // 不存在时注册
+            $user = (new UserModel())->getByMobile($mobile);
+            if (empty($user))  throw new Exception(__('账号不存在'));
+             
+            // 尝试绑定上级
+            if (empty($user['parent_id'])) { 
+               
+                $parent = (new UserModel())->getByAddress($inviteCode);
+                if (empty($parent))  throw new Exception(__('The invitation code is invalid, please manually bind your superior')) ;
+                // 创建网体
+                (new UserPathModel())->createPath($user['id'], $parent->id);
+                //添加推荐人奖励
+                $user->parent_id = $parent->id;
+                $user->save();  
+            }
+
+            Token::marshal($user['id']);
+            $user['token'] = Token::getEncryptedToken($user['id']);   
+            Db::commit();
+        } catch (Exception $e) {
+            // 回滚事务
+            Db::rollback();
+            $this->error( $e->getMessage());
+        }
+            $this->success('ok', $user);
         }
 
 }

+ 5 - 0
application/common/model/UserModel.php

@@ -55,6 +55,11 @@ class UserModel extends Model
         return $this->where('id', $userID)->find();
     }
 
+    public function getByMobile($mobile)
+    {
+        return $this->where("mobile", $mobile)->find();
+    }
+
     public function getByAddress($address)
     {
         return $this->where("address", $address)->find();