feat:增加用户资产和会员状态
This commit is contained in:
15
app/api/enterprise/api/home.go
Normal file
15
app/api/enterprise/api/home.go
Normal file
@ -0,0 +1,15 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"SciencesServer/app/api/enterprise/controller/home"
|
||||
"SciencesServer/app/basic/api"
|
||||
"SciencesServer/app/session"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type Home struct{}
|
||||
|
||||
func (*Home) Instance(c *gin.Context) {
|
||||
data, err := home.NewInstance()(api.GetSession()(c).(*session.Enterprise), api.GetLocal()(c).(string)).Instance()
|
||||
api.APIResponse(err, data)
|
||||
}
|
@ -7,6 +7,7 @@ import (
|
||||
"SciencesServer/config"
|
||||
"SciencesServer/utils"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Instance struct{}
|
||||
@ -19,6 +20,9 @@ type (
|
||||
InstanceLoginParams struct {
|
||||
UID uint64
|
||||
Avatar, Name, Mobile string
|
||||
Vip bool
|
||||
VipDeadline time.Time
|
||||
Currency float64
|
||||
Identity, SelectIdentity int
|
||||
Status model.AccountStatusKind
|
||||
}
|
||||
@ -39,6 +43,9 @@ func (c *Instance) Login() InstanceLoginCallback {
|
||||
_session.Avatar = params.Avatar
|
||||
_session.Name = params.Name
|
||||
_session.Mobile = params.Mobile
|
||||
_session.Vip = params.Vip
|
||||
_session.VipDeadline = params.VipDeadline
|
||||
_session.Currency = params.Currency
|
||||
_session.Identity = params.Identity
|
||||
_session.SelectIdentity = params.SelectIdentity
|
||||
|
||||
|
@ -1,12 +1,14 @@
|
||||
package account
|
||||
|
||||
import (
|
||||
model3 "SciencesServer/app/api/enterprise/model"
|
||||
"SciencesServer/app/api/enterprise/model"
|
||||
model2 "SciencesServer/app/common/model"
|
||||
"SciencesServer/app/handle"
|
||||
"SciencesServer/config"
|
||||
"SciencesServer/serve/orm"
|
||||
"SciencesServer/utils"
|
||||
"errors"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Login struct{ local string }
|
||||
@ -58,32 +60,47 @@ func loginForSmsCaptcha(params *LoginParams, local string) (*InstanceLoginParams
|
||||
}
|
||||
var isExist bool
|
||||
// 查询账号信息
|
||||
mUserInstance := model3.NewUserInstance()
|
||||
mUserInstance := model.NewUserInstance()
|
||||
|
||||
if isExist, err = model2.FirstField(mUserInstance.UserInstance, []string{"id", "uuid", "avatar", "name", "mobile", "status"},
|
||||
if isExist, err = model2.FirstField(mUserInstance.UserInstance, []string{"id", "uuid", "avatar", "name", "mobile",
|
||||
"is_vip", "vip_deadline", "status"},
|
||||
model2.NewWhere("mobile", params.Captcha.Mobile)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
mUserIdentity := model3.NewUserIdentity()
|
||||
mUserIdentity := model.NewUserIdentity()
|
||||
// 用户资产信息
|
||||
mUserAssets := new(model.UserAssets)
|
||||
|
||||
if isExist {
|
||||
// 查询该区域下最后一次选中的信息
|
||||
if err = mUserIdentity.LastChooseInfo(mUserInstance.UUID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if mUserAssets, err = model.NewUserAssets().Assets(mUserInstance.UUID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
goto RETURNS
|
||||
}
|
||||
mUserInstance.Name = params.Captcha.Mobile
|
||||
mUserInstance.Mobile = params.Captcha.Mobile
|
||||
mUserInstance.Password = utils.GetRandomString(12)
|
||||
if err = orm.GetDB().Transaction(func(tx *gorm.DB) error {
|
||||
mUserInstance.Name = params.Captcha.Mobile
|
||||
mUserInstance.Mobile = params.Captcha.Mobile
|
||||
mUserInstance.Password = utils.GetRandomString(12)
|
||||
|
||||
if err = model2.Create(mUserInstance.UserInstance); err != nil {
|
||||
if err = model2.Create(mUserInstance.UserInstance, tx); err != nil {
|
||||
return err
|
||||
}
|
||||
mUserAssets.UID = mUserInstance.UUID
|
||||
|
||||
return model2.Create(mUserAssets.UserAssets, tx)
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
RETURNS:
|
||||
return &InstanceLoginParams{
|
||||
UID: mUserInstance.UUID,
|
||||
Avatar: mUserInstance.GetAvatarAttribute(config.SettingInfo.Domain), Name: mUserInstance.Name, Mobile: mUserInstance.Mobile,
|
||||
Vip: mUserInstance.VipStatus(), VipDeadline: mUserInstance.VipDeadline, Currency: mUserAssets.Currency,
|
||||
Identity: mUserInstance.Identity, SelectIdentity: mUserIdentity.Identity,
|
||||
Status: mUserInstance.Status,
|
||||
}, nil
|
||||
@ -94,9 +111,10 @@ func loginForPassword(params *LoginParams, local string) (*InstanceLoginParams,
|
||||
if !utils.ValidateMobile(params.Password.Account) {
|
||||
return nil, errors.New("操作错误,手机号码格式异常")
|
||||
}
|
||||
mUserInstance := model3.NewUserInstance()
|
||||
mUserInstance := model.NewUserInstance()
|
||||
|
||||
isExist, err := model2.FirstField(mUserInstance.UserInstance, []string{"id", "uuid", "name", "avatar", "mobile", "password", "salt", "status"},
|
||||
isExist, err := model2.FirstField(mUserInstance.UserInstance, []string{"id", "uuid", "name", "avatar", "mobile",
|
||||
"is_vip", "vip_deadline", "password", "salt", "status"},
|
||||
model2.NewWhere("mobile", params.Password.Account))
|
||||
|
||||
if err != nil {
|
||||
@ -107,8 +125,14 @@ func loginForPassword(params *LoginParams, local string) (*InstanceLoginParams,
|
||||
if !mUserInstance.ValidatePassword(params.Password.Password) {
|
||||
return nil, errors.New("操作错误,账户或密码错误")
|
||||
}
|
||||
// 用户资产信息
|
||||
mUserAssets := new(model.UserAssets)
|
||||
|
||||
if mUserAssets, err = model.NewUserAssets().Assets(mUserInstance.UUID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 最后一次选中的身份信息
|
||||
mUserIdentity := model3.NewUserIdentity()
|
||||
mUserIdentity := model.NewUserIdentity()
|
||||
|
||||
if err = mUserIdentity.LastChooseInfo(mUserInstance.UUID); err != nil {
|
||||
return nil, err
|
||||
@ -116,6 +140,7 @@ func loginForPassword(params *LoginParams, local string) (*InstanceLoginParams,
|
||||
return &InstanceLoginParams{
|
||||
UID: mUserInstance.UUID,
|
||||
Avatar: mUserInstance.GetAvatarAttribute(config.SettingInfo.Domain), Name: mUserInstance.Name, Mobile: mUserInstance.Mobile,
|
||||
Vip: mUserInstance.VipStatus(), VipDeadline: mUserInstance.VipDeadline, Currency: mUserAssets.Currency,
|
||||
Identity: mUserInstance.Identity, SelectIdentity: mUserIdentity.Identity,
|
||||
Status: mUserInstance.Status,
|
||||
}, nil
|
||||
|
24
app/api/enterprise/controller/home/instance.go
Normal file
24
app/api/enterprise/controller/home/instance.go
Normal file
@ -0,0 +1,24 @@
|
||||
package home
|
||||
|
||||
import "SciencesServer/app/session"
|
||||
|
||||
type Instance struct {
|
||||
*session.Enterprise
|
||||
local string
|
||||
}
|
||||
|
||||
type InstanceHandle func(session *session.Enterprise, local string) *Instance
|
||||
|
||||
type InstanceInfo struct {
|
||||
Currency float64 `json:"currency"`
|
||||
}
|
||||
|
||||
func (c *Instance) Instance() (*InstanceInfo, error) {
|
||||
return &InstanceInfo{Currency: c.Currency}, nil
|
||||
}
|
||||
|
||||
func NewInstance() InstanceHandle {
|
||||
return func(session *session.Enterprise, local string) *Instance {
|
||||
return &Instance{Enterprise: session, local: local}
|
||||
}
|
||||
}
|
15
app/api/enterprise/controller/user/assets.go
Normal file
15
app/api/enterprise/controller/user/assets.go
Normal file
@ -0,0 +1,15 @@
|
||||
package user
|
||||
|
||||
import "SciencesServer/app/session"
|
||||
|
||||
type Assets struct {
|
||||
*session.Enterprise
|
||||
}
|
||||
|
||||
type AssetsHandle func(session *session.Enterprise) *Assets
|
||||
|
||||
func NewAssets() AssetsHandle {
|
||||
return func(session *session.Enterprise) *Assets {
|
||||
return &Assets{Enterprise: session}
|
||||
}
|
||||
}
|
@ -17,10 +17,13 @@ type InstanceHandle func(session *session.Enterprise) *Instance
|
||||
type (
|
||||
// InstanceInfo 基本信息
|
||||
InstanceInfo struct {
|
||||
Avatar string `json:"avatar"` // 头像
|
||||
Name string `json:"name"` // 名称
|
||||
Identity int `json:"identity"` // 具体身份
|
||||
SelectIdentity int `json:"select_identity"` // 最后一次选中的身份信息
|
||||
Avatar string `json:"avatar"` // 头像
|
||||
Name string `json:"name"` // 名称
|
||||
Currency float64 `json:"currency"` // 创新币
|
||||
Vip bool `json:"vip"` // Vip状态
|
||||
VipDays int `json:"vip_days"` // Vip剩余天数
|
||||
Identity int `json:"identity"` // 具体身份
|
||||
SelectIdentity int `json:"select_identity"` // 最后一次选中的身份信息
|
||||
}
|
||||
// InstanceDetailInfo 详细信息
|
||||
InstanceDetailInfo struct {
|
||||
@ -33,7 +36,16 @@ type (
|
||||
|
||||
// Info 基本信息
|
||||
func (c *Instance) Info() *InstanceInfo {
|
||||
return &InstanceInfo{Avatar: c.Avatar, Name: c.Name, Identity: c.Identity, SelectIdentity: c.SelectIdentity}
|
||||
out := &InstanceInfo{Avatar: c.Avatar, Name: c.Name,
|
||||
Currency: c.Currency,
|
||||
Vip: c.VipStatus(),
|
||||
Identity: c.Identity,
|
||||
SelectIdentity: c.SelectIdentity}
|
||||
|
||||
if out.Vip {
|
||||
out.VipDays = utils.DiffTimeDays(time.Now(), c.VipDeadline)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// BindMobile 绑定手机号码
|
||||
|
18
app/api/enterprise/model/user_assets.go
Normal file
18
app/api/enterprise/model/user_assets.go
Normal file
@ -0,0 +1,18 @@
|
||||
package model
|
||||
|
||||
import "SciencesServer/app/common/model"
|
||||
|
||||
type UserAssets struct {
|
||||
*model.UserAssets
|
||||
}
|
||||
|
||||
func (m *UserAssets) Assets(uid uint64) (*UserAssets, error) {
|
||||
if _, err := model.FirstField(m.UserAssets, []string{"id", "currency"}, model.NewWhere("uid", uid)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func NewUserAssets() *UserAssets {
|
||||
return &UserAssets{model.NewUserAssets()}
|
||||
}
|
@ -15,9 +15,11 @@ type UserInstance struct {
|
||||
Name string `gorm:"column:name;type:varchar(20);default:'';comment:真实姓名" json:"name"`
|
||||
Mobile string `gorm:"column:mobile;index:idx_user_instance_mobile;type:varchar(15);default:'';comment:联系方式" json:"mobile"`
|
||||
Gender
|
||||
Identity int `gorm:"column:identity;type:int(8);default:0;comment:身份信息" json:"-"`
|
||||
Password string `gorm:"column:password;type:varchar(100);default:'';comment:密码" json:"-"`
|
||||
Salt string `gorm:"column:salt;type:varchar(10);default:'';comment:盐值" json:"-"`
|
||||
IsVip UserInstanceVip `gorm:"column:is_vip;type:tinyint(1);default:0;comment:是否会员" json:"is_vip"`
|
||||
VipDeadline time.Time `gorm:"column:vip_deadline;type:datetime;default:null;comment:会员过期时间" json:"vip_deadline"`
|
||||
Identity int `gorm:"column:identity;type:int(8);default:0;comment:身份信息" json:"-"`
|
||||
Password string `gorm:"column:password;type:varchar(100);default:'';comment:密码" json:"-"`
|
||||
Salt string `gorm:"column:salt;type:varchar(10);default:'';comment:盐值" json:"-"`
|
||||
AccountStatus
|
||||
ModelDeleted
|
||||
ModelAt
|
||||
@ -33,6 +35,16 @@ const (
|
||||
UserInstanceSourceForWechat
|
||||
)
|
||||
|
||||
// UserInstanceVip 用户会员状态
|
||||
type UserInstanceVip int
|
||||
|
||||
const (
|
||||
// UserInstanceVipForNot 不是会员
|
||||
UserInstanceVipForNot UserInstanceVip = iota
|
||||
// UserInstanceVipForYes 是会员
|
||||
UserInstanceVipForYes
|
||||
)
|
||||
|
||||
func (m *UserInstance) TableName() string {
|
||||
return "user_instance"
|
||||
}
|
||||
@ -62,6 +74,10 @@ func (m *UserInstance) SetPasswordAttribute() {
|
||||
m.Password = utils.HashString([]byte(utils.Md5String(m.Password, m.Salt)))
|
||||
}
|
||||
|
||||
func (m *UserInstance) VipStatus() bool {
|
||||
return m.IsVip == UserInstanceVipForYes && m.VipDeadline.After(time.Now())
|
||||
}
|
||||
|
||||
func NewUserInstance() *UserInstance {
|
||||
return &UserInstance{}
|
||||
}
|
||||
|
@ -3,18 +3,22 @@ package session
|
||||
import (
|
||||
"SciencesServer/utils"
|
||||
"encoding/json"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Enterprise 企业用户
|
||||
type Enterprise struct {
|
||||
Token string `json:"token"` // token
|
||||
UID uint64 `json:"uid"` // 唯一标识ID
|
||||
IdentityUID uint64 `json:"identity_uid"` // 用户身份唯一ID
|
||||
Avatar string `json:"avatar"` // 头像
|
||||
Name string `json:"name"` // 名称
|
||||
Mobile string `json:"mobile"` // 手机号码
|
||||
Identity int `json:"identity"` // 总身份信息
|
||||
SelectIdentity int `json:"select_identity"` // 选中身份信息
|
||||
Token string `json:"token"` // token
|
||||
UID uint64 `json:"uid"` // 唯一标识ID
|
||||
IdentityUID uint64 `json:"identity_uid"` // 用户身份唯一ID
|
||||
Avatar string `json:"avatar"` // 头像
|
||||
Name string `json:"name"` // 名称
|
||||
Mobile string `json:"mobile"` // 手机号码
|
||||
Vip bool `json:"vip"` // VIP状态
|
||||
VipDeadline time.Time `json:"vip_deadline"` // VIP过期时间
|
||||
Currency float64 `json:"currency"` // 货币-创新币
|
||||
Identity int `json:"identity"` // 总身份信息
|
||||
SelectIdentity int `json:"select_identity"` // 选中身份信息
|
||||
}
|
||||
|
||||
func (this *Enterprise) SetToken(token string) {
|
||||
@ -29,6 +33,10 @@ func (this *Enterprise) UIDToString() string {
|
||||
return utils.UintToString(this.UID)
|
||||
}
|
||||
|
||||
func (this *Enterprise) VipStatus() bool {
|
||||
return this.Vip && this.VipDeadline.After(time.Now())
|
||||
}
|
||||
|
||||
func (this *Enterprise) MarshalBinary() ([]byte, error) {
|
||||
return json.Marshal(this)
|
||||
}
|
||||
|
Reference in New Issue
Block a user