loallout_server.go 1.3 KB

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