go_dreamfactory/modules/web/api_comp.go
2022-06-06 18:42:03 +08:00

50 lines
1.2 KiB
Go

package web
import (
"go_dreamfactory/comm"
"go_dreamfactory/pb"
"go_dreamfactory/sys/db"
"net/http"
"github.com/liwei1dao/lego/core"
"github.com/liwei1dao/lego/core/cbase"
"github.com/liwei1dao/lego/sys/gin"
"github.com/liwei1dao/lego/sys/gin/engine"
"github.com/liwei1dao/lego/sys/log"
)
type Api_Comp struct {
cbase.ModuleCompBase
options *Options
module *Web
gin gin.ISys
}
func (this *Api_Comp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
err = this.ModuleCompBase.Init(service, module, comp, options)
this.options = options.(*Options)
this.gin, err = gin.NewSys(gin.SetListenPort(this.options.Port))
this.gin.POST("/register", this.Register)
return
}
//模拟账户注册
func (this *Api_Comp) Register(c *engine.Context) {
var req pb.UserRegisterReq
rsp := &pb.UserRegisterRsp{}
err := c.BindJSON(&req)
if err == nil {
err := db.Defsys.User_CreateUser(&pb.DB_UserData{
Account: req.Account,
})
if err != nil {
log.Errorf("create user err: %v", err)
rsp.Code = comm.ErrorCode_SqlExecutionError
}
rsp.Code = comm.ErrorCode_Success
} else {
rsp.Code = comm.ErrorCode_ReqParameterError
}
c.JSON(http.StatusOK, rsp)
}