yidong.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. package services
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "hongze/hongze_clpt/models"
  6. "hongze/hongze_clpt/utils"
  7. "io/ioutil"
  8. "net/http"
  9. "strings"
  10. "time"
  11. )
  12. // GetActivityTypeIdWithYiDong 根据易董同步过来的活动类型,自动匹配行业
  13. func GetActivityTypeIdWithYiDong(industrySwName string) (chartPermissionId int) {
  14. mapIndustrySwName := map[string]int{
  15. "家用电器": utils.XIAO_FEI_ID,
  16. "纺织服饰": utils.XIAO_FEI_ID,
  17. "休闲服务": utils.XIAO_FEI_ID,
  18. "食品饮料": utils.XIAO_FEI_ID,
  19. "美容护理": utils.XIAO_FEI_ID,
  20. //消费
  21. "汽车": utils.ZHI_ZAO_ID,
  22. "机械设备": utils.ZHI_ZAO_ID,
  23. "电力设备": utils.ZHI_ZAO_ID,
  24. //智造
  25. "电子": utils.KE_JI_ID,
  26. "计算机": utils.KE_JI_ID,
  27. "通信": utils.KE_JI_ID,
  28. //科技
  29. "医药生物": utils.YI_YAO_ID,
  30. //医药
  31. "策略": utils.CE_LUE_ID,
  32. //策略
  33. "非银金融": utils.CHART_PERMISSION_ID_YANXUAN,
  34. "建筑装饰": utils.CHART_PERMISSION_ID_YANXUAN,
  35. "基础化工": utils.CHART_PERMISSION_ID_YANXUAN,
  36. "农林牧渔": utils.CHART_PERMISSION_ID_YANXUAN,
  37. "传媒": utils.CHART_PERMISSION_ID_YANXUAN,
  38. "公用事业": utils.CHART_PERMISSION_ID_YANXUAN,
  39. "建筑材料": utils.CHART_PERMISSION_ID_YANXUAN,
  40. "房地产": utils.CHART_PERMISSION_ID_YANXUAN,
  41. "采掘": utils.CHART_PERMISSION_ID_YANXUAN,
  42. "轻工制造": utils.CHART_PERMISSION_ID_YANXUAN,
  43. "钢铁": utils.CHART_PERMISSION_ID_YANXUAN,
  44. "交通运输": utils.CHART_PERMISSION_ID_YANXUAN,
  45. "国防军工": utils.CHART_PERMISSION_ID_YANXUAN,
  46. "银行": utils.CHART_PERMISSION_ID_YANXUAN,
  47. "有色金属": utils.CHART_PERMISSION_ID_YANXUAN,
  48. "综合": utils.CHART_PERMISSION_ID_YANXUAN,
  49. "商业贸易": utils.CHART_PERMISSION_ID_YANXUAN,
  50. "宏观": utils.CHART_PERMISSION_ID_YANXUAN,
  51. "金融工程": utils.CHART_PERMISSION_ID_YANXUAN,
  52. "商贸零售": utils.CHART_PERMISSION_ID_YANXUAN,
  53. "社会服务": utils.CHART_PERMISSION_ID_YANXUAN,
  54. "煤炭": utils.CHART_PERMISSION_ID_YANXUAN,
  55. "石油石化": utils.CHART_PERMISSION_ID_YANXUAN,
  56. "环保": utils.CHART_PERMISSION_ID_YANXUAN,
  57. //研选
  58. }
  59. chartPermissionId = mapIndustrySwName[industrySwName]
  60. return
  61. }
  62. // GetActivityTypeIdWithYiDong 根据易董同步过来的活动类型,自动匹配行业
  63. func GetActivityTypeNameWithYiDong(industrySwName string) (chartPermissionName string) {
  64. mapIndustrySwName := map[string]string{
  65. "家用电器": utils.XIAO_FEI_NAME,
  66. "纺织服饰": utils.XIAO_FEI_NAME,
  67. "休闲服务": utils.XIAO_FEI_NAME,
  68. "食品饮料": utils.XIAO_FEI_NAME,
  69. "美容护理": utils.XIAO_FEI_NAME,
  70. //消费
  71. "汽车": utils.ZHI_ZAO_NAME,
  72. "机械设备": utils.ZHI_ZAO_NAME,
  73. "电力设备": utils.ZHI_ZAO_NAME,
  74. //智造
  75. "电子": utils.KE_JI_NAME,
  76. "计算机": utils.KE_JI_NAME,
  77. "通信": utils.KE_JI_NAME,
  78. //科技
  79. "医药生物": utils.YI_YAO_NAME,
  80. //医药
  81. "策略": utils.CE_LUE_NAME,
  82. //策略
  83. "非银金融": utils.CHART_PERMISSION_NAME_MF_YANXUAN,
  84. "建筑装饰": utils.CHART_PERMISSION_NAME_MF_YANXUAN,
  85. "基础化工": utils.CHART_PERMISSION_NAME_MF_YANXUAN,
  86. "农林牧渔": utils.CHART_PERMISSION_NAME_MF_YANXUAN,
  87. "传媒": utils.CHART_PERMISSION_NAME_MF_YANXUAN,
  88. "公用事业": utils.CHART_PERMISSION_NAME_MF_YANXUAN,
  89. "建筑材料": utils.CHART_PERMISSION_NAME_MF_YANXUAN,
  90. "房地产": utils.CHART_PERMISSION_NAME_MF_YANXUAN,
  91. "采掘": utils.CHART_PERMISSION_NAME_MF_YANXUAN,
  92. "轻工制造": utils.CHART_PERMISSION_NAME_MF_YANXUAN,
  93. "钢铁": utils.CHART_PERMISSION_NAME_MF_YANXUAN,
  94. "交通运输": utils.CHART_PERMISSION_NAME_MF_YANXUAN,
  95. "国防军工": utils.CHART_PERMISSION_NAME_MF_YANXUAN,
  96. "银行": utils.CHART_PERMISSION_NAME_MF_YANXUAN,
  97. "有色金属": utils.CHART_PERMISSION_NAME_MF_YANXUAN,
  98. "综合": utils.CHART_PERMISSION_NAME_MF_YANXUAN,
  99. "商业贸易": utils.CHART_PERMISSION_NAME_MF_YANXUAN,
  100. "宏观": utils.CHART_PERMISSION_NAME_MF_YANXUAN,
  101. "金融工程": utils.CHART_PERMISSION_NAME_MF_YANXUAN,
  102. "商贸零售": utils.CHART_PERMISSION_NAME_MF_YANXUAN,
  103. "社会服务": utils.CHART_PERMISSION_NAME_MF_YANXUAN,
  104. "煤炭": utils.CHART_PERMISSION_NAME_MF_YANXUAN,
  105. "石油石化": utils.CHART_PERMISSION_NAME_MF_YANXUAN,
  106. "环保": utils.CHART_PERMISSION_NAME_MF_YANXUAN,
  107. //研选
  108. }
  109. chartPermissionName = mapIndustrySwName[industrySwName]
  110. return
  111. }
  112. // 获取易董token
  113. func GetYiDongToken() (token string, err error) {
  114. defer func() {
  115. if err != nil {
  116. fmt.Println(err)
  117. utils.FileLog.Info(err.Error())
  118. }
  119. }()
  120. cacheKey := utils.YD_TOKEN
  121. ttlTime := utils.Rc.GetRedisTTL(cacheKey)
  122. if ttlTime > 0 {
  123. token, _ = utils.Rc.RedisString(cacheKey)
  124. }
  125. if token == "" {
  126. url := utils.YiDongZhengTongYunUrl + "oauth/token?grant_type=client_credentials&response_type=token&client_id=" + utils.YiDongZhengTongYunAppid + "&client_secret=" + utils.YiDongZhengTongYunSecret
  127. method := "GET"
  128. client := &http.Client{}
  129. req, errReq := http.NewRequest(method, url, nil)
  130. if errReq != nil {
  131. err = errReq
  132. return
  133. }
  134. res, errReq := client.Do(req)
  135. if errReq != nil {
  136. err = errReq
  137. return
  138. }
  139. defer res.Body.Close()
  140. type Ydtoklen struct {
  141. Success bool `json:"success"`
  142. ExpiresIn int `json:"expires_in"`
  143. AccessToken string `json:"access_token"`
  144. }
  145. var ydtoklen Ydtoklen
  146. body, errReq := ioutil.ReadAll(res.Body)
  147. if errReq != nil {
  148. err = errReq
  149. return
  150. }
  151. errReq = json.Unmarshal(body, &ydtoklen)
  152. if errReq != nil {
  153. err = errReq
  154. return
  155. }
  156. token = ydtoklen.AccessToken
  157. utils.Rc.Put(cacheKey, ydtoklen.AccessToken, time.Second*7000)
  158. }
  159. return
  160. }
  161. // 生成用户相关信息接口 获取用户tgc
  162. func GetYiDongCreateUserInfo(user *models.WxUserItem) (userTgc string, err error) {
  163. defer func() {
  164. if err != nil {
  165. fmt.Println(err)
  166. //易董测试环境有问题,先屏蔽一段时间
  167. if utils.RunMode == "" {
  168. go utils.SendAlarmMsg("生成用户相关信息接口 获取用户tgc失败:"+err.Error(), 2)
  169. }
  170. utils.FileLog.Info(err.Error())
  171. }
  172. }()
  173. url := utils.YiDongHuaWeiYunUrl + "app/hz/createUserInfo"
  174. method := "POST"
  175. payload := strings.NewReader(`{
  176. "phone": "` + user.Mobile + `",
  177. "personName": "` + user.RealName + `",
  178. "companyName": "` + user.CompanyName + `",
  179. "jobName": "` + user.Position + `",
  180. "mobileCountryCode": "+` + user.CountryCode + `",
  181. "sourceFrom": "01"
  182. }`)
  183. client := &http.Client{}
  184. req, err := http.NewRequest(method, url, payload)
  185. if err != nil {
  186. fmt.Println(err)
  187. utils.FileLog.Info(err.Error())
  188. return
  189. }
  190. req.Header.Add("Content-Type", "application/json")
  191. res, err := client.Do(req)
  192. if err != nil {
  193. fmt.Println(err)
  194. utils.FileLog.Info(err.Error())
  195. return
  196. }
  197. defer res.Body.Close()
  198. var ApifoxModal *models.ApifoxModalUserTgc
  199. body, err := ioutil.ReadAll(res.Body)
  200. if err != nil {
  201. fmt.Println(err)
  202. utils.FileLog.Info(err.Error())
  203. return
  204. }
  205. err = json.Unmarshal(body, &ApifoxModal)
  206. if err != nil {
  207. fmt.Println("Getres.PublicGetDate Err:", err.Error())
  208. utils.FileLog.Info(err.Error())
  209. return
  210. }
  211. userTgc = ApifoxModal.Result.Tgc
  212. return
  213. }
  214. // GetYiDongOriginalLink 将易懂的短连接转为长链接
  215. func GetYiDongOriginalLink(item *models.ActivityDetail) (yidongLongLink string, err error) {
  216. defer func() {
  217. if err != nil {
  218. fmt.Println(err)
  219. go utils.SendAlarmMsg("将易懂的短连接转为长链接 失败:"+err.Error(), 2)
  220. utils.FileLog.Info(err.Error())
  221. }
  222. }()
  223. sliceYidongActivityUrl := strings.Split(item.YidongActivityUrl, "/")
  224. var shortLink string
  225. if len(sliceYidongActivityUrl) > 1 {
  226. shortLink = sliceYidongActivityUrl[len(sliceYidongActivityUrl)-1]
  227. }
  228. var token string
  229. token, err = GetYiDongToken()
  230. if err != nil {
  231. fmt.Println(err)
  232. return
  233. }
  234. url := utils.YiDonggetOriginalLink + shortLink + "&access_token=" + token
  235. fmt.Println(url)
  236. method := "GET"
  237. client := &http.Client{}
  238. req, err := http.NewRequest(method, url, nil)
  239. if err != nil {
  240. fmt.Println(err)
  241. return
  242. }
  243. res, err := client.Do(req)
  244. if err != nil {
  245. fmt.Println(err)
  246. return
  247. }
  248. defer res.Body.Close()
  249. var ApifoxModal *models.ApifoxgetOriginalLink
  250. body, err := ioutil.ReadAll(res.Body)
  251. if err != nil {
  252. fmt.Println(err)
  253. utils.FileLog.Info(err.Error())
  254. return
  255. }
  256. err = json.Unmarshal(body, &ApifoxModal)
  257. if err != nil {
  258. fmt.Println("Getres.PublicGetDate Err:", err.Error())
  259. utils.FileLog.Info(err.Error())
  260. return
  261. }
  262. lingLink := ApifoxModal.Result
  263. //fmt.Println(lingLink)
  264. //
  265. //slicelingLink := strings.Split(lingLink, "redirect_uri=")
  266. ////var shortLink string
  267. //if len(slicelingLink) > 1 {
  268. // lingLink = slicelingLink[len(slicelingLink)-1]
  269. //}
  270. yidongLongLink = lingLink
  271. return
  272. }