usernew.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package reserver
  2. import (
  3. "fmt"
  4. "github.com/RaymondCode/simple-demo/api"
  5. "github.com/RaymondCode/simple-demo/dao"
  6. "github.com/RaymondCode/simple-demo/entities"
  7. "github.com/RaymondCode/simple-demo/service"
  8. "github.com/gin-gonic/gin"
  9. "net/http"
  10. "sync/atomic"
  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. }
  31. var userIdSequence = int64(1)
  32. type UserLoginResponse struct {
  33. api.Response
  34. UserId int64 `json:"user_id,omitempty"`
  35. Token string `json:"token"`
  36. }
  37. type UserResponse struct {
  38. api.Response
  39. User api.User `json:"user"`
  40. }
  41. func Register(c *gin.Context) {
  42. username := c.Query("username")
  43. password := c.Query("password")
  44. token := username + password
  45. if _, exist := usersLoginInfo[token]; exist {
  46. c.JSON(http.StatusOK, UserLoginResponse{
  47. Response: api.Response{StatusCode: 1, StatusMsg: "User already exist"},
  48. })
  49. } else {
  50. atomic.AddInt64(&userIdSequence, 1)
  51. newUser := api.User{
  52. Id: userIdSequence,
  53. Name: username,
  54. }
  55. usersLoginInfo[token] = newUser
  56. err := service.AddNewUser(username, password)
  57. if err != nil {
  58. c.JSON(http.StatusOK, UserLoginResponse{
  59. Response: api.Response{StatusCode: 1, StatusMsg: "error please try again"},
  60. })
  61. return
  62. }
  63. c.JSON(http.StatusOK, UserLoginResponse{
  64. Response: api.Response{StatusCode: 0},
  65. UserId: userIdSequence,
  66. Token: username + password,
  67. })
  68. }
  69. }
  70. func Login(c *gin.Context) {
  71. username := c.Query("username")
  72. password := c.Query("password")
  73. token := username + password
  74. if user, exist := usersLoginInfo[token]; exist {
  75. c.JSON(http.StatusOK, UserLoginResponse{
  76. Response: api.Response{StatusCode: 0},
  77. UserId: user.Id,
  78. Token: token,
  79. })
  80. } else {
  81. user2, _ := dao.GetUserByIdAndPassword(username, password)
  82. fmt.Printf("%v+", user2)
  83. c.JSON(http.StatusOK, UserLoginResponse{
  84. Response: api.Response{StatusCode: 1, StatusMsg: "User doesn't exist"},
  85. })
  86. }
  87. }
  88. func UserInfo(c *gin.Context) {
  89. token := c.Query("token")
  90. if user, exist := usersLoginInfo[token]; exist {
  91. c.JSON(http.StatusOK, UserResponse{
  92. Response: api.Response{StatusCode: 0},
  93. User: user,
  94. })
  95. } else {
  96. c.JSON(http.StatusOK, UserResponse{
  97. Response: api.Response{StatusCode: 1, StatusMsg: "User doesn't exist"},
  98. })
  99. }
  100. }