package web import ( "go_dreamfactory/pb" "net/http" "go_dreamfactory/lego/core" "go_dreamfactory/lego/core/cbase" "go_dreamfactory/lego/sys/gin" "go_dreamfactory/lego/sys/gin/engine" "go_dreamfactory/lego/sys/log" ) /* web api 服务组件 */ type Api_Comp struct { cbase.ModuleCompBase options *Options //模块参数 module *Web //当前模块对象 gin gin.ISys //gin 框架 web的热门框架 } //组件初始化接口 启动web服务 并注册api 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.module = module.(*Web) 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 := this.module.modelUser.User_Create(&pb.DBUser{ Binduid: req.Account, Sid: req.Sid, }) if err != nil { log.Errorf("create user err: %v", err) rsp.Code = pb.ErrorCode_SqlExecutionError } rsp.Account = req.Account rsp.Code = pb.ErrorCode_Success } else { rsp.Code = pb.ErrorCode_ReqParameterError } c.JSON(http.StatusOK, rsp) }