获取access_token
()
| 27 | |
| 28 | // 获取access_token |
| 29 | func GetAccessToken() (token *WechatToken) { |
| 30 | now := time.Now().Unix() |
| 31 | if now < wechatToken.ExpiresIn-600 { //提前10分钟失效 |
| 32 | return wechatToken |
| 33 | } |
| 34 | appId := beego.AppConfig.String("appId") |
| 35 | appSecret := beego.AppConfig.String("appSecret") |
| 36 | api := fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%v&secret=%v", appId, appSecret) |
| 37 | req := httplib.Get(api).SetTimeout(10*time.Second, 10*time.Second).SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) |
| 38 | resp, err := req.String() |
| 39 | if err != nil { |
| 40 | beego.Error(err.Error()) |
| 41 | } |
| 42 | token = &WechatToken{} |
| 43 | json.Unmarshal([]byte(resp), token) |
| 44 | token.ExpiresIn = now + token.ExpiresIn |
| 45 | wechatToken = token |
| 46 | return |
| 47 | } |
| 48 | |
| 49 | // 获取书籍页面小程序码 |
| 50 | func GetBookWXACode(accessToken string, bookId int) (tmpFile string, err error) { |