194 lines
4.8 KiB
Go
194 lines
4.8 KiB
Go
package service
|
||
|
||
import (
|
||
"fmt"
|
||
"time"
|
||
"twin-api/app/api/response"
|
||
"twin-api/app/common/dao"
|
||
"twin-api/app/common/model"
|
||
"twin-api/base/config"
|
||
|
||
"git.u8t.cn/open/go-server/session"
|
||
"github.com/gin-gonic/gin"
|
||
"github.com/spf13/cast"
|
||
)
|
||
|
||
type Params map[string]any
|
||
|
||
func (p Params) Str(key string) string {
|
||
return cast.ToString(p[key])
|
||
}
|
||
|
||
func (p Params) Int(key string) int {
|
||
return cast.ToInt(p[key])
|
||
}
|
||
|
||
type User struct {
|
||
sess *session.ApiSession
|
||
params Params
|
||
ctx *gin.Context
|
||
}
|
||
|
||
func NewUser(ctx *gin.Context) *User {
|
||
u := new(User)
|
||
u.params = make(Params)
|
||
u.ctx = ctx
|
||
if v := ctx.Keys[session.ContextSession]; v != nil {
|
||
if v, ok := v.(*session.ApiSession); ok {
|
||
u.sess = v
|
||
}
|
||
}
|
||
|
||
if u.sess == nil {
|
||
panic(config.ErrPermission.New())
|
||
}
|
||
|
||
exp := u.sess.GetExperiment()
|
||
// 初始化参数
|
||
for k, v := range config.KeysDefault {
|
||
u.params[k] = exp.GetParam(k, v)
|
||
}
|
||
|
||
return u
|
||
}
|
||
|
||
func (u *User) resp(code int, msgKey string) *response.Response {
|
||
return response.New(u.ctx, code, u.params.Str(msgKey))
|
||
}
|
||
|
||
// 判断注册时间
|
||
// 判断免费使用时间
|
||
// 判断是否有会员
|
||
// 包更新判断
|
||
// 判断包是否提示强制更新
|
||
// 判断包是否提示更新
|
||
|
||
func (u *User) success() *response.Response {
|
||
return response.New(u.ctx, config.Success, "success").Closable(true)
|
||
}
|
||
|
||
func (u *User) Verify() *response.Response {
|
||
// 检查会员状况
|
||
if resp := u.VipCheck(); resp != nil {
|
||
return resp
|
||
}
|
||
// 检查包更新状况
|
||
if resp := u.PkgCheck(); resp != nil {
|
||
return resp
|
||
}
|
||
|
||
return u.success()
|
||
}
|
||
|
||
func (u *User) VipCheck() *response.Response {
|
||
var resp *response.Response
|
||
expire := u.sess.GetUserInfo().VipExpireTime
|
||
if expire == -1 {
|
||
return nil
|
||
}
|
||
|
||
free := time.Unix(u.sess.GetUserInfo().ActiveTime, 0).AddDate(0, 0, u.params.Int(config.ServiceFreeDay)).Unix()
|
||
if free-u.sess.GetUserInfo().ActiveTime > 0 {
|
||
return nil
|
||
}
|
||
|
||
//
|
||
if expire < time.Now().Unix() {
|
||
resp = u.resp(config.VipExpire, config.ServiceVipExpireNotice).Closable(false)
|
||
|
||
// 首次支付使用 ServiceFirstPayWay,后续使用 ServicePayWay
|
||
payWayKey := config.ServicePayWay
|
||
if u.sess.GetUserInfo().VipExpireTime == 0 {
|
||
payWayKey = config.ServiceFirstPayWay
|
||
}
|
||
|
||
if u.params[payWayKey] == config.RechargeByCoupon {
|
||
resp.Message(u.params.Str(config.ServerCouponNotice)).
|
||
Button(NewButton(u.params).CouponButton()...)
|
||
} else {
|
||
resp.Button(NewButton(u.params).PayButton()...)
|
||
}
|
||
}
|
||
|
||
return resp
|
||
}
|
||
|
||
func (u *User) PkgCheck() *response.Response {
|
||
var resp *response.Response
|
||
var userPkg *model.UserPkgInfo
|
||
var pkg *model.Pkg
|
||
var err error
|
||
user := u.sess.GetUserInfo()
|
||
if userPkg, err = dao.NewUserPkg().GetLatest(user.Id); err != nil {
|
||
panic(config.ErrDb.New().Append(err))
|
||
}
|
||
|
||
if userPkg == nil {
|
||
pkg, err = dao.NewPackage().GetByPkgName("")
|
||
if err != nil {
|
||
panic(config.ErrDb.New().Append(err))
|
||
}
|
||
|
||
if pkg == nil {
|
||
panic(config.ErrData.New().Append(err))
|
||
}
|
||
return u.resp(config.UerNoPkg, config.ServiceUserPackageLack).
|
||
Closable(false).
|
||
Button(NewButton(u.params).LackButton()...)
|
||
}
|
||
|
||
// 先判断是否强制更新:expireCheck 已设置强制更新文案且不可关闭,直接返回
|
||
if resp = u.expireCheck(userPkg, u.params.Int(config.ServiceForceUpdateDay)); resp != nil {
|
||
return resp
|
||
}
|
||
|
||
// 判断是否提示更新:覆盖为更新提示文案,且允许关闭
|
||
if resp = u.expireCheck(userPkg, u.params.Int(config.ServiceUpdateBeforeDay)); resp != nil {
|
||
msg := fmt.Sprintf(u.params.Str(config.ServiceUpdateNotice), time.Unix(userPkg.Pkg.ExpireTime, 0).Format("2006-01-02"))
|
||
resp.Closable(true).Message(msg)
|
||
return resp
|
||
}
|
||
|
||
return resp
|
||
}
|
||
|
||
// 判断包是否在有效时间内
|
||
func (u *User) expireCheck(userPkg *model.UserPkgInfo, day int) *response.Response {
|
||
var pkg *model.Pkg
|
||
var err error
|
||
var resp *response.Response
|
||
|
||
expire := time.Now().AddDate(0, 0, day)
|
||
if time.Unix(userPkg.Pkg.ExpireTime, 0).Before(expire) {
|
||
// 提示强制更新
|
||
// 查询当前有无最新可用的包
|
||
// 1. 先查询当前包有无可用
|
||
if pkg, err = dao.NewPackage().GetByPkgName(userPkg.Pkg.Name); err != nil {
|
||
panic(config.ErrDb.New().Append(err))
|
||
}
|
||
|
||
// 2. 无则查询其他可用包
|
||
if pkg == nil || time.Unix(pkg.ExpireTime, 0).Before(expire) {
|
||
if pkg, err = dao.NewPackage().GetByPkgName(""); err != nil {
|
||
panic(config.ErrDb.New().Append(err))
|
||
}
|
||
// 3. 如果都不可用,则提示联系客服
|
||
if pkg == nil || pkg.ExpireTime < time.Now().Unix() {
|
||
return u.resp(config.NoPkg, config.ServiceContactCustomer).Closable(false)
|
||
}
|
||
}
|
||
|
||
// 否则返回强制更新
|
||
msgKey := config.ServiceForceUpdateNotice
|
||
if pkg.Name != userPkg.Pkg.Name {
|
||
msgKey = config.ServiceUpdateOtherNotice
|
||
}
|
||
|
||
resp = u.resp(config.NeedUpgrade, msgKey).
|
||
Closable(false).
|
||
Button(NewButton(u.params).UpgradeButton(pkg.Link)...)
|
||
}
|
||
|
||
return resp
|
||
}
|