123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- package service
- import (
- "context"
- "fmt"
- "git.chuangxin1.com/csacred/toolkit"
- "git.chuangxin1.com/csacred/toolkit/http"
- "app/model"
- "git.chuangxin1.com/csacred/toolkit/example/cuser/define"
- )
- const (
- // ServiceName microservice name
- ServiceName = `user`
- )
- // UserServicer define
- type UserServicer interface {
- List(context.Context, define.FormUser) (*toolkit.ReplyData, error)
- Profile(context.Context, define.FormID) (*toolkit.ReplyData, error)
- SignIn(context.Context, define.FormUserSignIn) (*toolkit.ReplyData, error)
- ChangePwd(
- context.Context,
- define.FormUserChangePwd) (*toolkit.ReplyData, error)
- }
- type userService struct{}
- // New UserServicer
- func New() UserServicer {
- var s UserServicer
- s = userService{}
- return s
- }
- func (userService) List(
- ctx context.Context,
- u define.FormUser) (reply *toolkit.ReplyData, err error) {
- var user model.UserModel
- fmt.Println("user.list")
- if rows, total, err := user.List(u); err == nil {
- reply = toolkit.RowsReplyData(total, rows)
- } else {
- reply = toolkit.ErrReplyData(toolkit.ErrDataNotFound, `没有用户数据`)
- }
- return
- }
- func (userService) Profile(
- ctx context.Context,
- u define.FormID) (reply *toolkit.ReplyData, err error) {
- //*
- var user model.UserModel
- if row, err := user.Get(u); err == nil {
- reply = toolkit.RowReplyData(row)
- } else {
- reply = toolkit.ErrReplyData(toolkit.ErrDataNotFound, `没有用户信息`)
- }
- /*
- ver := map[string]string{
- "version": "0.2.0",
- "comments": "chuangxin1.com API",
- "author": "ls"}
- reply = toolkit.RowReplyData(ver)
- err = nil
- // */
- return
- }
- func (userService) SignIn(
- ctx context.Context,
- u define.FormUserSignIn) (reply *toolkit.ReplyData, err error) {
- err = nil
- /*
- reply = toolkit.NewReplyData(toolkit.ErrOk)
- var (
- uid int
- token toolkit.AccessToken
- ctoken toolkit.CacheAccessToken
- )
- aes := toolkit.NewAesCrypto()
- userID := "100036"
- id, _ := aes.Encrypt([]byte(userID))
- //base64.StdEncoding()
- t := time.Now()
- t = t.Add(2 * time.Hour)
- expires := t.Unix()
- token.ID = base64.StdEncoding.EncodeToString([]byte(id))
- token.Name = "ls0 ❄️"
- token.Expires = expires
- accessToken, _ := toolkit.NewAccessToken(token)
- data := map[string]interface{}{
- toolkit.VarUserAuthorization: accessToken,
- "expires_in": expires,
- }
- reply.Data = data
- uid, err = strconv.Atoi(userID)
- ctoken.ID = uid
- ctoken.Name = token.Name
- ctoken.Expires = expires
- ctoken.Status = 0
- ctoken.Message = `ok`
- err = toolkit.AccessTokenStorageCache(userID, ctoken)
- fmt.Println("access_token cache", err)
- // */
- return
- }
- func (userService) ChangePwd(
- ctx context.Context,
- u define.FormUserChangePwd) (reply *toolkit.ReplyData, err error) {
- err = nil
- reply = toolkit.NewReplyData(toolkit.ErrOk)
- //fmt.Println(u)
- reply.Data = http.RouteVars(ctx)
- /*
- if vars := toolkit.RouteVars(ctx); vars != nil {
- fmt.Println("route vars name", vars["name"])
- }
- fmt.Println(toolkit.JWTToken, ctx.Value(toolkit.JWTToken))
- // */
- return
- }
- // UserServiceMiddleware is a chainable behavior modifier for UserServicer.
- type UserServiceMiddleware func(UserServicer) UserServicer
|