package utils import ( "applet/app/md" "bytes" "crypto/sha1" "encoding/hex" "fmt" "io" "io/ioutil" "mime/multipart" "net/http" "os" "sort" "strings" ) // CheckSignature 微信公众号签名检查 func CheckSignature(signature, timestamp, nonce, token string) bool { arr := []string{timestamp, nonce, token} // 字典序排序 sort.Strings(arr) n := len(timestamp) + len(nonce) + len(token) var b strings.Builder b.Grow(n) for i := 0; i < len(arr); i++ { b.WriteString(arr[i]) } return Sha1(b.String()) == signature } // 进行Sha1编码 func Sha1(str string) string { h := sha1.New() h.Write([]byte(str)) return hex.EncodeToString(h.Sum(nil)) } func GetWechatToken(appid, secret string) (string, error) { get, err := CurlGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+secret, nil) return string(get), err } func GetWechatSelfMenu(token string) (string, error) { get, err := CurlGet("https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token="+token, nil) return string(get), err } func SetWechatSelfMenu(token string, args md.WechatParam) (string, error) { str := SerializeStr(args) str = strings.ReplaceAll(str, "\\u0026", "&") str = strings.ReplaceAll(str, "\\&", "&") fmt.Println(str) get, err := CurlPost("https://api.weixin.qq.com/cgi-bin/menu/create?access_token="+token, str, nil) return string(get), err } func UploadWxImg(token string, args md.WechatParam) (string, error) { str := SerializeStr(args) str = strings.ReplaceAll(str, "\\u0026", "&") fmt.Println(str) get, err := CurlPost("https://api.weixin.qq.com/cgi-bin/material/add_material?access_token"+token+"&type=image", str, nil) return string(get), err } func PostFile(fieldname, filename, uri string) ([]byte, error) { fields := []MultipartFormField{ { IsFile: true, Fieldname: fieldname, Filename: filename, }, } return PostMultipartForm(fields, uri) } //MultipartFormField 保存文件或其他字段信息 type MultipartFormField struct { IsFile bool Fieldname string Value []byte Filename string } //PostMultipartForm 上传文件或其他多个字段 func PostMultipartForm(fields []MultipartFormField, uri string) (respBody []byte, err error) { bodyBuf := &bytes.Buffer{} bodyWriter := multipart.NewWriter(bodyBuf) for _, field := range fields { if field.IsFile { fileWriter, e := bodyWriter.CreateFormFile(field.Fieldname, field.Filename) if e != nil { err = fmt.Errorf("error writing to buffer , err=%v", e) return } fh, e := os.Open(field.Filename) if e != nil { err = fmt.Errorf("error opening file , err=%v", e) return } defer fh.Close() if _, err = io.Copy(fileWriter, fh); err != nil { return } } else { partWriter, e := bodyWriter.CreateFormField(field.Fieldname) if e != nil { err = e return } valueReader := bytes.NewReader(field.Value) if _, err = io.Copy(partWriter, valueReader); err != nil { return } } } contentType := bodyWriter.FormDataContentType() bodyWriter.Close() resp, e := http.Post(uri, contentType, bodyBuf) if e != nil { err = e return } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { return nil, err } respBody, err = ioutil.ReadAll(resp.Body) return }