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.

115 lines
3.1 KiB

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