package jobcron import ( "encoding/json" "fmt" "time" "github.com/silenceper/wechat/util" "SCRM/service" "SCRM/service/wechat_service" "SCRM/utils" "github.com/astaxie/beego" "github.com/robfig/cron" ) var openWechatCron *cron.Cron func init() { openWechatCron = cron.New() openWechatCron.AddFunc("@every 1h30m", func() { go func() { RequestComponentAccessToken() }() }) openWechatCron.AddFunc("@every 1h30m", func() { go func() { RequestMpWechatAccessToken() }() }) } func StartOpenWechatCron() { openWechatCron.Start() } var ( token = beego.AppConfig.String("openwechattoken") appID = beego.AppConfig.String("openwechatappid") encodingAESKey = beego.AppConfig.String("openwechatencodingaeskey") appSecret = beego.AppConfig.String("openwechatsecret") ) type ReqComponentAccessStruct struct { ComponentAppid string `json:"component_appid"` ComponentAppSecret string `json:"component_appsecret"` ComponentVerifyTicket string `json:"component_verify_ticket"` } //请求第三方平台的accessToken func RequestComponentAccessToken() { utils.InfoLog("Request ComponentAccessToken Start...") redisClient := service.RedisClient() defer redisClient.Close() componentVerifyTicket, err := redisClient.Get("sgj_patient:ComponentVerifyTicket").Result() if err != nil { utils.ErrorLog("读取ComponentVerifyTicket失败:%s", err) return } //假设 拿到了 component_verify_ticket ,开始通过接口 拿 component_access_token //获取第三方平台component_access_token,POST数据 var ReqComponentAccess ReqComponentAccessStruct ReqComponentAccess.ComponentAppid = appID ReqComponentAccess.ComponentAppSecret = appSecret ReqComponentAccess.ComponentVerifyTicket = componentVerifyTicket fmt.Println(ReqComponentAccess) //创建请求 uri := fmt.Sprintf("%s", "https://api.weixin.qq.com/cgi-bin/component/api_component_token") var responseBytes []byte responseBytes, err = util.PostJSON(uri, ReqComponentAccess) if err != nil { utils.ErrorLog("创建请求失败:%s", err) return } var res wechat_service.MPResult err = json.Unmarshal(responseBytes, &res) if err != nil { utils.ErrorLog("MPResult error:%s", err) return } if res.ErrCode > 0 { fmt.Println(res.ErrMsg) return } //body 是上面请求返回的json数据 //{"component_access_token":"61W3mEpU66027wgNZ_MhGHNQDHnFATkDa9-2llqrMBjUwxRSNPbVsMmyD-yq8wZETSoE5NQgecigDrSHkPtIYA", "expires_in":7200} var access_token wechat_service.ComponentAccessToken // 将body的json解析成 access_token ComponentAccessToken err = json.Unmarshal(responseBytes, &access_token) if err != nil { utils.ErrorLog("Unmarshal json error:%s", err) return } err = redisClient.Set("sgj_patient:component_access_token", access_token.ComponentAccessToken, time.Second*7000).Err() fmt.Println("err错误是哦",err) if err != nil { utils.ErrorLog("redis set failed:%s", err) return } componentID, err := beego.AppConfig.Int64("openwechatcomponentid") if err != nil { utils.ErrorLog("get component id failed: %s", err) return } info := make(map[string]interface{}, 0) info["ComponentAccessToken"] = access_token.ComponentAccessToken info["UpdatedTime"] = time.Now().Unix() info["ComponentStatus"] = 1 err = wechat_service.SaveWechatComponentInfoByMap(componentID, info) utils.InfoLog("Request ComponentAccessToken Finish.") } type ReqRefreshTokenStruct struct { ComponentAppid string `json:"component_appid"` AuthorizerAppid string `json:"authorizer_appid"` AuthorizerRefreshToken string `json:"authorizer_refresh_token"` } func RequestMpWechatAccessToken() { utils.InfoLog("Request MpWechatAccessToken start...") mps, err := wechat_service.GetAuthorizations() if err != nil { utils.ErrorLog("get mps error: %s", err) return } redisClient := service.RedisClient() defer redisClient.Close() componentAccessToken, err := redisClient.Get("sgj_patient:component_access_token").Result() if err != nil { utils.ErrorLog("get component_access_token error: %s", err) return } for _, item := range mps { var ReqRefreshToken ReqRefreshTokenStruct ReqRefreshToken.ComponentAppid = appID ReqRefreshToken.AuthorizerAppid = item.AuthorizerAppid ReqRefreshToken.AuthorizerRefreshToken = item.AuthorizerRefreshToken uri := fmt.Sprintf("%s?component_access_token=%s", "https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token", componentAccessToken) var responseBytes []byte responseBytes, err = util.PostJSON(uri, ReqRefreshToken) if err != nil { utils.ErrorLog("error: %s", err) continue } var res wechat_service.MPResult err = json.Unmarshal(responseBytes, &res) if err != nil { utils.ErrorLog("%s, error: %s", item.AuthorizerAppid, err) continue } if res.ErrCode > 0 { utils.ErrorLog("%s, error: %s", item.AuthorizerAppid, res.ErrMsg) continue } var accessToken wechat_service.RefreshToken err = json.Unmarshal(responseBytes, &accessToken) if err != nil { utils.ErrorLog("%s, Unmarshal error: %s", item.AuthorizerAppid, err) continue } info := *item info.AuthorizerAccessToken = accessToken.AuthorizerAccessToken info.AuthorizerRefreshToken = accessToken.AuthorizerRefreshToken info.UpdatedTime = time.Now().Unix() err = wechat_service.SaveAuthorizationInfo(&info) if err != nil { utils.ErrorLog("%s,Save error: %s", item.AuthorizerAppid, err) continue } atKey := fmt.Sprintf("Patient:AccessToken[%s]", item.AuthorizerAppid) _, err = redisClient.Set(atKey, accessToken.AuthorizerAccessToken, 0).Result() if err != nil { utils.ErrorLog("%s,Save redis error: %s", item.AuthorizerAppid, err) continue } } utils.InfoLog("Request MpWechatAccessToken END") }