39 lines
920 B
Go
39 lines
920 B
Go
package wx
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha1"
|
|
"fmt"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"github.com/gogf/gf/v2/os/glog"
|
|
"server/utility/wechat"
|
|
"sort"
|
|
"strings"
|
|
|
|
"server/api/wx/v1"
|
|
)
|
|
|
|
func (c *ControllerV1) WeChatVertify(ctx context.Context, req *v1.WeChatVertifyReq) (res *v1.WeChatVertifyRes, err error) {
|
|
|
|
// 1. 排序
|
|
params := []string{wechat.GetWeChatClient().GetToken(), req.Timestamp, req.Nonce}
|
|
sort.Strings(params)
|
|
glog.Infof(ctx, "排序后的参数: %s", params)
|
|
|
|
// 2. 拼接成字符串
|
|
str := strings.Join(params, "")
|
|
glog.Infof(ctx, "拼接后的字符串: %s", str)
|
|
|
|
// 3. SHA1 加密
|
|
h := sha1.New()
|
|
h.Write([]byte(str))
|
|
sha1Str := fmt.Sprintf("%x", h.Sum(nil))
|
|
glog.Infof(ctx, "SHA1 加密后的字符串: %s", sha1Str)
|
|
// 4. 比较签名
|
|
if sha1Str != req.Signature {
|
|
return nil, fmt.Errorf("签名错误")
|
|
}
|
|
g.RequestFromCtx(ctx).Response.Write(req.EchoStr)
|
|
return nil, nil
|
|
}
|