123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- package newcomer
- import (
- "context"
- "encoding/json"
- "errors"
- "net/http"
- "net/url"
- "strconv"
- "strings"
- "time"
- "fmt"
- "go-common/app/interface/main/creative/conf"
- "go-common/app/interface/main/creative/dao/tool"
- "go-common/library/ecode"
- "go-common/library/log"
- "go-common/library/xstr"
- "math/rand"
- )
- func init() {
- rand.Seed(time.Now().UnixNano())
- }
- func (d *Dao) Mall(c context.Context, mid int64, couponID, uname string) (err error) {
- type params struct {
- MID int64 `json:"mid"`
- CouponID string `json:"couponId"`
- Uname string `json:"uname"`
- }
- p := params{}
- p.MID = mid
- p.CouponID = couponID
- p.Uname = uname
- paramJSON, err := json.Marshal(p)
- if err != nil {
- log.Error("Mall json.Marshal param(%+v) error(%v)", p, err)
- return
- }
- paramStr := string(paramJSON)
- var (
- req *http.Request
- )
- if req, err = http.NewRequest("POST", d.mallURI, strings.NewReader(paramStr)); err != nil {
- log.Error("Mall http.NewRequest url(%s) error(%v)", d.mallURI+"?"+paramStr, err)
- err = ecode.CreativeNewcomerMallAPIErr
- return
- }
- log.Info("Mall url(%s)", d.mallURI+"?"+paramStr)
- req.Header.Set("Content-Type", "application/json")
- var res struct {
- Code int `json:"code"`
- Msg string `json:"msg"`
- }
- if err = d.client.Do(c, req, &res); err != nil {
- log.Error("Mall d.client.Do url(%s) res(%v) err(%v)", d.mallURI+"?"+paramStr, res, err)
- err = ecode.CreativeNewcomerMallAPIErr
- return
- }
- if res.Code != 0 {
- log.Error("Mall url(%s) res(%v)", d.mallURI+"?"+paramStr, res)
- err = ecode.Int(res.Code)
- }
- return
- }
- func (d *Dao) BCoin(c context.Context, mid int64, activityID string, money int64) (err error) {
- var (
- req *http.Request
- params = url.Values{}
- )
- params.Set("mid", strconv.FormatInt(mid, 10))
- params.Set("activity_id", activityID)
- params.Set("money", strconv.FormatInt(money, 10))
- params.Set("appkey", conf.Conf.App.Key)
- params.Set("appsecret", conf.Conf.App.Secret)
- params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
- var (
- query, _ = tool.Sign(params)
- mallURL = d.bPayURI
- )
- log.Info("BCoin url(%s)", mallURL+"?"+query)
- if req, err = http.NewRequest("POST", mallURL, strings.NewReader(params.Encode())); err != nil {
- log.Error("BCoin url(%s) error(%v)", mallURL, err)
- err = ecode.CreativeNewcomerBCoinAPIErr
- return
- }
- req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
- var res struct {
- Code int `json:"code"`
- Msg string `json:"msg"`
- }
- if err = d.client.Do(c, req, &res); err != nil {
- log.Error("BCoin d.client.Do url(%s) res(%+v) err(%v)", mallURL, res, err)
- err = ecode.CreativeNewcomerBCoinAPIErr
- return
- }
- if res.Code != 0 {
- log.Error("BCoin url(%s) res(%+v)", d.bPayURI+"?"+params.Encode(), res)
- err = ecode.Int(res.Code)
- }
- return
- }
- func (d *Dao) Pendant(c context.Context, mid int64, priceID string, expires int64) (err error) {
- var (
- req *http.Request
- params = url.Values{}
- )
- pid, err := strconv.ParseInt(priceID, 10, 64)
- if err != nil {
- return
- }
- params.Set("mids", strconv.FormatInt(mid, 10))
- params.Set("pid", strconv.FormatInt(pid, 10))
- params.Set("expire", strconv.FormatInt(expires, 10))
- params.Set("appkey", conf.Conf.App.Key)
- params.Set("appsecret", conf.Conf.App.Secret)
- params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
- var (
- query, _ = tool.Sign(params)
- pendURL = d.pendantURI
- )
- log.Info("Pendant url(%s)", pendURL+"?"+query)
- if req, err = http.NewRequest("POST", pendURL, strings.NewReader(params.Encode())); err != nil {
- log.Error("Pendant url(%s) error(%v)", pendURL, err)
- err = ecode.CreativeNewcomerPendantAPIErr
- return
- }
- req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
- var res struct {
- Code int `json:"code"`
- Msg string `json:"msg"`
- }
- if err = d.client.Do(c, req, &res); err != nil {
- log.Error("Pendant d.client.Do url(%s) res(%+v) err(%v)", pendURL, res, err)
- err = ecode.CreativeNewcomerPendantAPIErr
- return
- }
- if res.Code != 0 {
- log.Error("Pendant url(%s) res(%v)", pendURL, res)
- err = ecode.Int(res.Code)
- }
- return
- }
- func (d *Dao) BigMemberCoupon(c context.Context, mid int64, batchToken string) (err error) {
- var (
- req *http.Request
- params = url.Values{}
- )
- params.Set("mid", strconv.FormatInt(mid, 10))
- params.Set("batch_token", batchToken)
- params.Set("order_no", genOrderNO(mid))
- params.Set("appkey", conf.Conf.App.Key)
- params.Set("appsecret", conf.Conf.App.Secret)
- params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
- var (
- query, _ = tool.Sign(params)
- bigMemberURL = d.bigMemberURI
- )
- log.Info("BigMemberCoupon url(%s)", bigMemberURL+"?"+query)
- if req, err = http.NewRequest("POST", bigMemberURL, strings.NewReader(params.Encode())); err != nil {
- log.Error("BigMemberCoupon url(%s) error(%v)", bigMemberURL, err)
- err = ecode.CreativeNewcomerBigMemberErr
- return
- }
- req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
- var res struct {
- Code int `json:"code"`
- Msg string `json:"message"`
- Data string `json:"data"`
- }
- if err = d.client.Do(c, req, &res); err != nil {
- log.Error("BigMemberCoupon d.client.Do url(%s) res(%+v) err(%v)", bigMemberURL, res, err)
- err = ecode.CreativeNewcomerBigMemberErr
- return
- }
- if res.Code != 0 {
- log.Error("BigMemberCoupon url(%s) res(%v)", bigMemberURL, res)
- err = ecode.Int(res.Code)
- }
- return
- }
- func genOrderNO(mid int64) string {
- s := fmt.Sprintf("%v%s", mid, time.Now().Format("20060102150405"))
- if len(s) >= 32 {
- return s[0:32]
- }
- size := 32 - len(s)
- bys := make([]byte, size)
- for i := 0; i < size; i++ {
- bys[i] = uint8(48 + rand.Intn(10))
- }
- return s + string(bys)
- }
- func (d *Dao) SendNotify(c context.Context, mids []int64, mc, title, context string) (err error) {
- var (
- params = url.Values{}
- res struct {
- Code int `json:"code"`
- Msg string `json:"msg"`
- Data *struct {
- TotalCount int `json:"total_count"`
- ErrorCount int `json:"error_count"`
- ErrorMidList []int64 `json:"error_mid_list"`
- } `json:"data"`
- }
- )
- params.Set("mc", mc)
- params.Set("data_type", "4")
- params.Set("title", title)
- params.Set("context", context)
- params.Set("mid_list", xstr.JoinInts(mids))
- log.Info("SendNotify params(%+v)|msgURI(%s)", params.Encode(), d.msgNotifyURI)
- if err = d.client.Post(c, d.msgNotifyURI, "", params, &res); err != nil {
- log.Error("d.httpClient.Post(%s,%v,%d)", d.msgNotifyURI, params, err)
- return
- }
- if res.Code != 0 {
- err = errors.New("code != 0")
- log.Error("d.httpClient.Post(%s,%v,%v,%d)", d.msgNotifyURI, params, err, res.Code)
- }
- if res.Data != nil {
- log.Info("SendNotify log total_count(%d) error_count(%d) error_mid_list(%v)", res.Data.TotalCount, res.Data.ErrorCount, res.Data.ErrorMidList)
- }
- return
- }
|