github.com/chanxuehong/wechat@v0.0.0-20230222024006-36f0325263cd/mp/card/qrcode/create.go (about) 1 package qrcode 2 3 import ( 4 "net/url" 5 6 "github.com/chanxuehong/wechat/mp/core" 7 ) 8 9 func QrcodePicURL(ticket string) string { 10 return "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" + url.QueryEscape(ticket) 11 } 12 13 type CreateParameters struct { 14 CardId string `json:"card_id"` // 必须; 卡券ID 15 Code string `json:"code,omitempty"` // 可选; use_custom_code字段为true的卡券必须填写,非自定义code不必填写 16 OpenId string `json:"openid,omitempty"` // 可选; 指定领取者的openid,只有该用户能领取。bind_openid字段为true的卡券必须填写,非指定openid不必填写。 17 ExpireSeconds int `json:"expire_seconds,omitempty"` // 可选; 指定二维码的有效时间,范围是60 ~ 1800秒。不填(值为0)默认为永久有效。 18 IsUniqueCode *bool `json:"is_unique_code,omitempty"` // 可选; 指定下发二维码,生成的二维码随机分配一个code,领取后不可再次扫描。填写true或false。默认false。 19 OuterId *int64 `json:"outer_id,omitempty"` // 可选; 领取场景值,用于领取渠道的数据统计,默认值为0,字段类型为整型,长度限制为60位数字。用户领取卡券后触发的事件推送中会带上此自定义场景值。 20 } 21 22 type QrcodeInfo struct { 23 Ticket string `json:"ticket"` 24 URL string `json:"url"` 25 ExpireSeconds int `json:"expire_seconds"` // 0 表示永久二维码 26 } 27 28 // 卡券投放, 创建二维码接口. 29 func Create(clt *core.Client, para *CreateParameters) (info *QrcodeInfo, err error) { 30 request := struct { 31 ActionName string `json:"action_name"` 32 ExpireSeconds int `json:"expire_seconds,omitempty"` 33 ActionInfo struct { 34 Card *CreateParameters `json:"card,omitempty"` 35 } `json:"action_info"` 36 }{ 37 ActionName: "QR_CARD", 38 ExpireSeconds: para.ExpireSeconds, 39 } 40 request.ActionInfo.Card = para 41 42 var result struct { 43 core.Error 44 QrcodeInfo 45 } 46 47 incompleteURL := "https://api.weixin.qq.com/card/qrcode/create?access_token=" 48 if err = clt.PostJSON(incompleteURL, &request, &result); err != nil { 49 return 50 } 51 52 if result.ErrCode != core.ErrCodeOK { 53 err = &result.Error 54 return 55 } 56 info = &result.QrcodeInfo 57 return 58 }