yidong.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package services
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "hongze/hongze_mfyx/models"
  6. "hongze/hongze_mfyx/utils"
  7. "io/ioutil"
  8. "net/http"
  9. "strings"
  10. "time"
  11. )
  12. // 获取易董token
  13. func GetYiDongToken() (token string, err error) {
  14. defer func() {
  15. if err != nil {
  16. fmt.Println(err)
  17. utils.FileLog.Info(err.Error())
  18. }
  19. }()
  20. cacheKey := utils.YI_DONG_ZHENG_TONG_YUN_TOKEN_KEY
  21. ttlTime := utils.Rc.GetRedisTTL(cacheKey)
  22. if ttlTime > 0 {
  23. token, _ = utils.Rc.RedisString(cacheKey)
  24. }
  25. if token == "" {
  26. url := utils.YiDongZhengTongYunUrl + "oauth/token?grant_type=client_credentials&response_type=token&client_id=" + utils.YiDongZhengTongYunAppid + "&client_secret=" + utils.YiDongZhengTongYunSecret
  27. method := "GET"
  28. client := &http.Client{}
  29. req, errReq := http.NewRequest(method, url, nil)
  30. if errReq != nil {
  31. err = errReq
  32. return
  33. }
  34. res, errReq := client.Do(req)
  35. if errReq != nil {
  36. err = errReq
  37. return
  38. }
  39. defer res.Body.Close()
  40. type Ydtoklen struct {
  41. Success bool `json:"success"`
  42. ExpiresIn int `json:"expires_in"`
  43. AccessToken string `json:"access_token"`
  44. }
  45. var ydtoklen Ydtoklen
  46. body, errReq := ioutil.ReadAll(res.Body)
  47. if errReq != nil {
  48. err = errReq
  49. return
  50. }
  51. errReq = json.Unmarshal(body, &ydtoklen)
  52. if errReq != nil {
  53. err = errReq
  54. return
  55. }
  56. token = ydtoklen.AccessToken
  57. utils.Rc.Put(cacheKey, ydtoklen.AccessToken, time.Second*7000)
  58. }
  59. return
  60. }
  61. // 生成用户相关信息接口 获取用户tgc
  62. func GetYiDongCreateUserInfo(user *models.WxUserItem) (userTgc string, err error) {
  63. defer func() {
  64. if err != nil {
  65. fmt.Println(err)
  66. go utils.SendAlarmMsg("生成用户相关信息接口 获取用户tgc失败:"+err.Error(), 2)
  67. utils.FileLog.Info(err.Error())
  68. }
  69. }()
  70. url := utils.YiDongHuaWeiYunUrl + "app/hz/createUserInfo"
  71. method := "POST"
  72. payload := strings.NewReader(`{
  73. "phone": "` + user.Mobile + `",
  74. "personName": "` + user.RealName + `",
  75. "companyName": "` + user.CompanyName + `",
  76. "jobName": "` + user.Position + `",
  77. "mobileCountryCode": "+` + user.CountryCode + `",
  78. "sourceFrom": "01"
  79. }`)
  80. client := &http.Client{}
  81. req, err := http.NewRequest(method, url, payload)
  82. if err != nil {
  83. fmt.Println(err)
  84. utils.FileLog.Info(err.Error())
  85. return
  86. }
  87. req.Header.Add("Content-Type", "application/json")
  88. res, err := client.Do(req)
  89. if err != nil {
  90. fmt.Println(err)
  91. utils.FileLog.Info(err.Error())
  92. return
  93. }
  94. defer res.Body.Close()
  95. var ApifoxModal *models.ApifoxModalUserTgc
  96. body, err := ioutil.ReadAll(res.Body)
  97. if err != nil {
  98. fmt.Println(err)
  99. utils.FileLog.Info(err.Error())
  100. return
  101. }
  102. err = json.Unmarshal(body, &ApifoxModal)
  103. if err != nil {
  104. fmt.Println("Getres.PublicGetDate Err:", err.Error())
  105. utils.FileLog.Info(err.Error())
  106. return
  107. }
  108. userTgc = ApifoxModal.Result.Tgc
  109. return
  110. }
  111. // GetYiDongOriginalLink 将易懂的短连接转为长链接
  112. func GetYiDongOriginalLink(item *models.ActivityDetail) (yidongLongLink string, err error) {
  113. defer func() {
  114. if err != nil {
  115. fmt.Println(err)
  116. go utils.SendAlarmMsg("将易懂的短连接转为长链接 失败:"+err.Error(), 2)
  117. utils.FileLog.Info(err.Error())
  118. }
  119. }()
  120. if utils.RunMode == "debug" {
  121. 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"
  122. return
  123. }
  124. sliceYidongActivityUrl := strings.Split(item.YidongActivityUrl, "/")
  125. var shortLink string
  126. if len(sliceYidongActivityUrl) > 1 {
  127. shortLink = sliceYidongActivityUrl[len(sliceYidongActivityUrl)-1]
  128. }
  129. var token string
  130. token, err = GetYiDongToken()
  131. if err != nil {
  132. fmt.Println(err)
  133. return
  134. }
  135. url := utils.YiDonggetOriginalLink + shortLink + "&access_token=" + token
  136. fmt.Println(url)
  137. method := "GET"
  138. client := &http.Client{}
  139. req, err := http.NewRequest(method, url, nil)
  140. if err != nil {
  141. fmt.Println(err)
  142. return
  143. }
  144. res, err := client.Do(req)
  145. if err != nil {
  146. fmt.Println(err)
  147. return
  148. }
  149. defer res.Body.Close()
  150. var ApifoxModal *models.ApifoxgetOriginalLink
  151. body, err := ioutil.ReadAll(res.Body)
  152. if err != nil {
  153. fmt.Println(err)
  154. utils.FileLog.Info(err.Error())
  155. return
  156. }
  157. err = json.Unmarshal(body, &ApifoxModal)
  158. if err != nil {
  159. fmt.Println("Getres.PublicGetDate Err:", err.Error())
  160. utils.FileLog.Info(err.Error())
  161. return
  162. }
  163. lingLink := ApifoxModal.Result
  164. //fmt.Println(lingLink)
  165. //
  166. //slicelingLink := strings.Split(lingLink, "redirect_uri=")
  167. ////var shortLink string
  168. //if len(slicelingLink) > 1 {
  169. // lingLink = slicelingLink[len(slicelingLink)-1]
  170. //}
  171. yidongLongLink = lingLink
  172. return
  173. }