package http import ( "encoding/json" "fmt" "io/ioutil" "net/http" "net/url" "strings" "video_course/errors" "video_course/global" ) type LoalloutServer struct { } func (l LoalloutServer) CheckPhone(phone string) (userMd5 string) { api := strings.Join([]string{global.Project.LoalloutUrl, "v1/Auth/CheckPhone"}, "/") resp, err := http.PostForm(api, url.Values{ "phone": {phone}, }) if err != nil { panic(err) } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { err = fmt.Errorf("response:[%s], err:\n%w", string(body), err) panic(err) } var responseBase struct { Code int Memo string } err = json.Unmarshal(body, &responseBase) if err != nil { err = fmt.Errorf("response:[%s], err:\n%w", string(body), err) panic(err) } const ( success = 0 notRegister = 1012 ban = 1007 ) switch responseBase.Code { case success: { var responseAll struct { Code int Memo string Md5 string } err = json.Unmarshal(body, &responseAll) if err != nil { err = fmt.Errorf("response:[%s], err:\n%w", string(body), err) panic(err) } userMd5 = responseAll.Md5 } case notRegister: panic(errors.ErrUserNotExists) case ban: panic(errors.ErrUserBan) default: panic(errors.ErrHrSensorsTimeOut) } return }