1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- package Server
- import (
- "Wx_MP/conf"
- "Wx_MP/logs"
- "encoding/json"
- "fmt"
- _ "github.com/astaxie/beego/cache/redis"
- "io/ioutil"
- "net/http"
- "runtime"
- )
- type token struct {
- AccessToken string `json:"access_token"`
- ExpiresIn int `json:"expires_in"`
- }
- type sentence struct {
- Content string `json:"content"`
- Note string `json:"note"`
- Translation string `json:"translation"`
- }
- var redisCache_WxServer string
- //
- //func init() {
- //
- // logs.Println("============= Redis redisCache_WxServer =============")
- //
- // config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
- // "redis_WxServer", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
- // logs.Println(config)
- // var err error
- // redisCache_WxServer, err = cache.NewCache("redis", config)
- // if err != nil || redisCache_WxServer == nil {
- // errMsg := "failed to init redis"
- // logs.Println(errMsg, err)
- // panic(err)
- // }
- //
- // logs.Println("============= init WxServer =============")
- //
- // //logs.Println("WxTokey:", getaccesstoken())
- //
- //}
- // 获取微信accesstoken
- func GetAccessToken(is bool) string {
- if runtime.GOOS == "windows" {
- return ""
- }
- if !is {
- if len(redisCache_WxServer) > 0 {
- return redisCache_WxServer
- }
- }
- url := fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%v&secret=%v", conf.WX_appid, conf.WX_secret)
- resp, err := http.Get(url)
- logs.Println("获取微信token:", resp)
- fmt.Println("获取微信token:", resp)
- if err != nil {
- logs.Println("获取微信token失败", err)
- return ""
- }
- defer resp.Body.Close()
- body, err := ioutil.ReadAll(resp.Body)
- logs.Println("获取微信tokenBody:", body)
- fmt.Println("获取微信tokenBody:", body)
- if err != nil {
- logs.Println("微信token读取失败", err)
- return ""
- }
- token := token{}
- err = json.Unmarshal(body, &token)
- if err != nil {
- logs.Println("微信token解析json失败", err)
- return ""
- }
- logs.Println("token:", token)
- fmt.Println("token:", token)
- redisCache_WxServer = token.AccessToken
- return token.AccessToken
- }
|