Browse Source

合并dhy分支内容

修改jwcode为int类型
dev_ljk
dhy 7 months ago
parent
commit
08bdda001a
  1. 20
      link_homework/utility/utility.go

20
link_homework/utility/utility.go

@ -60,11 +60,11 @@ func selectBaseUrl(hashKey string) *dto.Result {
} }
// 获取 jwcode // 获取 jwcode
func GetJwcodeJSON(token string) (string, error) {
func GetJwcodeJSON(token string) (int, error) { // 返回类型改为 int
// 1. 获取基础 URL // 1. 获取基础 URL
urlResult := getUrl(consts.URL_KEY, consts.URL_HASH_KEY) urlResult := getUrl(consts.URL_KEY, consts.URL_HASH_KEY)
if urlResult.Code != 200 { if urlResult.Code != 200 {
return "", errors.New("获取基础 URL 失败: " + urlResult.Message)
return 0, errors.New("获取基础 URL 失败: " + urlResult.Message)
} }
baseUrl := urlResult.Data.(string) baseUrl := urlResult.Data.(string)
@ -75,7 +75,7 @@ func GetJwcodeJSON(token string) (string, error) {
// 3. 创建 HTTP 请求 // 3. 创建 HTTP 请求
req, err := http.NewRequest("POST", url, requestBody) req, err := http.NewRequest("POST", url, requestBody)
if err != nil { if err != nil {
return "", fmt.Errorf("HTTP 请求创建失败: %v", err)
return 0, fmt.Errorf("HTTP 请求创建失败: %v", err)
} }
req.Header.Set("Content-Type", "application/json") req.Header.Set("Content-Type", "application/json")
@ -83,33 +83,33 @@ func GetJwcodeJSON(token string) (string, error) {
client := &http.Client{} client := &http.Client{}
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
return "", fmt.Errorf("HTTP 请求失败: %v", err)
return 0, fmt.Errorf("HTTP 请求失败: %v", err)
} }
defer resp.Body.Close() defer resp.Body.Close()
// 5. 检查 HTTP 状态码 // 5. 检查 HTTP 状态码
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("HTTP 状态码错误: %v", resp.Status)
return 0, fmt.Errorf("HTTP 状态码错误: %v", resp.Status)
} }
// 6. 读取并解析响应体 // 6. 读取并解析响应体
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
return "", fmt.Errorf("读取响应失败: %v", err)
return 0, fmt.Errorf("读取响应失败: %v", err)
} }
// 7. 解析 JSON 数据 // 7. 解析 JSON 数据
var jsonResponse map[string]interface{} var jsonResponse map[string]interface{}
if err = json.Unmarshal(body, &jsonResponse); err != nil { if err = json.Unmarshal(body, &jsonResponse); err != nil {
return "", fmt.Errorf("解析 JSON 失败: %v", err)
return 0, fmt.Errorf("解析 JSON 失败: %v", err)
} }
// 8. 提取并直接返回 jwcode // 8. 提取并直接返回 jwcode
if data, ok := jsonResponse["data"].(map[string]interface{}); ok { if data, ok := jsonResponse["data"].(map[string]interface{}); ok {
if jwcode, exists := data["jwcode"].(string); exists {
return jwcode, nil // 直接返回 jwcode
if jwcode, exists := data["jwcode"].(float64); exists { // 确保将 jwcode 作为数字解析
return int(jwcode), nil // 转换为整数并返回
} }
} }
return "", errors.New("响应体中没有 jwcode")
return 0, errors.New("响应体中没有 jwcode")
} }
Loading…
Cancel
Save