lotteryService.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package activity
  2. import (
  3. "app.yhyue.com/moapp/message/config"
  4. "app.yhyue.com/moapp/message/db"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/gogf/gf/v2/os/gcfg"
  8. "github.com/gogf/gf/v2/os/gctx"
  9. "io/ioutil"
  10. "log"
  11. "net/http"
  12. url2 "net/url"
  13. "strconv"
  14. "strings"
  15. "time"
  16. )
  17. type LotteryReq struct {
  18. Code int `json:"Code"`
  19. Msg string `json:"Message"`
  20. Data []LotteryDetail `json:"Data"`
  21. }
  22. type LotteryDetail struct {
  23. Full int `json:"Full"` //满
  24. Reduce int `json:"Reduce"` //减
  25. IsReceive bool `json:"IsReceive"` //是否领取 true可领取 false 不能领取
  26. IsUser bool `json:"IsUser"` //是否使用 true已用 false 未使用
  27. LotteryName string `json:"LotteryName"` //卡券名字
  28. LotteryIdStr string `json:"LotteryIdStr"` //卡券加密id
  29. }
  30. func (BP *JyActivity) GetActivityLotteryList(userId string) ([]LotteryDetail, error) {
  31. if _, _, inActivity := BP.InActivity(); !inActivity {
  32. return nil, fmt.Errorf("不在活动时间内")
  33. }
  34. req, err := activityLotteryRequest(userId, "0")
  35. if err != nil {
  36. return nil, err
  37. }
  38. if req.Code != 1 {
  39. return nil, fmt.Errorf("获取优惠券异常" + req.Msg)
  40. }
  41. if len(req.Data) == 0 {
  42. return nil, fmt.Errorf("优惠券列表空")
  43. }
  44. return req.Data, err
  45. }
  46. //GetLottery 领取卡券
  47. func (BP *JyActivity) GetLottery(userId, userName, lotteryIds string) error {
  48. if _, inActivity, _ := BP.InActivity(); !inActivity {
  49. return fmt.Errorf("不在活动时间内")
  50. }
  51. lotteryRep := config.LotteryReceiveReq{
  52. UserName: userName,
  53. UserId: userId,
  54. LotteryIdArr: lotteryIds,
  55. }
  56. code, msg := getLotteryReceive(lotteryRep)
  57. if code != 1 {
  58. return fmt.Errorf("领取优惠券异常%s", msg)
  59. }
  60. //存入奖励列表
  61. var awardList []map[string]interface{}
  62. nowStamp := time.Now().Unix()
  63. list, _ := activityLotteryRequest(userId, "0")
  64. for _, v := range list.Data {
  65. for _, v1 := range strings.Split(lotteryIds, ",") {
  66. if v.LotteryIdStr == v1 {
  67. awardList = append(awardList, map[string]interface{}{
  68. "activity_code": BP.ActivityCode,
  69. "detail": v.LotteryName,
  70. "userid": userId,
  71. "award": "lottery-fullReduce", //满减券
  72. "num": v.Reduce,
  73. "getway": "福利一",
  74. "date": nowStamp,
  75. })
  76. }
  77. }
  78. }
  79. if len(awardList) > 0 {
  80. db.Mgo.SaveBulk("activity_award", awardList...)
  81. }
  82. return nil
  83. }
  84. //activityLottery 获取活动下的奖券 receivingLocation 奖券形式0落地页 1付款页 999全部 3 我的奖券 productCode 活动code
  85. func activityLotteryRequest(userId, receivingLocation string) (*LotteryReq, error) {
  86. //url := config.PushConfig.ActivityUrl + "/activityLottery?userId=" + userId + "&appId=" + appId + "&activityId=" + activityId + "&receivingLocation=" + receivingLocation + "&productCode=" + productCode
  87. appId := gcfg.Instance().MustGet(gctx.New(), "appid").String()
  88. productCode := gcfg.Instance().MustGet(gctx.New(), "productCode.subscriptionMonth").String()
  89. url := gcfg.Instance().MustGet(gctx.New(), "lotteryUrl").String() + "/activityLottery?userId=" + userId + "&appId=" + appId + "&receivingLocation=" + receivingLocation + "&productCode=" + productCode
  90. request, err := http.NewRequest("GET", url, nil)
  91. if err != nil {
  92. return nil, err
  93. }
  94. client := http.Client{}
  95. resp, err1 := client.Do(request)
  96. if err1 != nil {
  97. return nil, err1
  98. }
  99. defer resp.Body.Close()
  100. var info LotteryReq
  101. result, _ := ioutil.ReadAll(resp.Body)
  102. err = json.Unmarshal(result, &info)
  103. return &info, err
  104. }
  105. //getLotteryReceive 奖卷领取 LotteryIdArr 奖券id 支持多个逗号拼接
  106. func getLotteryReceive(lotteryRep config.LotteryReceiveReq) (int, string) {
  107. appId := gcfg.Instance().MustGet(gctx.New(), "appid").String()
  108. userNameStr := url2.PathEscape(lotteryRep.UserName)
  109. url := gcfg.Instance().MustGet(gctx.New(), "lotteryUrl").String() + "/lotteryReceive?userName=" + userNameStr + "&userId=" + lotteryRep.UserId + "&appId=" + appId + "&lotteryIdArr=" + lotteryRep.LotteryIdArr
  110. log.Println(url)
  111. request, err := http.NewRequest("POST", url, nil)
  112. if err != nil {
  113. return 0, err.Error()
  114. }
  115. client := http.Client{}
  116. resp, err1 := client.Do(request)
  117. if err1 != nil {
  118. return 0, "奖券领取失败"
  119. }
  120. defer resp.Body.Close()
  121. var info map[string]interface{}
  122. result, _ := ioutil.ReadAll(resp.Body)
  123. if err = json.Unmarshal(result, &info); err != nil {
  124. return 0, err.Error()
  125. }
  126. fmt.Println("==============json str 转map=======================", info)
  127. codeInt, err2 := strconv.Atoi(fmt.Sprint(info["Code"]))
  128. if err2 != nil {
  129. return 0, err2.Error()
  130. }
  131. return codeInt, fmt.Sprint(info["Message"])
  132. }