diff --git a/server/app/internal/handlers/actions/auth.go b/server/app/internal/handlers/actions/auth.go index 59e7101..56f0efa 100644 --- a/server/app/internal/handlers/actions/auth.go +++ b/server/app/internal/handlers/actions/auth.go @@ -2,6 +2,7 @@ package actions import ( "strings" + "time" "github.com/gin-gonic/gin" "github.com/iapologizewhenimwrong/Vestmore_GO/model" @@ -20,10 +21,12 @@ var CompanyKey = "vestmore-bjwl" // app_market: string; // lang: string; // token: string; -func BaseGetToken(ctx *gin.Context, param *BaseGetTokenParam, resp *basic.Response) { +func BaseGetToken(ctx *gin.Context, param *BaseGetTokenParam) (resp *basic.Response) { ctx.ShouldBind(param) // model.Models.KillaraCustomerModel.Find() log.Println() + + return resp.Success() } // @Action account/loginWithTelephonePassword @@ -40,22 +43,22 @@ func BaseGetToken(ctx *gin.Context, param *BaseGetTokenParam, resp *basic.Respon // app_market: uint64; // email: string; // timestamp: int64; -func AccountLoginWithTelephonePassword(ctx *gin.Context, param *AccountLoginWithTelephonePasswordParam, resp *basic.Response) { +func AccountLoginWithTelephonePassword(ctx *gin.Context, param *AccountLoginWithTelephonePasswordParam) (resp *basic.Response) { // ctx.ShouldBind() // model.Models.KillaraCustomerModel.Find() if param.CountryCode == "" { - resp.ErrorEx(1, "country_code 参数缺失") + resp.ErrorMsg(1, "country_code 参数缺失") return } if param.Telephone == "" { - resp.ErrorEx(1, "telephone 参数缺失") + resp.ErrorMsg(1, "telephone 参数缺失") return } if param.Password == "" { - resp.ErrorEx(1, "password 参数缺失") + resp.ErrorMsg(1, "password 参数缺失") return } @@ -70,43 +73,38 @@ func AccountLoginWithTelephonePassword(ctx *gin.Context, param *AccountLoginWith customer, err = model.Models.KillaraCustomerModel.GetCustomerByTelephoneForBackEnd(telephone) if err != nil { - resp.ErrorEx(1, err.Error()) + resp.ErrorMsg(1, err.Error()) return } if customer == nil { customer, err = model.Models.KillaraCustomerModel.GetCustomerByCode(telephone) if err != nil { - resp.ErrorEx(1, err.Error()) + resp.ErrorMsg(1, err.Error()) return } } if customer == nil { - resp.ErrorEx(1, "账号未注册") - return + return resp.ErrorMsg(1, "账号未注册") } if *customer.CountryCode != countryCode { - resp.ErrorEx(1, "电话号码国家不正确") - return + return resp.ErrorMsg(1, "电话号码国家不正确") } if *customer.Status != 1 { - resp.ErrorEx(1, "账号已禁用,无法登录") - return + return resp.ErrorMsg(1, "账号已禁用,无法登录") } if !auth.CheckPassword(*customer.Password, *customer.Salt, *customer.RandomPassword, password) { - resp.ErrorEx(1, "账号或密码错误") - return + return resp.ErrorMsg(1, "账号或密码错误") } customerID := *customer.CustomerId err = model.Models.KillaraCustomerTokenModel.UpdateTokenCustomerID(param.Token, customerID) if err != nil { - resp.ErrorEx(1, err.Error()) - return + return resp.ErrorMsg(1, err.Error()) } var deviceCode string @@ -127,20 +125,25 @@ func AccountLoginWithTelephonePassword(ctx *gin.Context, param *AccountLoginWith log.Println(deviceCode, version, ip) - // data := model.KillaraCustomerDevice{ - // CustomerId: &customerID, - // Device: &deviceCode, - // Version: &version, - // Ip: &ip, - // AppMarket: ¶m., - // Date: time.Now().Format("2006-01-02 15:04:05"), - // } + data := &model.KillaraCustomerDevice{ + CustomerId: &customerID, + Device: &deviceCode, + Version: &version, + Ip: &ip, + AppMarket: ¶m.AppMarket, + Date: basic.TimePtr(time.Now()), + } - // // 假设 insertCustomerDevice 是对应的服务接口 + // 假设 insertCustomerDevice 是对应的服务接口 // insertCustomerDevice(data) + err = model.Models.KillaraCustomerModel.InsertCustomerDevice(data) + if err != nil { + return resp.ErrorErr(1, err) + } // // 假设 clearDuplicateToken 是对应的服务接口 // clearDuplicateToken(customerID, param.Token) + model.Models.KillaraCustomerTokenModel.ClearDuplicateToken(customerID, param.Token, 1) // return map[string]interface{}{ // "success": true, @@ -150,6 +153,7 @@ func AccountLoginWithTelephonePassword(ctx *gin.Context, param *AccountLoginWith // }, nil // log.Println(param) + return resp.Success() } // @Action account/registerSmsCode @@ -158,9 +162,10 @@ func AccountLoginWithTelephonePassword(ctx *gin.Context, param *AccountLoginWith // country_code?: string; // telephone?: string; // token: string; -func AccountRegisterSmsCode(ctx *gin.Context, param *AccountRegisterSmsCodeParam, resp *basic.Response) { +func AccountRegisterSmsCode(ctx *gin.Context, param *AccountRegisterSmsCodeParam) (resp *basic.Response) { // ctx.ShouldBind() log.Println() + return resp.Success() } // @Action account/forgetSmsCode @@ -169,9 +174,10 @@ func AccountRegisterSmsCode(ctx *gin.Context, param *AccountRegisterSmsCodeParam // country_code?: string; // telephone?: string; // token: string; -func AccountForgetSmsCode(ctx *gin.Context, param *AccountForgetSmsCodeParam, resp *basic.Response) { +func AccountForgetSmsCode(ctx *gin.Context, param *AccountForgetSmsCodeParam) (resp *basic.Response) { // ctx.ShouldBind() log.Println() + return resp.Success() } type RegisterValidEmailCode struct { @@ -189,12 +195,11 @@ type RegisterValidEmailCode struct { // email: string; // timestamp: int64; // token: string; -func AccountRegisterEmailCode(ctx *gin.Context, param *AccountRegisterEmailCodeParam, resp *basic.Response) { +func AccountRegisterEmailCode(ctx *gin.Context, param *AccountRegisterEmailCodeParam) (resp *basic.Response) { log.Println(param) if !email.IsEmailValid(param.Email) { - resp.Error(basic.ErrEmailFormat) - return + return resp.Error(basic.ErrEmailFormat) } gcm := encryption_decryption.NewSecretGCM[RegisterValidEmailCode](CompanyKey) @@ -207,13 +212,13 @@ func AccountRegisterEmailCode(ctx *gin.Context, param *AccountRegisterEmailCodeP } tokenstr, err := gcm.Encrypt(codetoken) if err != nil { - resp.Error(basic.ErrEncGcm) - return + return resp.Error(basic.ErrEncGcm) } resp.Success(map[string]any{ "token": tokenstr, }) + return resp.Success() } // @Action member/alterPassword @@ -223,9 +228,10 @@ func AccountRegisterEmailCode(ctx *gin.Context, param *AccountRegisterEmailCodeP // new_password: string; // old_password: string; // token?: string; -func MemberAlterPassword(ctx *gin.Context, param *MemberAlterPasswordParam, resp *basic.Response) { +func MemberAlterPassword(ctx *gin.Context, param *MemberAlterPasswordParam) (resp *basic.Response) { // ctx.ShouldBind() log.Println() + return resp.Success() } // @Action account/loginWithEmailPassword @@ -240,7 +246,8 @@ func MemberAlterPassword(ctx *gin.Context, param *MemberAlterPasswordParam, resp // email: string; // timestamp: int64; // token: string; -func AccountLoginWithEmailPassword(ctx *gin.Context, param *AccountLoginWithEmailPasswordParam, resp *basic.Response) { +func AccountLoginWithEmailPassword(ctx *gin.Context, param *AccountLoginWithEmailPasswordParam) (resp *basic.Response) { log.Println(param) + return resp.Success() } diff --git a/server/app/internal/handlers/actions/types_gen.go b/server/app/internal/handlers/actions/types_gen.go index af08965..2afba6f 100644 --- a/server/app/internal/handlers/actions/types_gen.go +++ b/server/app/internal/handlers/actions/types_gen.go @@ -1,28 +1,27 @@ package actions import ( - "/utils/basic" - "/utils/log" - "github.com/gin-gonic/gin" + "github.com/iapologizewhenimwrong/Vestmore_GO/utils/basic" + "github.com/iapologizewhenimwrong/Vestmore_GO/utils/log" ) var HandlersFuncRoutes map[string]gin.HandlerFunc = make(map[string]gin.HandlerFunc) func init() { - // func AccountForgetSmsCode(ctx *gin.Context, param *AccountForgetSmsCodeParam, resp *basic.Response) + // func AccountForgetSmsCode(ctx *gin.Context, param *AccountForgetSmsCodeParam) (resp *basic.Response) HandlersFuncRoutes["account/forgetSmsCode"] = AccountForgetSmsCodeHandler - // func AccountLoginWithEmailPassword(ctx *gin.Context, param *AccountLoginWithEmailPasswordParam, resp *basic.Response) + // func AccountLoginWithEmailPassword(ctx *gin.Context, param *AccountLoginWithEmailPasswordParam) (resp *basic.Response) HandlersFuncRoutes["account/loginWithEmailPassword"] = AccountLoginWithEmailPasswordHandler - // func AccountLoginWithTelephonePassword(ctx *gin.Context, param *AccountLoginWithTelephonePasswordParam, resp *basic.Response) + // func AccountLoginWithTelephonePassword(ctx *gin.Context, param *AccountLoginWithTelephonePasswordParam) (resp *basic.Response) HandlersFuncRoutes["account/loginWithTelephonePassword"] = AccountLoginWithTelephonePasswordHandler - // func AccountRegisterEmailCode(ctx *gin.Context, param *AccountRegisterEmailCodeParam, resp *basic.Response) + // func AccountRegisterEmailCode(ctx *gin.Context, param *AccountRegisterEmailCodeParam) (resp *basic.Response) HandlersFuncRoutes["account/registerEmailCode"] = AccountRegisterEmailCodeHandler - // func AccountRegisterSmsCode(ctx *gin.Context, param *AccountRegisterSmsCodeParam, resp *basic.Response) + // func AccountRegisterSmsCode(ctx *gin.Context, param *AccountRegisterSmsCodeParam) (resp *basic.Response) HandlersFuncRoutes["account/registerSmsCode"] = AccountRegisterSmsCodeHandler - // func BaseGetToken(ctx *gin.Context, param *BaseGetTokenParam, resp *basic.Response) + // func BaseGetToken(ctx *gin.Context, param *BaseGetTokenParam) (resp *basic.Response) HandlersFuncRoutes["base/getToken"] = BaseGetTokenHandler - // func MemberAlterPassword(ctx *gin.Context, param *MemberAlterPasswordParam, resp *basic.Response) + // func MemberAlterPassword(ctx *gin.Context, param *MemberAlterPasswordParam) (resp *basic.Response) HandlersFuncRoutes["member/alterPassword"] = MemberAlterPasswordHandler } @@ -34,18 +33,21 @@ type AccountForgetSmsCodeParam struct { } func AccountForgetSmsCodeHandler(ctx *gin.Context) { - resp := &basic.Response{IsSuccess: true} - defer ctx.JSON(200, resp) + var resp *basic.Response param := &AccountForgetSmsCodeParam{} err := ctx.ShouldBind(param) if err != nil { log.Println(err) - resp.Error(basic.ErrParamParse) + resp = resp.Error(basic.ErrParamParse) return } - AccountForgetSmsCode(ctx, param, resp) + resp = AccountForgetSmsCode(ctx, param) + if resp == nil { + resp = resp.Error(basic.ErrRespNotNil) + } + ctx.JSON(200, resp) } type AccountLoginWithEmailPasswordParam struct { @@ -62,18 +64,21 @@ type AccountLoginWithEmailPasswordParam struct { } func AccountLoginWithEmailPasswordHandler(ctx *gin.Context) { - resp := &basic.Response{IsSuccess: true} - defer ctx.JSON(200, resp) + var resp *basic.Response param := &AccountLoginWithEmailPasswordParam{} err := ctx.ShouldBind(param) if err != nil { log.Println(err) - resp.Error(basic.ErrParamParse) + resp = resp.Error(basic.ErrParamParse) return } - AccountLoginWithEmailPassword(ctx, param, resp) + resp = AccountLoginWithEmailPassword(ctx, param) + if resp == nil { + resp = resp.Error(basic.ErrRespNotNil) + } + ctx.JSON(200, resp) } type AccountLoginWithTelephonePasswordParam struct { @@ -92,18 +97,21 @@ type AccountLoginWithTelephonePasswordParam struct { } func AccountLoginWithTelephonePasswordHandler(ctx *gin.Context) { - resp := &basic.Response{IsSuccess: true} - defer ctx.JSON(200, resp) + var resp *basic.Response param := &AccountLoginWithTelephonePasswordParam{} err := ctx.ShouldBind(param) if err != nil { log.Println(err) - resp.Error(basic.ErrParamParse) + resp = resp.Error(basic.ErrParamParse) return } - AccountLoginWithTelephonePassword(ctx, param, resp) + resp = AccountLoginWithTelephonePassword(ctx, param) + if resp == nil { + resp = resp.Error(basic.ErrRespNotNil) + } + ctx.JSON(200, resp) } type AccountRegisterEmailCodeParam struct { @@ -117,18 +125,21 @@ type AccountRegisterEmailCodeParam struct { } func AccountRegisterEmailCodeHandler(ctx *gin.Context) { - resp := &basic.Response{IsSuccess: true} - defer ctx.JSON(200, resp) + var resp *basic.Response param := &AccountRegisterEmailCodeParam{} err := ctx.ShouldBind(param) if err != nil { log.Println(err) - resp.Error(basic.ErrParamParse) + resp = resp.Error(basic.ErrParamParse) return } - AccountRegisterEmailCode(ctx, param, resp) + resp = AccountRegisterEmailCode(ctx, param) + if resp == nil { + resp = resp.Error(basic.ErrRespNotNil) + } + ctx.JSON(200, resp) } type AccountRegisterSmsCodeParam struct { @@ -139,18 +150,21 @@ type AccountRegisterSmsCodeParam struct { } func AccountRegisterSmsCodeHandler(ctx *gin.Context) { - resp := &basic.Response{IsSuccess: true} - defer ctx.JSON(200, resp) + var resp *basic.Response param := &AccountRegisterSmsCodeParam{} err := ctx.ShouldBind(param) if err != nil { log.Println(err) - resp.Error(basic.ErrParamParse) + resp = resp.Error(basic.ErrParamParse) return } - AccountRegisterSmsCode(ctx, param, resp) + resp = AccountRegisterSmsCode(ctx, param) + if resp == nil { + resp = resp.Error(basic.ErrRespNotNil) + } + ctx.JSON(200, resp) } type BaseGetTokenParam struct { @@ -161,18 +175,21 @@ type BaseGetTokenParam struct { } func BaseGetTokenHandler(ctx *gin.Context) { - resp := &basic.Response{IsSuccess: true} - defer ctx.JSON(200, resp) + var resp *basic.Response param := &BaseGetTokenParam{} err := ctx.ShouldBind(param) if err != nil { log.Println(err) - resp.Error(basic.ErrParamParse) + resp = resp.Error(basic.ErrParamParse) return } - BaseGetToken(ctx, param, resp) + resp = BaseGetToken(ctx, param) + if resp == nil { + resp = resp.Error(basic.ErrRespNotNil) + } + ctx.JSON(200, resp) } type MemberAlterPasswordParam struct { @@ -184,16 +201,19 @@ type MemberAlterPasswordParam struct { } func MemberAlterPasswordHandler(ctx *gin.Context) { - resp := &basic.Response{IsSuccess: true} - defer ctx.JSON(200, resp) + var resp *basic.Response param := &MemberAlterPasswordParam{} err := ctx.ShouldBind(param) if err != nil { log.Println(err) - resp.Error(basic.ErrParamParse) + resp = resp.Error(basic.ErrParamParse) return } - MemberAlterPassword(ctx, param, resp) + resp = MemberAlterPassword(ctx, param) + if resp == nil { + resp = resp.Error(basic.ErrRespNotNil) + } + ctx.JSON(200, resp) } diff --git a/server/app/internal/handlers/gen_action_routes_test.go b/server/app/internal/handlers/gen_action_routes_test.go index b758e18..0dedf5f 100644 --- a/server/app/internal/handlers/gen_action_routes_test.go +++ b/server/app/internal/handlers/gen_action_routes_test.go @@ -22,24 +22,21 @@ func TestGenActionsHandlerFunc(t *testing.T) { createActionRoutesGen() } -func TestCase2(t *testing.T) { - eg := `// device: string;` - re := regexp.MustCompile(` +([a-zA-Z_\-/]+)(\?{0,1}): +(\w+) ?;`) - vs := re.FindAllStringSubmatch(eg, -1) - log.Println(vs) -} - // 生成路由代码 func createActionRoutesGen() { var err error - af := getActionsInfo() + moduleName := GetModuleName(6) + af := getActionsInfo(moduleName) tpl, err := template.ParseGlob("./*.tpl") if err != nil { panic(err) } // genFile(tpl, "gen_action_routes.tpl", "./action_routes_gen.go", af) - genFile(tpl, "types_gen.tpl", "./actions/types_gen.go", af) + genFile(tpl, "types_gen.tpl", "./actions/types_gen.go", map[string]any{ + "ActionFunctions": af, + "ModuleName": moduleName, + }) } func genFile(tpl *template.Template, executeTemplate string, genFilePath string, tplParam any) { @@ -55,7 +52,7 @@ func genFile(tpl *template.Template, executeTemplate string, genFilePath string, panic(err) } - log.Println(string(data)) + // log.Println(string(data)) f, err := os.OpenFile(genFilePath, os.O_TRUNC|os.O_WRONLY|os.O_CREATE, 0644) if err != nil { @@ -85,15 +82,14 @@ type paramStruct struct { ParamFields []paramStructField } -func getActionsInfo() map[string]*actionsFunc { +func getActionsInfo(moduleName string) map[string]*actionsFunc { regActionRe := regexp.MustCompile(`(?i)action +([^ ]+)`) paramStructRe := regexp.MustCompile(` +([a-zA-Z_\-/]+)(\?{0,1}): +(\w+) ?;`) - moduleName := GetModuleName(6) dir := "actions" actionsMap := make(map[string]*actionsFunc) - + // actionsMap["ModuleName"] = moduleName err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { if err != nil { return err diff --git a/server/app/internal/handlers/types_gen.tpl b/server/app/internal/handlers/types_gen.tpl index cd01540..f640ac3 100644 --- a/server/app/internal/handlers/types_gen.tpl +++ b/server/app/internal/handlers/types_gen.tpl @@ -9,13 +9,13 @@ import ( var HandlersFuncRoutes map[string]gin.HandlerFunc = make(map[string]gin.HandlerFunc) func init() { -{{- range .}} - // func {{.FuncName}}(ctx *gin.Context, param *{{.ParamStruct.ParamStructName}}, resp *basic.Response) +{{- range .ActionFunctions}} + // func {{.FuncName}}(ctx *gin.Context, param *{{.ParamStruct.ParamStructName}}) (resp *basic.Response) HandlersFuncRoutes["{{.ActionName}}"] = {{.FuncName}}Handler {{- end}} } -{{- range .}} +{{- range .ActionFunctions}} type {{.ParamStruct.ParamStructName}} struct { {{- range .ParamStruct.ParamFields}} {{.ParamNameCamel}} {{.ParamType}} `json:"{{.ParamName}}" form:"{{.ParamName}}" binding:"{{.ParamBinding}}"` @@ -23,17 +23,20 @@ type {{.ParamStruct.ParamStructName}} struct { } func {{.FuncName}}Handler(ctx *gin.Context) { - resp := &basic.Response{IsSuccess: true} - defer ctx.JSON(200, resp) + var resp *basic.Response param := &{{.ParamStruct.ParamStructName}}{} err := ctx.ShouldBind(param) if err != nil { log.Println(err) - resp.Error(basic.ErrParamParse) + resp = resp.Error(basic.ErrParamParse) return } - {{.FuncName}}(ctx, param, resp) + resp = {{.FuncName}}(ctx, param) + if resp == nil { + resp = resp.Error(basic.ErrRespNotNil) + } + ctx.JSON(200, resp) } {{end}} \ No newline at end of file diff --git a/utils/basic/error_code.go b/utils/basic/error_code.go index 11abaa1..57bd89e 100644 --- a/utils/basic/error_code.go +++ b/utils/basic/error_code.go @@ -6,6 +6,8 @@ type ErrorCode struct { } var ( + ErrRespNotNil = &ErrorCode{Code: 10000, Message: "resp must not nil"} + ErrEncGcm = &ErrorCode{Code: 10001, Message: "gmc加密错误"} ErrParamParse = &ErrorCode{Code: 10100, Message: "参数解析错误"} diff --git a/utils/basic/ptr_type.go b/utils/basic/ptr_type.go index 315fe9e..ddc5965 100644 --- a/utils/basic/ptr_type.go +++ b/utils/basic/ptr_type.go @@ -1,5 +1,49 @@ package basic +import "time" + +func IntPtr(src int) *int { + return &src +} + +func Int32Ptr(src int32) *int32 { + return &src +} + +func Int64Ptr(src int64) *int64 { + return &src +} + +func UintPtr(src uint) *uint { + return &src +} + +func Uint32Ptr(src uint32) *uint32 { + return &src +} + +func Uint64Ptr(src uint64) *uint64 { + return &src +} + +// + +func Float32Ptr(src float32) *float32 { + return &src +} + +func Float64Ptr(src float64) *float64 { + return &src +} + +func BytesPtr(src []byte) *[]byte { + return &src +} + func StringPtr(src string) *string { return &src } + +func TimePtr(time time.Time) *time.Time { + return &time +} diff --git a/utils/basic/types.go b/utils/basic/types.go index 91d7903..1f19ffa 100644 --- a/utils/basic/types.go +++ b/utils/basic/types.go @@ -1,5 +1,7 @@ package basic +import "github.com/iapologizewhenimwrong/Vestmore_GO/utils/log" + // 全局返回的结构体 type Response struct { Data interface{} `json:"data"` @@ -8,23 +10,49 @@ type Response struct { IsSuccess bool `json:"success"` } -func (resp *Response) Error(errcode *ErrorCode, Data ...interface{}) { +func (resp *Response) Error(errcode *ErrorCode, Data ...interface{}) *Response { + if resp == nil { + resp = &Response{} + } resp.ErrorCode = errcode.Code resp.ErrorText = errcode.Message resp.IsSuccess = false resp.setData(Data) + log.Error(resp.ErrorText) + return resp } -func (resp *Response) ErrorEx(Code int, Message string, Data ...interface{}) { +func (resp *Response) ErrorErr(Code int, err error, Data ...interface{}) *Response { + if resp == nil { + resp = &Response{} + } + resp.ErrorCode = Code + resp.ErrorText = err.Error() + resp.IsSuccess = false + resp.setData(Data) + log.Error(resp.ErrorText) + return resp +} + +func (resp *Response) ErrorMsg(Code int, Message string, Data ...interface{}) *Response { + if resp == nil { + resp = &Response{} + } resp.ErrorCode = Code resp.ErrorText = Message resp.IsSuccess = false resp.setData(Data) + log.Error(resp.ErrorText) + return resp } -func (resp *Response) Success(Data ...interface{}) { +func (resp *Response) Success(Data ...interface{}) *Response { + if resp == nil { + resp = &Response{} + } resp.IsSuccess = true resp.setData(Data) + return resp } func (resp *Response) setData(Data []interface{}) {