This commit is contained in:
macbook-maxwell 2024-10-08 20:57:35 +08:00
parent a46f197586
commit 43f5b14de9

View File

@ -228,16 +228,19 @@ func fetchTokenFromAPI(username, password string) (string, error) {
// 解析返回的json数据判断是否有code字段并且code是否为1如果是则获取并返回token
var result map[string]interface{}
json.Unmarshal(body, &result)
fmt.Println("code: %d",result["code"])
if _, ok := result["code"]; !ok || (result["code"] != 1) {
fmt.Println(ok)
return "", errors.New("fetchTokenFromAPI:获取token失败,token无效,code:"+fmt.Sprintf("%d", int(result["code"].(float64)))+" message:"+result["message"].(string))
// 如果不存在code 字段则token无效。如果存在code强制类型转换为int判断code是否为1如果不是1则token无效
if _, ok := result["code"]; !ok {
return "", errors.New("fetchTokenFromAPI:获取token失败,token无效,code字段不存在")
}
// 如果不存在message字段或message字段的值为空则token为空
code := int(result["code"].(float64))
if code != 1 {
return "", errors.New("fetchTokenFromAPI:获取token失败,token无效,code:"+fmt.Sprintf("%d", code)+" message:"+result["message"].(string))
}
// 如果不存在result字段或result字段的值为空则token为空
if _, ok := result["result"]; !ok || result["result"] == "" {
return "", errors.New("获取token失败,token为空")
}
return result["message"].(string), nil
return result["result"].(string), nil
}
// 验证接口有效性