wupengfei 2 年之前
父節點
當前提交
52ff6c5a54

+ 0 - 1
application/api/controller/Base.php

@@ -94,7 +94,6 @@ class Base extends Controller
                 $member = Db::name('store_member')->field('status')->where('id',$authInfo['uid'])->find();
                 if($member['status']){
                     $this->user_id = $authInfo['uid'];
-                    $this->user_id;
                     return  true;
                 }
             }

+ 64 - 2
application/api/controller/Coupon.php

@@ -1,5 +1,6 @@
 <?php
 namespace app\api\controller;
+use app\common\model\CouponActivity;
 use app\common\model\CouponConfig;
 use app\common\model\UserCouponList;
 use think\Db;
@@ -12,13 +13,74 @@ use function Symfony\Component\String\s;
  */
 class Coupon extends Base
 {
-
+    protected $need_login = [
+        'getUserCouponList',
+        'getCouponByCode',
+    ];
     public function initialize(){
         parent::initialize();
-        parent::checkLogin();
+        parent::setUid();
     }
 
     /**
+     * @title 自动获取活动优惠券
+     * @desc  获取失败页面不需要处理!!!仅获取成功处理list数据
+     * @author  qc
+     * @url /api/Coupon/autoGetActivityCoupon
+     * @method POST
+     * @header name:Authorization require:1 desc:Token
+     * @return name:title type:string default:-- desc:优惠券名称
+     * @return name:module type:int default:-- desc:应用模块(1所有,2洗鞋,3洗衣)
+     * @return name:coupon_type type:int default:-- desc:类型(1满减券,2抵扣券【抵一件衣服或是一双鞋价格】)
+     * @return name:is_new type:int default:-- desc:是否新人券(0否1是)
+     * @return name:low_amount type:int default:-- desc:最低消费限制(coupon_type=1时用)
+     * @return name:amount type:int default:-- desc:抵扣金额(coupon_type=1时用)
+     * @return name:create_at type:string default:-- desc:领取时间
+     * @return name:past_at type:string default:-- desc:过期时间
+     * @return name:status type:int default:-- desc:状态码(1未使用2已使用3已过期)
+     */
+    public function autoGetActivityCoupon()
+    {
+        if(!$this->user_id) $this->error('用户未登录');
+        $where = [];
+        $where[] = ['status','=',1];
+        $where[] = ['is_deleted','=',0];
+        $where[] = ['start_time','< time',date("Y-m-d H:i:s")];
+        $where[] = ['end_time','> time',date("Y-m-d H:i:s")];
+        // 进行中的活动
+        $activity_list = CouponActivity::where($where)->select()->toArray();
+        $get_ids = [];// 活动的优惠券记录id
+        foreach ($activity_list as $act_info)
+        {
+            if(empty($act_info['coupon_set'])) continue;
+            $ids =   explode('|',trim($act_info['coupon_set'],'|'));// 活动优惠券设置
+            // 是否已获得该活动的优惠券
+            $has_get = UserCouponList::where(['user_id'=>$this->user_id,'act_id'=>$act_info['id']])->count();
+            if($has_get) continue;
+            foreach ($ids as $coupon_id) {
+                // 优惠券详情
+                $coupon_info = CouponConfig::where('id',$coupon_id)->find()->toArray();
+                // 优惠券是否是正常状态
+                if(!$coupon_info['status'] !=1 || $coupon_info['is_deleted'] == 1) continue;
+                // 检查优惠券活动数量
+                $has_num = UserCouponList::getUserCouponNum($this->user_id,$coupon_id);
+                if($has_num >= $coupon_info['user_num']) continue;
+                $user_coupon =   UserCouponList::saveUserCoupon($coupon_info,$this->user_id,$act_info['id']);
+                $get_ids[] = $user_coupon['id'];
+            }
+        }
+        if(empty($get_ids)) $this->error('没有优惠活动或已获得');
+        $list = UserCouponList::where([['l.user_id','=',$this->user_id],['id','in',$get_ids]])
+            ->alias('l.*,c.title,c.module,c.coupon_type,c.is_new')
+            ->leftJoin('CouponConfig c','c.id = l.coupon_id')
+            ->limit($this->off_set,$this->page_num)
+            ->order('l.id desc')
+            ->select()->toArray();
+        $this->success('ok',['list'=>$list]);
+    }
+
+
+    /**
      * @title 获取会员优惠券列表
      * @desc  获取会员优惠券列表
      * @author  qc

+ 9 - 0
application/common/model/CouponActivity.php

@@ -0,0 +1,9 @@
+<?php
+namespace app\common\model;
+use think\Model;
+// 优惠券活动
+class CouponActivity extends Model
+{
+
+
+}

+ 3 - 2
application/common/model/UserCouponList.php

@@ -14,7 +14,7 @@ class UserCouponList extends Model
     }
 
     // 会员优惠券领取
-    public static function saveUserCoupon($coupon_info,$user_id){
+    public static function saveUserCoupon($coupon_info,$user_id,$act_id = 0){
         $data = [
             'coupon_id' => $coupon_info['id'],
             'user_id' => $user_id,
@@ -23,8 +23,9 @@ class UserCouponList extends Model
             'amount' => $coupon_info['amount'],
             'low_day' => $coupon_info['low_day'],
             'past_at' => date("Y-m-d H:i:s",strtotime("+ ".$coupon_info['low_day'] ."days")),
+            'act_id' => $act_id,
         ];
-        return self::create($data);
+        return self::create($data)->toArray();
     }
 
 }