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.

113 lines
3.0 KiB

8 months ago
  1. package utility
  2. import (
  3. "encoding/json"
  4. "github.com/gogf/gf/v2/frame/g"
  5. "github.com/gogf/gf/v2/os/gctx"
  6. "io/ioutil"
  7. "link_homework/internal/consts"
  8. "link_homework/internal/model/dto"
  9. "net/http"
  10. "strings"
  11. )
  12. // 获取 URL
  13. func getUrl(key, hashKey string) *dto.Result {
  14. ctx := gctx.New()
  15. // 1. 从 Redis 获取 URL
  16. redisUrl, err := g.Redis().Do(ctx, "HGET", key, hashKey)
  17. if err == nil && redisUrl.String() != "" {
  18. g.Log().Infof(ctx, "从 Redis 获取到 URL: %s", redisUrl.String())
  19. return dto.SuccessWithData(redisUrl.String())
  20. }
  21. // 2. 如果 Redis 中没有,查询数据库
  22. urlResult := selectBaseUrl(hashKey)
  23. if urlResult.Code != 200 {
  24. return urlResult // 数据库查询失败,返回错误
  25. }
  26. url := urlResult.Data.(string)
  27. // 3. 将 URL 存入 Redis
  28. if _, err = g.Redis().Do(ctx, "HSET", key, hashKey, url); err != nil {
  29. g.Log().Warningf(ctx, "将数据存入 Redis 失败: %v", err)
  30. }
  31. g.Log().Infof(ctx, "将 URL 存入 Redis: %s", url)
  32. return dto.SuccessWithData(url)
  33. }
  34. // 查询数据库中的 URL
  35. func selectBaseUrl(hashKey string) *dto.Result {
  36. ctx := gctx.New()
  37. // 查询数据库
  38. value, err := g.DB("cms").Model("env").Where("`key` = ?", hashKey).Value("value")
  39. if err != nil {
  40. g.Log().Errorf(ctx, "数据库查询失败, 错误: %v, key: %s", err, hashKey)
  41. return dto.Error("数据库查询失败")
  42. }
  43. if value.IsNil() || value.String() == "" {
  44. g.Log().Errorf(ctx, "未找到对应数据, key: %s", hashKey)
  45. return dto.Error("未找到对应数据")
  46. }
  47. return dto.SuccessWithData(value.String())
  48. }
  49. // 获取 jwcode
  50. func GetJwcodeJSON(token string) *dto.Result {
  51. // 1. 获取基础 URL
  52. urlResult := getUrl(consts.URL_KEY, consts.URL_HASH_KEY)
  53. if urlResult.Code != 200 {
  54. return urlResult // 如果获取 URL 失败,直接返回错误
  55. }
  56. baseUrl := urlResult.Data.(string)
  57. // 2. 拼接完整的 URL
  58. url := baseUrl + "/api/v2/member/info"
  59. requestBody := strings.NewReader(`{"token":"` + token + `"}`)
  60. // 3. 创建 HTTP 请求
  61. req, err := http.NewRequest("POST", url, requestBody)
  62. if err != nil {
  63. return dto.Error("HTTP 请求创建失败: " + err.Error())
  64. }
  65. req.Header.Set("Content-Type", "application/json")
  66. // 4. 发送请求
  67. client := &http.Client{}
  68. resp, err := client.Do(req)
  69. if err != nil {
  70. return dto.Error("HTTP 请求失败: " + err.Error())
  71. }
  72. defer resp.Body.Close()
  73. // 5. 检查 HTTP 状态码
  74. if resp.StatusCode != http.StatusOK {
  75. return dto.Error("HTTP 状态码错误: " + resp.Status)
  76. }
  77. // 6. 读取并解析响应体
  78. body, err := ioutil.ReadAll(resp.Body)
  79. if err != nil {
  80. return dto.Error("读取响应失败: " + err.Error())
  81. }
  82. // 7. 解析 JSON 数据
  83. var jsonResponse map[string]interface{}
  84. if err = json.Unmarshal(body, &jsonResponse); err != nil {
  85. return dto.Error("解析 JSON 失败: " + err.Error())
  86. }
  87. // 8. 提取 jwcode
  88. if data, ok := jsonResponse["data"].(map[string]interface{}); ok {
  89. if jwcode, exists := data["jwcode"].(string); exists {
  90. return dto.SuccessWithData(g.Map{"jwcode": jwcode})
  91. }
  92. }
  93. return dto.Error("响应体中没有 jwcode")
  94. }