代码拉取完成,页面将自动刷新
同步操作将从 jf/wechat-go 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
package wx
import (
rand2 "crypto/rand"
"crypto/rsa"
"crypto/sha1"
"crypto/tls"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"errors"
"fmt"
"io"
"math/rand"
"net/http"
"reflect"
"sort"
"strings"
"time"
)
type H map[string]interface{}
func postWithCert(cert tls.Certificate, api string, body io.Reader) (resp *http.Response, err error) {
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
Certificates: []tls.Certificate{cert},
},
DisableCompression: true,
},
}
req, err := http.NewRequest("POST", api, body)
if err != nil {
return
}
resp, err = client.Do(req)
return
}
func postStreamWithCert(cert tls.Certificate, api string, data io.Reader) (body io.ReadCloser, err error) {
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
Certificates: []tls.Certificate{cert},
},
DisableCompression: true,
},
}
req, err := http.NewRequest("POST", api, data)
if err != nil {
return
}
resp, err := client.Do(req)
if err != nil {
return
}
body = resp.Body
return
}
// 安全地限制长度,并将微信不支持的字符替换成'x',能满足商户平台的字符要求
func SafeString(str string, length int) string {
if length <= 3 {
return ""
}
runs := []rune(str)
// 单字符长度高于3的,不是一般的utf8字符,剔除掉
for k, v := range runs {
switch len([]byte(string(v))) {
case 1:
// 全部放行
case 3:
if v < 19968 || v > 40869 {
// 只支持中文
runs[k] = 'x'
}
default:
runs[k] = 'x'
}
}
str = string(runs)
if len(str) > length {
var r2 []rune
for k := range runs {
if len(string(runs[:k])) <= length-3 {
r2 = runs[:k]
}
}
r2 = append(r2, '…')
str = string(r2)
}
return str
}
// 限制长度,并将微信不支持的字符替换成'x',能满足公众号App的字符要求
func LimitString(str string, length int) string {
runs := []rune(str)
// 单字符长度高于3的,不是一般的utf8字符,剔除掉
for k, v := range runs {
switch len([]byte(string(v))) {
case 1:
// 全部放行
case 3:
// 全部放行
default:
runs[k] = 'x'
}
}
str = string(runs)
if len(runs) > length {
var r2 = runs[:length-1]
r2 = append(r2, '…')
str = string(r2)
}
return str
}
// 生成符合微信要求随机字符
func NewRandStr(length int) string {
codes := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
codeLen := len(codes)
data := make([]byte, length)
rand.Seed(time.Now().UnixNano())
for i := 0; i < length; i++ {
idx := rand.Intn(codeLen)
data[i] = codes[idx]
}
return string(data)
}
func obj2map(obj interface{}) (p map[string]interface{}) {
vs := reflect.ValueOf(obj)
if vs.Kind() == reflect.Ptr {
vs = vs.Elem()
}
p = make(map[string]interface{})
obj2mapOnce(vs, &p)
return
}
func obj2mapOnce(vs reflect.Value, data *map[string]interface{}) {
for i := 0; i < vs.NumField(); i++ {
if vs.Type().Field(i).Anonymous {
obj2mapOnce(vs.Field(i), data)
} else {
k := vs.Type().Field(i).Tag.Get("json")
if k == "" {
k = vs.Type().Field(i).Tag.Get("xml")
if k == "xml" {
continue
}
}
if k == "sign" || k == "-" {
continue
}
k = strings.Split(k, ",")[0]
// 跳过空值
if reflect.Zero(vs.Field(i).Type()).Interface() == vs.Field(i).Interface() {
continue
}
(*data)[k] = vs.Field(i).Interface()
}
}
}
func mapSortByKey(data map[string]interface{}) string {
var keys []string
nData := ""
for k := range data {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
nData = fmt.Sprintf("%v&%v=%v", nData, k, data[k])
}
return nData[1:]
}
var certs = make(map[string]*tls.Certificate)
func parseCertificate(pemByte, keyByte []byte, password string) (cert *tls.Certificate, err error) {
if certs[password] != nil {
return certs[password], nil
}
block, restPem := pem.Decode(pemByte)
if block == nil {
err = errors.New("pem解析失败")
return
}
var c tls.Certificate
c.Certificate = append(c.Certificate, block.Bytes)
certDerBlockChain, _ := pem.Decode(restPem)
if certDerBlockChain != nil {
c.Certificate = append(c.Certificate, certDerBlockChain.Bytes)
}
// 解码pem格式的私钥
var key interface{}
keyDer, _ := pem.Decode(keyByte)
if keyDer.Type == "RSA PRIVATE KEY" {
key, err = x509.ParsePKCS1PrivateKey(keyDer.Bytes)
} else if keyDer.Type == "PRIVATE KEY" {
key, err = x509.ParsePKCS8PrivateKey(keyDer.Bytes)
}
if err != nil {
return
}
c.PrivateKey = key
cert = &c
certs[password] = cert
return
}
func rsaEncrypt(rsaPubic []byte, plain string) (cipherText string, err error) {
block, _ := pem.Decode(rsaPubic)
publicKey, err := x509.ParsePKCS1PublicKey(block.Bytes)
if err != nil {
return
}
raw, err := rsa.EncryptOAEP(sha1.New(), rand2.Reader, publicKey, []byte(plain), nil)
if err != nil {
return
}
cipherText = base64.StdEncoding.EncodeToString(raw)
return
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。