/
single_cardaddconfig.go
88 lines (77 loc) · 2.64 KB
/
single_cardaddconfig.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/**
* Created by GoLand.
* User: 姜伟
* Date: 2020/1/5 0005
* Time: 0:18
*/
package wx
import (
"sort"
"strconv"
"strings"
"time"
"github.com/a07061625/gompf/mpf"
"github.com/a07061625/gompf/mpf/mpconstant/errorcode"
"github.com/a07061625/gompf/mpf/mperr"
)
// 添加卡券配置
type singleCardAddConfig struct {
BaseWxSingle
appId string // 应用ID
cardList map[string]map[string]string // 卡券列表
timestamp int64 // 时间戳
nonceStr string // 随机字符串
needJs bool // JS签名标识,true:需要JS签名 false:不需要JS签名
}
// 设置卡券列表,每个卡券信息包含card_id,code,openid,outer_str,fixed_begintimestamp
func (cec *singleCardAddConfig) SetCardList(cardList []map[string]string) {
for _, v := range cardList {
eCard := v
delete(eCard, "card_id")
cec.cardList[v["card_id"]] = eCard
}
}
func (cec *singleCardAddConfig) SetNeedJs(needJs bool) {
cec.needJs = needJs
}
func (cec *singleCardAddConfig) checkData() {
if len(cec.cardList) == 0 {
panic(mperr.NewWx(errorcode.WxParam, "卡券列表不能为空", nil))
}
}
func (cec *singleCardAddConfig) GetResult(getType string) map[string]interface{} {
cec.checkData()
ticket := NewUtilWx().GetSingleCache(cec.appId, getType)
timeStr := strconv.FormatInt(cec.timestamp, 10)
result := make(map[string]interface{})
cards := make([]map[string]string, 0)
for k, v := range cec.cardList {
cardInfo := v
signData := make([]string, 0)
signData = append(signData, ticket, timeStr, k, v["code"], v["openid"], cec.nonceStr)
sort.Strings(signData)
cardInfo["signature"] = mpf.HashSha1(strings.Join(signData, ""), "")
cardInfo["timestamp"] = timeStr
cardInfo["nonce_str"] = cec.nonceStr
eCard := make(map[string]string)
eCard["cardId"] = k
eCard["cardExt"] = mpf.JSONMarshal(cardInfo)
cards = append(cards, eCard)
}
result["card_list"] = cards
if cec.needJs {
jc := NewSingleJsConfig(cec.appId)
jc.SetTimestamp(cec.timestamp)
jc.SetNonceStr(cec.nonceStr)
result["js_config"] = jc.GetResult(getType)
}
return result
}
func NewSingleCardAddConfig(appId string) *singleCardAddConfig {
cec := &singleCardAddConfig{NewBaseWxSingle(), "", make(map[string]map[string]string), 0, "", false}
cec.appId = appId
cec.timestamp = time.Now().Unix()
cec.nonceStr = mpf.ToolCreateNonceStr(32, "numlower")
cec.needJs = false
return cec
}