256 lines
5.9 KiB
Go
256 lines
5.9 KiB
Go
package application
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/fonchain_enterprise/micro-account/api/account"
|
|
"github.com/fonchain_enterprise/micro-account/cmd/config"
|
|
"github.com/fonchain_enterprise/micro-account/pkg/cache"
|
|
"github.com/fonchain_enterprise/micro-account/pkg/common/jwt"
|
|
"github.com/fonchain_enterprise/micro-account/pkg/common/redis_key"
|
|
"github.com/fonchain_enterprise/micro-account/pkg/common/utils"
|
|
"github.com/fonchain_enterprise/micro-account/pkg/m"
|
|
"github.com/fonchain_enterprise/micro-account/pkg/model"
|
|
"net"
|
|
"time"
|
|
)
|
|
|
|
// Logout 登出
|
|
func Logout(in *account.DecryptJwtRequest, status uint8) error {
|
|
var loginLog *model.LoginLog
|
|
|
|
//解析token
|
|
claims, err := jwt.ParseToken(in.Token, m.JWTSecret)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
//删除token信息
|
|
tokenKey := redis_key.GetTokenInfo(in.Domain, in.Token)
|
|
fmt.Print("删除token", tokenKey)
|
|
if merr := cache.RedisClient.Del(tokenKey).Err(); merr != nil {
|
|
return merr
|
|
}
|
|
|
|
//记录token信息
|
|
if err := model.DB.Model(&model.LoginLog{}).Where(&model.LoginLog{Token: in.Token}).First(&loginLog).Error; err != nil {
|
|
loginLog = &model.LoginLog{
|
|
UserId: claims.ID,
|
|
Token: in.Token,
|
|
Status: status,
|
|
ExpireDate: time.Now().Format("2006-01-02 15:04:05"),
|
|
LastDate: time.Now().Format("2006-01-02 15:04:05"),
|
|
LogoutDate: time.Now().Format("2006-01-02 15:04:05"),
|
|
}
|
|
model.DB.Model(&model.LoginLog{}).Create(loginLog)
|
|
} else {
|
|
//不存在
|
|
updateLog := &model.LoginLog{
|
|
Status: status,
|
|
LogoutDate: time.Now().Format("2006-01-02 15:04:05"),
|
|
}
|
|
|
|
model.DB.Model(&model.LoginLog{}).Where(&model.LoginLog{ID: loginLog.ID}).Updates(&updateLog)
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// UpdateLastDate 更新最新操作时间
|
|
func UpdateLastDate(token string) {
|
|
var loginLog *model.LoginLog
|
|
|
|
if err := model.DB.Model(&model.LoginLog{}).Where(&model.LoginLog{Token: token}).First(&loginLog).Error; err != nil {
|
|
return
|
|
}
|
|
|
|
updateObj := &model.LoginLog{LastDate: time.Now().Format("2006-01-02 15:04:05")}
|
|
|
|
model.DB.Model(&model.LoginLog{}).Where(&model.LoginLog{Token: token}).Updates(updateObj)
|
|
|
|
return
|
|
}
|
|
|
|
// OffByLogId 踢下线
|
|
func OffByLogId(id uint64) error {
|
|
var loginLog *model.LoginLog
|
|
|
|
if err := model.DB.Model(&model.LoginLog{}).First(&loginLog, id).Error; err != nil {
|
|
return nil
|
|
}
|
|
|
|
in := &account.DecryptJwtRequest{
|
|
Token: loginLog.Token,
|
|
Domain: *loginLog.Domain,
|
|
}
|
|
|
|
return Logout(in, model.Status_Off)
|
|
}
|
|
|
|
func OnlineLogById(id uint64) (*account.LoginLog, error) {
|
|
var log *model.LoginLog
|
|
|
|
//失效时间大于当前时间
|
|
if err := model.DB.Model(&model.LoginLog{}).
|
|
First(&log, id).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
res := &account.LoginLog{
|
|
ID: uint64(log.ID),
|
|
UserId: uint64(log.UserId),
|
|
Ip: log.Ip,
|
|
Status: uint64(log.Status),
|
|
ExpireDate: log.ExpireDate,
|
|
LastDate: log.LastDate,
|
|
LogoutDate: log.LogoutDate,
|
|
Address: log.Address,
|
|
CreatedAt: log.CreatedAt.Format("2006-01-02 15:04:05"),
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
// OnlineLogList 获取用户当前的在线记录
|
|
func OnlineLogList(in *account.LoginInfosByUserIdRequest) []*account.LoginLog {
|
|
var loginLogs []*model.LoginLog
|
|
var logs []*account.LoginLog
|
|
|
|
//失效时间大于当前时间
|
|
model.DB.Model(&model.LoginLog{}).
|
|
Where(&model.LoginLog{UserId: uint(in.UserId), Status: model.Status_In}).
|
|
Where("expire_date > ?", time.Now().Format("2006-01-02 15:04:05")).
|
|
Find(&loginLogs)
|
|
|
|
for _, t := range loginLogs {
|
|
|
|
temp := &account.LoginLog{
|
|
ID: uint64(t.ID),
|
|
UserId: uint64(t.UserId),
|
|
Ip: t.Ip,
|
|
//Token: t.Token,
|
|
Status: uint64(t.Status),
|
|
ExpireDate: t.ExpireDate,
|
|
LastDate: t.LastDate,
|
|
LogoutDate: t.LogoutDate,
|
|
}
|
|
|
|
logs = append(logs, temp)
|
|
}
|
|
|
|
return logs
|
|
|
|
}
|
|
|
|
// IsSampleAddress 获取用户当前的在线记录
|
|
func IsSampleAddress(nowIp string, userId uint64) (bool, error) {
|
|
var loginLog *model.LoginLog
|
|
|
|
if config.AppConfig.System.Mode == "dev" {
|
|
return true, nil
|
|
}
|
|
|
|
//失效时间大于当前时间
|
|
if err := model.DB.Model(&model.LoginLog{}).
|
|
Where(&model.LoginLog{UserId: uint(userId)}).
|
|
Where("status in (?)", []uint8{model.Status_In, model.Status_Out}).
|
|
Order("id desc").
|
|
First(&loginLog).Error; err != nil {
|
|
return false, err
|
|
}
|
|
|
|
if loginLog.Ip == nowIp { //两个ip一样直接退出
|
|
return true, nil
|
|
}
|
|
|
|
location, err1 := utils.AliIpAddress(loginLog.Ip)
|
|
nowLocation, err2 := utils.AliIpAddress(nowIp)
|
|
|
|
fmt.Println("查询ip地址:", err1, err2, location, nowIp, nowLocation)
|
|
if err1 == nil && err2 == nil {
|
|
if location == nowLocation {
|
|
return true, nil
|
|
} else {
|
|
return false, nil
|
|
}
|
|
}
|
|
|
|
return true, nil
|
|
}
|
|
|
|
// GetIpAddressByIp 根据ip获取时间
|
|
func GetIpAddressByIp(nowIp string) string {
|
|
|
|
tempKey := redis_key.GetIpAddressKey(nowIp)
|
|
cache.RedisClient.Get(redis_key.GetIpAddressKey(nowIp))
|
|
val, _ := cache.RedisClient.Get(tempKey).Result()
|
|
|
|
if val != "" {
|
|
return val
|
|
}
|
|
|
|
/*
|
|
if err == redis.Nil {
|
|
fmt.Println("键 'mykey' 不存在")
|
|
} else if err != nil {
|
|
panic(err)
|
|
} else {
|
|
fmt.Println("键 'mykey' 的值为:", val)
|
|
}
|
|
*/
|
|
|
|
location, _ := utils.AliIpAddress(nowIp)
|
|
|
|
cache.RedisClient.Set(tempKey, location, 3*24*time.Hour)
|
|
|
|
return location
|
|
}
|
|
|
|
func IsPublicIp(ipString string) bool {
|
|
|
|
ip := net.ParseIP(ipString)
|
|
if ip == nil {
|
|
fmt.Println("Invalid IP")
|
|
return false
|
|
}
|
|
|
|
if ip.IsLoopback() {
|
|
fmt.Println("Loopback IP")
|
|
return false
|
|
}
|
|
|
|
if ip.To4() != nil {
|
|
if ip[0] == 10 {
|
|
fmt.Println("Private network IP")
|
|
return false
|
|
}
|
|
|
|
if ip[0] == 172 && (ip[1]&0xf0) == 16 {
|
|
return false
|
|
}
|
|
|
|
if ip[0] == 192 && ip[1] == 168 {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
func GetAddressByID(domain string, userId uint64) string {
|
|
var loginLog *model.LoginLog
|
|
|
|
//失效时间大于当前时间
|
|
if err := model.DB.Model(&model.LoginLog{}).
|
|
Where(&model.LoginLog{UserId: uint(userId), Domain: &domain}).
|
|
Where("status in (?)", []uint8{model.Status_In, model.Status_Out}).
|
|
Order("id desc").
|
|
First(&loginLog).Error; err != nil {
|
|
return ""
|
|
}
|
|
|
|
return loginLog.Address
|
|
}
|