diff --git a/cmd/api/internal/config/config.go b/cmd/api/internal/config/config.go index 5dbfc69..4b65bb7 100644 --- a/cmd/api/internal/config/config.go +++ b/cmd/api/internal/config/config.go @@ -21,5 +21,5 @@ type Rpc struct { } type Nacos struct { - Target string + Endpoints []string } diff --git a/cmd/api/internal/logic/yl/ylLogic.go b/cmd/api/internal/logic/yl/ylLogic.go index 67bd33e..6ac7fdf 100644 --- a/cmd/api/internal/logic/yl/ylLogic.go +++ b/cmd/api/internal/logic/yl/ylLogic.go @@ -6,6 +6,7 @@ import ( "github.com/bytedance/sonic" "rs/cmd/api/internal/logic/vo" "rs/rpc/transfer" + "strings" "time" "rs/cmd/api/internal/svc" @@ -57,7 +58,7 @@ func (l *YlLogic) Yl(req *types.Req) (resp *types.Resp, err error) { reqData.Sign = l.svcCtx.Config.Sys.PrimaryKey result, err := l.svcCtx.TransferRpc.MarketKeySend(l.ctx, &reqData) if err != nil { - return nil, fmt.Errorf("请求失败:%v", err) + return nil, fmt.Errorf("rpc请求失败:%v", err.Error()) } if result.ErrCode != vo.RES_SUCCESS { return nil, fmt.Errorf("请求失败:%v", result.Msg) @@ -70,12 +71,22 @@ func (l *YlLogic) Yl(req *types.Req) (resp *types.Resp, err error) { if err != nil { endTime = startTime.AddDate(0, 0, 30).Add(23*time.Hour + 59*time.Minute + 59*time.Second) } + if result.Data == nil { + return nil, fmt.Errorf("请求失败:%v", result.Msg) + } return &types.Resp{ CdKey: result.Data.VoucherCode, CdNum: "", - ExchangeUrl: fmt.Sprintf("%s%s", l.svcCtx.Config.Sys.Url, result.Data.VoucherCode), + ExchangeUrl: l.getExchangeUrl(result.Data.VoucherCode), SupplierOrderNo: req.DeliverOrderNo, StartTime: startTime.Format(time.DateTime), EndTime: endTime.Add(23*time.Hour + 59*time.Minute + 59*time.Second).Format(time.DateTime), }, nil } + +func (l *YlLogic) getExchangeUrl(voucherCode string) string { + if strings.Contains(voucherCode, "http") { + return voucherCode + } + return fmt.Sprintf("%s%s", l.svcCtx.Config.Sys.Url, voucherCode) +} diff --git a/cmd/api/internal/middleware/signMiddleware.go b/cmd/api/internal/middleware/signMiddleware.go index 34f5c88..c4c00f0 100644 --- a/cmd/api/internal/middleware/signMiddleware.go +++ b/cmd/api/internal/middleware/signMiddleware.go @@ -45,6 +45,7 @@ func (m *SignMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc { } signString := reqDataMap["sign"].(string) sysSign, err := sign.GetSign(reqDataMap, m.c.Sys.Key) + if sysSign != signString { response.Err(w, response.HTTP_AUTH_FAIL, "签名不正确") return diff --git a/cmd/api/internal/svc/serviceContext.go b/cmd/api/internal/svc/serviceContext.go index fe62ea3..1d449b9 100644 --- a/cmd/api/internal/svc/serviceContext.go +++ b/cmd/api/internal/svc/serviceContext.go @@ -16,12 +16,10 @@ type ServiceContext struct { } func NewServiceContext(c config.Config) *ServiceContext { - conn := zrpc.MustNewClient(zrpc.RpcClientConf{ - Target: c.Nacos.Target, - //Endpoints: []string{"127.0.0.1:10001"}, - }) + client := transferClient.NewTransfer(zrpc.MustNewClient(zrpc.RpcClientConf{ + Endpoints: c.Nacos.Endpoints, + })) - client := transferClient.NewTransfer(conn) base := types.BaseServiceContext{ Config: c, TransferRpc: client, diff --git a/cmd/api/internal/test/sign_test.go b/cmd/api/internal/test/sign_test.go index c1fddbf..06c04b8 100644 --- a/cmd/api/internal/test/sign_test.go +++ b/cmd/api/internal/test/sign_test.go @@ -5,16 +5,17 @@ import ( "fmt" "rs/untils/sign" "testing" + "time" ) -const PrimaryKey = "da4953198bce4bc8eca9ff7101ae7eb8" +const PrimaryKey = "7af832e02270c317ad2ed97ded249dce" func TestSign(t *testing.T) { var ( reqDataMap map[string]interface{} ) - j := "{\"createTime\":1721111144,\"deliverOrderNo\":\"DELIVER_1721111144961285\",\"extendParams\":\"{\\\"appId\\\":2783278,\\\"num\\\":1,\\\"posId\\\":2783278}\",\"requestTime\":1721111144,\"sign\":\"NDEyNWNhODMwY2FjYjJmNTgwYWY5Njk1ODJjMTI1YmEyYzNlN2M0MTlmNDliZjg5MDc0NTQ3MDVkYjI1MDAyOA==\",\"supplierId\":51,\"supplierSkuId\":808,\"thirdSkuId\":\"yl20240712\"}" + j := "{\"createTime\":1725517987,\"deliverOrderNo\":\"DELIVER_1725517987405652\",\"extendParams\":\"{\\\"appId\\\":\\\"YL20220531\\\",\\\"num\\\":1,\\\"posId\\\":\\\"YL20220531\\\"}\",\"requestTime\":1725517987,\"sign\":\"NGQzZGVkODI3NTk5MGQxNTM1MDdkMjY3ZGY3YzI2ODViNTk0NTgyYmI0YmY2YmUzMzNlNGRhZTY4OTg4NjU2NQ==\",\"supplierId\":10702124,\"supplierSkuId\":632,\"thirdSkuId\":\"20240806YLYK\"}" err := json.Unmarshal([]byte(j), &reqDataMap) if err != nil { panic("j") @@ -22,3 +23,8 @@ func TestSign(t *testing.T) { sysSign, err := sign.GetSign(reqDataMap, PrimaryKey) fmt.Println(sysSign) } + +func TestTime(t *testing.T) { + + fmt.Println(time.Now().Unix()) +} diff --git a/cmd/api/yl.go b/cmd/api/yl.go index 74f3718..7deaf1b 100644 --- a/cmd/api/yl.go +++ b/cmd/api/yl.go @@ -3,6 +3,7 @@ package main import ( "flag" "fmt" + until "rs/untils" "rs/cmd/api/internal/config" "rs/cmd/api/internal/handler" @@ -12,7 +13,7 @@ import ( "github.com/zeromicro/go-zero/rest" ) -var configFile = flag.String("f", "etc/yl-test.yaml", "the config file") +var configFile = flag.String("f", fmt.Sprintf("%s%s", "etc/", until.GetConfig()), "the config file") func main() { flag.Parse() diff --git a/sh/build.sh b/sh/build.sh new file mode 100644 index 0000000..058ccd0 --- /dev/null +++ b/sh/build.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +docker build -t "${IMAGE}:${VERSION}" . --no-cache + +docker tag "${IMAGE}:${VERSION}" ${ADDRESS}:${VERSION} + +docker push ${ADDRESS}:${VERSION} \ No newline at end of file diff --git a/sh/build_produce.sh b/sh/build_produce.sh new file mode 100644 index 0000000..8a6ee0d --- /dev/null +++ b/sh/build_produce.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +. $(pwd)/sh/config.sh +Environment="produce" +ADDRESS="${PRODUCE_ADDRESS}" +echo "produce"> $(pwd)/env + +. $(pwd)/sh/build.sh \ No newline at end of file diff --git a/sh/build_test.sh b/sh/build_test.sh new file mode 100644 index 0000000..1dc39d3 --- /dev/null +++ b/sh/build_test.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +. $(pwd)/sh/config.sh +Environment="test" +ADDRESS="${TEST_ADDRESS}" +echo "test"> $(pwd)/env +. $(pwd)/sh/build.sh diff --git a/sh/config.sh b/sh/config.sh new file mode 100644 index 0000000..8ff01f2 --- /dev/null +++ b/sh/config.sh @@ -0,0 +1,7 @@ +IMAGE="transfer_yl" +CONTAINER="transfer_yl" +PORT="10102" +TEST_ADDRESS="registry.cn-chengdu.aliyuncs.com/go_ls/transfer_yl" +PRODUCE_ADDRESS="registry.cn-chengdu.aliyuncs.com/go_ls/transfer_yl_produce" +COMMIT_CONTENT="UPDATE" +VERSION="v3" diff --git a/sh/create.sh b/sh/create.sh deleted file mode 100644 index 77a6789..0000000 --- a/sh/create.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/bash - -IMAGE="transfer_rs:v1" -CONTAINER="transfer_rs" -API_PORT="10102" -V_REFLECT="" - -docker build -t "${IMAGE}" . --no-cache - -docker stop "${CONTAINER}" - -docker rm "${CONTAINER}" - -#docker run -it -p "${API_PORT}:${API_PORT}" --name "$CONTAINER" "${IMAGE}" diff --git a/sh/create_produce.sh b/sh/create_produce.sh deleted file mode 100644 index 36cbf54..0000000 --- a/sh/create_produce.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/bash - -IMAGE="transfer_yl" -RPC_CONTAINER="transfer_yl" -RPC_PORT="10102" -V_REFLECT="" -TAGS_NAME="v1.3" -ADDRESS="registry.cn-chengdu.aliyuncs.com/go_ls/transfer_yl_produce" - -docker build -t "${IMAGE}:${TAGS_NAME}" . --no-cache - -docker tag "${IMAGE}:${TAGS_NAME}" ${ADDRESS}:${TAGS_NAME} - -docker push ${ADDRESS}:${TAGS_NAME} \ No newline at end of file diff --git a/sh/create_test.sh b/sh/create_test.sh deleted file mode 100644 index 5e00ab1..0000000 --- a/sh/create_test.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/bin/bash - -IMAGE="transfer_yl" -RPC_CONTAINER="transfer_yl" -RPC_PORT="10102" -V_REFLECT="" -TAGS_NAME="v1.4" -ADDRESS="registry.cn-chengdu.aliyuncs.com/go_ls/transfer_yl" - -docker build -t "${IMAGE}:${TAGS_NAME}" . --no-cache - -docker tag "${IMAGE}:${TAGS_NAME}" ${ADDRESS}:${TAGS_NAME} - -docker push ${ADDRESS}:${TAGS_NAME} \ No newline at end of file diff --git a/sh/depoly.sh b/sh/depoly.sh new file mode 100644 index 0000000..668c15f --- /dev/null +++ b/sh/depoly.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +. $(pwd)/sh/config.sh + +ENV=$(cat "$(pwd)/env") +# 使用if-elif-else结构判断环境 +if [ "$ENV"=="produce" ]; then + ADDRESS="${PRODUCE_ADDRESS}" +elif [ "$ENV"=="test" ]; then + ADDRESS="${TEST_ADDRESS}" +else + ADDRESS="${PRODUCE_ADDRESS}" +fi + +echo docker pull ${ADDRESS}:${VERSION} +echo rm -f "${CONTAINER}" +echo docker run -it -p "${PORT}:${PORT}" --net transfer --name "${CONTAINER}" "${ADDRESS}:${VERSION}" \ No newline at end of file diff --git a/sh/startup.sh b/sh/startup.sh deleted file mode 100644 index 8397062..0000000 --- a/sh/startup.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/bash - -# supervisord -supervisord -c /etc/supervisord.conf - - - - - diff --git a/sh/supervisord.conf b/sh/supervisord.conf deleted file mode 100644 index 42d6233..0000000 --- a/sh/supervisord.conf +++ /dev/null @@ -1,29 +0,0 @@ -; supervisor config file - -[unix_http_server] -file=/var/run/supervisor.sock ; (the path to the socket file) -chmod=0700 ; sockef file mode (default 0700) - -[supervisord] -logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log) -pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid) -childlogdir=/var/log/supervisor ; ('AUTO' child log dir, default $TEMP) - -; the below section must remain in the config file for RPC -; (supervisorctl/web interface) to work, additional interfaces may be -; added by defining them in separate rpcinterface: sections -[rpcinterface:supervisor] -supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface - -[supervisorctl] -serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL for a unix socket - -; The [include] section can just contain the "files" setting. This -; setting can list multiple files (separated by whitespace or -; newlines). It can also contain wildcards. The filenames are -; interpreted as relative to this file. Included files *cannot* -; include files themselves. - - -[include] -files = /src/sh/supervisord_include/*.conf \ No newline at end of file diff --git a/sh/supervisord_include/start.conf b/sh/supervisord_include/start.conf deleted file mode 100644 index ece6d68..0000000 --- a/sh/supervisord_include/start.conf +++ /dev/null @@ -1,21 +0,0 @@ - -[program:api] -directory=/src/cmd/api -# 执行的命令 -command=/src/cmd/api/yl -#在 supervisord 启动的时候也自动启动 -autorstart=false -#程序异常退出后自动重启 -autorestart=true -#启动 5 秒后没有异常退出,就当作已经正常启动了 -startsecs=5 -#启动失败自动重试次数,默认是 3 -startretries=3 -#把 stderr 重定向到 stdout,默认 false -redirect_stderr=false -#stdout 日志文件大小,默认 50MB -stdout_logfile_maxbytes = 20MB -#stdout 日志文件备份数 -stdout_logfile_backups = 20 -stdout_logfile=/var/log/out.log -stderr_logfile=/var/log/err.log \ No newline at end of file diff --git a/untils/env.go b/untils/env.go new file mode 100644 index 0000000..5d0b571 --- /dev/null +++ b/untils/env.go @@ -0,0 +1,23 @@ +package until + +import ( + "fmt" + "os" +) + +func GetConfig() string { + var dir string + pwd, _ := os.Getwd() + path := fmt.Sprintf("%s%s", pwd, "/../../env") + bytes, _ := os.ReadFile(path) + env := string(bytes) + switch env { + case "produce\n": + dir = "produce.yaml" + case "test\n": + dir = "test.yaml" + default: + dir = "local.yaml" + } + return dir +} diff --git a/untils/response/code.go b/untils/response/code.go index c4d8caf..c667577 100644 --- a/untils/response/code.go +++ b/untils/response/code.go @@ -7,6 +7,7 @@ const ( ) var CodeMsg = map[string]string{ - HTTP_SUCCESS: "操作成功", - HTTP_AUTH_FAIL: "验签失败", + HTTP_SUCCESS: "操作成功", + HTTP_AUTH_FAIL: "验签失败", + HTTP_REUEST_FAIL: "操作失败", } diff --git a/untils/rsa/rsa_test.go b/untils/rsa/rsa_test.go index 796906d..d0a963e 100644 --- a/untils/rsa/rsa_test.go +++ b/untils/rsa/rsa_test.go @@ -13,9 +13,7 @@ const prik = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCmjK6SPkrP03ssex const pubk = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApoyukj5Kz9N7LHseQ8Z+CmXlZva0KCTLJa7tdxjYKBiiQKtd8cFBpepaRpYczPSDM4NJXTjtmtEkDz43NHy6w4yIBz3yE+h9YQxpGXT2heA9Dn4hxs5S8F6uxcsshnNiVyC7J4jAV3AsTRYW1tIwGicmKwSW96vOGucuQLStyAvng1egY9ieB5UFRioFvbtonOicZdt7GnF2Ooon4vihLqFzQia5mJYEVk2kKLy5kwi5BK5M8cxvQxUQ1hqM9uS7YQz6nAklJlhGyfqG8CBgeK9lgmxV72nlQUDkZclUcjVL2DCLYxwUCCopw1mesvCswNGCjqMRH69CnxPFJOD0AQIDAQAB" func Test_en(t *testing.T) { - data := make(map[string]interface{}, 2) - data["a"] = 1 - data["b"] = "sdadas" + data := "{\"createTime\":1727597377,\"deliverOrderNo\":\"DELIVER_test\",\"extendParams\":\"{\\\"appId\\\":\\\"YL20220531\\\",\\\"num\\\":1,\\\"posId\\\":\\\"YL20220531\\\"}\",\"requestTime\":1727597377,\"supplierId\":10702124,\"supplierSkuId\":632,\"thirdSkuId\":\"20240806YLYK\"}" dataJson, _ := json.Marshal(data) pub := `-----BEGIN PUBLIC KEY----- ` + pubk + `