user.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "git.chuangxin1.com/csacred/toolkit"
  6. "git.chuangxin1.com/csacred/toolkit/http"
  7. "app/model"
  8. "git.chuangxin1.com/csacred/toolkit/example/cuser/define"
  9. )
  10. const (
  11. // ServiceName microservice name
  12. ServiceName = `user`
  13. )
  14. // UserServicer define
  15. type UserServicer interface {
  16. List(context.Context, define.FormUser) (*toolkit.ReplyData, error)
  17. Profile(context.Context, define.FormID) (*toolkit.ReplyData, error)
  18. SignIn(context.Context, define.FormUserSignIn) (*toolkit.ReplyData, error)
  19. ChangePwd(
  20. context.Context,
  21. define.FormUserChangePwd) (*toolkit.ReplyData, error)
  22. }
  23. type userService struct{}
  24. // New UserServicer
  25. func New() UserServicer {
  26. var s UserServicer
  27. s = userService{}
  28. return s
  29. }
  30. func (userService) List(
  31. ctx context.Context,
  32. u define.FormUser) (reply *toolkit.ReplyData, err error) {
  33. var user model.UserModel
  34. fmt.Println("user.list")
  35. if rows, total, err := user.List(u); err == nil {
  36. reply = toolkit.RowsReplyData(total, rows)
  37. } else {
  38. reply = toolkit.ErrReplyData(toolkit.ErrDataNotFound, `没有用户数据`)
  39. }
  40. return
  41. }
  42. func (userService) Profile(
  43. ctx context.Context,
  44. u define.FormID) (reply *toolkit.ReplyData, err error) {
  45. //*
  46. var user model.UserModel
  47. if row, err := user.Get(u); err == nil {
  48. reply = toolkit.RowReplyData(row)
  49. } else {
  50. reply = toolkit.ErrReplyData(toolkit.ErrDataNotFound, `没有用户信息`)
  51. }
  52. /*
  53. ver := map[string]string{
  54. "version": "0.2.0",
  55. "comments": "chuangxin1.com API",
  56. "author": "ls"}
  57. reply = toolkit.RowReplyData(ver)
  58. err = nil
  59. // */
  60. return
  61. }
  62. func (userService) SignIn(
  63. ctx context.Context,
  64. u define.FormUserSignIn) (reply *toolkit.ReplyData, err error) {
  65. err = nil
  66. /*
  67. reply = toolkit.NewReplyData(toolkit.ErrOk)
  68. var (
  69. uid int
  70. token toolkit.AccessToken
  71. ctoken toolkit.CacheAccessToken
  72. )
  73. aes := toolkit.NewAesCrypto()
  74. userID := "100036"
  75. id, _ := aes.Encrypt([]byte(userID))
  76. //base64.StdEncoding()
  77. t := time.Now()
  78. t = t.Add(2 * time.Hour)
  79. expires := t.Unix()
  80. token.ID = base64.StdEncoding.EncodeToString([]byte(id))
  81. token.Name = "ls0 ❄️"
  82. token.Expires = expires
  83. accessToken, _ := toolkit.NewAccessToken(token)
  84. data := map[string]interface{}{
  85. toolkit.VarUserAuthorization: accessToken,
  86. "expires_in": expires,
  87. }
  88. reply.Data = data
  89. uid, err = strconv.Atoi(userID)
  90. ctoken.ID = uid
  91. ctoken.Name = token.Name
  92. ctoken.Expires = expires
  93. ctoken.Status = 0
  94. ctoken.Message = `ok`
  95. err = toolkit.AccessTokenStorageCache(userID, ctoken)
  96. fmt.Println("access_token cache", err)
  97. // */
  98. return
  99. }
  100. func (userService) ChangePwd(
  101. ctx context.Context,
  102. u define.FormUserChangePwd) (reply *toolkit.ReplyData, err error) {
  103. err = nil
  104. reply = toolkit.NewReplyData(toolkit.ErrOk)
  105. //fmt.Println(u)
  106. reply.Data = http.RouteVars(ctx)
  107. /*
  108. if vars := toolkit.RouteVars(ctx); vars != nil {
  109. fmt.Println("route vars name", vars["name"])
  110. }
  111. fmt.Println(toolkit.JWTToken, ctx.Value(toolkit.JWTToken))
  112. // */
  113. return
  114. }
  115. // UserServiceMiddleware is a chainable behavior modifier for UserServicer.
  116. type UserServiceMiddleware func(UserServicer) UserServicer