33 lines
828 B
Go
33 lines
828 B
Go
package wx
|
|
|
|
import (
|
|
"context"
|
|
"github.com/gogf/gf/v2/crypto/gsha1"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"server/utility/ecode"
|
|
"sort"
|
|
"strings"
|
|
|
|
"server/api/auth/v1"
|
|
)
|
|
|
|
func (c *ControllerV1) WeChatVertify(ctx context.Context, req *v1.WeChatVertifyReq) (res *v1.WeChatVertifyRes, err error) {
|
|
|
|
weChatToken := g.Config().MustGet(ctx, "wechat.token").String()
|
|
// 1. 将 token、timestamp、nonce 组成 slice
|
|
params := []string{weChatToken, req.Timestamp, req.Nonce}
|
|
// 2. 字典序排序
|
|
sort.Strings(params)
|
|
// 3. 拼接字符串
|
|
joined := strings.Join(params, "")
|
|
// 4. SHA1 加密
|
|
encrypt := gsha1.Encrypt(joined)
|
|
|
|
// 5. 与 signature 对比
|
|
if encrypt != req.Signature {
|
|
return nil, ecode.InvalidOperation.Sub("微信服务器验证失败")
|
|
}
|
|
g.RequestFromCtx(ctx).Response.WriteJson(req.EchoStr)
|
|
return
|
|
}
|