MCPcopy Index your code
hub / github.com/TruthHun/BookStack / GetBookWXACode

Function GetBookWXACode

utils/wechat.go:50–79  ·  view source on GitHub ↗

获取书籍页面小程序码

(accessToken string, bookId int)

Source from the content-addressed store, hash-verified

48
49// 获取书籍页面小程序码
50func GetBookWXACode(accessToken string, bookId int) (tmpFile string, err error) {
51 api := fmt.Sprintf("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken)
52 data := map[string]interface{}{"page": "pages/intro/intro", "scene": fmt.Sprint(bookId), "width": 280}
53 req := httplib.Post(api).SetTimeout(10*time.Second, 10*time.Second).SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
54
55 var resp *http.Response
56 var b []byte
57
58 b, err = json.Marshal(data)
59 if err != nil {
60 return
61 }
62
63 resp, err = req.Body(b).DoRequest()
64 if err != nil {
65 return
66 }
67 defer resp.Body.Close()
68 b, err = ioutil.ReadAll(resp.Body)
69 if err != nil {
70 return
71 }
72 if contentType := strings.ToLower(resp.Header.Get("content-type")); strings.HasPrefix(contentType, "image/") {
73 tmpFile = fmt.Sprintf("book-id-%v.%v", bookId, strings.TrimPrefix(contentType, "image/"))
74 err = ioutil.WriteFile(tmpFile, b, os.ModePerm)
75 } else {
76 err = errors.New(string(b))
77 }
78 return
79}

Callers 1

CreateWechatCodeMethod · 0.92

Calls 2

stringFunction · 0.85
PostMethod · 0.80

Tested by

no test coverage detected