-
Notifications
You must be signed in to change notification settings - Fork 3
/
share.go
43 lines (38 loc) · 1.17 KB
/
share.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package wechatoffice
import (
"context"
"crypto/sha1"
"fmt"
"github.com/dtapps/go-library/utils/gorandom"
"io"
"time"
)
type ShareResponse struct {
AppId string `json:"app_id"`
NonceStr string `json:"nonce_str"`
Timestamp int64 `json:"timestamp"`
Url string `json:"url"`
RawString string `json:"raw_string"`
Signature string `json:"signature"`
}
type ShareResult struct {
Result ShareResponse // 结果
Err error // 错误
}
func newShareResult(result ShareResponse, err error) *ShareResult {
return &ShareResult{Result: result}
}
func (c *Client) Share(ctx context.Context, url string) *ShareResult {
c.getAccessToken(ctx)
c.config.jsapiTicket = c.GetJsapiTicket(ctx)
var response ShareResponse
response.AppId = c.GetAppId()
response.NonceStr = gorandom.Alphanumeric(32)
response.Timestamp = time.Now().Unix()
response.Url = url
response.RawString = fmt.Sprintf("jsapi_ticket=%v&noncestr=%v×tamp=%v&url=%v", c.config.jsapiTicket, response.NonceStr, response.Timestamp, response.Url)
t := sha1.New()
_, err := io.WriteString(t, response.RawString)
response.Signature = fmt.Sprintf("%x", t.Sum(nil))
return newShareResult(response, err)
}