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