refactor: response

This commit is contained in:
wangfuduo 2026-07-08 17:47:33 +08:00
parent 0920b7680b
commit fcd5afbe13
2 changed files with 64 additions and 78 deletions

View File

@ -6,36 +6,49 @@ import (
)
type Button struct {
param map[string]any
param Params
}
func New(param map[string]any) *Button {
b := new(Button)
b.param = param
return b
func New(param Params) *Button {
return &Button{param: param}
}
func (b *Button) CouponButton() []*response.Button {
button := new(response.Button)
button.Url = b.param[config.ServiceVipExpireUrl].(string)
button.Type = config.ButtonInput
submitBtn := new(response.Button)
submitBtn.Text = "提交"
submitBtn.Type = config.ButtonClick
customBtn := new(response.Button)
customBtn.Text = b.param[config.ButtonTxtContractCustomer].(string)
customBtn.Type = config.ButtonClick
return []*response.Button{button, submitBtn, customBtn}
return []*response.Button{
{Url: b.param.Str(config.ServiceVipExpireUrl), Type: config.ButtonInput},
{Text: "提交", Type: config.ButtonClick},
{Text: b.param.Str(config.ButtonTxtContractCustomer), Type: config.ButtonClick},
}
}
func (b *Button) PayButton() []*response.Button {
button := new(response.Button)
button.Text = b.param[config.ServiceVipExpireButton].(string)
button.Url = b.param[config.ServiceVipExpireUrl].(string)
button.Type = config.ButtonInput
return []*response.Button{button}
return []*response.Button{
{
Text: b.param.Str(config.ServiceVipExpireButton),
Url: b.param.Str(config.ServiceVipExpireUrl),
Type: config.ButtonInput,
},
}
}
// LackButton 用户包缺失时的安装按钮
func (b *Button) LackButton() []*response.Button {
return []*response.Button{
{
Text: b.param.Str(config.ServicePackageLackButton),
Url: b.param.Str(config.ServicePackageLackUrl),
Type: config.ButtonClick,
},
}
}
// UpgradeButton 强制更新时的安装按钮link 为运行时的包下载地址
func (b *Button) UpgradeButton(link string) []*response.Button {
return []*response.Button{
{
Text: b.param.Str(config.ServiceInstallButton),
Url: link,
Type: config.ButtonClick,
},
}
}

View File

@ -31,7 +31,7 @@ type User struct {
func NewUser(ctx *gin.Context) *User {
u := new(User)
u.params = make(map[string]any)
u.params = make(Params)
u.ctx = ctx
if v := ctx.Keys[session.ContextSession]; v != nil {
if v, ok := v.(*session.ApiSession); ok {
@ -64,10 +64,7 @@ func (u *User) resp(code int, msgKey string) *response.Response {
// 判断包是否提示更新
func (u *User) success() *response.Response {
resp := u.resp(config.Success, "success").
Window(&response.Window{Title: "", Close: true}).
AddButton("", "", "")
return resp
return response.New(u.ctx, config.Success, "success").Closable(true)
}
func (u *User) Verify() *response.Response {
@ -97,28 +94,20 @@ func (u *User) VipCheck() *response.Response {
//
if expire < time.Now().Unix() {
resp = response.New(u.ctx, config.VipExpire, u.params.Str(config.ServiceVipExpireNotice))
window := &response.Window{Close: false}
resp.Window(window)
buttons := make([]*response.Button, 0)
// 首次支付按钮
resp = u.resp(config.VipExpire, config.ServiceVipExpireNotice).Closable(false)
// 首次支付使用 ServiceFirstPayWay后续使用 ServicePayWay
payWayKey := config.ServicePayWay
if u.sess.GetUserInfo().VipCreateTime == 0 {
if u.params[config.ServiceFirstPayWay] == config.FirstRechargeByCoupon {
resp.Message(u.params.Str(config.ServerCouponNotice))
buttons = append(buttons, New(u.params).CouponButton()...)
} else {
buttons = append(buttons, New(u.params).PayButton()...)
}
} else {
if u.params[config.ServicePayWay] == config.FirstRechargeByCoupon {
resp.Message(u.params.Str(config.ServerCouponNotice))
buttons = append(buttons, New(u.params).CouponButton()...)
} else {
buttons = append(buttons, New(u.params).PayButton()...)
}
payWayKey = config.ServiceFirstPayWay
}
resp.Button(buttons...)
if u.params[payWayKey] == config.FirstRechargeByCoupon {
resp.Message(u.params.Str(config.ServerCouponNotice)).
Button(New(u.params).CouponButton()...)
} else {
resp.Button(New(u.params).PayButton()...)
}
}
return resp
@ -143,29 +132,21 @@ func (u *User) PkgCheck() *response.Response {
if pkg == nil {
panic(config.ErrData.New().Append(err))
}
resp = response.New(u.ctx, config.UerNoPkg, u.params.Str(config.ServiceUserPackageLack))
button := &response.Button{
Text: u.params.Str(config.ServicePackageLackButton),
Url: u.params.Str(config.ServicePackageLackUrl),
Type: config.ButtonClick,
}
window := &response.Window{Close: false}
resp.Button(button)
resp.Window(window)
return resp
return u.resp(config.UerNoPkg, config.ServiceUserPackageLack).
Closable(false).
Button(New(u.params).LackButton()...)
}
// 先判断是否强制更新
if resp = u.expireCheck(userPkg, cast.ToInt(u.params[config.ServiceForceUpdateDay])); resp != nil {
resp.Window(&response.Window{Close: false, Title: u.params.Str(config.ServiceForceUpdateNotice)})
if resp = u.expireCheck(userPkg, u.params.Int(config.ServiceForceUpdateDay)); resp != nil {
resp.Closable(false).Title(u.params.Str(config.ServiceForceUpdateNotice))
return resp
}
// 判断是否提示更新
if resp = u.expireCheck(userPkg, cast.ToInt(u.params[config.ServiceUpdateBeforeDay])); resp != nil {
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.Window(&response.Window{Close: true, Title: msg})
resp.Closable(true).Title(msg)
return resp
}
@ -194,27 +175,19 @@ func (u *User) expireCheck(userPkg *model.UserPkgInfo, day int) *response.Respon
}
// 3. 如果都不可用,则提示联系客服
if pkg == nil {
resp = response.New(u.ctx, config.NoPkg, u.params.Str(config.ServiceContactCustomer))
window := &response.Window{Close: false}
resp.Window(window)
return resp
return u.resp(config.NoPkg, config.ServiceContactCustomer).Closable(false)
}
}
// 否则返回强制更新
message := u.params.Str(config.ServiceForceUpdateNotice)
msgKey := config.ServiceForceUpdateNotice
if pkg.Name != userPkg.Pkg.Name {
message = u.params.Str(config.ServiceUpdateOtherNotice)
msgKey = config.ServiceUpdateOtherNotice
}
resp = response.New(u.ctx, config.NeedUpgrade, message)
button := &response.Button{
Text: u.params.Str(config.ServiceInstallButton),
Url: pkg.Link,
Type: config.ButtonClick,
}
window := &response.Window{Close: false}
resp.Button(button)
resp.Window(window)
resp = u.resp(config.NeedUpgrade, msgKey).
Closable(false).
Button(New(u.params).UpgradeButton(pkg.Link)...)
}
return resp