From 424e67acdc87f622079e26d7e8f269e1add38bb0 Mon Sep 17 00:00:00 2001 From: zzw <1464003642@qq.com> Date: Tue, 11 Aug 2026 17:12:37 +0800 Subject: [PATCH] feat: test ci cd --- .dockerignore | 9 +++ .gitea/workflows/cd-test.yml | 33 ++++++++ .gitea/workflows/ci.yml | 41 ++++++++-- .gitignore | 3 +- README.md | 81 +++++++++++++++++-- admin/Dockerfile | 34 ++++++++ bff/Dockerfile | 35 +++++++- deploy/docker-compose.mysql.yml | 16 ---- deploy/docker-compose.redis.yml | 14 ---- deploy/docker-compose.yml | 2 +- pkg/utils/status.go | 14 +++- product/Dockerfile | 34 ++++++++ product/internal/logic/createlogic.go | 16 ++-- product/internal/logic/editapplylogic.go | 14 +++- product/internal/logic/editbaselogic.go | 13 +-- .../internal/logic/editsusceptiblelogic.go | 11 ++- product/internal/logic/infologic.go | 7 +- product/internal/logic/itemslogic.go | 8 +- product/internal/logic/response.go | 8 -- product/internal/logic/sortlogic.go | 10 ++- product/internal/logic/statuslogic.go | 12 ++- product/internal/logic/verifylogic.go | 2 +- product/internal/logic/verifystatuslogic.go | 13 +-- product/validator/validator.go | 4 +- 24 files changed, 340 insertions(+), 94 deletions(-) create mode 100644 .dockerignore create mode 100644 .gitea/workflows/cd-test.yml create mode 100644 admin/Dockerfile delete mode 100644 deploy/docker-compose.mysql.yml delete mode 100644 deploy/docker-compose.redis.yml create mode 100644 product/Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..b73ac10 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,9 @@ +**/.git +**/logs +**/*.log +**/tmp +**/.idea +**/.vscode +**/node_modules +deploy/mysql/data +deploy/rnacos/data diff --git a/.gitea/workflows/cd-test.yml b/.gitea/workflows/cd-test.yml new file mode 100644 index 0000000..83c80b8 --- /dev/null +++ b/.gitea/workflows/cd-test.yml @@ -0,0 +1,33 @@ +name: CD Test + +on: + workflow_run: + workflows: + - CI + types: + - completed + workflow_dispatch: + +jobs: + deploy: + if: ${{ gitea.event_name == 'workflow_dispatch' || gitea.event.workflow_run.conclusion == 'success' }} + runs-on: runner + + defaults: + run: + shell: sh + + steps: + - name: Login Registry + run: | + echo "${{ secrets.REGISTRY_PASSWORD }}" | docker login ${{ vars.REGISTRY }} \ + -u "${{ vars.REGISTRY_USERNAME }}" \ + --password-stdin + + - name: Pull and Deploy + working-directory: /data/www/server/deploy + run: | + docker pull ${{ vars.REGISTRY }}/lone/bff:latest + docker pull ${{ vars.REGISTRY }}/lone/product:latest + docker pull ${{ vars.REGISTRY }}/lone/admin:latest + docker compose up -d diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml index 89058a6..c957c9b 100644 --- a/.gitea/workflows/ci.yml +++ b/.gitea/workflows/ci.yml @@ -7,6 +7,7 @@ on: paths: - "bff/**" - "product/**" + - "admin/**" - "pkg/**" - ".gitea/workflows/ci.yml" workflow_dispatch: @@ -17,6 +18,7 @@ jobs: outputs: bff: ${{ steps.filter.outputs.bff }} product: ${{ steps.filter.outputs.product }} + admin: ${{ steps.filter.outputs.admin }} steps: - name: Checkout uses: https://git.ailuowan.com/deploy/checkout@v4 @@ -28,10 +30,12 @@ jobs: run: | bff=false product=false + admin=false if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then bff=true product=true + admin=true else before="${{ github.event.before }}" if [ -z "$before" ] || [ "$before" = "0000000000000000000000000000000000000000" ]; then @@ -45,11 +49,13 @@ jobs: echo "$changed" | grep -qE '^(bff|pkg)/|^\.gitea/workflows/ci\.yml$' && bff=true || true echo "$changed" | grep -qE '^(product|pkg)/|^\.gitea/workflows/ci\.yml$' && product=true || true + echo "$changed" | grep -qE '^(admin|pkg)/|^\.gitea/workflows/ci\.yml$' && admin=true || true fi echo "bff=$bff" >> "$GITHUB_OUTPUT" echo "product=$product" >> "$GITHUB_OUTPUT" - echo "bff=$bff product=$product" + echo "admin=$admin" >> "$GITHUB_OUTPUT" + echo "bff=$bff product=$product admin=$admin" docker-bff: needs: changes @@ -57,7 +63,6 @@ jobs: runs-on: runner env: NAME: lone/bff - CONTEXT: ./bff steps: - name: Checkout uses: https://git.ailuowan.com/deploy/checkout@v4 @@ -69,8 +74,9 @@ jobs: - name: Docker build run: | docker build \ + -f bff/Dockerfile \ -t ${{ vars.REGISTRY }}/$NAME:latest \ - $CONTEXT + . - name: Docker push run: | @@ -82,7 +88,6 @@ jobs: runs-on: runner env: NAME: lone/product - CONTEXT: ./product steps: - name: Checkout uses: https://git.ailuowan.com/deploy/checkout@v4 @@ -94,8 +99,34 @@ jobs: - name: Docker build run: | docker build \ + -f product/Dockerfile \ -t ${{ vars.REGISTRY }}/$NAME:latest \ - $CONTEXT + . + + - name: Docker push + run: | + docker push ${{ vars.REGISTRY }}/$NAME:latest + + docker-admin: + needs: changes + if: needs.changes.outputs.admin == 'true' + runs-on: runner + env: + NAME: lone/admin + steps: + - name: Checkout + uses: https://git.ailuowan.com/deploy/checkout@v4 + + - name: Login Registry + run: | + echo "${{ secrets.REGISTRY_PASSWORD }}" | docker login ${{ vars.REGISTRY }} -u "${{ vars.REGISTRY_USERNAME }}" --password-stdin + + - name: Docker build + run: | + docker build \ + -f admin/Dockerfile \ + -t ${{ vars.REGISTRY }}/$NAME:latest \ + . - name: Docker push run: | diff --git a/.gitignore b/.gitignore index a7fa598..274bfc8 100644 --- a/.gitignore +++ b/.gitignore @@ -9,7 +9,8 @@ deploy/redis/data *.log **/logs/ -deploy/docker-compose.override.yml +deploy/*.yml + runner.conf Dockerfile.dev */tmp \ No newline at end of file diff --git a/README.md b/README.md index 7b5186f..9120946 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,36 @@ +## 常用命令 + +### 命令 ①:goctl 生成/更新脚手架 + +```bash +goctl -I. -I../pkg/third_party rpc protoc proto/order.proto --go_out=. --go-grpc_out=. --zrpc_out=. +``` + +### 命令 ②:生成服务端 pb / grpc + +```bash +goctl rpc protoc proto/order.proto --proto_path=. --proto_path=../pkg/third_party --go_out=. --go-grpc_out=. --zrpc_out=. +``` + +**什么时候跑:** + +- 只改 **校验规则**(必填/长度/范围等)→ **只跑这条** +- 增删改字段、增删 RPC → 也要跑(服务端描述符要更新) + +### 命令 ③:生成 BFF ProtoSet + +```bash +protoc -I. -I../pkg/third_party --descriptor_set_out=../bff/etc/order.pb --include_imports proto/order.proto +``` + +**什么时候跑:** + +- 增删改 **字段**(BFF gateway 编解码需要) +- 增删 **RPC** +- 改 **google.api.http** 路由 + + + ## 如何加一个新服务 以加 `order` 为例(对标现有 `product`)。 @@ -11,6 +44,8 @@ go install google.golang.org/protobuf/cmd/protoc-gen-go@latest go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest ``` + + ### 1. 创建 RPC 服务(goctl) 在仓库根目录执行: @@ -20,6 +55,8 @@ goctl rpc new order cd order ``` + + ### 2. 将 proto 移到 `proto/` 目录 `goctl rpc new` 默认把 proto 放在服务根目录(如 `order/order.proto`)。统一挪到 `proto/`: @@ -29,6 +66,8 @@ mkdir proto mv order.proto proto/ ``` + + ### 3. Zero 兼容 Nacos 等配置 编辑 `order/internal/config/config.go`: @@ -88,6 +127,8 @@ type LogConf struct { ``` + + ### 4. 配置服务名与监听端口 编辑 `order/etc/order.yaml`: @@ -147,6 +188,8 @@ AppLog: ``` + + ### 5. 构建 mod 编辑 `order/go.mod` 增加本地包 pkg.local @@ -157,6 +200,8 @@ go 1.26 replace pkg.local => ../pkg ``` + + ### 6. 服务注册 编辑 `order/order.go`: @@ -173,6 +218,7 @@ replace pkg.local => ../pkg ``` 增加初始化 + ```go func main() { @@ -308,9 +354,11 @@ func NewServiceContext(c config.Config, db *gorm.DB) *ServiceContext { ``` + + ### 7. 接入 Docker Compose -在 `deploy/docker-compose.override.yml` 增加服务: +在 `deploy/docker-compose.dev.override.yml` 增加服务: ```yaml order: @@ -328,6 +376,8 @@ func NewServiceContext(c config.Config, db *gorm.DB) *ServiceContext { - rnacos ``` + + ### 8. 生成 ProtoSet 给 BFF ```bash @@ -335,6 +385,8 @@ func NewServiceContext(c config.Config, db *gorm.DB) *ServiceContext { protoc --include_imports --proto_path=proto --descriptor_set_out=../bff/etc/order.pb order.proto ``` + + ### 9. 在 BFF 配 Upstream(HTTP 路由写在 proto) 在对应 RPC 上声明 `google.api.http`(需 `import "google/api/annotations.proto";`): @@ -359,6 +411,8 @@ rpc Ping(Request) returns (Response) { - etc/order.pb ``` + + ### 编写业务须知 只改业务逻辑(`internal/logic/`)不必跑 goctl。 @@ -368,16 +422,21 @@ rpc Ping(Request) returns (Response) { --- + + ### 命令 ①:生成服务端 pb / grpc ```bash -goctl rpc protoc proto/admin.proto --proto_path=. --proto_path=../pkg/third_party --go_out=. --go-grpc_out=. --zrpc_out=. +goctl rpc protoc proto/order.proto --proto_path=. --proto_path=../pkg/third_party --go_out=. --go-grpc_out=. --zrpc_out=. ``` **什么时候跑:** + - 只改 **校验规则**(必填/长度/范围等)→ **只跑这条** - 增删改字段、增删 RPC → 也要跑(服务端描述符要更新) + + ### 命令 ②:生成 BFF ProtoSet ```bash @@ -390,10 +449,12 @@ protoc -I. -I../pkg/third_party --descriptor_set_out=../bff/etc/order.pb --inclu - 增删 **RPC** - 改 **google.api.http** 路由 + + ### 命令 ③:goctl 生成/更新脚手架 ```bash -goctl rpc protoc proto/order.proto --go_out=. --go-grpc_out=. --zrpc_out=. +goctl -I. -I../pkg/third_party rpc protoc proto/order.proto --go_out=. --go-grpc_out=. --zrpc_out=. ``` **什么时候跑:** @@ -401,9 +462,11 @@ goctl rpc protoc proto/order.proto --go_out=. --go-grpc_out=. --zrpc_out=. - **新增 RPC**:需要生成 `internal/logic`、更新 `server` / `orderclient` 等 - 注意:可能覆盖已改过的 `order.go` 等,生成后对比合并;也可用手写 logic/server 代替 + + ## Proto 常用校验规则(protovalidate) -文档:https://protovalidate.com/schemas/standard-rules/ +文档:[https://protovalidate.com/schemas/standard-rules/](https://protovalidate.com/schemas/standard-rules/) 依赖:`import "buf/validate/validate.proto";`,服务启动挂 `pkg.local/validate` 拦截器。 ### 必填字符串 @@ -429,6 +492,8 @@ double price = 6 [(buf.validate.field).double = {gte: 0}]; int32 period_validity = 16 [(buf.validate.field).int32 = {gte: 0}]; ``` + + ### 非必填数值(0 表示未传,跳过校验) ```protobuf @@ -454,22 +519,28 @@ uint32 sales_model = 4 [(buf.validate.field) = { }]; ``` + + ### ID 必须大于 0 ```protobuf int64 id = 1 [(buf.validate.field).int64 = {gt: 0}]; ``` + + ### 无规则字段 ```protobuf uint32 number = 20; // 不做 protovalidate ``` + + ### 删除字段编号(避免复用) ```protobuf // reserved 2; ``` -业务条件校验(如「普通商品必须带齐价格」)仍写在 `internal/logic`,不要全塞进 proto。 +业务条件校验(如「普通商品必须带齐价格」)仍写在 `internal/logic`,不要全塞进 proto。 \ No newline at end of file diff --git a/admin/Dockerfile b/admin/Dockerfile new file mode 100644 index 0000000..1c5da96 --- /dev/null +++ b/admin/Dockerfile @@ -0,0 +1,34 @@ +FROM golang:1.26.5-alpine AS builder + +WORKDIR /src + +ENV GOPROXY=https://goproxy.cn,direct \ + CGO_ENABLED=0 \ + GOOS=linux \ + GOARCH=amd64 + +COPY pkg ./pkg +COPY admin/go.mod admin/go.sum ./admin/ + +WORKDIR /src/admin +RUN go mod download + +COPY admin/ ./ +RUN go build -ldflags="-s -w" -o /out/admin . + +FROM alpine:3.22 + +WORKDIR /app + +ENV TZ=Asia/Shanghai + +RUN apk add --no-cache tzdata \ + && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ + && echo "Asia/Shanghai" > /etc/timezone + +COPY --from=builder /out/admin . +COPY admin/etc/admin.yaml ./etc/admin.yaml + +EXPOSE 10200 + +CMD ["./admin", "-f", "etc/admin.yaml"] diff --git a/bff/Dockerfile b/bff/Dockerfile index 279a881..f0888f4 100644 --- a/bff/Dockerfile +++ b/bff/Dockerfile @@ -1 +1,34 @@ -FROM golang:1.26.5 AS builder +FROM golang:1.26.5-alpine AS builder + +WORKDIR /src + +ENV GOPROXY=https://goproxy.cn,direct \ + CGO_ENABLED=0 \ + GOOS=linux \ + GOARCH=amd64 + +COPY pkg ./pkg +COPY bff/go.mod bff/go.sum ./bff/ + +WORKDIR /src/bff +RUN go mod download + +COPY bff/ ./ +RUN go build -ldflags="-s -w" -o /out/bff . + +FROM alpine:3.22 + +WORKDIR /app + +ENV TZ=Asia/Shanghai + +RUN apk add --no-cache tzdata \ + && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ + && echo "Asia/Shanghai" > /etc/timezone + +COPY --from=builder /out/bff . +COPY bff/etc/ ./etc/ + +EXPOSE 10000 + +CMD ["./bff", "-f", "etc/bff.yaml"] diff --git a/deploy/docker-compose.mysql.yml b/deploy/docker-compose.mysql.yml deleted file mode 100644 index 1f9dd98..0000000 --- a/deploy/docker-compose.mysql.yml +++ /dev/null @@ -1,16 +0,0 @@ -services: - mysql: - image: mysql:8.0 - restart: unless-stopped - environment: - MYSQL_ROOT_PASSWORD: "123123" - ports: - - "3306:3306" - volumes: - - ./mysql/data:/var/lib/mysql - # - ./mysql/init:/docker-entrypoint-initdb.d:ro - -networks: - default: - external: true - name: services-network diff --git a/deploy/docker-compose.redis.yml b/deploy/docker-compose.redis.yml deleted file mode 100644 index 70f799b..0000000 --- a/deploy/docker-compose.redis.yml +++ /dev/null @@ -1,14 +0,0 @@ -services: - redis: - image: redis:7.4-alpine - restart: unless-stopped - ports: - - "6379:6379" - volumes: - - ./redis/data:/data - command: ["redis-server", "--appendonly", "yes"] - -networks: - default: - external: true - name: services-network diff --git a/deploy/docker-compose.yml b/deploy/docker-compose.yml index 7cc430b..84292ed 100644 --- a/deploy/docker-compose.yml +++ b/deploy/docker-compose.yml @@ -27,4 +27,4 @@ services: networks: default: external: true - name: services-network \ No newline at end of file + name: services-network diff --git a/pkg/utils/status.go b/pkg/utils/status.go index 82e8fa9..d132d68 100644 --- a/pkg/utils/status.go +++ b/pkg/utils/status.go @@ -73,6 +73,17 @@ var ( ErrorAuthority = Status{Code: 10008, Msg: "没有权限"} ErrorNoLoginInfo = Status{Code: 10003, Msg: "没有获取到登录信息"} + // 数据类 + ErrorDataIsExist = Status{Code: 11004, Msg: "数据已存在"} + + // 参数类 + ErrorReasonParams = Status{Code: 10001, Msg: "原因不能为空"} + + // ErrorNotFund 数据类 + ErrorFormatDataError = Status{Code: 11010, Msg: "格式化数据出错"} + + // —————————————————————————————————————————————————————————————————————————————————————— + // 基础类 ErrorExist = NewError(998, "已存在") ErrorOvertime = NewError(997, "请求已超时") @@ -81,7 +92,6 @@ var ( ErrorWecomHttpError = NewError(994, "企业微信请求失败") // 参数类 - ErrorReasonParams = NewError(10001, "原因不能为空") ErrorDefaultError = NewError(10004, "默认地址不能删除") ErrorJwtExpired = NewError(10005, "jwt过期") ErrorMissingProjectId = NewError(10006, "缺少权限项目ID") @@ -95,13 +105,11 @@ var ( // ErrorNotFund 数据类 ErrorNoChange = NewError(11002, "没有改变") - ErrorDataIsExist = NewError(11004, "数据已存在") ErrorRepeatPayment = NewError(11005, "不能重复支付") ErrorNotSufficientFunds = NewError(11006, "余额不足") ErrorSaleLevel = NewError(11007, "有下级销售,不允许修改") ErrorFirstTryErr = NewError(11008, "已购买过首次试用品") ErrorTryError = NewError(11009, "不满足购买条件") - ErrorFormatDataError = NewError(11010, "格式化数据出错") ErrorDataNotExist = NewError(11011, "数据不存在") ErrorBindError = NewError(11012, "设备已绑定,请不要重复绑定") ErrorBindOtherError = NewError(11013, "该设备已绑定其它用户,请不要重复绑定") diff --git a/product/Dockerfile b/product/Dockerfile new file mode 100644 index 0000000..59f0124 --- /dev/null +++ b/product/Dockerfile @@ -0,0 +1,34 @@ +FROM golang:1.26.5-alpine AS builder + +WORKDIR /src + +ENV GOPROXY=https://goproxy.cn,direct \ + CGO_ENABLED=0 \ + GOOS=linux \ + GOARCH=amd64 + +COPY pkg ./pkg +COPY product/go.mod product/go.sum ./product/ + +WORKDIR /src/product +RUN go mod download + +COPY product/ ./ +RUN go build -ldflags="-s -w" -o /out/product . + +FROM alpine:3.22 + +WORKDIR /app + +ENV TZ=Asia/Shanghai + +RUN apk add --no-cache tzdata \ + && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \ + && echo "Asia/Shanghai" > /etc/timezone + +COPY --from=builder /out/product . +COPY product/etc/product.yaml ./etc/product.yaml + +EXPOSE 10100 + +CMD ["./product", "-f", "etc/product.yaml"] diff --git a/product/internal/logic/createlogic.go b/product/internal/logic/createlogic.go index 8496140..946f817 100644 --- a/product/internal/logic/createlogic.go +++ b/product/internal/logic/createlogic.go @@ -68,13 +68,13 @@ func (l *CreateLogic) Create(in *product.CreateReq) (*product.Response, error) { return failResponse(utils.Fail), nil } if check.Id > 0 { - return failErr(utils.ErrorDataIsExist), nil + return failResponse(utils.ErrorDataIsExist), nil } - // 管理员信息后续从 ctx / metadata 取 - adminId := 0 - adminName := "" - + adminInfo := utils.GetUserFromCtx(l.ctx) + if adminInfo.ID < utils.NumberOne { + return failResponse(utils.ErrorNoLoginInfo), nil + } data := dao.ProductCreate{ Name: req.Name, Subhead: req.Subhead, @@ -103,8 +103,8 @@ func (l *CreateLogic) Create(in *product.CreateReq) (*product.Response, error) { IsBuy: uint8(req.IsBuy), PublishTime: publishTime, TryNumber: uint8(req.TryNumber), - AdminName: adminName, - AdminId: adminId, + AdminName: adminInfo.Name, + AdminId: int(adminInfo.ID), } if data.IsIndex == 0 { data.IsIndex = 2 @@ -153,7 +153,7 @@ func (l *CreateLogic) Create(in *product.CreateReq) (*product.Response, error) { return failResponse(utils.Fail), nil } - return okResponse(utils.StructToJson(data.Id)), nil + return okResponse(utils.StringEmpty), nil } func parsePublishTime(s string) (time.Time, error) { diff --git a/product/internal/logic/editapplylogic.go b/product/internal/logic/editapplylogic.go index 515041a..324315c 100644 --- a/product/internal/logic/editapplylogic.go +++ b/product/internal/logic/editapplylogic.go @@ -51,8 +51,11 @@ func (l *EditApplyLogic) EditApply(in *product.EditApplyReq) (*product.Response, return failResponse(utils.ErrorNotFund), nil } - // 管理员信息后续从 ctx / metadata 取 - adminId := 0 + adminInfo := utils.GetUserFromCtx(l.ctx) + if adminInfo.ID < utils.NumberOne { + return failResponse(utils.ErrorNoLoginInfo), nil + } + adminId := int(adminInfo.ID) info, err := apply.Get(l.ctx, adminId, int(req.Id)) if err != nil { @@ -90,8 +93,11 @@ func (l *EditApplyLogic) EditApplyPass(in *product.EditApplyPassReq) (*product.R return failResponse(utils.ErrorNotFund), nil } - // 管理员信息后续从 ctx / metadata 取 - adminId := 0 + adminInfo := utils.GetUserFromCtx(l.ctx) + if adminInfo.ID < utils.NumberOne { + return failResponse(utils.ErrorNoLoginInfo), nil + } + adminId := int(adminInfo.ID) if err = apply.Pass(l.ctx, adminId, int(req.Id)); err != nil { if apply.IsAlreadyHandled(err) { diff --git a/product/internal/logic/editbaselogic.go b/product/internal/logic/editbaselogic.go index 2511673..57cf3e0 100644 --- a/product/internal/logic/editbaselogic.go +++ b/product/internal/logic/editbaselogic.go @@ -41,12 +41,15 @@ func (l *EditBaseLogic) EditBase(in *product.EditBaseReq) (*product.Response, er return outResponse(utils.ErrorParams, msg), nil } - // 管理员信息后续从 ctx / metadata 取 - adminId := 0 - adminName := "" + adminInfo := utils.GetUserFromCtx(l.ctx) + if adminInfo.ID < utils.NumberOne { + return failResponse(utils.ErrorNoLoginInfo), nil + } + adminId := int(adminInfo.ID) + adminName := adminInfo.Name if !apply.CanEdit(l.ctx, adminId, int(req.Id)) { - return failErr(utils.ErrorAuthority), nil + return failResponse(utils.ErrorAuthority), nil } w := modelbase.Params{Eq: map[string]string{"id": strconv.FormatInt(req.Id, 10)}} @@ -68,7 +71,7 @@ func (l *EditBaseLogic) EditBase(in *product.EditBaseReq) (*product.Response, er return failResponse(utils.Fail), nil } if nameCheck.Id > 0 && nameCheck.Id != int(req.Id) { - return failErr(utils.ErrorDataIsExist), nil + return failResponse(utils.ErrorDataIsExist), nil } publishTime := time.Now() diff --git a/product/internal/logic/editsusceptiblelogic.go b/product/internal/logic/editsusceptiblelogic.go index 89273c7..a00d340 100644 --- a/product/internal/logic/editsusceptiblelogic.go +++ b/product/internal/logic/editsusceptiblelogic.go @@ -50,12 +50,15 @@ func (l *EditSusceptibleLogic) EditSusceptible(in *product.EditSusceptibleReq) ( } } - // 管理员信息后续从 ctx / metadata 取 - adminId := 0 - adminName := "" + adminInfo := utils.GetUserFromCtx(l.ctx) + if adminInfo.ID < utils.NumberOne { + return failResponse(utils.ErrorNoLoginInfo), nil + } + adminId := int(adminInfo.ID) + adminName := adminInfo.Name if !apply.CanEdit(l.ctx, adminId, int(req.Id)) { - return failErr(utils.ErrorAuthority), nil + return failResponse(utils.ErrorAuthority), nil } w := modelbase.Params{ diff --git a/product/internal/logic/infologic.go b/product/internal/logic/infologic.go index 26f1bc4..368c559 100644 --- a/product/internal/logic/infologic.go +++ b/product/internal/logic/infologic.go @@ -51,8 +51,11 @@ func (l *InfoLogic) Info(in *product.InfoReq) (*product.Response, error) { return failResponse(utils.ErrorNotFund), nil } - // 管理员信息后续从 ctx / metadata 取 - adminId := 0 + adminInfo := utils.GetUserFromCtx(l.ctx) + if adminInfo.ID < utils.NumberOne { + return failResponse(utils.ErrorNoLoginInfo), nil + } + adminId := int(adminInfo.ID) if applyData, err := apply.Get(l.ctx, adminId, info.Id); err == nil { info.Edit = applyData.Status } diff --git a/product/internal/logic/itemslogic.go b/product/internal/logic/itemslogic.go index b374a46..5df948e 100644 --- a/product/internal/logic/itemslogic.go +++ b/product/internal/logic/itemslogic.go @@ -65,9 +65,13 @@ func (l *ItemsLogic) Items(in *product.ItemsReq) (*product.Response, error) { return failResponse(utils.Fail), nil } + adminInfo := utils.GetUserFromCtx(l.ctx) + if adminInfo.ID < utils.NumberOne { + return failResponse(utils.ErrorNoLoginInfo), nil + } + adminId := int(adminInfo.ID) + items := make([]*product.Item, 0, len(info)) - // 管理员信息后续从 ctx / metadata 取 - adminId := 0 for _, row := range info { if applyData, err := apply.Get(l.ctx, adminId, row.Id); err == nil { row.Edit = applyData.Status diff --git a/product/internal/logic/response.go b/product/internal/logic/response.go index ac9713b..93b0c47 100644 --- a/product/internal/logic/response.go +++ b/product/internal/logic/response.go @@ -27,11 +27,3 @@ func outResponse(status utils.Status, msg string) *product.Response { Msg: msg, } } - -// failErr 兼容仍返回 utils.Error 的旧错误码 -func failErr(err utils.Error) *product.Response { - return &product.Response{ - Code: int32(err.GetCode()), - Msg: err.GetMsg(), - } -} diff --git a/product/internal/logic/sortlogic.go b/product/internal/logic/sortlogic.go index 64c2f20..2dc679c 100644 --- a/product/internal/logic/sortlogic.go +++ b/product/internal/logic/sortlogic.go @@ -52,10 +52,14 @@ func (l *SortLogic) Sort(in *product.SortReq) (*product.Response, error) { return failResponse(utils.ErrorNotFund), nil } + adminInfo := utils.GetUserFromCtx(l.ctx) + if adminInfo.ID < utils.NumberOne { + return failResponse(utils.ErrorNoLoginInfo), nil + } + info.Sort = uint16(req.Sort) - // 管理员信息后续从 ctx / metadata 取 - info.AdminName = "" - info.AdminId = 0 + info.AdminName = adminInfo.Name + info.AdminId = int(adminInfo.ID) if _, err = m.Edit(w, &info); err != nil { log.Errorf("product sort edit: %v", err) diff --git a/product/internal/logic/statuslogic.go b/product/internal/logic/statuslogic.go index 10a741c..b89fad1 100644 --- a/product/internal/logic/statuslogic.go +++ b/product/internal/logic/statuslogic.go @@ -39,7 +39,7 @@ func (l *StatusLogic) Status(in *product.StatusReq) (*product.Response, error) { } if req.Status == uint32(dao.ProductStatusDisabled) && len(req.Reason) < 1 { - return failErr(utils.ErrorReasonParams), nil + return failResponse(utils.ErrorReasonParams), nil } var info dao.ProductCheckExist @@ -59,12 +59,16 @@ func (l *StatusLogic) Status(in *product.StatusReq) (*product.Response, error) { return failResponse(utils.ErrorNotFund), nil } - // 管理员信息后续从 ctx / metadata 取 + adminInfo := utils.GetUserFromCtx(l.ctx) + if adminInfo.ID < utils.NumberOne { + return failResponse(utils.ErrorNoLoginInfo), nil + } + update := dao.ProductStatusUpdate{ Status: uint8(req.Status), Reason: req.Reason, - AdminName: "", - AdminId: 0, + AdminName: adminInfo.Name, + AdminId: int(adminInfo.ID), } if _, err = m.Edit(w, &update); err != nil { log.Errorf("product status edit: %v", err) diff --git a/product/internal/logic/verifylogic.go b/product/internal/logic/verifylogic.go index 9d42ce3..13a0540 100644 --- a/product/internal/logic/verifylogic.go +++ b/product/internal/logic/verifylogic.go @@ -108,7 +108,7 @@ func (l *VerifyLogic) Verify(in *product.VerifyReq) (*product.Response, error) { err = json.Unmarshal([]byte(v.Content), &content) if err != nil { log.Errorf("product verify content json: %v", err) - return failErr(utils.ErrorFormatDataError), nil + return failResponse(utils.ErrorFormatDataError), nil } items = append(items, &product.VerifyItem{ Name: row.Name, diff --git a/product/internal/logic/verifystatuslogic.go b/product/internal/logic/verifystatuslogic.go index e15360a..f5aab11 100644 --- a/product/internal/logic/verifystatuslogic.go +++ b/product/internal/logic/verifystatuslogic.go @@ -51,7 +51,7 @@ func (l *VerifyStatusLogic) verifyStatus(expectStatus uint8, in *product.VerifyS } if req.Status == uint32(dao.VerifyActionReject) && len(req.Reason) < 1 { - return failErr(utils.ErrorReasonParams), nil + return failResponse(utils.ErrorReasonParams), nil } var info dao.VerifyStatusRow @@ -70,9 +70,12 @@ func (l *VerifyStatusLogic) verifyStatus(expectStatus uint8, in *product.VerifyS return failResponse(utils.ErrorNotFund), nil } - // 管理员信息后续从 ctx / metadata 取 - adminId := 0 - adminName := "" + adminInfo := utils.GetUserFromCtx(l.ctx) + if adminInfo.ID < utils.NumberOne { + return failResponse(utils.ErrorNoLoginInfo), nil + } + adminId := int(adminInfo.ID) + adminName := adminInfo.Name nextStatus := dao.VerifyStatusPass reason := req.Reason @@ -163,7 +166,7 @@ func (l *VerifyStatusLogic) verifyStatus(expectStatus uint8, in *product.VerifyS if err != nil { log.Errorf("verify status tx: %v", err) if isJSONError(err) { - return failErr(utils.ErrorFormatDataError), nil + return failResponse(utils.ErrorFormatDataError), nil } return failResponse(utils.Fail), nil } diff --git a/product/validator/validator.go b/product/validator/validator.go index 242b148..8318668 100644 --- a/product/validator/validator.go +++ b/product/validator/validator.go @@ -203,8 +203,8 @@ type ProductEditSusceptibleValidator struct { SalePrice float64 SharePrice float64 AgentPrice float64 - SaleReward string `validate:"max=255"` - NormsNumber uint32 `validate:"required,min=1"` + SaleReward string `validate:"max=255"` + NormsNumber uint32 `validate:"required,min=1"` BoxNumber float64 Type uint32 `validate:"required,oneof=1 2 3"` }