feat: add wxshare

This commit is contained in:
Rogee
2025-04-30 17:06:10 +08:00
parent af0507d0c1
commit 42c1c17c0a
24 changed files with 313 additions and 147 deletions

View File

@@ -1,14 +1,24 @@
package wechat
import "math/rand"
import (
"crypto/sha1"
"encoding/hex"
"math/rand"
)
// RandomString generate random size string
func randomString(size int) (string, error) {
func randomString(size int) string {
// generate size string [0-9a-zA-Z]
const chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
b := make([]byte, size)
for i := range b {
b[i] = chars[rand.Intn(len(chars))]
}
return string(b), nil
return string(b)
}
func hashSha1(input string) string {
h := sha1.New()
h.Write([]byte(input))
return hex.EncodeToString(h.Sum(nil))
}

View File

@@ -3,6 +3,7 @@ package wechat
import (
"crypto/sha1"
"encoding/hex"
"fmt"
"net/url"
"sort"
"strings"
@@ -287,3 +288,32 @@ func (we *Client) GetJSTicket(token string) (string, error) {
return data.Ticket, nil
}
type JsSDK struct {
Debug bool `json:"debug"`
AppID string `json:"appId"`
Timestamp int64 `json:"timestamp"`
NonceStr string `json:"nonceStr"`
Signature string `json:"signature"`
}
// GetJSTicket
func (we *Client) GetJsSDK(token, url string) (*JsSDK, error) {
sdk := &JsSDK{
Debug: false,
AppID: we.appID,
Timestamp: time.Now().Unix(),
NonceStr: randomString(16),
Signature: "",
}
// get ticket
ticket, err := we.GetJSTicket(token)
if err != nil {
return nil, errors.Wrap(err, "get wechat ticket failed")
}
input := fmt.Sprintf("jsapi_ticket=%s&noncestr=%s&timestamp=%d&url=%s", ticket, sdk.NonceStr, sdk.Timestamp, url)
sdk.Signature = hashSha1(input)
return sdk, nil
}