ソースを参照

wesmiler 报恩寺项目提交

wesmiler 4 年 前
コミット
0b3d5a3a29
1 ファイル変更88 行追加2 行削除
  1. 88 2
      app/Services/MemberService.php

+ 88 - 2
app/Services/MemberService.php

@@ -364,12 +364,98 @@ class MemberService extends BaseService
     }
 
     /**
-     * 加入VIP
+     * 加入会员
      * @param $userId
      */
     public function buyVip($userId){
+        $memberInfo = $this->model::where(['id'=> $userId,'mark'=> 1,'status'=> 1])
+            ->select(['id','nickname','openid','coupon','mobile','is_vip','vip_expire'])
+            ->first();
+        if(!$memberInfo){
+            return message('用户账户不可操作,请联系客服',false);
+        }
+
+        // 是否已经加入了会员
+        $isVip = false;
+        if($memberInfo->is_vip && $memberInfo->vip_expire>=time()){
+            $isVip = true;
+        }
+
+        // 验证账户
+        $config = ConfigService::make()->getConfigByGroup(15);
+        $vipPrice = isset($config['vip_price'])? intval($config['vip_price']['value']) : 0;
+        $vipGiveCoupon = isset($config['vip_give_coupon'])? intval($config['vip_give_coupon']['value']) : 0;
+        if($vipPrice<=0){
+            return message('当前会员价格参数设置错误,请联系客服',false);
+        }
+
+        if($vipPrice<=$vipGiveCoupon){
+            return message('当前会员赠送参数设置错误,请联系客服',false);
+        }
+
+        if($memberInfo->coupon < $vipPrice){
+            return message('您的账户花灯券不足,请先充值后重试',false);
+        }
+
+        // 操作账户
+        \DB::beginTransaction();
+        if(!$this->model::where(['id'=> $userId,'mark'=> 1])->decrement('coupon',($vipPrice - $vipGiveCoupon))){
+            \DB::rollBack();
+            return message('扣除用户账户失败,请刷新后重试',false);
+        }
+
+        // 更新会员有效期
+        if($isVip){
+            $vipExpire = $memberInfo->vip_expire+365*24*3600;
+        }else{
+            $vipExpire = time()+365*24*3600;
+        }
 
+        if(!$this->model::where(['id'=> $userId,'mark'=> 1])->update(['is_vip'=> 1,'vip_expire'=> $vipExpire]))){
+            \DB::rollBack();
+            return message('更新会员有效期失败,请刷新后重试',false);
+        }
+
+        // 账户明细
+        $data = [
+            'user_id'=> $userId,
+            'type'=> 1,
+            'coin_type'=> 1,
+            'pay_type'=> 1,
+            'money'=> $vipPrice,
+            'change_type'=> 2,
+            'balance'=> $memberInfo->coupon,
+            'create_time'=> time(),
+            'remark'=> ($isVip? '续费':'加入')."会员扣除{$vipPrice}花灯券",
+            'status'=> 1,
+        ];
+
+        if(!TradeModel::insertGetId($data)){
+            \DB::rollBack();
+            return message('处理账户明细失败,请刷新后重试',false);
+        }
+
+        // 赠送
+        if($vipGiveCoupon>0){
+            $data = [
+                'user_id'=> $userId,
+                'type'=> 3,
+                'coin_type'=> 1,
+                'pay_type'=> 4,
+                'money'=> $vipGiveCoupon,
+                'change_type'=> 1,
+                'balance'=> ($memberInfo->coupon-$vipPrice),
+                'create_time'=> time(),
+                'remark'=> ($isVip? '续费':'加入')."会员奖励{$vipGiveCoupon}花灯券",
+                'status'=> 1,
+            ];
+
+            if(!TradeModel::insertGetId($data)){
+                \DB::rollBack();
+                return message('赠送券处理失败,请刷新后重试',false);
+            }
+        }
 
-        return message(MESSAGE_OK,true);
+        return message(($isVip? '续费':'加入').'会员成功',true);
     }
 }