package activity import ( "app.yhyue.com/moapp/message/config" "app.yhyue.com/moapp/message/db" "encoding/json" "fmt" "github.com/gogf/gf/v2/os/gcfg" "github.com/gogf/gf/v2/os/gctx" "io/ioutil" "log" "net/http" url2 "net/url" "strconv" "strings" "time" ) type LotteryReq struct { Code int `json:"Code"` Msg string `json:"Message"` Data []LotteryDetail `json:"Data"` } type LotteryDetail struct { Full int `json:"Full"` //满 Reduce int `json:"Reduce"` //减 IsReceive bool `json:"IsReceive"` //是否领取 true可领取 false 不能领取 IsUser bool `json:"IsUser"` //是否使用 true已用 false 未使用 LotteryName string `json:"LotteryName"` //卡券名字 LotteryIdStr string `json:"LotteryIdStr"` //卡券加密id } func (BP *JyActivity) GetActivityLotteryList(userId string) ([]LotteryDetail, error) { if _, _, inActivity := BP.InActivity(); !inActivity { return nil, fmt.Errorf("不在活动时间内") } req, err := activityLotteryRequest(userId, "0") if err != nil { return nil, err } if req.Code != 1 { return nil, fmt.Errorf("获取优惠券异常" + req.Msg) } if len(req.Data) == 0 { return nil, fmt.Errorf("优惠券列表空") } return req.Data, err } //GetLottery 领取卡券 func (BP *JyActivity) GetLottery(userId, userName, lotteryIds string) error { if _, inActivity, _ := BP.InActivity(); !inActivity { return fmt.Errorf("不在活动时间内") } lotteryRep := config.LotteryReceiveReq{ UserName: userName, UserId: userId, LotteryIdArr: lotteryIds, } code, msg := getLotteryReceive(lotteryRep) if code != 1 { return fmt.Errorf("领取优惠券异常%s", msg) } //存入奖励列表 var awardList []map[string]interface{} nowStamp := time.Now().Unix() list, _ := activityLotteryRequest(userId, "0") for _, v := range list.Data { for _, v1 := range strings.Split(lotteryIds, ",") { if v.LotteryIdStr == v1 { awardList = append(awardList, map[string]interface{}{ "activity_code": BP.ActivityCode, "detail": v.LotteryName, "userid": userId, "award": "lottery-fullReduce", //满减券 "num": v.Reduce, "getway": "福利一", "date": nowStamp, }) } } } if len(awardList) > 0 { db.Mgo.SaveBulk("activity_award", awardList...) } return nil } //activityLottery 获取活动下的奖券 receivingLocation 奖券形式0落地页 1付款页 999全部 3 我的奖券 productCode 活动code func activityLotteryRequest(userId, receivingLocation string) (*LotteryReq, error) { //url := config.PushConfig.ActivityUrl + "/activityLottery?userId=" + userId + "&appId=" + appId + "&activityId=" + activityId + "&receivingLocation=" + receivingLocation + "&productCode=" + productCode appId := gcfg.Instance().MustGet(gctx.New(), "appid").String() productCode := gcfg.Instance().MustGet(gctx.New(), "productCode.subscriptionMonth").String() url := gcfg.Instance().MustGet(gctx.New(), "lotteryUrl").String() + "/activityLottery?userId=" + userId + "&appId=" + appId + "&receivingLocation=" + receivingLocation + "&productCode=" + productCode request, err := http.NewRequest("GET", url, nil) if err != nil { return nil, err } client := http.Client{} resp, err1 := client.Do(request) if err1 != nil { return nil, err1 } defer resp.Body.Close() var info LotteryReq result, _ := ioutil.ReadAll(resp.Body) err = json.Unmarshal(result, &info) return &info, err } //getLotteryReceive 奖卷领取 LotteryIdArr 奖券id 支持多个逗号拼接 func getLotteryReceive(lotteryRep config.LotteryReceiveReq) (int, string) { appId := gcfg.Instance().MustGet(gctx.New(), "appid").String() userNameStr := url2.PathEscape(lotteryRep.UserName) url := gcfg.Instance().MustGet(gctx.New(), "lotteryUrl").String() + "/lotteryReceive?userName=" + userNameStr + "&userId=" + lotteryRep.UserId + "&appId=" + appId + "&lotteryIdArr=" + lotteryRep.LotteryIdArr log.Println(url) request, err := http.NewRequest("POST", url, nil) if err != nil { return 0, err.Error() } client := http.Client{} resp, err1 := client.Do(request) if err1 != nil { return 0, "奖券领取失败" } defer resp.Body.Close() var info map[string]interface{} result, _ := ioutil.ReadAll(resp.Body) if err = json.Unmarshal(result, &info); err != nil { return 0, err.Error() } fmt.Println("==============json str 转map=======================", info) codeInt, err2 := strconv.Atoi(fmt.Sprint(info["Code"])) if err2 != nil { return 0, err2.Error() } return codeInt, fmt.Sprint(info["Message"]) }