nezha/model/user.go

53 lines
1.5 KiB
Go
Raw Normal View History

2019-12-08 03:59:58 -05:00
package model
import (
"fmt"
"time"
2020-12-12 11:43:25 -05:00
"github.com/google/go-github/github"
2019-12-08 03:59:58 -05:00
"github.com/naiba/com"
)
// User ...
type User struct {
Common `json:"common,omitempty"`
Login string `gorm:"UNIQUE_INDEX" json:"login,omitempty"` // 登录名
AvatarURL string `json:"avatar_url,omitempty"` // 头像地址
Name string `json:"name,omitempty"` // 昵称
Blog string `json:"blog,omitempty"` // 网站链接
Email string `json:"email,omitempty"` // 邮箱
Hireable bool `json:"hireable,omitempty"`
Bio string `json:"bio,omitempty"` // 个人简介
Token string `gorm:"UNIQUE_INDEX" json:"-"` // 认证 Token
TokenExpired time.Time `json:"token_expired,omitempty"` // Token 过期时间
SuperAdmin bool `json:"super_admin,omitempty"` // 超级管理员
TeamsID []uint64 `gorm:"-"`
}
// NewUserFromGitHub ..
func NewUserFromGitHub(gu *github.User) User {
var u User
u.ID = uint64(gu.GetID())
u.Login = gu.GetLogin()
u.AvatarURL = gu.GetAvatarURL()
u.Name = gu.GetName()
// 昵称为空的情况
if u.Name == "" {
u.Name = u.Login
}
u.Blog = gu.GetBlog()
u.Blog = gu.GetBlog()
u.Email = gu.GetEmail()
u.Hireable = gu.GetHireable()
u.Bio = gu.GetBio()
return u
}
// IssueNewToken ...
func (u *User) IssueNewToken() {
u.Token = com.MD5(fmt.Sprintf("%d%d%s", time.Now().UnixNano(), u.ID, u.Login))
2019-12-20 10:58:09 -05:00
u.TokenExpired = time.Now().AddDate(0, 2, 0)
2019-12-08 03:59:58 -05:00
}