package services import ( "encoding/json" "fmt" "hongze/hongze_mfyx/models" "hongze/hongze_mfyx/utils" "io/ioutil" "net/http" "strings" "time" ) // 获取易董token func GetYiDongToken() (token string, err error) { defer func() { if err != nil { fmt.Println(err) utils.FileLog.Info(err.Error()) } }() cacheKey := utils.YI_DONG_ZHENG_TONG_YUN_TOKEN_KEY ttlTime := utils.Rc.GetRedisTTL(cacheKey) if ttlTime > 0 { token, _ = utils.Rc.RedisString(cacheKey) } if token == "" { url := utils.YiDongZhengTongYunUrl + "oauth/token?grant_type=client_credentials&response_type=token&client_id=" + utils.YiDongZhengTongYunAppid + "&client_secret=" + utils.YiDongZhengTongYunSecret method := "GET" client := &http.Client{} req, errReq := http.NewRequest(method, url, nil) if errReq != nil { err = errReq return } res, errReq := client.Do(req) if errReq != nil { err = errReq return } defer res.Body.Close() type Ydtoklen struct { Success bool `json:"success"` ExpiresIn int `json:"expires_in"` AccessToken string `json:"access_token"` } var ydtoklen Ydtoklen body, errReq := ioutil.ReadAll(res.Body) if errReq != nil { err = errReq return } errReq = json.Unmarshal(body, &ydtoklen) if errReq != nil { err = errReq return } token = ydtoklen.AccessToken utils.Rc.Put(cacheKey, ydtoklen.AccessToken, time.Second*7000) } return } // 生成用户相关信息接口 获取用户tgc func GetYiDongCreateUserInfo(user *models.WxUserItem) (userTgc string, err error) { defer func() { if err != nil { fmt.Println(err) go utils.SendAlarmMsg("生成用户相关信息接口 获取用户tgc失败:"+err.Error(), 2) utils.FileLog.Info(err.Error()) } }() url := utils.YiDongHuaWeiYunUrl + "app/hz/createUserInfo" method := "POST" payload := strings.NewReader(`{ "phone": "` + user.Mobile + `", "personName": "` + user.RealName + `", "companyName": "` + user.CompanyName + `", "jobName": "` + user.Position + `", "mobileCountryCode": "+` + user.CountryCode + `", "sourceFrom": "01" }`) client := &http.Client{} req, err := http.NewRequest(method, url, payload) if err != nil { fmt.Println(err) utils.FileLog.Info(err.Error()) return } req.Header.Add("Content-Type", "application/json") res, err := client.Do(req) if err != nil { fmt.Println(err) utils.FileLog.Info(err.Error()) return } defer res.Body.Close() var ApifoxModal *models.ApifoxModalUserTgc body, err := ioutil.ReadAll(res.Body) if err != nil { fmt.Println(err) utils.FileLog.Info(err.Error()) return } err = json.Unmarshal(body, &ApifoxModal) if err != nil { fmt.Println("Getres.PublicGetDate Err:", err.Error()) utils.FileLog.Info(err.Error()) return } userTgc = ApifoxModal.Result.Tgc return } // GetYiDongOriginalLink 将易懂的短连接转为长链接 func GetYiDongOriginalLink(item *models.ActivityDetail) (yidongLongLink string, err error) { defer func() { if err != nil { fmt.Println(err) go utils.SendAlarmMsg("将易懂的短连接转为长链接 失败:"+err.Error(), 2) utils.FileLog.Info(err.Error()) } }() if utils.RunMode == "debug" { yidongLongLink = "https://services.easy-board.com.cn/oauth/authorize?client_id=6032b613f1dea1d0&response_type=token&scope=read&grant_type=implicit&redirect_uri=https://mobile.valueonline.cn/h5ActivitySign202104/%23/activeIndex?activtyId=1180584154920759717%26isApp=0%26source=11%26fromHz=true%26tgc=TGT-680-DNAbdaxZwRmbtI7R3MRGspvwHYArzyoeXbQ" return } sliceYidongActivityUrl := strings.Split(item.YidongActivityUrl, "/") var shortLink string if len(sliceYidongActivityUrl) > 1 { shortLink = sliceYidongActivityUrl[len(sliceYidongActivityUrl)-1] } var token string token, err = GetYiDongToken() if err != nil { fmt.Println(err) return } url := utils.YiDonggetOriginalLink + shortLink + "&access_token=" + token fmt.Println(url) method := "GET" client := &http.Client{} req, err := http.NewRequest(method, url, nil) if err != nil { fmt.Println(err) return } res, err := client.Do(req) if err != nil { fmt.Println(err) return } defer res.Body.Close() var ApifoxModal *models.ApifoxgetOriginalLink body, err := ioutil.ReadAll(res.Body) if err != nil { fmt.Println(err) utils.FileLog.Info(err.Error()) return } err = json.Unmarshal(body, &ApifoxModal) if err != nil { fmt.Println("Getres.PublicGetDate Err:", err.Error()) utils.FileLog.Info(err.Error()) return } lingLink := ApifoxModal.Result //fmt.Println(lingLink) // //slicelingLink := strings.Split(lingLink, "redirect_uri=") ////var shortLink string //if len(slicelingLink) > 1 { // lingLink = slicelingLink[len(slicelingLink)-1] //} yidongLongLink = lingLink return }