user.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package controller
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "github.com/go-playground/validator/v10"
  5. "project_management/app/e"
  6. "project_management/app/model"
  7. "project_management/app/services"
  8. "project_management/utils"
  9. )
  10. var User services.User = &model.User{}
  11. // SendCode 发送验证码登录
  12. func SendCode(c *gin.Context) {
  13. phone := c.PostForm("phone")
  14. validate := validator.New()
  15. err := validate.Var(phone, "required,min=11,max=11")
  16. if err != nil {
  17. e.ResponseWithMsg(c, e.ThePhoneNumberIsWrong, e.ThePhoneNumberIsWrong.GetMsg())
  18. return
  19. }
  20. sendModel := utils.SendModel(phone)
  21. if sendModel != e.SUCCESS {
  22. e.ResponseWithMsg(c, sendModel, sendModel.GetMsg())
  23. return
  24. } else {
  25. e.ResponseSuccess(c, "发送成功")
  26. }
  27. }
  28. // Login 登录
  29. func Login(c *gin.Context) {
  30. var userRegit model.UserRegist
  31. if err := c.ShouldBind(&userRegit); err != nil {
  32. e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
  33. return
  34. }
  35. validate := validator.New()
  36. if err := validate.Struct(userRegit); err != nil {
  37. e.ResponseWithMsg(c, e.JSONParsingFailed, err.Error())
  38. return
  39. }
  40. token, rescode := User.Login(userRegit)
  41. if rescode != "" {
  42. e.ResponseWithMsg(c, e.ERROR, rescode)
  43. return
  44. } else {
  45. e.ResponseSuccess(c, token)
  46. }
  47. }
  48. // CollectionApp 收藏应用
  49. func CollectionApp(c *gin.Context) {
  50. var user model.CollectionAppDto
  51. if err := c.ShouldBind(&user); err != nil {
  52. e.ResponseWithMsg(c, e.JSONParsingFailed, e.JSONParsingFailed.GetMsg())
  53. return
  54. }
  55. value, _ := c.Get("phone")
  56. user.Phone = value.(string)
  57. app := User.CollectionApp(user)
  58. if app != e.SUCCESS {
  59. e.ResponseWithMsg(c, app, app.GetMsg())
  60. return
  61. }
  62. e.ResponseSuccess(c, nil)
  63. }
  64. // CollectionList 展示收藏列表
  65. func CollectionList(c *gin.Context) {
  66. value, _ := c.Get("phone")
  67. list, applies := User.CollectionList(value.(string))
  68. if list != e.SUCCESS {
  69. e.ResponseWithMsg(c, list, list.GetMsg())
  70. return
  71. }
  72. e.ResponseSuccess(c, applies)
  73. }