diff --git a/middleware/student.go b/middleware/student.go index 38e8574..4ec51e3 100644 --- a/middleware/student.go +++ b/middleware/student.go @@ -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 } // 验证接口有效性