auth.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 picCode formData string false "图形验证码,目前不起作用"
  74. // @Param phone formData string true "手机号"
  75. // @Param codeType formData int true "验证码类型 1:登录"
  76. // @Success 200 {object} controller.ResponseBase
  77. // @Router /Auth/GetPhoneVFCode [post]
  78. func (a *Auth) GetPhoneVFCode() (err error) {
  79. picCode := a.Ctx().PostForm("picCode")
  80. phone := a.postString("phone", true)
  81. codeType := a.postIntNecessary("codeType")
  82. //picId := a.Ctx().PostForm("picId")
  83. //picCode := a.Ctx().PostForm("picCode")
  84. err = service.Auth{}.GenVFCode(codeType, phone, a.getIp(), picCode)
  85. if err != nil {
  86. return
  87. }
  88. a.Ctx().JSON(http.StatusOK, newResponseBase())
  89. return
  90. }
  91. type StandardResponse struct {
  92. ResponseBase
  93. Rs map[string]string
  94. }
  95. // PhoneSignIn godoc
  96. // @Summary 手机验证码登录
  97. // @tags Auth
  98. // @Description 手机验证码登录
  99. // @Accept x-www-form-urlencoded
  100. // @Produce json
  101. // @Param phone formData string true "手机号"
  102. // @Param smsCode formData int true "验证码"
  103. // @Success 200 {object} controller.ResponseBase
  104. // @Router /Auth/PhoneSignIn [post]
  105. func (a *Auth) PhoneSignIn() (err error) {
  106. phone := a.postString("phone", true)
  107. smsCode := a.postString("smsCode", true)
  108. session, err := service.Auth{}.PhoneSignIn(phone, smsCode, a.getIp())
  109. if err != nil {
  110. return
  111. }
  112. r := StandardResponse{
  113. ResponseBase: newResponseBase(),
  114. Rs: map[string]string{
  115. "token": session.Token,
  116. },
  117. }
  118. a.Ctx().JSON(http.StatusOK, r)
  119. return
  120. }