Browse Source

新增:用户绑定手机号

Yahoo 4 năm trước cách đây
mục cha
commit
a0b9c2cfd5
3 tập tin đã thay đổi với 86 bổ sung và 24 xóa
  1. 20 4
      app/api/controller/User.php
  2. 34 0
      app/api/model/User.php
  3. 32 20
      app/common/model/User.php

+ 20 - 4
app/api/controller/User.php

@@ -12,9 +12,11 @@ declare (strict_types = 1);
 
 namespace app\api\controller;
 
+use app\api\model\User as UserModel;
 use app\common\exception\BaseException;
 use app\api\model\UserCoupon as UserCouponModel;
 use app\api\service\User as UserService;
+use think\response\Json;
 
 /**
  * 用户管理
@@ -25,10 +27,10 @@ class User extends Controller
 {
     /**
      * 当前用户详情
-     * @return array|\think\response\Json
+     * @return Json
      * @throws BaseException
      */
-    public function info()
+    public function info(): Json
     {
         // 当前用户信息
         $userInfo = UserService::getCurrentLoginUser(true);
@@ -41,10 +43,10 @@ class User extends Controller
 
     /**
      * 账户资产
-     * @return array|\think\response\Json
+     * @return Json
      * @throws BaseException
      */
-    public function assets()
+    public function assets(): Json
     {
         // 当前用户信息
         $userInfo = UserService::getCurrentLoginUser(true);
@@ -59,4 +61,18 @@ class User extends Controller
             ]
         ]);
     }
+
+    /**
+     * 手机号绑定
+     * @return Json
+     * @throws \cores\exception\BaseException
+     */
+    public function bindMobile(): Json
+    {
+        $model = new UserModel;
+        if (!$model->bindMobile($this->postForm())) {
+            return $this->renderSuccess($model->getError() ?: '操作失败');
+        }
+        return $this->renderSuccess('恭喜您,手机号绑定成功');
+    }
 }

+ 34 - 0
app/api/model/User.php

@@ -62,4 +62,38 @@ class User extends UserModel
         }
         return false;
     }
+
+    /**
+     * 绑定手机号(当前登录用户)
+     * @param array $data
+     * @return bool
+     * @throws BaseException
+     */
+    public function bindMobile(array $data): bool
+    {
+        // 当前登录的用户信息
+        $userInfo = UserService::getCurrentLoginUser(true);
+        // 验证绑定的手机号
+        $this->checkBindMobile($data);
+        // 更新手机号记录
+        return $userInfo->save(['mobile' => $data['mobile']]);
+    }
+
+    /**
+     * 验证绑定的手机号
+     * @param array $data
+     * @return void
+     * @throws BaseException
+     */
+    private function checkBindMobile(array $data): void
+    {
+        // 验证短信验证码是否匹配
+        if (!CaptchaApi::checkSms($data['smsCode'], $data['mobile'])) {
+            throwError('短信验证码不正确');
+        }
+        // 判断手机号是否已存在
+        if (static::checkExistByMobile($data['mobile'])) {
+            throwError('很抱歉,该手机号已绑定其他账户');
+        }
+    }
 }

+ 32 - 20
app/common/model/User.php

@@ -13,10 +13,10 @@ declare (strict_types=1);
 namespace app\common\model;
 
 use cores\BaseModel;
-use app\common\model\user\PointsLog as PointsLogModel;
-use think\model\relation\BelongsTo;
-use think\model\relation\HasMany;
 use think\model\relation\HasOne;
+use think\model\relation\HasMany;
+use think\model\relation\BelongsTo;
+use app\common\model\user\PointsLog as PointsLogModel;
 
 /**
  * 用户模型类
@@ -38,7 +38,7 @@ class User extends BaseModel
      * 关联用户头像表
      * @return HasOne
      */
-    public function avatar()
+    public function avatar(): HasOne
     {
         return $this->hasOne('UploadFile', 'file_id', 'avatar_id')
             ->bind(['avatar_url' => 'preview_url']);
@@ -48,7 +48,7 @@ class User extends BaseModel
      * 关联会员等级表
      * @return BelongsTo
      */
-    public function grade()
+    public function grade(): BelongsTo
     {
         $module = self::getCalledModule();
         return $this->belongsTo("app\\{$module}\\model\\user\\Grade", 'grade_id');
@@ -58,7 +58,7 @@ class User extends BaseModel
      * 关联收货地址表
      * @return HasMany
      */
-    public function address()
+    public function address(): HasMany
     {
         return $this->hasMany('UserAddress');
     }
@@ -67,7 +67,7 @@ class User extends BaseModel
      * 关联收货地址表 (默认地址)
      * @return BelongsTo
      */
-    public function addressDefault()
+    public function addressDefault(): BelongsTo
     {
         return $this->belongsTo('UserAddress', 'address_id');
     }
@@ -75,9 +75,9 @@ class User extends BaseModel
     /**
      * 获取器:显示性别
      * @param $value
-     * @return mixed
+     * @return string
      */
-    public function getGenderAttr($value)
+    public function getGenderAttr($value): string
     {
         return $this->gender[$value];
     }
@@ -86,9 +86,9 @@ class User extends BaseModel
      * 获取用户信息
      * @param $where
      * @param array $with
-     * @return array|null|static
+     * @return array|\think\Model|null
      */
-    public static function detail($where, $with = [])
+    public static function detail($where, array $with = [])
     {
         $filter = ['is_delete' => 0];
         if (is_array($where)) {
@@ -102,7 +102,7 @@ class User extends BaseModel
     /**
      * 累积用户的实际消费金额
      * @param int $userId
-     * @param $expendMoney
+     * @param float $expendMoney
      * @return mixed
      */
     public static function setIncUserExpend(int $userId, float $expendMoney)
@@ -113,7 +113,7 @@ class User extends BaseModel
     /**
      * 累积用户可用余额
      * @param int $userId
-     * @param $money
+     * @param float $money
      * @return mixed
      */
     public static function setIncBalance(int $userId, float $money)
@@ -124,7 +124,7 @@ class User extends BaseModel
     /**
      * 消减用户可用余额
      * @param int $userId
-     * @param $money
+     * @param float $money
      * @return mixed
      */
     public static function setDecBalance(int $userId, float $money)
@@ -137,18 +137,31 @@ class User extends BaseModel
      * @param int $gradeId
      * @return bool
      */
-    public static function checkExistByGradeId(int $gradeId)
+    public static function checkExistByGradeId(int $gradeId): bool
     {
         $model = new static;
         return (bool)$model->where('grade_id', '=', (int)$gradeId)
             ->where('is_delete', '=', 0)
-            ->value('user_id');
+            ->value($model->getPk());
+    }
+
+    /**
+     * 指定的手机号是否已存在
+     * @param string $mobile
+     * @return bool
+     */
+    public static function checkExistByMobile(string $mobile): bool
+    {
+        $model = new static;
+        return (bool)$model->where('mobile', '=', $mobile)
+            ->where('is_delete', '=', 0)
+            ->value($model->getPk());
     }
 
     /**
      * 累积用户总消费金额
      * @param int $userId
-     * @param $money
+     * @param float $money
      * @return mixed
      */
     public static function setIncPayMoney(int $userId, float $money)
@@ -161,7 +174,7 @@ class User extends BaseModel
      * @param array $data
      * @return bool
      */
-    public function onBatchIncExpendMoney(array $data)
+    public function onBatchIncExpendMoney(array $data): bool
     {
         foreach ($data as $userId => $expendMoney) {
             static::setIncUserExpend($userId, (float)$expendMoney);
@@ -174,7 +187,7 @@ class User extends BaseModel
      * @param array $data
      * @return bool
      */
-    public function onBatchIncPoints(array $data)
+    public function onBatchIncPoints(array $data): bool
     {
         foreach ($data as $userId => $value) {
             $this->setInc($userId, 'points', $value);
@@ -200,5 +213,4 @@ class User extends BaseModel
         // 更新用户可用积分
         return (new static)->setInc($userId, 'points', $points);
     }
-
 }