auth.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package controller
  2. import (
  3. "net/http"
  4. "video_course/service"
  5. "github.com/mojocn/base64Captcha"
  6. )
  7. type Auth struct {
  8. BaseController
  9. }
  10. //// SignUp godoc
  11. //// @Summary 用户添加
  12. //// @tags Auth
  13. //// @Description 用户添加
  14. //// @Accept x-www-form-urlencoded
  15. //// @Produce json
  16. //// @Param userCode formData string true "用户名"
  17. //// @Param password formData string true "密码"
  18. //// @Param email formData string false "邮箱"
  19. //// @Param phone formData string false "手机号"
  20. //// @Param name formData string false "姓名"
  21. //// @Success 200 {object} controller.ResponseBase
  22. //// @Router /Auth/SignUp [post]
  23. //func (a *Auth) HttpPostSignUp() (err error) {
  24. // userCode := a.Ctx().PostForm("userCode")
  25. // test := a.PostFromInt("test")
  26. // test2 := a.PostFromIntPtr("test2")
  27. // logrus.Info(userCode, test, test2)
  28. //
  29. // a.Ctx().JSON(http.StatusOK, newResponseBase())
  30. // return
  31. //}
  32. type ResponseVerifyPic struct {
  33. Id string
  34. Pic string
  35. }
  36. // GenVerifyPic godoc
  37. // @Summary 获取验证图片
  38. // @tags Auth
  39. // @Description 获取验证图片和验证id
  40. // @Accept x-www-form-urlencoded
  41. // @Param height formData string true "高"
  42. // @Param width formData string true "宽"
  43. // @Param noiseCount formData string true "噪点数量"
  44. // @Param length formData string true "验证码字数"
  45. // @Param source formData string true "验证码取值范围 比如 1234567890 或者 abcdef等"
  46. // @Produce json
  47. // @Success 200 {object} controller.ResponseVerifyPic
  48. // @Router /Auth/GenVerifyPic [post]
  49. func (a *Auth) GenVerifyPic() (err error) {
  50. config := &base64Captcha.DriverString{}
  51. config.Height = a.postIntNecessary("height")
  52. config.Width = a.postIntNecessary("width")
  53. config.NoiseCount = a.postIntNecessary("noiseCount")
  54. config.Length = a.postIntNecessary("length")
  55. config.Source = a.Ctx().PostForm("source")
  56. if config.Source == "" {
  57. config.Source = "1234567890"
  58. }
  59. id, pic := service.Auth{}.GenVerifyPic(config)
  60. r := ResponseVerifyPic{
  61. id,
  62. pic,
  63. }
  64. a.Ctx().JSON(http.StatusOK, r)
  65. return
  66. }
  67. // GetPhoneVFCode godoc
  68. // @Summary 手机获取验证码
  69. // @tags Auth
  70. // @Description 手机获取验证码
  71. // @Accept x-www-form-urlencoded
  72. // @Produce json
  73. // @Param phone formData string true "手机号"
  74. // @Param codeType formData int true "验证码类型 1:登录"
  75. // @Success 200 {object} controller.ResponseBase
  76. // @Router /Auth/GetPhoneVFCode [post]
  77. func (a *Auth) GetPhoneVFCode() (err error) {
  78. phone := a.postString("phone", true)
  79. codeType := a.postIntNecessary("codeType")
  80. //picId := a.Ctx().PostForm("picId")
  81. //picCode := a.Ctx().PostForm("picCode")
  82. err = service.Auth{}.GenVFCode(codeType, phone, a.getIp())
  83. if err != nil {
  84. return
  85. }
  86. a.Ctx().JSON(http.StatusOK, newResponseBase())
  87. return
  88. }
  89. type StandardResponse struct {
  90. ResponseBase
  91. Rs map[string]string
  92. }
  93. // PhoneSignIn godoc
  94. // @Summary 手机验证码登录
  95. // @tags Auth
  96. // @Description 手机验证码登录
  97. // @Accept x-www-form-urlencoded
  98. // @Produce json
  99. // @Param phone formData string true "手机号"
  100. // @Param smsCode formData int true "验证码"
  101. // @Success 200 {object} controller.ResponseBase
  102. // @Router /Auth/PhoneSignIn [post]
  103. func (a *Auth) PhoneSignIn() (err error) {
  104. phone := a.postString("phone", true)
  105. smsCode := a.postString("smsCode", true)
  106. session, err := service.Auth{}.PhoneSignIn(phone, smsCode, a.getIp())
  107. if err != nil {
  108. return
  109. }
  110. r := StandardResponse{
  111. ResponseBase: newResponseBase(),
  112. Rs: map[string]string{
  113. "token": session.Token,
  114. },
  115. }
  116. a.Ctx().JSON(http.StatusOK, r)
  117. return
  118. }