121 lines
3.2 KiB
Go
121 lines
3.2 KiB
Go
package user
|
|
|
|
import (
|
|
userModel "busniess-user-center/internal/models/user"
|
|
"busniess-user-center/internal/repo"
|
|
"context"
|
|
"fmt"
|
|
"sort"
|
|
"time"
|
|
|
|
"github.com/dgrijalva/jwt-go"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/golang-module/dongle"
|
|
)
|
|
|
|
const (
|
|
userAppCode = "user-busniss-center"
|
|
)
|
|
|
|
func creteLoginTokenClaims(user *repo.User, expire int) jwt.MapClaims {
|
|
now := time.Now()
|
|
expiredAt := now.Unix() + int64(expire)
|
|
userTokenClaims := jwt.MapClaims{
|
|
"id": user.ID,
|
|
"account": user.Account,
|
|
"app_code": userAppCode,
|
|
"exp": expiredAt,
|
|
"_flag": now,
|
|
}
|
|
|
|
return userTokenClaims
|
|
}
|
|
|
|
func (u *userService) sha256(pwd string, salt string) string {
|
|
fromStr := fmt.Sprintf("%s:%s", pwd, salt)
|
|
return dongle.Encrypt.FromString(fromStr).BySha256().ToHexString()
|
|
}
|
|
|
|
func (u *userService) getToken(claims jwt.MapClaims) (string, error) {
|
|
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
|
tokenStr, err := token.SignedString([]byte(u.conf.Jwt.Secret))
|
|
return tokenStr, err
|
|
}
|
|
|
|
func (u *userService) setLoginStatus(ctx context.Context, user repo.User, claims jwt.MapClaims) error {
|
|
tokenStr, err := u.getToken(claims)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// 只做简单的token记录,校验时如果没有从reids获取到登陆信息,返回失败
|
|
err = u.tokenRefresher.SetUseridTokenRelation(user.ID, tokenStr)
|
|
if err != nil {
|
|
return fmt.Errorf("设置redis失败:%s", err.Error())
|
|
}
|
|
|
|
if c, ok := ctx.(*gin.Context); ok {
|
|
expires := u.conf.Jwt.Expires
|
|
domain := u.conf.App.Host
|
|
c.Writer.Header().Add("Set-Cookie", fmt.Sprintf("%s=%s; Max-Age=%d; Path=/;Domain=%s", COOKIE_KEY_TOKEN, tokenStr, expires, domain))
|
|
c.Writer.Header().Add("Set-Cookie", fmt.Sprintf("%s=%s; Max-Age=%d; Path=/;Domain=%s", COOKIE_KEY_ACCOUNT, claims["account"], expires, domain))
|
|
c.Writer.Header().Add("Set-Cookie", fmt.Sprintf("%s=%s; Max-Age=%d; Path=/;Domain=%s", COOKIE_KEY_ID, claims["id"], expires, domain))
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (u *userService) removeCookie(ctx context.Context) {
|
|
if c, ok := ctx.(*gin.Context); ok {
|
|
domain := u.conf.App.Host
|
|
c.Writer.Header().Add("Set-Cookie", fmt.Sprintf("%s=; Max-Age=0; Path=/;Domain=%s", COOKIE_KEY_TOKEN, domain))
|
|
c.Writer.Header().Add("Set-Cookie", fmt.Sprintf("%s=; Max-Age=0; Path=/;Domain=%s", COOKIE_KEY_ACCOUNT, domain))
|
|
c.Writer.Header().Add("Set-Cookie", fmt.Sprintf("%s=; Max-Age=0; Path=/;Domain=%s", COOKIE_KEY_ID, domain))
|
|
}
|
|
}
|
|
|
|
func convertUserList(users []repo.User) []userModel.User {
|
|
list := make([]userModel.User, 0, len(users))
|
|
for _, item := range users {
|
|
list = append(list, convertUser(item))
|
|
}
|
|
|
|
return list
|
|
}
|
|
|
|
func convertUser(user repo.User) userModel.User {
|
|
return userModel.User{
|
|
UserInfo: userModel.UserInfo{
|
|
Id: user.ID,
|
|
Name: user.Name,
|
|
Account: user.Account,
|
|
Mobile: user.Mobile,
|
|
Email: user.Email,
|
|
Sex: user.Sex,
|
|
},
|
|
}
|
|
}
|
|
|
|
func removeRepeatRole(sroles ...[]repo.Role) []repo.Role {
|
|
roles := make([]repo.Role, 0)
|
|
roleMap := make(map[uint]repo.Role, 0)
|
|
|
|
for _, item := range sroles {
|
|
for _, role := range item {
|
|
if _, ok := roleMap[role.ID]; !ok {
|
|
roleMap[role.ID] = role
|
|
}
|
|
}
|
|
}
|
|
|
|
for _, role := range roleMap {
|
|
roles = append(roles, role)
|
|
}
|
|
|
|
sort.Slice(roles, func(i, j int) bool {
|
|
return roles[i].ID < roles[j].ID
|
|
})
|
|
|
|
return roles
|
|
}
|