Przeglądaj źródła

绑定推荐人

moonsflyer 9 miesięcy temu
rodzic
commit
4c7ac902dc

+ 0 - 1
app/common/validate/BaseValidate.php

@@ -82,7 +82,6 @@ class BaseValidate extends Validate
             $result = $this->check($params);
         }
 
-
         if (!$result) {
             $exception = is_array($this->error) ? implode(';', $this->error) : $this->error;
             JsonService::throw($exception);

+ 17 - 0
app/shopapi/controller/UserController.php

@@ -127,6 +127,23 @@ class UserController extends BaseShopController
     }
 
     /**
+     * @notes 绑定推荐码
+     * @return \think\response\Json
+     * @author Tab
+     * @date 2021/8/25 17:46
+     */
+    public function bindInviterCode()
+    {
+        $params = (new UserValidate())->post()->goCheck('bindInviterCode');
+        $params['id'] = $this->userId;
+        $result = UserLogic::bindInviterId($params);
+        if($result) {
+            return $this->success('绑定成功', [], 1, 1);
+        }
+        return $this->fail(UserLogic::getError());
+    }
+
+    /**
      * @notes 用户等级
      * @return \think\response\Json
      * @author cjhao

+ 1 - 0
app/shopapi/logic/LoginLogic.php

@@ -89,6 +89,7 @@ class LoginLogic extends BaseLogic
                 'mobile' => $userInfo['mobile'],
                 'avatar' => $avatar,
                 'token' => $userInfo['token'],
+                'inviter_id' => $userInfo['inviter_id'],
             ];
         } catch (\Exception $e) {
             self::setError($e->getMessage());

+ 25 - 0
app/shopapi/logic/UserLogic.php

@@ -156,6 +156,31 @@ class UserLogic extends BaseLogic
     }
 
     /**
+     * @notes 绑定推荐人
+     * @param $params
+     * @return bool
+     * @author Tab
+     * @date 2021/8/25 17:55
+     */
+    public static function bindInviterId($params)
+    {
+        try {
+            $user = User::where('id', $params['id'])->findOrEmpty();
+            if ($user->inviter_id) {
+                throw new \Exception('您已经绑定过推荐人了!无需重复绑定');
+            }
+            $bind_user = User::where('code', $params['inviter_code'])->findOrEmpty();
+            unset($params['code']);
+            $params['inviter_id'] = $bind_user['id'];
+            User::update($params);
+            return true;
+        } catch (\Exception $e) {
+            self::setError($e->getMessage());
+            return false;
+        }
+    }
+
+    /**
      * @notes 会员中心
      * @param int $userId
      * @return array

+ 32 - 0
app/shopapi/validate/UserValidate.php

@@ -39,6 +39,7 @@ class UserValidate extends BaseValidate
         'mobile' => 'require|mobile',
         'password' => 'require|length:6,20|alphaDash',
         'old_password' => 'require',
+        'inviter_code'=> 'require|checkInviterCode',
     ];
 
     protected $message = [
@@ -57,6 +58,7 @@ class UserValidate extends BaseValidate
         'password.require' => '请输入登录密码',
         'password.length' => '登录密码须在6-20位之间',
         'password.alphaDash' => '登录密码须为字母数字下划线或破折号',
+        'inviter_code.require' => '请输入推荐码',
     ];
 
     /**
@@ -162,6 +164,16 @@ class UserValidate extends BaseValidate
     }
 
     /**
+     * @notes 绑定推荐吗
+     * @return UserValidate
+     * @author Tab
+     * @date 2021/8/25 17:44
+     */
+    public function sceneBindInviterCode()
+    {
+        return $this->only(['inviter_code']);
+    }
+    /**
      * @notes 重置支付密码
      * @return UserValidate
      * @author Tab
@@ -210,6 +222,26 @@ class UserValidate extends BaseValidate
     }
 
     /**
+     * @notes 校验推荐码密码
+     * @param $value
+     * @param $rule
+     * @param $data
+     * @return bool|string
+     * @author Tab
+     * @date 2021/8/11 20:35
+     */
+    public function checkInviterCode($value, $rule, $data)
+    {
+        $user = User::where(['code'=>$data['inviter_code']])->findOrEmpty();
+        if($user->isEmpty()) {
+            return '无效的推荐码!';
+        }
+        if($user->user_delete) {
+            return '该推荐码用户已注销!';
+        }
+        return true;
+    }
+    /**
      * @notes 校验支付密码
      * @param $value
      * @param $rule