loallout_server.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package http
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "net/url"
  8. "strings"
  9. "video_course/errors"
  10. )
  11. type LoalloutServer struct {
  12. }
  13. func (l LoalloutServer) CheckPhone(phone string) (userMd5 string) {
  14. api := strings.Join([]string{"http://192.168.0.3:19095", "v1/Auth/CheckPhone"}, "/")
  15. resp, err := http.PostForm(api, url.Values{
  16. "phone": {phone},
  17. })
  18. if err != nil {
  19. panic(err)
  20. }
  21. defer resp.Body.Close()
  22. body, err := ioutil.ReadAll(resp.Body)
  23. if err != nil {
  24. err = fmt.Errorf("response:[%s], err:\n%w", string(body), err)
  25. panic(err)
  26. }
  27. var responseBase struct {
  28. Code int
  29. Memo string
  30. }
  31. err = json.Unmarshal(body, &responseBase)
  32. if err != nil {
  33. err = fmt.Errorf("response:[%s], err:\n%w", string(body), err)
  34. panic(err)
  35. }
  36. const (
  37. success = 0
  38. notRegister = 1012
  39. ban = 1007
  40. )
  41. switch responseBase.Code {
  42. case success:
  43. {
  44. var responseAll struct {
  45. Code int
  46. Memo string
  47. Md5 string
  48. }
  49. err = json.Unmarshal(body, &responseAll)
  50. if err != nil {
  51. err = fmt.Errorf("response:[%s], err:\n%w", string(body), err)
  52. panic(err)
  53. }
  54. userMd5 = responseAll.Md5
  55. }
  56. case notRegister:
  57. panic(errors.ErrUserNotExists)
  58. case ban:
  59. panic(errors.ErrUserBan)
  60. default:
  61. panic(errors.ErrHrSensorsTimeOut)
  62. }
  63. return
  64. }