You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
3.5 KiB

6 months ago
  1. package utility
  2. import (
  3. "MangheGo/internal/consts"
  4. "MangheGo/internal/model/dto"
  5. "encoding/json"
  6. "errors"
  7. "fmt"
  8. "github.com/gogf/gf/v2/frame/g"
  9. "github.com/gogf/gf/v2/os/gctx"
  10. "io/ioutil"
  11. "net/http"
  12. "strconv"
  13. "strings"
  14. )
  15. // 查询数据库中的URL
  16. func selectBaseUrl(hashKey string) *dto.Result {
  17. //查询数据库
  18. //env表中存储着一些所需要的地址,值等,可能是需要查出来存放到redis中(value字段中是值)
  19. //key为"HLJW_URL"时,value为:http://39.101.133.168:8828/hljw,为测试环境时前置地址
  20. value, err := g.DB().Model("env").Where("`key` = ?", hashKey).Value("value")
  21. if err != nil {
  22. return dto.Error("数据库env查询失败")
  23. }
  24. if value.IsNil() || value.String() == "" {
  25. return dto.Error("未找到对应数据")
  26. }
  27. return dto.SuccessWithData(value.String())
  28. }
  29. // 获取URL
  30. func getUrl(key, hashKey string) *dto.Result {
  31. ctx := gctx.New()
  32. //测试环境: http://39.101.133.168:8828/hljw/api/v2/member/info
  33. //正式环境: http://api.homilychart.com:8828/hljw/api/v2/member/info
  34. //1. 从Redis获取URL
  35. redisUrl, err := g.Redis().Do(ctx, "HGET", key, hashKey)
  36. if err == nil && redisUrl.String() != "" {
  37. return dto.SuccessWithMsgAndData(fmt.Sprintf("从Redis获取URL: %s", redisUrl.String()), redisUrl.String())
  38. }
  39. //2. 如果Redis中没有, 查询数据库
  40. urlResult := selectBaseUrl(hashKey)
  41. if urlResult.Code != 200 {
  42. return urlResult
  43. }
  44. url := urlResult.Data.(string)
  45. //3. 将URL存入Redis
  46. _, err = g.Redis().Do(ctx, "HSET", key, hashKey, url)
  47. if err != nil {
  48. return dto.Error("将数据存入Redis失败")
  49. }
  50. return dto.SuccessWithData(url)
  51. }
  52. // 获取jwcode
  53. func GetJwcodeJSON(token string) (int, error) {
  54. //获取基础URL 用于解析token的接口地址
  55. urlResult := getUrl(consts.URL_KEY, consts.URL_HASH_KEY)
  56. if urlResult.Code != 200 {
  57. return 0, errors.New(fmt.Sprintf("获取基础URL失败: %s", urlResult.Message))
  58. }
  59. baseUrl := urlResult.Data.(string)
  60. //拼接完整的URL, 用于发送请求并解析token
  61. url := baseUrl + "/api/v2/member/info"
  62. requestBody := strings.NewReader(`{"token":"` + token + `"}`)
  63. //创建HTTP请求(POST)
  64. req, err := http.NewRequest("POST", url, requestBody)
  65. if err != nil {
  66. return 0, fmt.Errorf("创建HTTP请求失败: %w", err)
  67. }
  68. //设置请求头
  69. req.Header.Set("Content-Type", "application/json")
  70. //发送请求
  71. client := &http.Client{}
  72. resp, err := client.Do(req)
  73. if err != nil {
  74. return 0, fmt.Errorf("发送HTTP请求失败: %w", err) //
  75. }
  76. //延时关闭响应体
  77. defer resp.Body.Close()
  78. //检查HTTP状态码
  79. if resp.StatusCode != http.StatusOK {
  80. return 0, fmt.Errorf("HTTP请求失败, 状态码: %d", resp.StatusCode)
  81. }
  82. //读取并解析响应体
  83. body, err := ioutil.ReadAll(resp.Body)
  84. if err != nil {
  85. return 0, fmt.Errorf("读取响应体失败: %w", err)
  86. }
  87. //解析JSON数据
  88. var jsonResponse map[string]interface{}
  89. err = json.Unmarshal(body, &jsonResponse)
  90. if err != nil {
  91. return 0, fmt.Errorf("解析JSON数据失败: %w", err)
  92. }
  93. //提取data节点
  94. data, ok := jsonResponse["data"].(map[string]interface{})
  95. if !ok {
  96. return 0, errors.New("响应体中没有有效的data节点")
  97. }
  98. //提取jwcode字段节点并转换为整数
  99. jwcode, ok := data["jwcode"].(string) //首先尝试解析为字符串
  100. if ok {
  101. jwcodeInt, err := strconv.Atoi(jwcode) //将字符串转换为整数
  102. if err != nil {
  103. return 0, fmt.Errorf("解析jwcode字段为整数失败: %w", err)
  104. }
  105. return jwcodeInt, nil
  106. }
  107. //如果jwcode不是字符串,返回错误日志
  108. return 0, errors.New("响应体中没有有效的jwcode字段")
  109. }