go_dreamfactory/modules/pay/module.go
2022-12-19 16:25:22 +08:00

137 lines
3.6 KiB
Go

package pay
import (
"context"
"go_dreamfactory/comm"
"go_dreamfactory/lego/base"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/modules"
"go_dreamfactory/pb"
"go_dreamfactory/sys/configure"
cfg "go_dreamfactory/sys/configure/structs"
)
/*
模块名:支付系统
描述:充值商城
开发:李伟
*/
func NewModule() core.IModule {
m := new(Pay)
return m
}
type Pay struct {
modules.ModuleBase
service base.IRPCXService
api *apiComp
modelPay *modelPayComp
modelPayUser *modelPayUserComp
modelDaily *modelDailyComp
configure *configureComp
}
//模块名
func (this *Pay) GetType() core.M_Modules {
return comm.ModulePay
}
//模块初始化接口 注册用户创建角色事件
func (this *Pay) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) {
err = this.ModuleBase.Init(service, module, options)
this.service = service.(base.IRPCXService)
return
}
func (this *Pay) Start() (err error) {
err = this.ModuleBase.Start()
this.service.RegisterFunctionName(string(comm.Rpc_ModulePayDelivery), this.Rpc_ModulePayDelivery)
return
}
//装备组件
func (this *Pay) OnInstallComp() {
this.ModuleBase.OnInstallComp()
this.api = this.RegisterComp(new(apiComp)).(*apiComp)
this.modelPay = this.RegisterComp(new(modelPayComp)).(*modelPayComp)
this.modelPayUser = this.RegisterComp(new(modelPayUserComp)).(*modelPayUserComp)
this.modelDaily = this.RegisterComp(new(modelDailyComp)).(*modelDailyComp)
this.configure = this.RegisterComp(new(configureComp)).(*configureComp)
}
//RPC-----------------------------------------------------------------------------------------------------------------------
func (this *Pay) Rpc_ModulePayDelivery(ctx context.Context, args *pb.PayDeliveryReq, reply *pb.PayDeliveryResp) (err error) {
this.Debug("Rpc_ModulePayDelivery", log.Field{Key: "args", Value: args.String()})
var (
conf *cfg.GameRechargeData
info *pb.DBUserPay
res []*cfg.Gameatn
items []*pb.UserAssets
session comm.IUserSession
online bool
)
if conf, err = this.configure.getGameRecharge(args.Productid); err != nil {
reply.Code = pb.ErrorCode_ConfigNoFound
return
}
if info, err = this.modelPayUser.queryUserPay(args.Uid); err != nil {
reply.Code = pb.ErrorCode_DBError
return
}
if info.Record[args.Productid] > 0 {
res = conf.Vipexp
} else {
res = conf.DiamondNumDouble
}
info.Record[args.Productid]++
if session, online = this.GetUserSession(args.Uid); online {
if reply.Code = this.DispenseRes(session, res, true); reply.Code != pb.ErrorCode_Success {
return
}
} else {
session = comm.NewUserSessionByPools(this.service)
session.SetSession("", "", "", "", args.Uid)
if reply.Code = this.DispenseRes(session, res, false); reply.Code != pb.ErrorCode_Success {
return
}
}
if err = this.modelPay.addUserPayOrder(&pb.DBPayOrder{
Orderid: args.Orderid,
Uid: args.Uid,
Productid: args.Productid,
Ctime: configure.Now().Unix(),
}); err != nil {
reply.Code = pb.ErrorCode_DBError
return
}
this.modelPayUser.updateUserPay(info)
switch conf.RechargeType {
case 1:
break
case 2:
reply.Code, items = this.modelDaily.delivery(session, args.Productid)
break
}
for _, v := range res {
items = append(items, &pb.UserAssets{A: v.A, T: v.T, N: v.N})
}
session.SendMsg(string(this.GetType()), "shipped", &pb.PayShippedPush{
Pid: args.Productid,
Orderid: args.Orderid,
Items: items,
Info: info,
})
if online {
if err = session.Push(); err != nil {
this.Errorln(err)
return
}
}
this.ModuleHero.RechargeMoney(session.GetUserId(), conf.Amount)
return
}