type WXPrams struct {
m map[string]interface{} // 参数
sign string // 签名
}
func (p *WXPrams) InitSign() {
var keys []string
for k, _ := range p.m {
keys = append(keys, k)
}
sort.Strings(keys)
builder := strings.Builder{}
for _, v := range keys {
builder.WriteString(v)
builder.WriteString("=")
builder.WriteString(fmt.Sprint(p.m[v]))
builder.WriteString("&")
}
builder.WriteString("key=" + apiKey)
p.sign = strings.ToUpper(lib.Md5(builder.String()))
}
func (p *WXPrams) ToPram() string {
p.InitSign()
builder := strings.Builder{}
builder.WriteString("<xml>")
for k, v := range p.m {
builder.WriteString("<" + k + ">")
builder.WriteString(fmt.Sprint(v))
builder.WriteString("</" + k + ">")
}
builder.WriteString("<sign>")
builder.WriteString(fmt.Sprint(p.sign))
builder.WriteString("</sign>")
builder.WriteString("</xml>")
return builder.String()
}
有疑问加站长微信联系(非本文作者)