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
115 lines
3.1 KiB
package utility
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"github.com/gogf/gf/v2/os/gctx"
|
|
"io/ioutil"
|
|
"link_homework/internal/consts"
|
|
"link_homework/internal/model/dto"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
// 获取 URL
|
|
func getUrl(key, hashKey string) *dto.Result {
|
|
ctx := gctx.New()
|
|
|
|
// 1. 从 Redis 获取 URL
|
|
redisUrl, err := g.Redis().Do(ctx, "HGET", key, hashKey)
|
|
if err == nil && redisUrl.String() != "" {
|
|
g.Log().Infof(ctx, "从 Redis 获取到 URL: %s", redisUrl.String())
|
|
return dto.SuccessWithData(redisUrl.String())
|
|
}
|
|
|
|
// 2. 如果 Redis 中没有,查询数据库
|
|
urlResult := selectBaseUrl(hashKey)
|
|
if urlResult.Code != 200 {
|
|
return urlResult // 数据库查询失败,返回错误
|
|
}
|
|
url := urlResult.Data.(string)
|
|
|
|
// 3. 将 URL 存入 Redis
|
|
if _, err = g.Redis().Do(ctx, "HSET", key, hashKey, url); err != nil {
|
|
g.Log().Warningf(ctx, "将数据存入 Redis 失败: %v", err)
|
|
}
|
|
|
|
g.Log().Infof(ctx, "将 URL 存入 Redis: %s", url)
|
|
return dto.SuccessWithData(url)
|
|
}
|
|
|
|
// 查询数据库中的 URL
|
|
func selectBaseUrl(hashKey string) *dto.Result {
|
|
ctx := gctx.New()
|
|
|
|
// 查询数据库
|
|
value, err := g.DB("cms").Model("env").Where("`key` = ?", hashKey).Value("value")
|
|
if err != nil {
|
|
g.Log().Errorf(ctx, "数据库查询失败, 错误: %v, key: %s", err, hashKey)
|
|
return dto.Error("数据库查询失败")
|
|
}
|
|
|
|
if value.IsNil() || value.String() == "" {
|
|
g.Log().Errorf(ctx, "未找到对应数据, key: %s", hashKey)
|
|
return dto.Error("未找到对应数据")
|
|
}
|
|
|
|
return dto.SuccessWithData(value.String())
|
|
}
|
|
|
|
// 获取 jwcode
|
|
func GetJwcodeJSON(token string) (int, error) { // 返回类型改为 int
|
|
// 1. 获取基础 URL
|
|
urlResult := getUrl(consts.URL_KEY, consts.URL_HASH_KEY)
|
|
if urlResult.Code != 200 {
|
|
return 0, errors.New("获取基础 URL 失败: " + urlResult.Message)
|
|
}
|
|
baseUrl := urlResult.Data.(string)
|
|
|
|
// 2. 拼接完整的 URL
|
|
url := baseUrl + "/api/v2/member/info"
|
|
requestBody := strings.NewReader(`{"token":"` + token + `"}`)
|
|
|
|
// 3. 创建 HTTP 请求
|
|
req, err := http.NewRequest("POST", url, requestBody)
|
|
if err != nil {
|
|
return 0, fmt.Errorf("HTTP 请求创建失败: %v", err)
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
// 4. 发送请求
|
|
client := &http.Client{}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return 0, fmt.Errorf("HTTP 请求失败: %v", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
// 5. 检查 HTTP 状态码
|
|
if resp.StatusCode != http.StatusOK {
|
|
return 0, fmt.Errorf("HTTP 状态码错误: %v", resp.Status)
|
|
}
|
|
|
|
// 6. 读取并解析响应体
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return 0, fmt.Errorf("读取响应失败: %v", err)
|
|
}
|
|
|
|
// 7. 解析 JSON 数据
|
|
var jsonResponse map[string]interface{}
|
|
if err = json.Unmarshal(body, &jsonResponse); err != nil {
|
|
return 0, fmt.Errorf("解析 JSON 失败: %v", err)
|
|
}
|
|
|
|
// 8. 提取并直接返回 jwcode
|
|
if data, ok := jsonResponse["data"].(map[string]interface{}); ok {
|
|
if jwcode, exists := data["jwcode"].(float64); exists { // 确保将 jwcode 作为数字解析
|
|
return int(jwcode), nil // 转换为整数并返回
|
|
}
|
|
}
|
|
|
|
return 0, errors.New("响应体中没有 jwcode")
|
|
}
|