From 648fc0f370236aee50da98a127f1a0aca0ba38a6 Mon Sep 17 00:00:00 2001 From: ZiHangQin <1420014281@qq.com> Date: Tue, 18 Jul 2023 16:07:56 +0800 Subject: [PATCH] =?UTF-8?q?new=EF=BC=9A=E6=96=B0=E5=A2=9Ewithdraw=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=EF=BC=8C=E8=AF=A5=E6=9A=82=E6=97=A0=E7=9C=9F=E5=AE=9E?= =?UTF-8?q?=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/dataSources.xml | 11 ++++ .idea/electricity_bill_calc_service.iml | 8 +++ .idea/misc.xml | 6 ++ .idea/modules.xml | 8 +++ .idea/vcs.xml | 6 ++ controller/user.go | 27 ++++----- controller/withdraw.go | 74 +++++++++++++++++++++++++ doc/routerSetting.md | 13 +++++ global/db.go | 3 + global/redis.go | 7 ++- response/user_response.go | 2 +- router/router.go | 25 +++++---- service/user.go | 2 +- settings.yaml | 4 +- 14 files changed, 165 insertions(+), 31 deletions(-) create mode 100644 .idea/dataSources.xml create mode 100644 .idea/electricity_bill_calc_service.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 controller/withdraw.go create mode 100644 doc/routerSetting.md diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml new file mode 100644 index 0000000..0973c28 --- /dev/null +++ b/.idea/dataSources.xml @@ -0,0 +1,11 @@ + + + + + postgresql + true + org.postgresql.Driver + jdbc:postgresql://39.105.39.8:9432/postgres + + + \ No newline at end of file diff --git a/.idea/electricity_bill_calc_service.iml b/.idea/electricity_bill_calc_service.iml new file mode 100644 index 0000000..c956989 --- /dev/null +++ b/.idea/electricity_bill_calc_service.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..28a804d --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..95c0f54 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/controller/user.go b/controller/user.go index c79385e..7240728 100644 --- a/controller/user.go +++ b/controller/user.go @@ -42,34 +42,35 @@ type _LoginForm struct { } func doLogin(c *fiber.Ctx) error { - result := response.NewResult(c) - loginData := new(_LoginForm) - if err := c.BodyParser(loginData); err != nil { + result := response.NewResult(c) //创建一个相应结果对象 + loginData := new(_LoginForm) //创建一个解析登录表单数据的实体 + if err := c.BodyParser(loginData); err != nil { //解析请求体中的Json数据到loginData里,如果解析出错就返回错误 userLog.Error("表单解析失败!", zap.Error(err)) - return result.Error(http.StatusInternalServerError, "表单解析失败。") + return result.Error(http.StatusInternalServerError, "表单解析失败。") //返回一个内部服务器错误的相应结果 } var ( session *model.Session err error ) - userLog.Info("有用户请求登录。", zap.String("username", loginData.Username), zap.Int16("type", loginData.Type)) - if loginData.Type == model.USER_TYPE_ENT { - session, err = service.UserService.ProcessEnterpriseUserLogin(loginData.Username, loginData.Password) + userLog.Info("有用户请求登录。", zap.String("username", loginData.Username), zap.Int16("type", loginData.Type)) //记录日志相关信息 + if loginData.Type == model.USER_TYPE_ENT { //根据登录类型选择不同的处理方法 + session, err = service.UserService.ProcessEnterpriseUserLogin(loginData.Username, loginData.Password) //企业用户 } else { - session, err = service.UserService.ProcessManagementUserLogin(loginData.Username, loginData.Password) + userLog.Info("该用户是管理用户") + session, err = service.UserService.ProcessManagementUserLogin(loginData.Username, loginData.Password) //管理用户 } if err != nil { - if authError, ok := err.(*exceptions.AuthenticationError); ok { - if authError.NeedReset { + if authError, ok := err.(*exceptions.AuthenticationError); ok { //检查错误是否为身份验证错误 + if authError.NeedReset { //如果需要重置密码则返回对应结果 return result.LoginNeedReset() } - return result.Error(int(authError.Code), authError.Message) + return result.Error(int(authError.Code), authError.Message) //返回身份验证错误相应 } else { userLog.Error("用户登录请求处理失败!", zap.Error(err)) - return result.Error(http.StatusInternalServerError, err.Error()) + return result.Error(http.StatusInternalServerError, err.Error()) //返回内部服务器错误 } } - return result.LoginSuccess(session) + return result.LoginSuccess(session) //返回登录成功相应结果,包含会话信息 } func doLogout(c *fiber.Ctx) error { diff --git a/controller/withdraw.go b/controller/withdraw.go new file mode 100644 index 0000000..cc490ac --- /dev/null +++ b/controller/withdraw.go @@ -0,0 +1,74 @@ +package controller + +import ( + "electricity_bill_calc/logger" + "electricity_bill_calc/response" + "github.com/gofiber/fiber/v2" + "go.uber.org/zap" +) + +var withdrawLog = logger.Named("Handler", "Withdraw") + +func InitializeWithdrawHandlers(router *fiber.App) { + router.Get("/withdraw", withdraw) +} + +//用于检索用户的核算报表 +func withdraw(c *fiber.Ctx) error { + //记录日志 + withdrawLog.Info("带分页的待审核的核算撤回申请列表") + //获取请求参数 + result := response.NewResult(c) + keyword := c.Query("keyword", "") + page := c.QueryInt("page", 1) + withdrawLog.Info("参数为: ", zap.String("keyword", keyword), zap.Int("page", page)) + //中间数据库操作暂且省略。。。。 + //首先进行核算报表的分页查询 + + //TODO: 2023-07-18 此处的data需要经过上面数据库查询后进行数据返回,此处只是作于演示 + data := fiber.Map{ + "report": fiber.Map{ + "id": "string", + "parkId": "string", + "periodBegin": "string", + "periodEnd": "string", + "published": true, + "publishedAt": "string", + "withdraw": 0, + "lastWithdrawAppliedAt": "string", + "lastWithdrawAuditAt": "string", + "status": 0, + "message": "string", + }, + "park": fiber.Map{ + "id": "string", + "userId": "string", + "name": "string", + "tenement": "string", + "area": "string", + "capacity": "string", + "category": 0, + "meter04kvType": 0, + "region": "string", + "address": "string", + "contact": "string", + "phone": "string", + }, + "user": fiber.Map{ + "id": "string", + "name": "string", + "contact": "string", + "phone": "string", + "region": "string", + "address": "string", + }, + } + datas := make([]interface{}, 0) + datas = append(datas, data) + //TODO: 2023-07-18 此处返回值是个示例,具体返回值需要查询数据库 + return result.Success( + "withdraw请求成功", + response.NewPagedResponse(page, 20).ToMap(), + fiber.Map{"records": datas}, + ) +} diff --git a/doc/routerSetting.md b/doc/routerSetting.md new file mode 100644 index 0000000..6e9c627 --- /dev/null +++ b/doc/routerSetting.md @@ -0,0 +1,13 @@ +## fiber +#### fiber实例 +- app(是fiber创建的实例通常用app表示,其中有可选配置选项) + - BodyLimit 设置请求正文允许的最大大小(默认为4 * 1024 * 1024) + - EnablePrintRoutes 不打印框架自带日志(默认false) + - EnableTrustedProxyCheck 禁用受信代理(默认false) + - Prefork 预处理配置(默认false) + - ErrorHandler 全局错误处理 (默认false) + - JSONEncoder json编码 (默认json.Marshal) + - JSONDecoder json解码 (默认json.Unmarshal) + - 。。。。。。。。(还有很多配置) +- Use(中间件设置,一个或者多个) +- Group(类似于gin框架中的路由分组) \ No newline at end of file diff --git a/global/db.go b/global/db.go index 1acdc74..a19c5f4 100644 --- a/global/db.go +++ b/global/db.go @@ -53,6 +53,9 @@ func (ql QueryLogger) TraceQueryStart(ctx context.Context, conn *pgx.Conn, data ql.logger.Info("查询参数", lo.Map(data.Args, func(elem any, index int) zap.Field { return zap.Any(fmt.Sprintf("[Arg %d]: ", index), elem) })...) + // for index, arg := range data.Args { + // ql.logger.Info(fmt.Sprintf("[Arg %d]: %v", index, arg)) + // } return ctx } diff --git a/global/redis.go b/global/redis.go index e99c19f..8afb039 100644 --- a/global/redis.go +++ b/global/redis.go @@ -15,10 +15,13 @@ var ( func SetupRedisConnection() error { var err error + a := fmt.Sprintf("%s:%d", config.RedisSettings.Host, config.RedisSettings.Port) + fmt.Println(a) Rd, err = rueidis.NewClient(rueidis.ClientOption{ - InitAddress: []string{fmt.Sprintf("%s:%d", config.RedisSettings.Host, config.RedisSettings.Port)}, - Password: config.RedisSettings.Password, + InitAddress: []string{"127.0.0.1:6379"}, + Password: "", SelectDB: config.RedisSettings.DB, + DisableCache:true, }) if err != nil { return err diff --git a/response/user_response.go b/response/user_response.go index 9eaf458..a60086e 100644 --- a/response/user_response.go +++ b/response/user_response.go @@ -16,7 +16,7 @@ type LoginResponse struct { func (r Result) LoginSuccess(session *model.Session) error { res := &LoginResponse{} res.Code = http.StatusOK - res.Message = "用户已成功登录。" + res.Message = "用户已成功登录。"+ "👋!" res.NeedReset = false res.Session = session return r.Ctx.Status(fiber.StatusOK).JSON(res) diff --git a/router/router.go b/router/router.go index fe8c98f..908558d 100644 --- a/router/router.go +++ b/router/router.go @@ -25,24 +25,24 @@ func init() { } func App() *fiber.App { - app := fiber.New(fiber.Config{ - BodyLimit: 30 * 1024 * 1024, - EnablePrintRoutes: true, - EnableTrustedProxyCheck: false, - Prefork: false, - ErrorHandler: errorHandler, - JSONEncoder: json.Marshal, - JSONDecoder: json.Unmarshal, + app := fiber.New(fiber.Config{ //创建fiber实例的时候选择配置选项 + BodyLimit: 30 * 1024 * 1024, //设置请求正文允许的最大大小。 + EnablePrintRoutes: true, //自定义方案,用于启动消息 + EnableTrustedProxyCheck: false, //禁用受信代理 + Prefork: false, //禁止预处理(如果要启用预处理则需要通过shell脚本运行) + ErrorHandler: errorHandler, //相应全局处理错误 + JSONEncoder: json.Marshal, //json编码 + JSONDecoder: json.Unmarshal, //json解码 }) - app.Use(compress.New()) + app.Use(compress.New()) //压缩中间件 app.Use(recover.New(recover.Config{ EnableStackTrace: true, StackTraceHandler: stackTraceHandler, - })) + })) //恢复中间件 app.Use(logger.NewLogMiddleware(logger.LogMiddlewareConfig{ Logger: logger.Named("App"), - })) - app.Use(security.SessionRecovery) + })) //日志中间件 + app.Use(security.SessionRecovery) //会话恢复中间件 controller.InitializeUserHandlers(app) controller.InitializeRegionHandlers(app) @@ -53,6 +53,7 @@ func App() *fiber.App { controller.InitializeInvoiceHandler(app) controller.InitializeTopUpHandlers(app) controller.InitializeReportHandlers(app) + controller.InitializeWithdrawHandlers(app) return app } diff --git a/service/user.go b/service/user.go index a81dc3f..2474921 100644 --- a/service/user.go +++ b/service/user.go @@ -80,7 +80,7 @@ func (us _UserService) ProcessEnterpriseUserLogin(username, password string) (*m us.log.Error("处理企业用户登录失败。", zap.String("username", username), zap.Error(err)) return nil, err } - token, _ := uuid.NewRandom() + token, _ := uuid.NewRandom() //生成uuid作为会话的token使用 userSession := &model.Session{ Uid: user.Id, Name: user.Username, diff --git a/settings.yaml b/settings.yaml index 4a0e548..0c4d01c 100644 --- a/settings.yaml +++ b/settings.yaml @@ -1,8 +1,8 @@ Database: User: electricity Pass: nLgxPO5s8gK2tR0OL0Q - Host: postgres - Port: 5432 + Host: 39.105.39.8 + Port: 9432 DB: electricity MaxIdleConns: 0 MaxOpenConns: 20