This commit is contained in:
renzhiyuan 2024-11-04 14:05:53 +08:00
parent 6f255b66d2
commit 76421e59b9
20 changed files with 101 additions and 117 deletions

View File

@ -21,5 +21,5 @@ type Rpc struct {
}
type Nacos struct {
Target string
Endpoints []string
}

View File

@ -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)
}

View File

@ -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

View File

@ -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,

View File

@ -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())
}

View File

@ -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()

7
sh/build.sh Normal file
View File

@ -0,0 +1,7 @@
#!/bin/bash
docker build -t "${IMAGE}:${VERSION}" . --no-cache
docker tag "${IMAGE}:${VERSION}" ${ADDRESS}:${VERSION}
docker push ${ADDRESS}:${VERSION}

8
sh/build_produce.sh Normal file
View File

@ -0,0 +1,8 @@
#!/bin/bash
. $(pwd)/sh/config.sh
Environment="produce"
ADDRESS="${PRODUCE_ADDRESS}"
echo "produce"> $(pwd)/env
. $(pwd)/sh/build.sh

7
sh/build_test.sh Normal file
View File

@ -0,0 +1,7 @@
#!/bin/bash
. $(pwd)/sh/config.sh
Environment="test"
ADDRESS="${TEST_ADDRESS}"
echo "test"> $(pwd)/env
. $(pwd)/sh/build.sh

7
sh/config.sh Normal file
View File

@ -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"

View File

@ -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}"

View File

@ -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}

View File

@ -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}

17
sh/depoly.sh Normal file
View File

@ -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}"

View File

@ -1,9 +0,0 @@
#!/bin/bash
# supervisord
supervisord -c /etc/supervisord.conf

View File

@ -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

View File

@ -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

23
untils/env.go Normal file
View File

@ -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
}

View File

@ -7,6 +7,7 @@ const (
)
var CodeMsg = map[string]string{
HTTP_SUCCESS: "操作成功",
HTTP_AUTH_FAIL: "验签失败",
HTTP_SUCCESS: "操作成功",
HTTP_AUTH_FAIL: "验签失败",
HTTP_REUEST_FAIL: "操作失败",
}

View File

@ -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 + `