user.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package controller
  2. import (
  3. "fmt"
  4. "github.com/RaymondCode/simple-demo/api"
  5. "github.com/RaymondCode/simple-demo/dao"
  6. "github.com/RaymondCode/simple-demo/util/snowflake"
  7. "github.com/RaymondCode/simple-demo/service"
  8. "github.com/gin-gonic/gin"
  9. "net/http"
  10. "strings"
  11. )
  12. // usersLoginInfo use map to store user info, and key is username+password for demo
  13. // user data will be cleared every time the server starts
  14. // test data: username=zhanglei, password=douyin
  15. var usersLoginInfo = map[string]api.User{
  16. "zhangleidouyin": {
  17. Id: 1,
  18. Name: "zhanglei",
  19. FollowCount: 10,
  20. FollowerCount: 5,
  21. IsFollow: true,
  22. },
  23. "123123456": {
  24. Id: 1,
  25. Name: "123",
  26. FollowCount: 0,
  27. FollowerCount: 0,
  28. IsFollow: false,
  29. },
  30. "12345:1656903457": {
  31. Id: 1,
  32. Name: "qwq",
  33. FollowCount: 0,
  34. FollowerCount: 0,
  35. IsFollow: false,
  36. },
  37. }
  38. var userIdSequence = int64(1)
  39. type UserLoginResponse struct {
  40. api.Response
  41. UserId int64 `json:"user_id,omitempty"`
  42. Token string `json:"token"`
  43. }
  44. type UserResponse struct {
  45. api.Response
  46. User api.User `json:"user"`
  47. }
  48. func Register(c *gin.Context) {
  49. username := c.Query("username")
  50. password := c.Query("password")
  51. token := username + password
  52. if _, exist := usersLoginInfo[token]; exist {
  53. c.JSON(http.StatusOK, UserLoginResponse{
  54. Response: api.Response{StatusCode: 1, StatusMsg: "User already exist"},
  55. })
  56. } else {
  57. userIdSequence := snowflake.MakeInt64SnowFlakeId()
  58. newUser := api.User{
  59. Id: userIdSequence,
  60. Name: username,
  61. }
  62. usersLoginInfo[token] = newUser
  63. err := service.AddNewUser(username, password)
  64. if err != nil {
  65. c.JSON(http.StatusOK, UserLoginResponse{
  66. Response: api.Response{StatusCode: 1, StatusMsg: "error please try again"},
  67. })
  68. return
  69. }
  70. c.JSON(http.StatusOK, UserLoginResponse{
  71. Response: api.Response{StatusCode: 0},
  72. UserId: userIdSequence,
  73. Token: username + password,
  74. })
  75. }
  76. }
  77. func Login(c *gin.Context) {
  78. username := c.Query("username")
  79. password := c.Query("password")
  80. fmt.Println(username + " " + password)
  81. if user, err := dao.GetUserByIdAndPassword(username, password); err == nil {
  82. token, _ := service.GenerateToken(username)
  83. c.JSON(http.StatusOK, UserLoginResponse{
  84. Response: api.Response{StatusCode: 0},
  85. UserId: user.Id,
  86. Token: token,
  87. })
  88. } else {
  89. c.JSON(http.StatusOK, UserLoginResponse{
  90. Response: api.Response{StatusCode: 1, StatusMsg: "User doesn't exist"},
  91. })
  92. }
  93. }
  94. func UserInfo(c *gin.Context) {
  95. token := c.Query("token")
  96. user := dao.GetUserById(strings.Split(string(token), ":")[0])
  97. if exist, _ := service.GetToken(token); exist == 0 {
  98. c.JSON(http.StatusOK, UserResponse{
  99. Response: api.Response{StatusCode: 0},
  100. User: api.User{
  101. Id: user.Id,
  102. Name: user.Name,
  103. FollowCount: 0,
  104. FollowerCount: 0,
  105. IsFollow: false,
  106. },
  107. })
  108. } else {
  109. c.JSON(http.StatusOK, UserResponse{
  110. Response: api.Response{StatusCode: 1, StatusMsg: "User doesn't exist"},
  111. })
  112. }
  113. }