This commit is contained in:
liangzai 2024-10-12 20:41:55 +08:00
parent 8c09120db6
commit 597e4f1b89
75 changed files with 5009 additions and 823 deletions

8
aes.go
View File

@ -48,13 +48,15 @@ type Data struct {
func main() {
// 定义 AES 密钥
key, _ := hex.DecodeString("85af347e771bd631bf734068ad5798cc")
key, _ := hex.DecodeString("0ebf6481d213c846070c8f2964617d42")
var data interface{}
data = map[string]interface{}{
"id_card": "45212220000827423X",
"name": "张荣宏",
"id_card": "45212220000827423X",
"name": "张荣宏",
"mobile_no": "18276151590",
"time_range": "5",
}
// 将结构体转为 JSON 字符串

View File

@ -33,7 +33,7 @@
},
{
"serviceId": "FLXG75FE",
"sourceId": "G04-HZ01",
"sourceId": "G04-HZ0jmkolnjiuoiujhn1",
"serviceName": "涉网风险",
"dataDescription": "全国公安部门的网络犯罪调查服务",
"group_cn": "法律相关",
@ -92,7 +92,7 @@
"sourceId": "G03-HZ01",
"serviceName": "易诉人群识别",
"dataDescription": "识别用户是否为易诉人群",
"group_cn": "法律相关",
"group_cn": "法律相关ddd",
"group": "FLXG"
},
{
@ -244,7 +244,7 @@
"sourceId": "G32-BJ05",
"serviceName": "团伙欺诈排查(通用版)",
"dataDescription": "团伙欺诈排查(通用版)",
"group_cn": "法律相关",
"group_cn": "法律相关ddd",
"group": "FLXG"
}
]

View File

@ -18,7 +18,7 @@ type response {
@server (
group: IVYZ
prefix: /api/v1
middleware: ApiAuthInterceptor
middleware: ApiAuthInterceptor,ApiMqsInterceptor
)
service api-api {
@handler IVYZ0B03
@ -43,7 +43,7 @@ service api-api {
@server (
group: FLXG
prefix: /api/v1
middleware: ApiAuthInterceptor
middleware: ApiAuthInterceptor,ApiMqsInterceptor
)
service api-api {
@handler FLXGCA3D
@ -80,7 +80,7 @@ service api-api {
@server (
group: QYGL
prefix: /api/v1
middleware: ApiAuthInterceptor
middleware: ApiAuthInterceptor,ApiMqsInterceptor
)
service api-api {
@handler QYGLB4C0
@ -102,7 +102,7 @@ service api-api {
@server (
group: YYSY
prefix: /api/v1
middleware: ApiAuthInterceptor
middleware: ApiAuthInterceptor,ApiMqsInterceptor
)
service api-api {
@handler YYSY6F2E
@ -127,7 +127,7 @@ service api-api {
@server (
group: JRZQ
prefix: /api/v1
middleware: ApiAuthInterceptor
middleware: ApiAuthInterceptor,ApiMqsInterceptor
)
service api-api {
@handler JRZQDCBE
@ -141,6 +141,5 @@ service api-api {
@handler JRZQ4AA8
post /JRZQ4AA8 (request) returns (response)
}

View File

@ -19,3 +19,8 @@ WestConfig:
Url: "http://proxy.tianyuanapi.com/api/invoke"
Key: "121a1e41fc1690dd6b90afbcacd80cf4"
SecretId: "449159"
UserRpc:
Etcd:
Hosts:
- 127.0.0.1:2379
Key: user.rpc

View File

@ -11,6 +11,11 @@ SentinelRpc:
Hosts:
- tyapi_etcd:2379
Key: sentinel.rpc
UserRpc:
Etcd:
Hosts:
- tyapi_etcd:2379
Key: user.rpc
KqPusherConf:
Brokers:
- tyapi_kafka:9092

View File

@ -0,0 +1,44 @@
package common
import (
"errors"
"fmt"
"reflect"
"tianyuan-api/pkg/crypto"
)
// EncryptFields 加密字段的函数,处理不同类型,并跳过空值字段
func EncryptStructFields(inputStruct interface{}, key string) (map[string]interface{}, error) {
encryptedFields := make(map[string]interface{})
// 使用反射获取结构体的类型和值
v := reflect.ValueOf(inputStruct)
if v.Kind() != reflect.Struct {
return nil, errors.New("input is not a struct")
}
// 遍历结构体字段
for i := 0; i < v.NumField(); i++ {
field := v.Type().Field(i)
fieldValue := v.Field(i)
// 如果字段为空值,跳过
if fieldValue.IsZero() {
continue
}
// 将字段的值转换为字符串进行加密
strValue := fmt.Sprintf("%v", fieldValue.Interface())
// 执行加密操作
encryptedValue, err := crypto.WestDexEncrypt(strValue, key)
if err != nil {
return nil, err
}
// 将加密后的值存入结果映射
encryptedFields[field.Name] = encryptedValue
}
return encryptedFields, nil
}

View File

@ -0,0 +1,21 @@
package common
func MapStructToAPIRequest(encryptedFields map[string]interface{}, fieldMapping map[string]string, wrapField string) map[string]interface{} {
apiRequest := make(map[string]interface{})
// 遍历字段映射表
for structField, apiField := range fieldMapping {
// 如果加密后的字段存在,才添加到请求
if value, exists := encryptedFields[structField]; exists {
apiRequest[apiField] = value
}
}
// 如果 wrapField 不为空,将 apiRequest 包裹到该字段下
if wrapField != "" {
return map[string]interface{}{
wrapField: apiRequest,
}
}
return apiRequest
}

View File

@ -11,6 +11,7 @@ type Config struct {
DataSource string // 数据库连接的 DSN 字符串
CacheRedis cache.CacheConf // 缓存配置,使用 go-zero 自带的缓存配置结构体
SentinelRpc zrpc.RpcClientConf
UserRpc zrpc.RpcClientConf
KqPusherConf KqPusherConf
WestConfig WestConfig
}

View File

@ -1,6 +1,7 @@
package IVYZ
import (
xhttp "github.com/zeromicro/x/http"
"net/http"
"tianyuan-api/pkg/response"
@ -14,10 +15,9 @@ func IVYZ5733Handler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.Request
if err := httpx.Parse(r, &req); err != nil {
response.Fail(r.Context(), w, err)
xhttp.JsonBaseResponseCtx(r.Context(), w, err)
return
}
l := IVYZ.NewIVYZ5733Logic(r.Context(), svcCtx)
resp, err := l.IVYZ5733(&req)
if err != nil {

View File

@ -19,7 +19,7 @@ import (
func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
server.AddRoutes(
rest.WithMiddlewares(
[]rest.Middleware{serverCtx.ApiAuthInterceptor},
[]rest.Middleware{serverCtx.ApiAuthInterceptor, serverCtx.ApiMqsInterceptor},
[]rest.Route{
{
Method: http.MethodPost,
@ -78,7 +78,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
server.AddRoutes(
rest.WithMiddlewares(
[]rest.Middleware{serverCtx.ApiAuthInterceptor},
[]rest.Middleware{serverCtx.ApiAuthInterceptor, serverCtx.ApiMqsInterceptor},
[]rest.Route{
{
Method: http.MethodPost,
@ -117,7 +117,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
server.AddRoutes(
rest.WithMiddlewares(
[]rest.Middleware{serverCtx.ApiAuthInterceptor},
[]rest.Middleware{serverCtx.ApiAuthInterceptor, serverCtx.ApiMqsInterceptor},
[]rest.Route{
{
Method: http.MethodPost,
@ -134,21 +134,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/JRZQ8203",
Handler: JRZQ.JRZQ8203Handler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/JRZQCEE8",
Handler: JRZQ.JRZQCEE8Handler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/JRZQDCBE",
Handler: JRZQ.JRZQDCBEHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/JRZQFEF8",
Handler: JRZQ.JRZQFEF8Handler(serverCtx),
},
}...,
),
rest.WithPrefix("/api/v1"),
@ -156,7 +146,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
server.AddRoutes(
rest.WithMiddlewares(
[]rest.Middleware{serverCtx.ApiAuthInterceptor},
[]rest.Middleware{serverCtx.ApiAuthInterceptor, serverCtx.ApiMqsInterceptor},
[]rest.Route{
{
Method: http.MethodPost,
@ -168,11 +158,6 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/QYGL45BD",
Handler: QYGL.QYGL45BDHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/QYGL51BC",
Handler: QYGL.QYGL51BCHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/QYGL6F2D",
@ -195,7 +180,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
server.AddRoutes(
rest.WithMiddlewares(
[]rest.Middleware{serverCtx.ApiAuthInterceptor},
[]rest.Middleware{serverCtx.ApiAuthInterceptor, serverCtx.ApiMqsInterceptor},
[]rest.Route{
{
Method: http.MethodPost,

View File

@ -2,6 +2,13 @@ package FLXG
import (
"context"
"encoding/hex"
"encoding/json"
"tianyuan-api/apps/api/internal/common"
"tianyuan-api/apps/api/internal/validator"
"tianyuan-api/apps/api/internal/westmodel"
"tianyuan-api/pkg/crypto"
"tianyuan-api/pkg/errs"
"tianyuan-api/apps/api/internal/svc"
"tianyuan-api/apps/api/internal/types"
@ -24,7 +31,64 @@ func NewFLXG162ALogic(ctx context.Context, svcCtx *svc.ServiceContext) *FLXG162A
}
func (l *FLXG162ALogic) FLXG162A(req *types.Request) (resp *types.Response, err error) {
// todo: add your logic here and delete this line
secretKey, ok := l.ctx.Value("secretKey").(string)
if !ok {
return &types.Response{}, errs.ErrSystem
}
transactionID, ok := l.ctx.Value("transactionID").(string)
if !ok {
return &types.Response{}, errs.ErrSystem
}
// 1、解密
key, decodeErr := hex.DecodeString(secretKey)
if decodeErr != nil {
return nil, errs.ErrSystem
}
decryptData, aesDecryptErr := crypto.AesDecrypt(req.Data, key)
if aesDecryptErr != nil || len(decryptData) == 0 {
return nil, errs.ErrParamDecryption
}
return
// 2、校验
var data validator.FLXG162ARequest
if validatorErr := validator.ValidateAndParse(decryptData, &data); validatorErr != nil {
return nil, errs.ErrParamValidation
}
// 3、西部加密
westConfig := l.svcCtx.Config.WestConfig
encryptedFields, err := common.EncryptStructFields(data, westConfig.Key)
if err != nil {
logx.Errorf("西部加密错误:%v", err)
return nil, errs.ErrSystem
}
// 4、发送请求到西部
logx.Infof("交易号:%s", transactionID)
apiRequest := common.MapStructToAPIRequest(encryptedFields, westmodel.FLXG162AFieldMapping, "data")
westResp, callAPIErr := l.svcCtx.WestDexService.CallAPI("G32BJ05", apiRequest)
if callAPIErr != nil {
return nil, errs.ErrSystem
}
// 5、响应解析
var respData westmodel.G32BJ05Response
unmarshalErr := json.Unmarshal(westResp, &respData)
if unmarshalErr != nil {
return nil, errs.ErrSystem
}
if respData.Data.Code == "00" || respData.Data.Code == "100002" {
l.ctx = context.WithValue(l.ctx, "Charges", true)
} else {
return nil, errs.ErrSystem
}
encryptData, aesEncrypt := crypto.AesEncrypt(westResp, key)
if aesEncrypt != nil {
return nil, errs.ErrSystem
}
return &types.Response{
Data: encryptData,
}, nil
}

View File

@ -3,9 +3,12 @@ package FLXG
import (
"context"
"encoding/hex"
"errors"
"encoding/json"
"tianyuan-api/apps/api/internal/common"
"tianyuan-api/apps/api/internal/validator"
"tianyuan-api/apps/api/internal/westmodel"
"tianyuan-api/pkg/crypto"
"tianyuan-api/pkg/errs"
"tianyuan-api/apps/api/internal/svc"
"tianyuan-api/apps/api/internal/types"
@ -27,72 +30,66 @@ func NewFLXG3D56Logic(ctx context.Context, svcCtx *svc.ServiceContext) *FLXG3D56
}
}
// FLXG3D56 特殊名单
func (l *FLXG3D56Logic) FLXG3D56(req *types.Request) (resp *types.Response, err error) {
//userId, ok := l.ctx.Value("userId").(int64)
//if !ok {
// return &types.Response{}, errors.New("系统错误,请联系管理员")
//}
secretKey, ok := l.ctx.Value("secretKey").(string)
if !ok {
return &types.Response{}, errors.New("系统错误,请联系管理员")
return &types.Response{}, errs.ErrSystem
}
transactionID, ok := l.ctx.Value("transactionID").(string)
if !ok {
return &types.Response{}, errs.ErrSystem
}
// 1、解密
key, err := hex.DecodeString(secretKey)
decryptData, err := crypto.AesDecrypt(req.Data, key)
if err != nil || len(decryptData) == 0 {
return nil, errors.New("参数解密失败")
key, decodeErr := hex.DecodeString(secretKey)
if decodeErr != nil {
return nil, errs.ErrSystem
}
decryptData, aesDecryptErr := crypto.AesDecrypt(req.Data, key)
if aesDecryptErr != nil || len(decryptData) == 0 {
return nil, errs.ErrParamDecryption
}
// 2、校验
var data validator.FLXG3D56Request
if validatorErr := validator.ValidateAndParse(decryptData, &data); validatorErr != nil {
return nil, validatorErr
return nil, errs.ErrParamValidation
}
// 3、西部加密
westConfig := l.svcCtx.Config.WestConfig
mobileNo, err := crypto.WestDexEncrypt(data.MobileNo, westConfig.Key)
encryptedFields, err := common.EncryptStructFields(data, westConfig.Key)
if err != nil {
logx.Errorf("西部加密错误:%v", err)
return nil, errors.New("业务异常")
}
name, err := crypto.WestDexEncrypt(data.Name, westConfig.Key)
if err != nil {
logx.Errorf("西部加密错误:%v", err)
return nil, errors.New("业务异常")
}
idCard, err := crypto.WestDexEncrypt(data.IDCard, westConfig.Key)
if err != nil {
logx.Errorf("西部加密错误:%v", err)
return nil, errors.New("业务异常")
}
timeRange, err := crypto.WestDexEncrypt(data.TimeRange, westConfig.Key)
if err != nil {
logx.Errorf("西部加密错误:%v", err)
return nil, errors.New("业务异常")
return nil, errs.ErrSystem
}
// 4、发送请求到西部
westdexRequest := map[string]interface{}{
"id": idCard,
"cell": mobileNo,
"name": name,
"time_range": timeRange,
}
westResp, err := l.svcCtx.WestDexService.CallAPI("G26BJ05", westdexRequest)
if err != nil {
return nil, err
logx.Infof("交易号:%s", transactionID)
apiRequest := common.MapStructToAPIRequest(encryptedFields, westmodel.FLXG3D56FieldMapping, "data")
westResp, callAPIErr := l.svcCtx.WestDexService.CallAPI("G26BJ05", apiRequest)
if callAPIErr != nil {
return nil, errs.ErrSystem
}
// 5、响应解析
//var respData westmodel.G09GX01Response
//unmarshalErr := json.Unmarshal(westResp, &respData)
//if unmarshalErr != nil {
// return nil, unmarshalErr
//}
//crypto.AesEncrypt()
var respData westmodel.G26BJ05Response
unmarshalErr := json.Unmarshal(westResp, &respData)
if unmarshalErr != nil {
return nil, errs.ErrSystem
}
if respData.Data.Code == "00" || respData.Data.Code == "100002" {
l.ctx = context.WithValue(l.ctx, "Charges", true)
} else {
return nil, errs.ErrSystem
}
encryptData, aesEncrypt := crypto.AesEncrypt(westResp, key)
if aesEncrypt != nil {
return nil, errs.ErrSystem
}
return &types.Response{
Data: string(westResp),
Data: encryptData,
}, nil
}

View File

@ -2,6 +2,13 @@ package FLXG
import (
"context"
"encoding/hex"
"encoding/json"
"tianyuan-api/apps/api/internal/common"
"tianyuan-api/apps/api/internal/validator"
"tianyuan-api/apps/api/internal/westmodel"
"tianyuan-api/pkg/crypto"
"tianyuan-api/pkg/errs"
"tianyuan-api/apps/api/internal/svc"
"tianyuan-api/apps/api/internal/types"
@ -24,7 +31,64 @@ func NewFLXG54F5Logic(ctx context.Context, svcCtx *svc.ServiceContext) *FLXG54F5
}
func (l *FLXG54F5Logic) FLXG54F5(req *types.Request) (resp *types.Response, err error) {
// todo: add your logic here and delete this line
secretKey, ok := l.ctx.Value("secretKey").(string)
if !ok {
return &types.Response{}, errs.ErrSystem
}
transactionID, ok := l.ctx.Value("transactionID").(string)
if !ok {
return &types.Response{}, errs.ErrSystem
}
// 1、解密
key, decodeErr := hex.DecodeString(secretKey)
if decodeErr != nil {
return nil, errs.ErrSystem
}
decryptData, aesDecryptErr := crypto.AesDecrypt(req.Data, key)
if aesDecryptErr != nil || len(decryptData) == 0 {
return nil, errs.ErrParamDecryption
}
return
// 2、校验
var data validator.FLXG54F5Request
if validatorErr := validator.ValidateAndParse(decryptData, &data); validatorErr != nil {
return nil, errs.ErrParamValidation
}
// 3、西部加密
westConfig := l.svcCtx.Config.WestConfig
encryptedFields, err := common.EncryptStructFields(data, westConfig.Key)
if err != nil {
logx.Errorf("西部加密错误:%v", err)
return nil, errs.ErrSystem
}
// 4、发送请求到西部
logx.Infof("交易号:%s", transactionID)
apiRequest := common.MapStructToAPIRequest(encryptedFields, westmodel.FLXG54F5FieldMapping, "data")
westResp, callAPIErr := l.svcCtx.WestDexService.CallAPI("G03HZ01", apiRequest)
if callAPIErr != nil {
return nil, errs.ErrSystem
}
// 5、响应解析
var respData westmodel.G03HZ01Response
unmarshalErr := json.Unmarshal(westResp, &respData)
if unmarshalErr != nil {
return nil, errs.ErrSystem
}
if respData.Code == "0000" {
l.ctx = context.WithValue(l.ctx, "Charges", true)
} else {
return nil, errs.ErrSystem
}
encryptData, aesEncrypt := crypto.AesEncrypt(westResp, key)
if aesEncrypt != nil {
return nil, errs.ErrSystem
}
return &types.Response{
Data: encryptData,
}, nil
}

View File

@ -2,6 +2,12 @@ package FLXG
import (
"context"
"encoding/hex"
"tianyuan-api/apps/api/internal/common"
"tianyuan-api/apps/api/internal/validator"
"tianyuan-api/apps/api/internal/westmodel"
"tianyuan-api/pkg/crypto"
"tianyuan-api/pkg/errs"
"tianyuan-api/apps/api/internal/svc"
"tianyuan-api/apps/api/internal/types"
@ -24,7 +30,64 @@ func NewFLXG5876Logic(ctx context.Context, svcCtx *svc.ServiceContext) *FLXG5876
}
func (l *FLXG5876Logic) FLXG5876(req *types.Request) (resp *types.Response, err error) {
// todo: add your logic here and delete this line
secretKey, ok := l.ctx.Value("secretKey").(string)
if !ok {
return &types.Response{}, errs.ErrSystem
}
transactionID, ok := l.ctx.Value("transactionID").(string)
if !ok {
return &types.Response{}, errs.ErrSystem
}
// 1、解密
key, decodeErr := hex.DecodeString(secretKey)
if decodeErr != nil {
return nil, errs.ErrSystem
}
decryptData, aesDecryptErr := crypto.AesDecrypt(req.Data, key)
if aesDecryptErr != nil || len(decryptData) == 0 {
return nil, errs.ErrParamDecryption
}
return
// 2、校验
var data validator.FLXG5876Request
if validatorErr := validator.ValidateAndParse(decryptData, &data); validatorErr != nil {
return nil, errs.ErrParamValidation
}
// 3、西部加密
westConfig := l.svcCtx.Config.WestConfig
encryptedFields, err := common.EncryptStructFields(data, westConfig.Key)
if err != nil {
logx.Errorf("西部加密错误:%v", err)
return nil, errs.ErrSystem
}
// 4、发送请求到西部
logx.Infof("交易号:%s", transactionID)
apiRequest := common.MapStructToAPIRequest(encryptedFields, westmodel.FLXG5876FieldMapping, "")
westResp, callAPIErr := l.svcCtx.WestDexService.CallAPI("G03XM02", apiRequest)
if callAPIErr != nil {
return nil, errs.ErrSystem
}
// 5、响应解析
//var respData westmodel.G26BJ05Response
//unmarshalErr := json.Unmarshal(westResp, &respData)
//if unmarshalErr != nil {
// return nil, errs.ErrSystem
//}
//
//if respData.Data.Code == "00" || respData.Data.Code == "100002" {
// l.ctx = context.WithValue(l.ctx, "Charges", true)
//} else {
// return nil, errs.ErrSystem
//}
//encryptData, aesEncrypt := crypto.AesEncrypt(westResp, key)
//if aesEncrypt != nil {
// return nil, errs.ErrSystem
//}
return &types.Response{
Data: string(westResp),
}, nil
}

View File

@ -2,6 +2,12 @@ package FLXG
import (
"context"
"encoding/hex"
"tianyuan-api/apps/api/internal/common"
"tianyuan-api/apps/api/internal/validator"
"tianyuan-api/apps/api/internal/westmodel"
"tianyuan-api/pkg/crypto"
"tianyuan-api/pkg/errs"
"tianyuan-api/apps/api/internal/svc"
"tianyuan-api/apps/api/internal/types"
@ -24,7 +30,64 @@ func NewFLXG970FLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FLXG970F
}
func (l *FLXG970FLogic) FLXG970F(req *types.Request) (resp *types.Response, err error) {
// todo: add your logic here and delete this line
secretKey, ok := l.ctx.Value("secretKey").(string)
if !ok {
return &types.Response{}, errs.ErrSystem
}
transactionID, ok := l.ctx.Value("transactionID").(string)
if !ok {
return &types.Response{}, errs.ErrSystem
}
// 1、解密
key, decodeErr := hex.DecodeString(secretKey)
if decodeErr != nil {
return nil, errs.ErrSystem
}
decryptData, aesDecryptErr := crypto.AesDecrypt(req.Data, key)
if aesDecryptErr != nil || len(decryptData) == 0 {
return nil, errs.ErrParamDecryption
}
return
// 2、校验
var data validator.FLXG970FRequest
if validatorErr := validator.ValidateAndParse(decryptData, &data); validatorErr != nil {
return nil, errs.ErrParamValidation
}
// 3、西部加密
westConfig := l.svcCtx.Config.WestConfig
encryptedFields, err := common.EncryptStructFields(data, westConfig.Key)
if err != nil {
logx.Errorf("西部加密错误:%v", err)
return nil, errs.ErrSystem
}
// 4、发送请求到西部
logx.Infof("交易号:%s", transactionID)
apiRequest := common.MapStructToAPIRequest(encryptedFields, westmodel.FLXG970FFieldMapping, "")
westResp, callAPIErr := l.svcCtx.WestDexService.CallAPI("WEST00028", apiRequest)
if callAPIErr != nil {
return nil, errs.ErrSystem
}
// 5、响应解析
//var respData westmodel.G32BJ05Response
//unmarshalErr := json.Unmarshal(westResp, &respData)
//if unmarshalErr != nil {
// return nil, errs.ErrSystem
//}
//
//if respData.Data.Code == "00" || respData.Data.Code == "100002" {
// l.ctx = context.WithValue(l.ctx, "Charges", true)
//} else {
// return nil, errs.ErrSystem
//}
//encryptData, aesEncrypt := crypto.AesEncrypt(westResp, key)
//if aesEncrypt != nil {
// return nil, errs.ErrSystem
//}
return &types.Response{
Data: string(westResp),
}, nil
}

View File

@ -3,11 +3,14 @@ package IVYZ
import (
"context"
"encoding/hex"
"errors"
"encoding/json"
"tianyuan-api/apps/api/internal/common"
"tianyuan-api/apps/api/internal/svc"
"tianyuan-api/apps/api/internal/types"
"tianyuan-api/apps/api/internal/validator"
"tianyuan-api/apps/api/internal/westmodel"
"tianyuan-api/pkg/crypto"
"tianyuan-api/pkg/errs"
"github.com/zeromicro/go-zero/core/logx"
)
@ -26,64 +29,66 @@ func NewIVYZ5733Logic(ctx context.Context, svcCtx *svc.ServiceContext) *IVYZ5733
}
}
type CustomResponse struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data interface{} `json:"data,omitempty"` // `omitempty` 使得当 Data 为空时字段不返回
}
func (l *IVYZ5733Logic) IVYZ5733(req *types.Request) (resp *types.Response, err error) {
//userId, ok := l.ctx.Value("userId").(int64)
//if !ok {
// return &types.Response{}, errors.New("系统错误,请联系管理员")
//}
func (l *IVYZ5733Logic) IVYZ5733(req *types.Request) (resp *types.Response, err *errs.AppError) {
secretKey, ok := l.ctx.Value("secretKey").(string)
if !ok {
return &types.Response{}, errors.New("系统错误,请联系管理员")
return &types.Response{}, errs.ErrSystem
}
transactionID, ok := l.ctx.Value("transactionID").(string)
if !ok {
return &types.Response{}, errs.ErrSystem
}
// 1、解密
key, err := hex.DecodeString(secretKey)
decryptData, err := crypto.AesDecrypt(req.Data, key)
if err != nil || len(decryptData) == 0 {
return nil, errors.New("参数解密失败")
key, decodeErr := hex.DecodeString(secretKey)
if decodeErr != nil {
return nil, errs.ErrSystem
}
decryptData, aesDecryptErr := crypto.AesDecrypt(req.Data, key)
if aesDecryptErr != nil || len(decryptData) == 0 {
return nil, errs.ErrParamDecryption
}
// 2、校验
var data validator.IVYZ5733Request
if validatorErr := validator.ValidateAndParse(decryptData, &data); validatorErr != nil {
return nil, validatorErr
return nil, errs.ErrParamValidation
}
// 3、西部加密
westConfig := l.svcCtx.Config.WestConfig
name, err := crypto.WestDexEncrypt(data.Name, westConfig.Key)
if err != nil {
encryptedFields, encryptStructFieldsErr := common.EncryptStructFields(data, westConfig.Key)
if encryptStructFieldsErr != nil {
logx.Errorf("西部加密错误:%v", err)
return nil, errors.New("业务异常")
}
idCard, err := crypto.WestDexEncrypt(data.IDCard, westConfig.Key)
if err != nil {
logx.Errorf("西部加密错误:%v", err)
return nil, errors.New("业务异常")
}
// 4、发送请求到西部
westdexRequest := map[string]interface{}{
"id": idCard,
"name": name,
}
westResp, err := l.svcCtx.WestDexService.CallAPI("G09GX01", westdexRequest)
if err != nil {
return nil, err
return nil, errs.ErrSystem
}
logx.Infof("交易号:%s", transactionID)
apiRequest := common.MapStructToAPIRequest(encryptedFields, westmodel.IVYZ5733FieldMapping, "data")
westResp, callAPIErr := l.svcCtx.WestDexService.CallAPI("G09GX01", apiRequest)
if callAPIErr != nil {
return nil, errs.ErrSystem
}
// 5、响应解析
//var respData westmodel.G09GX01Response
//unmarshalErr := json.Unmarshal(westResp, &respData)
//if unmarshalErr != nil {
// return nil, unmarshalErr
//}
//crypto.AesEncrypt()
var respData westmodel.G09GX01Response
unmarshalErr := json.Unmarshal(westResp, &respData)
if unmarshalErr != nil {
return nil, errs.ErrSystem
}
if respData.Code == 400 || respData.Code == 500 || respData.Code == 999 {
logx.Errorf("西部响应错误%v", respData)
return nil, errs.ErrSystem
}
if respData.Code == 201 || respData.Code == 202 || respData.Code == 203 {
l.ctx = context.WithValue(l.ctx, "Charges", true)
}
encryptData, aesEncrypt := crypto.AesEncrypt(westResp, key)
if aesEncrypt != nil {
return nil, errs.ErrSystem
}
return &types.Response{
Data: string(westResp),
Data: encryptData,
}, nil
}

View File

@ -24,11 +24,6 @@ func NewQYGL2ACDLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QYGL2ACD
}
func (l *QYGL2ACDLogic) QYGL2ACD(req *types.Request) (resp *types.Response, err error) {
data := "zhangSan"
err = l.svcCtx.KqPusherClient.Push(l.ctx, data)
if err != nil {
logx.Errorf("KqPusherClient Push Error , err :%v", err)
}
return &types.Response{
Data: "三要素合演",
}, nil

View File

@ -14,12 +14,15 @@ import (
"tianyuan-api/apps/sentinel/client/userproduct"
"tianyuan-api/apps/sentinel/client/whitelist"
"tianyuan-api/apps/sentinel/sentinel"
"tianyuan-api/apps/user/user"
"tianyuan-api/pkg/crypto"
)
type ApiAuthInterceptorMiddleware struct {
WhitelistRpc sentinel.WhitelistClient
SecretRpc sentinel.SecretClient
UserProductRpc sentinel.UserProductClient
UserRpc user.UserClient
Rds *redis.Redis
}
@ -27,11 +30,13 @@ func NewApiAuthInterceptorMiddleware(
whitelistRpc sentinel.WhitelistClient,
secretRpc sentinel.SecretClient,
userProductRpc sentinel.UserProductClient,
userRpc user.UserClient,
rds *redis.Redis) *ApiAuthInterceptorMiddleware {
return &ApiAuthInterceptorMiddleware{
WhitelistRpc: whitelistRpc,
SecretRpc: secretRpc,
UserProductRpc: userProductRpc,
UserRpc: userRpc,
Rds: rds,
}
}
@ -83,8 +88,21 @@ func (m *ApiAuthInterceptorMiddleware) Handle(next http.HandlerFunc) http.Handle
return
}
// 3、是否有开通该产品
userId := secrets.UserId
// 3、额度是否冻结
info, err := m.UserRpc.GetUserInfo(r.Context(), &user.UserInfoReq{UserId: userId})
if err != nil {
xhttp.JsonBaseResponseCtx(r.Context(), w, errors.New("系统错误,请联系管理员"))
return
}
if info.QuotaExceeded == 1 {
xhttp.JsonBaseResponseCtx(r.Context(), w, errors.New("账户余额不足,无法请求"))
return
}
// 4、是否有开通该产品
pathParts := strings.Split(r.URL.Path, "/")
productCode := pathParts[len(pathParts)-1]
userProductRedisKey := fmt.Sprintf("user_products:%d", userId)
@ -106,7 +124,13 @@ func (m *ApiAuthInterceptorMiddleware) Handle(next http.HandlerFunc) http.Handle
// 将 userId 存入 context供后续逻辑使用
ctx := context.WithValue(r.Context(), "userId", userId)
ctx = context.WithValue(r.Context(), "secretKey", secrets.AesKey)
ctx = context.WithValue(ctx, "secretKey", secrets.AesKey)
ctx = context.WithValue(ctx, "productCode", productCode)
// 生成流水号
transactionID := crypto.GenerateTransactionID()
ctx = context.WithValue(ctx, "transactionID", transactionID)
next(w, r.WithContext(ctx))
}
}

View File

@ -0,0 +1,53 @@
package middleware
import (
"github.com/zeromicro/go-zero/core/logx"
"net/http"
"tianyuan-api/apps/api/internal/service"
)
type ApiMqsInterceptorMiddleware struct {
ApiRequestMqsService *service.ApiRequestMqsService
}
func NewApiMqsInterceptorMiddleware(apiRequestMqsService *service.ApiRequestMqsService) *ApiMqsInterceptorMiddleware {
return &ApiMqsInterceptorMiddleware{
ApiRequestMqsService: apiRequestMqsService,
}
}
func (m *ApiMqsInterceptorMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
next(w, r)
//响应拦截
ctx := r.Context()
transactionID, ok := ctx.Value("transactionID").(string)
if ok && transactionID != "" {
userId, userIdOk := ctx.Value("userId").(int64)
if !userIdOk {
logx.Error("userId 不存在或类型错误")
return
}
productCode, productCodeOk := ctx.Value("productCode").(string)
if !productCodeOk || productCode == "" {
logx.Errorf("productCode 不存在或为空")
}
status, statusOk := ctx.Value("status").(string)
if !statusOk || status == "" {
status = "failed"
}
remark, remarkOk := ctx.Value("remark").(string)
if !remarkOk || remark == "" {
}
charges, chargesOk := ctx.Value("charges").(bool)
if !chargesOk || !charges {
charges = false
}
err := m.ApiRequestMqsService.SendApiRequestMessage(ctx, transactionID, userId, productCode, status, charges, remark)
if err != nil {
logx.Errorf("发送 API 请求消息失败: %v", err)
}
}
}
}

View File

@ -0,0 +1,41 @@
package service
import (
"context"
"encoding/json"
"github.com/zeromicro/go-queue/kq"
"tianyuan-api/pkg/models"
"time"
)
type ApiRequestMqsService struct {
KqPusherClient *kq.Pusher
}
// NewApiRequestMqsService
func NewApiRequestMqsService(KqPusherClient *kq.Pusher) *ApiRequestMqsService {
return &ApiRequestMqsService{
KqPusherClient: KqPusherClient,
}
}
func (s *ApiRequestMqsService) SendApiRequestMessage(ctx context.Context, transactionID string, userId int64, productCode string, status string, charges bool, remark string) error {
message := models.ApiRequestMessage{
TransactionID: transactionID,
UserId: userId,
ProductCode: productCode,
Status: status,
Charges: charges,
Remark: remark,
Timestamp: time.Now(),
}
msgBytes, marshalErr := json.Marshal(message)
if marshalErr != nil {
return marshalErr
}
if pushErr := s.KqPusherClient.Push(ctx, string(msgBytes)); pushErr != nil {
return pushErr
}
return nil
}

View File

@ -44,11 +44,7 @@ func (w *WestDexService) CallAPI(code string, reqData map[string]interface{}) (r
// 构造请求URL
reqUrl := fmt.Sprintf("%s/%s/%s?timestamp=%s", w.config.Url, w.config.SecretId, code, timestamp)
// 将请求参数编码为JSON格式
data := map[string]interface{}{
"data": reqData,
}
jsonData, err := json.Marshal(data)
jsonData, err := json.Marshal(reqData)
if err != nil {
logx.Errorf("【西部数据请求】JSON编码错误: %v", err)
return
@ -94,7 +90,7 @@ func (w *WestDexService) CallAPI(code string, reqData map[string]interface{}) (r
logx.Errorf("【西部数据请求】JSON反序列化错误: %v", UnmarshalErr)
return nil, UnmarshalErr
}
logx.Infof("西部流水号: %s", westDexResp.ID)
// 到这层是西部系统
if westDexResp.Code != "00000" {
logx.Errorf("【西部数据请求】响应数据业务异常: %s %s", westDexResp.Message, westDexResp.Reason)

View File

@ -9,18 +9,29 @@ import (
"tianyuan-api/apps/api/internal/middleware"
"tianyuan-api/apps/api/internal/service"
"tianyuan-api/apps/sentinel/sentinel"
"tianyuan-api/apps/user/user"
"time"
)
type ServiceContext struct {
Config config.Config
ApiAuthInterceptor rest.Middleware
Redis *redis.Redis
WhitelistRpc sentinel.WhitelistClient
SecretRpc sentinel.SecretClient
ProductRpc sentinel.ProductClient
UserProductRpc sentinel.UserProductClient
KqPusherClient *kq.Pusher
WestDexService *service.WestDexService
Config config.Config
ApiAuthInterceptor rest.Middleware
ApiMqsInterceptor rest.Middleware
Redis *redis.Redis
WhitelistRpc sentinel.WhitelistClient
SecretRpc sentinel.SecretClient
ProductRpc sentinel.ProductClient
UserProductRpc sentinel.UserProductClient
WestDexService *service.WestDexService
ApiRequestMqsService *service.ApiRequestMqsService
}
type ApiRequestMessage struct {
TransactionID string `json:"transactionID"`
UserId string `json:"userId"`
ProductCode string `json:"productCode"`
Status string `json:"status"` // 1. success 2. error
Charges bool `json:"charges"` // 是否扣费
Timestamp time.Time `json:"timestamp"` // 添加时间戳
}
func NewServiceContext(c config.Config) *ServiceContext {
@ -36,15 +47,19 @@ func NewServiceContext(c config.Config) *ServiceContext {
userProductRpc := sentinel.NewUserProductClient(zrpc.MustNewClient(c.SentinelRpc).Conn())
whitelistRpc := sentinel.NewWhitelistClient(zrpc.MustNewClient(c.SentinelRpc).Conn())
secretRpc := sentinel.NewSecretClient(zrpc.MustNewClient(c.SentinelRpc).Conn())
userRpc := user.NewUserClient(zrpc.MustNewClient(c.UserRpc).Conn())
KqPusherClient := kq.NewPusher(c.KqPusherConf.Brokers, c.KqPusherConf.Topic)
apiRequestMqsService := service.NewApiRequestMqsService(KqPusherClient)
return &ServiceContext{
Config: c,
Redis: rds,
WhitelistRpc: whitelistRpc,
SecretRpc: secretRpc,
ProductRpc: productRpc,
UserProductRpc: userProductRpc,
ApiAuthInterceptor: middleware.NewApiAuthInterceptorMiddleware(whitelistRpc, secretRpc, userProductRpc, rds).Handle,
KqPusherClient: kq.NewPusher(c.KqPusherConf.Brokers, c.KqPusherConf.Topic),
WestDexService: service.NewWestDexService(c.WestConfig), // 假设你将密钥和 ID 配置在 config 中
Config: c,
Redis: rds,
WhitelistRpc: whitelistRpc,
SecretRpc: secretRpc,
ProductRpc: productRpc,
UserProductRpc: userProductRpc,
ApiAuthInterceptor: middleware.NewApiAuthInterceptorMiddleware(whitelistRpc, secretRpc, userProductRpc, userRpc, rds).Handle,
ApiMqsInterceptor: middleware.NewApiMqsInterceptorMiddleware(apiRequestMqsService).Handle,
ApiRequestMqsService: apiRequestMqsService,
WestDexService: service.NewWestDexService(c.WestConfig), // 假设你将密钥和 ID 配置在 config 中
}
}

View File

@ -21,6 +21,22 @@ type FLXG3D56Request struct {
Name string `json:"name" validate:"required,min=1,validName"`
TimeRange string `json:"time_range" validate:"omitempty,validTimeRange"` // 非必填字段
}
type FLXG54F5Request struct {
MobileNo string `json:"mobile_no" validate:"required,min=11,max=11,validMobileNo"`
}
type FLXG162ARequest struct {
MobileNo string `json:"mobile_no" validate:"required,min=11,max=11,validMobileNo"`
IDCard string `json:"id_card" validate:"required,validIDCard"`
Name string `json:"name" validate:"required,min=1,validName"`
}
type FLXG970FRequest struct {
IDCard string `json:"id_card" validate:"required,validIDCard"`
Name string `json:"name" validate:"required,min=1,validName"`
}
type FLXG5876Request struct {
MobileNo string `json:"mobile_no" validate:"required,min=11,max=11,validMobileNo"`
}
type IVYZ5733Request struct {
Name string `json:"name" validate:"required,min=1,validName"`
IDCard string `json:"id_card" validate:"required,validIDCard"`

View File

@ -0,0 +1,28 @@
package westmodel
var IVYZ5733FieldMapping = map[string]string{
"IDCard": "id",
"Name": "name",
"MobileNo": "cell",
"TimeRange": "time_range",
}
var FLXG3D56FieldMapping = map[string]string{
"IDCard": "id",
"Name": "name",
}
var FLXG54F5FieldMapping = map[string]string{
"MobileNo": "mobile",
}
var FLXG162AFieldMapping = map[string]string{
"IDCard": "id",
"Name": "name",
"MobileNo": "cell",
}
var FLXG970FFieldMapping = map[string]string{
"IDCard": "id",
"Name": "name",
}
var FLXG5876FieldMapping = map[string]string{
"MobileNo": "mobile",
}

View File

@ -6,6 +6,19 @@ type G09GX01Response struct {
Data string `json:"data"`
Success bool `json:"success"`
}
type G26BJ05Response struct {
Data struct {
Code string `json:"code"`
} `json:"data"`
}
type G03HZ01Response struct {
Code string `json:"code"`
}
type G32BJ05Response struct {
Data struct {
Code string `json:"code"`
} `json:"data"`
}
type G16BJ02Response struct {
Code int `json:"code"`
Msg string `json:"msg"`

View File

@ -23,3 +23,9 @@ KqConsumerCharge:
Offset: first
Consumers: 2 # 同样分配2个消费者
Processors: 2
UserRpc:
Etcd:
Hosts:
- 127.0.0.1:2379
Key: user.rpc

View File

@ -23,3 +23,9 @@ KqConsumerCharge:
Offset: first
Consumers: 2 # 同样分配2个消费者
Processors: 2
UserRpc:
Etcd:
Hosts:
- tyapi_etcd:2379
Key: user.rpc

View File

@ -3,10 +3,12 @@ package config
import (
"github.com/zeromicro/go-queue/kq"
"github.com/zeromicro/go-zero/rest"
"github.com/zeromicro/go-zero/zrpc"
)
type Config struct {
rest.RestConf
KqConsumerLog kq.KqConf
KqConsumerCharge kq.KqConf
UserRpc zrpc.RpcClientConf
}

View File

@ -2,8 +2,11 @@ package apirequest
import (
"context"
"encoding/json"
"github.com/zeromicro/go-zero/core/logx"
"tianyuan-api/apps/mqs/internal/svc"
"tianyuan-api/apps/user/user"
"tianyuan-api/pkg/models"
)
type Charge struct {
@ -19,6 +22,18 @@ func NewCharge(ctx context.Context, svcCtx *svc.ServiceContext) *Charge {
}
func (l *Charge) Consume(ctx context.Context, key, val string) error {
logx.Infof("Charge key :%s , val :%s", key, val)
var apiRequestMessage models.ApiRequestMessage
err := json.Unmarshal([]byte(val), &apiRequestMessage)
if err != nil {
return err
}
logx.Infof("Charge val :%v", apiRequestMessage)
if !apiRequestMessage.Charges {
return nil
}
_, updateWalletErr := l.svcCtx.WalletRpc.UpdateWallet(ctx, &user.UpdateWalletRequest{UserId: apiRequestMessage.UserId, TransactionId: apiRequestMessage.TransactionID, ProductCode: apiRequestMessage.ProductCode, Remark: apiRequestMessage.Remark})
if updateWalletErr != nil {
return err
}
return nil
}

View File

@ -2,8 +2,11 @@ package apirequest
import (
"context"
"github.com/zeromicro/go-zero/core/logx"
"encoding/json"
"tianyuan-api/apps/mqs/internal/svc"
"tianyuan-api/apps/user/user"
"tianyuan-api/pkg/models"
"time"
)
type Log struct {
@ -19,6 +22,22 @@ func NewLog(ctx context.Context, svcCtx *svc.ServiceContext) *Log {
}
func (l *Log) Consume(ctx context.Context, key, val string) error {
logx.Infof("log key :%s , val :%s", key, val)
var apiRequestMessage models.ApiRequestMessage
err := json.Unmarshal([]byte(val), &apiRequestMessage)
if err != nil {
return err
}
_, err = l.svcCtx.ApiRequestRpc.AddApiRequest(ctx, &user.AddApiRequestRequest{
TransactionId: apiRequestMessage.TransactionID,
UserId: apiRequestMessage.UserId,
ProductCode: apiRequestMessage.ProductCode,
Status: apiRequestMessage.Status,
Charges: apiRequestMessage.Charges,
Remark: apiRequestMessage.Remark,
Timestamp: apiRequestMessage.Timestamp.Format(time.RFC3339Nano),
})
if err != nil {
return err
}
return nil
}

View File

@ -1,13 +1,24 @@
package svc
import "tianyuan-api/apps/mqs/internal/config"
import (
"github.com/zeromicro/go-zero/zrpc"
"tianyuan-api/apps/mqs/internal/config"
"tianyuan-api/apps/user/user"
)
type ServiceContext struct {
Config config.Config
Config config.Config
WalletRpc user.WalletServiceClient
ApiRequestRpc user.ApiRequestServiceClient
}
func NewServiceContext(c config.Config) *ServiceContext {
walletRpc := user.NewWalletServiceClient(zrpc.MustNewClient(c.UserRpc).Conn())
apiRequestRpc := user.NewApiRequestServiceClient(zrpc.MustNewClient(c.UserRpc).Conn())
return &ServiceContext{
Config: c,
Config: c,
WalletRpc: walletRpc,
ApiRequestRpc: apiRequestRpc,
}
}

View File

@ -22,6 +22,7 @@ type (
DeleteSecretRequest = sentinel.DeleteSecretRequest
DeleteUserProductRequest = sentinel.DeleteUserProductRequest
DeleteWhitelistRequest = sentinel.DeleteWhitelistRequest
GetRecordByCodeRequest = sentinel.GetRecordByCodeRequest
GetRecordByIdRequest = sentinel.GetRecordByIdRequest
GetSecretBySecretIdRequest = sentinel.GetSecretBySecretIdRequest
MatchResponse = sentinel.MatchResponse
@ -51,6 +52,7 @@ type (
DeleteProduct(ctx context.Context, in *DeleteProductRequest, opts ...grpc.CallOption) (*Product, error)
GetProductPageList(ctx context.Context, in *PageListRequest, opts ...grpc.CallOption) (*ProductResponse, error)
GetProductById(ctx context.Context, in *GetRecordByIdRequest, opts ...grpc.CallOption) (*Product, error)
GetProductByCode(ctx context.Context, in *GetRecordByCodeRequest, opts ...grpc.CallOption) (*Product, error)
}
defaultProductZrpcClient struct {
@ -89,3 +91,8 @@ func (m *defaultProductZrpcClient) GetProductById(ctx context.Context, in *GetRe
client := sentinel.NewProductClient(m.cli.Conn())
return client.GetProductById(ctx, in, opts...)
}
func (m *defaultProductZrpcClient) GetProductByCode(ctx context.Context, in *GetRecordByCodeRequest, opts ...grpc.CallOption) (*Product, error) {
client := sentinel.NewProductClient(m.cli.Conn())
return client.GetProductByCode(ctx, in, opts...)
}

View File

@ -22,6 +22,7 @@ type (
DeleteSecretRequest = sentinel.DeleteSecretRequest
DeleteUserProductRequest = sentinel.DeleteUserProductRequest
DeleteWhitelistRequest = sentinel.DeleteWhitelistRequest
GetRecordByCodeRequest = sentinel.GetRecordByCodeRequest
GetRecordByIdRequest = sentinel.GetRecordByIdRequest
GetSecretBySecretIdRequest = sentinel.GetSecretBySecretIdRequest
MatchResponse = sentinel.MatchResponse

View File

@ -22,6 +22,7 @@ type (
DeleteSecretRequest = sentinel.DeleteSecretRequest
DeleteUserProductRequest = sentinel.DeleteUserProductRequest
DeleteWhitelistRequest = sentinel.DeleteWhitelistRequest
GetRecordByCodeRequest = sentinel.GetRecordByCodeRequest
GetRecordByIdRequest = sentinel.GetRecordByIdRequest
GetSecretBySecretIdRequest = sentinel.GetSecretBySecretIdRequest
MatchResponse = sentinel.MatchResponse

View File

@ -22,6 +22,7 @@ type (
DeleteSecretRequest = sentinel.DeleteSecretRequest
DeleteUserProductRequest = sentinel.DeleteUserProductRequest
DeleteWhitelistRequest = sentinel.DeleteWhitelistRequest
GetRecordByCodeRequest = sentinel.GetRecordByCodeRequest
GetRecordByIdRequest = sentinel.GetRecordByIdRequest
GetSecretBySecretIdRequest = sentinel.GetSecretBySecretIdRequest
MatchResponse = sentinel.MatchResponse

View File

@ -0,0 +1,42 @@
package productlogic
import (
"context"
"tianyuan-api/pkg/sqlutil"
"tianyuan-api/apps/sentinel/internal/svc"
"tianyuan-api/apps/sentinel/sentinel"
"github.com/zeromicro/go-zero/core/logx"
)
type GetProductByCodeLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewGetProductByCodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetProductByCodeLogic {
return &GetProductByCodeLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *GetProductByCodeLogic) GetProductByCode(in *sentinel.GetRecordByCodeRequest) (*sentinel.Product, error) {
product, err := l.svcCtx.ProductsModel.FindOneByProductCode(l.ctx, in.Code)
if err != nil {
return nil, err
}
return &sentinel.Product{
Id: product.Id,
ProductName: product.ProductName,
ProductCode: product.ProductCode,
ProductPrice: product.ProductPrice,
ProductDescription: sqlutil.NullStringToString(product.ProductDescription),
ProductContent: sqlutil.NullStringToString(product.ProductContent),
ProductGroup: product.ProductGroup,
}, nil
}

View File

@ -48,3 +48,8 @@ func (s *ProductServer) GetProductById(ctx context.Context, in *sentinel.GetReco
l := productlogic.NewGetProductByIdLogic(ctx, s.svcCtx)
return l.GetProductById(in)
}
func (s *ProductServer) GetProductByCode(ctx context.Context, in *sentinel.GetRecordByCodeRequest) (*sentinel.Product, error) {
l := productlogic.NewGetProductByCodeLogic(ctx, s.svcCtx)
return l.GetProductByCode(in)
}

View File

@ -21,7 +21,9 @@ message WhitePageListRequest {
message GetRecordByIdRequest {
int64 id = 1;
}
message GetRecordByCodeRequest {
string code = 1;
}
message Whitelist {
int64 id = 1;
@ -186,6 +188,7 @@ service product {
rpc DeleteProduct(DeleteProductRequest) returns (Product);
rpc GetProductPageList(PageListRequest) returns (ProductResponse);
rpc GetProductById(GetRecordByIdRequest) returns (Product);
rpc GetProductByCode(GetRecordByCodeRequest) returns (Product);
}
service userProduct {
// UserProduct methods

File diff suppressed because it is too large Load Diff

View File

@ -442,6 +442,7 @@ const (
Product_DeleteProduct_FullMethodName = "/product/DeleteProduct"
Product_GetProductPageList_FullMethodName = "/product/GetProductPageList"
Product_GetProductById_FullMethodName = "/product/GetProductById"
Product_GetProductByCode_FullMethodName = "/product/GetProductByCode"
)
// ProductClient is the client API for Product service.
@ -454,6 +455,7 @@ type ProductClient interface {
DeleteProduct(ctx context.Context, in *DeleteProductRequest, opts ...grpc.CallOption) (*Product, error)
GetProductPageList(ctx context.Context, in *PageListRequest, opts ...grpc.CallOption) (*ProductResponse, error)
GetProductById(ctx context.Context, in *GetRecordByIdRequest, opts ...grpc.CallOption) (*Product, error)
GetProductByCode(ctx context.Context, in *GetRecordByCodeRequest, opts ...grpc.CallOption) (*Product, error)
}
type productClient struct {
@ -514,6 +516,16 @@ func (c *productClient) GetProductById(ctx context.Context, in *GetRecordByIdReq
return out, nil
}
func (c *productClient) GetProductByCode(ctx context.Context, in *GetRecordByCodeRequest, opts ...grpc.CallOption) (*Product, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(Product)
err := c.cc.Invoke(ctx, Product_GetProductByCode_FullMethodName, in, out, cOpts...)
if err != nil {
return nil, err
}
return out, nil
}
// ProductServer is the server API for Product service.
// All implementations must embed UnimplementedProductServer
// for forward compatibility
@ -524,6 +536,7 @@ type ProductServer interface {
DeleteProduct(context.Context, *DeleteProductRequest) (*Product, error)
GetProductPageList(context.Context, *PageListRequest) (*ProductResponse, error)
GetProductById(context.Context, *GetRecordByIdRequest) (*Product, error)
GetProductByCode(context.Context, *GetRecordByCodeRequest) (*Product, error)
mustEmbedUnimplementedProductServer()
}
@ -546,6 +559,9 @@ func (UnimplementedProductServer) GetProductPageList(context.Context, *PageListR
func (UnimplementedProductServer) GetProductById(context.Context, *GetRecordByIdRequest) (*Product, error) {
return nil, status.Errorf(codes.Unimplemented, "method GetProductById not implemented")
}
func (UnimplementedProductServer) GetProductByCode(context.Context, *GetRecordByCodeRequest) (*Product, error) {
return nil, status.Errorf(codes.Unimplemented, "method GetProductByCode not implemented")
}
func (UnimplementedProductServer) mustEmbedUnimplementedProductServer() {}
// UnsafeProductServer may be embedded to opt out of forward compatibility for this service.
@ -649,6 +665,24 @@ func _Product_GetProductById_Handler(srv interface{}, ctx context.Context, dec f
return interceptor(ctx, in, info, handler)
}
func _Product_GetProductByCode_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(GetRecordByCodeRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(ProductServer).GetProductByCode(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: Product_GetProductByCode_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(ProductServer).GetProductByCode(ctx, req.(*GetRecordByCodeRequest))
}
return interceptor(ctx, in, info, handler)
}
// Product_ServiceDesc is the grpc.ServiceDesc for Product service.
// It's only intended for direct use with grpc.RegisterService,
// and not to be introspected or modified (even as a copy)
@ -676,6 +710,10 @@ var Product_ServiceDesc = grpc.ServiceDesc{
MethodName: "GetProductById",
Handler: _Product_GetProductById_Handler,
},
{
MethodName: "GetProductByCode",
Handler: _Product_GetProductByCode_Handler,
},
},
Streams: []grpc.StreamDesc{},
Metadata: "sentinel.proto",

View File

@ -0,0 +1,85 @@
// Code generated by goctl. DO NOT EDIT.
// goctl 1.7.2
// Source: user.proto
package apirequestservice
import (
"context"
"tianyuan-api/apps/user/user"
"github.com/zeromicro/go-zero/zrpc"
"google.golang.org/grpc"
)
type (
AddApiRequestRequest = user.AddApiRequestRequest
AddApiRequestResponse = user.AddApiRequestResponse
ApiRequest = user.ApiRequest
Deduction = user.Deduction
EmptyResponse = user.EmptyResponse
EnterpriseAuthReq = user.EnterpriseAuthReq
EnterpriseItem = user.EnterpriseItem
GetApiRequestByTransactionIdRequest = user.GetApiRequestByTransactionIdRequest
GetApiRequestByTransactionIdResponse = user.GetApiRequestByTransactionIdResponse
GetApiRequestsRequest = user.GetApiRequestsRequest
GetApiRequestsResponse = user.GetApiRequestsResponse
GetDeductionByTransactionIdRequest = user.GetDeductionByTransactionIdRequest
GetDeductionByTransactionIdResponse = user.GetDeductionByTransactionIdResponse
GetDeductionsRequest = user.GetDeductionsRequest
GetDeductionsResponse = user.GetDeductionsResponse
GetEnterpriseAuthStatusReq = user.GetEnterpriseAuthStatusReq
GetEnterpriseAuthStatusResp = user.GetEnterpriseAuthStatusResp
GetPendingEnterpriseReq = user.GetPendingEnterpriseReq
GetPendingEnterpriseResp = user.GetPendingEnterpriseResp
GetUserInfoResp = user.GetUserInfoResp
GetWalletRequest = user.GetWalletRequest
GetWalletResponse = user.GetWalletResponse
LoginReq = user.LoginReq
LoginResp = user.LoginResp
PhoneLoginReq = user.PhoneLoginReq
RegisterReq = user.RegisterReq
ReviewEnterpriseReq = user.ReviewEnterpriseReq
UpdateWalletRequest = user.UpdateWalletRequest
UpdateWalletResponse = user.UpdateWalletResponse
UserInfoReq = user.UserInfoReq
UserInfoResp = user.UserInfoResp
ApiRequestService interface {
// 添加API请求记录
AddApiRequest(ctx context.Context, in *AddApiRequestRequest, opts ...grpc.CallOption) (*AddApiRequestResponse, error)
// 查询API请求记录
GetApiRequests(ctx context.Context, in *GetApiRequestsRequest, opts ...grpc.CallOption) (*GetApiRequestsResponse, error)
// 查询API请求记录ByTransactionId
GetApiRequestByTransactionId(ctx context.Context, in *GetApiRequestByTransactionIdRequest, opts ...grpc.CallOption) (*GetApiRequestByTransactionIdResponse, error)
}
defaultApiRequestService struct {
cli zrpc.Client
}
)
func NewApiRequestService(cli zrpc.Client) ApiRequestService {
return &defaultApiRequestService{
cli: cli,
}
}
// 添加API请求记录
func (m *defaultApiRequestService) AddApiRequest(ctx context.Context, in *AddApiRequestRequest, opts ...grpc.CallOption) (*AddApiRequestResponse, error) {
client := user.NewApiRequestServiceClient(m.cli.Conn())
return client.AddApiRequest(ctx, in, opts...)
}
// 查询API请求记录
func (m *defaultApiRequestService) GetApiRequests(ctx context.Context, in *GetApiRequestsRequest, opts ...grpc.CallOption) (*GetApiRequestsResponse, error) {
client := user.NewApiRequestServiceClient(m.cli.Conn())
return client.GetApiRequests(ctx, in, opts...)
}
// 查询API请求记录ByTransactionId
func (m *defaultApiRequestService) GetApiRequestByTransactionId(ctx context.Context, in *GetApiRequestByTransactionIdRequest, opts ...grpc.CallOption) (*GetApiRequestByTransactionIdResponse, error) {
client := user.NewApiRequestServiceClient(m.cli.Conn())
return client.GetApiRequestByTransactionId(ctx, in, opts...)
}

View File

@ -14,20 +14,37 @@ import (
)
type (
EmptyResponse = user.EmptyResponse
EnterpriseAuthReq = user.EnterpriseAuthReq
EnterpriseItem = user.EnterpriseItem
GetEnterpriseAuthStatusReq = user.GetEnterpriseAuthStatusReq
GetEnterpriseAuthStatusResp = user.GetEnterpriseAuthStatusResp
GetPendingEnterpriseReq = user.GetPendingEnterpriseReq
GetPendingEnterpriseResp = user.GetPendingEnterpriseResp
LoginReq = user.LoginReq
LoginResp = user.LoginResp
PhoneLoginReq = user.PhoneLoginReq
RegisterReq = user.RegisterReq
ReviewEnterpriseReq = user.ReviewEnterpriseReq
UserInfoReq = user.UserInfoReq
UserInfoResp = user.UserInfoResp
AddApiRequestRequest = user.AddApiRequestRequest
AddApiRequestResponse = user.AddApiRequestResponse
ApiRequest = user.ApiRequest
Deduction = user.Deduction
EmptyResponse = user.EmptyResponse
EnterpriseAuthReq = user.EnterpriseAuthReq
EnterpriseItem = user.EnterpriseItem
GetApiRequestByTransactionIdRequest = user.GetApiRequestByTransactionIdRequest
GetApiRequestByTransactionIdResponse = user.GetApiRequestByTransactionIdResponse
GetApiRequestsRequest = user.GetApiRequestsRequest
GetApiRequestsResponse = user.GetApiRequestsResponse
GetDeductionByTransactionIdRequest = user.GetDeductionByTransactionIdRequest
GetDeductionByTransactionIdResponse = user.GetDeductionByTransactionIdResponse
GetDeductionsRequest = user.GetDeductionsRequest
GetDeductionsResponse = user.GetDeductionsResponse
GetEnterpriseAuthStatusReq = user.GetEnterpriseAuthStatusReq
GetEnterpriseAuthStatusResp = user.GetEnterpriseAuthStatusResp
GetPendingEnterpriseReq = user.GetPendingEnterpriseReq
GetPendingEnterpriseResp = user.GetPendingEnterpriseResp
GetUserInfoResp = user.GetUserInfoResp
GetWalletRequest = user.GetWalletRequest
GetWalletResponse = user.GetWalletResponse
LoginReq = user.LoginReq
LoginResp = user.LoginResp
PhoneLoginReq = user.PhoneLoginReq
RegisterReq = user.RegisterReq
ReviewEnterpriseReq = user.ReviewEnterpriseReq
UpdateWalletRequest = user.UpdateWalletRequest
UpdateWalletResponse = user.UpdateWalletResponse
UserInfoReq = user.UserInfoReq
UserInfoResp = user.UserInfoResp
Auth interface {
// 注册接口

View File

@ -14,20 +14,37 @@ import (
)
type (
EmptyResponse = user.EmptyResponse
EnterpriseAuthReq = user.EnterpriseAuthReq
EnterpriseItem = user.EnterpriseItem
GetEnterpriseAuthStatusReq = user.GetEnterpriseAuthStatusReq
GetEnterpriseAuthStatusResp = user.GetEnterpriseAuthStatusResp
GetPendingEnterpriseReq = user.GetPendingEnterpriseReq
GetPendingEnterpriseResp = user.GetPendingEnterpriseResp
LoginReq = user.LoginReq
LoginResp = user.LoginResp
PhoneLoginReq = user.PhoneLoginReq
RegisterReq = user.RegisterReq
ReviewEnterpriseReq = user.ReviewEnterpriseReq
UserInfoReq = user.UserInfoReq
UserInfoResp = user.UserInfoResp
AddApiRequestRequest = user.AddApiRequestRequest
AddApiRequestResponse = user.AddApiRequestResponse
ApiRequest = user.ApiRequest
Deduction = user.Deduction
EmptyResponse = user.EmptyResponse
EnterpriseAuthReq = user.EnterpriseAuthReq
EnterpriseItem = user.EnterpriseItem
GetApiRequestByTransactionIdRequest = user.GetApiRequestByTransactionIdRequest
GetApiRequestByTransactionIdResponse = user.GetApiRequestByTransactionIdResponse
GetApiRequestsRequest = user.GetApiRequestsRequest
GetApiRequestsResponse = user.GetApiRequestsResponse
GetDeductionByTransactionIdRequest = user.GetDeductionByTransactionIdRequest
GetDeductionByTransactionIdResponse = user.GetDeductionByTransactionIdResponse
GetDeductionsRequest = user.GetDeductionsRequest
GetDeductionsResponse = user.GetDeductionsResponse
GetEnterpriseAuthStatusReq = user.GetEnterpriseAuthStatusReq
GetEnterpriseAuthStatusResp = user.GetEnterpriseAuthStatusResp
GetPendingEnterpriseReq = user.GetPendingEnterpriseReq
GetPendingEnterpriseResp = user.GetPendingEnterpriseResp
GetUserInfoResp = user.GetUserInfoResp
GetWalletRequest = user.GetWalletRequest
GetWalletResponse = user.GetWalletResponse
LoginReq = user.LoginReq
LoginResp = user.LoginResp
PhoneLoginReq = user.PhoneLoginReq
RegisterReq = user.RegisterReq
ReviewEnterpriseReq = user.ReviewEnterpriseReq
UpdateWalletRequest = user.UpdateWalletRequest
UpdateWalletResponse = user.UpdateWalletResponse
UserInfoReq = user.UserInfoReq
UserInfoResp = user.UserInfoResp
Enterprise interface {
// 获取待审核企业列表

View File

@ -14,24 +14,42 @@ import (
)
type (
EmptyResponse = user.EmptyResponse
EnterpriseAuthReq = user.EnterpriseAuthReq
EnterpriseItem = user.EnterpriseItem
GetEnterpriseAuthStatusReq = user.GetEnterpriseAuthStatusReq
GetEnterpriseAuthStatusResp = user.GetEnterpriseAuthStatusResp
GetPendingEnterpriseReq = user.GetPendingEnterpriseReq
GetPendingEnterpriseResp = user.GetPendingEnterpriseResp
LoginReq = user.LoginReq
LoginResp = user.LoginResp
PhoneLoginReq = user.PhoneLoginReq
RegisterReq = user.RegisterReq
ReviewEnterpriseReq = user.ReviewEnterpriseReq
UserInfoReq = user.UserInfoReq
UserInfoResp = user.UserInfoResp
AddApiRequestRequest = user.AddApiRequestRequest
AddApiRequestResponse = user.AddApiRequestResponse
ApiRequest = user.ApiRequest
Deduction = user.Deduction
EmptyResponse = user.EmptyResponse
EnterpriseAuthReq = user.EnterpriseAuthReq
EnterpriseItem = user.EnterpriseItem
GetApiRequestByTransactionIdRequest = user.GetApiRequestByTransactionIdRequest
GetApiRequestByTransactionIdResponse = user.GetApiRequestByTransactionIdResponse
GetApiRequestsRequest = user.GetApiRequestsRequest
GetApiRequestsResponse = user.GetApiRequestsResponse
GetDeductionByTransactionIdRequest = user.GetDeductionByTransactionIdRequest
GetDeductionByTransactionIdResponse = user.GetDeductionByTransactionIdResponse
GetDeductionsRequest = user.GetDeductionsRequest
GetDeductionsResponse = user.GetDeductionsResponse
GetEnterpriseAuthStatusReq = user.GetEnterpriseAuthStatusReq
GetEnterpriseAuthStatusResp = user.GetEnterpriseAuthStatusResp
GetPendingEnterpriseReq = user.GetPendingEnterpriseReq
GetPendingEnterpriseResp = user.GetPendingEnterpriseResp
GetUserInfoResp = user.GetUserInfoResp
GetWalletRequest = user.GetWalletRequest
GetWalletResponse = user.GetWalletResponse
LoginReq = user.LoginReq
LoginResp = user.LoginResp
PhoneLoginReq = user.PhoneLoginReq
RegisterReq = user.RegisterReq
ReviewEnterpriseReq = user.ReviewEnterpriseReq
UpdateWalletRequest = user.UpdateWalletRequest
UpdateWalletResponse = user.UpdateWalletResponse
UserInfoReq = user.UserInfoReq
UserInfoResp = user.UserInfoResp
User interface {
// 获取用户信息
UserInfo(ctx context.Context, in *UserInfoReq, opts ...grpc.CallOption) (*UserInfoResp, error)
GetUserInfo(ctx context.Context, in *UserInfoReq, opts ...grpc.CallOption) (*GetUserInfoResp, error)
GetEnterpriseAuthStatus(ctx context.Context, in *GetEnterpriseAuthStatusReq, opts ...grpc.CallOption) (*GetEnterpriseAuthStatusResp, error)
}
@ -52,6 +70,11 @@ func (m *defaultUser) UserInfo(ctx context.Context, in *UserInfoReq, opts ...grp
return client.UserInfo(ctx, in, opts...)
}
func (m *defaultUser) GetUserInfo(ctx context.Context, in *UserInfoReq, opts ...grpc.CallOption) (*GetUserInfoResp, error) {
client := user.NewUserClient(m.cli.Conn())
return client.GetUserInfo(ctx, in, opts...)
}
func (m *defaultUser) GetEnterpriseAuthStatus(ctx context.Context, in *GetEnterpriseAuthStatusReq, opts ...grpc.CallOption) (*GetEnterpriseAuthStatusResp, error) {
client := user.NewUserClient(m.cli.Conn())
return client.GetEnterpriseAuthStatus(ctx, in, opts...)

View File

@ -0,0 +1,91 @@
// Code generated by goctl. DO NOT EDIT.
// goctl 1.7.2
// Source: user.proto
package walletservice
import (
"context"
"tianyuan-api/apps/user/user"
"github.com/zeromicro/go-zero/zrpc"
"google.golang.org/grpc"
)
type (
AddApiRequestRequest = user.AddApiRequestRequest
AddApiRequestResponse = user.AddApiRequestResponse
ApiRequest = user.ApiRequest
Deduction = user.Deduction
EmptyResponse = user.EmptyResponse
EnterpriseAuthReq = user.EnterpriseAuthReq
EnterpriseItem = user.EnterpriseItem
GetApiRequestByTransactionIdRequest = user.GetApiRequestByTransactionIdRequest
GetApiRequestByTransactionIdResponse = user.GetApiRequestByTransactionIdResponse
GetApiRequestsRequest = user.GetApiRequestsRequest
GetApiRequestsResponse = user.GetApiRequestsResponse
GetDeductionByTransactionIdRequest = user.GetDeductionByTransactionIdRequest
GetDeductionByTransactionIdResponse = user.GetDeductionByTransactionIdResponse
GetDeductionsRequest = user.GetDeductionsRequest
GetDeductionsResponse = user.GetDeductionsResponse
GetEnterpriseAuthStatusReq = user.GetEnterpriseAuthStatusReq
GetEnterpriseAuthStatusResp = user.GetEnterpriseAuthStatusResp
GetPendingEnterpriseReq = user.GetPendingEnterpriseReq
GetPendingEnterpriseResp = user.GetPendingEnterpriseResp
GetUserInfoResp = user.GetUserInfoResp
GetWalletRequest = user.GetWalletRequest
GetWalletResponse = user.GetWalletResponse
LoginReq = user.LoginReq
LoginResp = user.LoginResp
PhoneLoginReq = user.PhoneLoginReq
RegisterReq = user.RegisterReq
ReviewEnterpriseReq = user.ReviewEnterpriseReq
UpdateWalletRequest = user.UpdateWalletRequest
UpdateWalletResponse = user.UpdateWalletResponse
UserInfoReq = user.UserInfoReq
UserInfoResp = user.UserInfoResp
WalletService interface {
// 修改钱包余额
UpdateWallet(ctx context.Context, in *UpdateWalletRequest, opts ...grpc.CallOption) (*UpdateWalletResponse, error)
// 查询钱包信息
GetWallet(ctx context.Context, in *GetWalletRequest, opts ...grpc.CallOption) (*GetWalletResponse, error)
// 查询扣款记录
GetDeductions(ctx context.Context, in *GetDeductionsRequest, opts ...grpc.CallOption) (*GetDeductionsResponse, error)
GetDeductionByTransactionId(ctx context.Context, in *GetDeductionByTransactionIdRequest, opts ...grpc.CallOption) (*GetDeductionByTransactionIdResponse, error)
}
defaultWalletService struct {
cli zrpc.Client
}
)
func NewWalletService(cli zrpc.Client) WalletService {
return &defaultWalletService{
cli: cli,
}
}
// 修改钱包余额
func (m *defaultWalletService) UpdateWallet(ctx context.Context, in *UpdateWalletRequest, opts ...grpc.CallOption) (*UpdateWalletResponse, error) {
client := user.NewWalletServiceClient(m.cli.Conn())
return client.UpdateWallet(ctx, in, opts...)
}
// 查询钱包信息
func (m *defaultWalletService) GetWallet(ctx context.Context, in *GetWalletRequest, opts ...grpc.CallOption) (*GetWalletResponse, error) {
client := user.NewWalletServiceClient(m.cli.Conn())
return client.GetWallet(ctx, in, opts...)
}
// 查询扣款记录
func (m *defaultWalletService) GetDeductions(ctx context.Context, in *GetDeductionsRequest, opts ...grpc.CallOption) (*GetDeductionsResponse, error) {
client := user.NewWalletServiceClient(m.cli.Conn())
return client.GetDeductions(ctx, in, opts...)
}
func (m *defaultWalletService) GetDeductionByTransactionId(ctx context.Context, in *GetDeductionByTransactionIdRequest, opts ...grpc.CallOption) (*GetDeductionByTransactionIdResponse, error) {
client := user.NewWalletServiceClient(m.cli.Conn())
return client.GetDeductionByTransactionId(ctx, in, opts...)
}

View File

@ -0,0 +1,52 @@
package apirequestservicelogic
import (
"context"
"tianyuan-api/apps/user/internal/model"
"tianyuan-api/apps/user/internal/svc"
"tianyuan-api/apps/user/user"
"tianyuan-api/pkg/sqlutil"
"time"
"github.com/zeromicro/go-zero/core/logx"
)
type AddApiRequestLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewAddApiRequestLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddApiRequestLogic {
return &AddApiRequestLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
// 添加API请求记录
func (l *AddApiRequestLogic) AddApiRequest(in *user.AddApiRequestRequest) (*user.AddApiRequestResponse, error) {
chargesValue := int64(0)
if in.Charges {
chargesValue = 1
}
parsedTime, err := time.Parse(time.RFC3339Nano, in.Timestamp)
if err != nil {
// 错误处理,比如日志输出或返回错误
parsedTime = time.Now()
}
_, err = l.svcCtx.ApiRequestsModel.Insert(l.ctx, &model.ApiRequests{
TransactionId: in.TransactionId,
UserId: in.UserId,
ProductCode: in.ProductCode,
Status: in.Status,
Charges: chargesValue,
Remark: sqlutil.StringToNullString(in.Remark),
Timestamp: parsedTime,
})
if err != nil {
return nil, err
}
return &user.AddApiRequestResponse{}, nil
}

View File

@ -0,0 +1,38 @@
package apirequestservicelogic
import (
"context"
"tianyuan-api/apps/user/internal/svc"
"tianyuan-api/apps/user/user"
"github.com/zeromicro/go-zero/core/logx"
)
type GetApiRequestByTransactionIdLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewGetApiRequestByTransactionIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetApiRequestByTransactionIdLogic {
return &GetApiRequestByTransactionIdLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
// 查询API请求记录ByTransactionId
func (l *GetApiRequestByTransactionIdLogic) GetApiRequestByTransactionId(in *user.GetApiRequestByTransactionIdRequest) (*user.GetApiRequestByTransactionIdResponse, error) {
apiRequest, err := l.svcCtx.ApiRequestsModel.FindOneByTransactionId(l.ctx, in.TransactionId)
if err != nil {
return nil, err
}
return &user.GetApiRequestByTransactionIdResponse{
Id: apiRequest.Id,
TransactionId: apiRequest.TransactionId,
UserId: apiRequest.UserId,
}, nil
}

View File

@ -0,0 +1,31 @@
package apirequestservicelogic
import (
"context"
"tianyuan-api/apps/user/internal/svc"
"tianyuan-api/apps/user/user"
"github.com/zeromicro/go-zero/core/logx"
)
type GetApiRequestsLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewGetApiRequestsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetApiRequestsLogic {
return &GetApiRequestsLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
// 查询API请求记录
func (l *GetApiRequestsLogic) GetApiRequests(in *user.GetApiRequestsRequest) (*user.GetApiRequestsResponse, error) {
// todo: add your logic here and delete this line
return &user.GetApiRequestsResponse{}, nil
}

View File

@ -72,7 +72,10 @@ func (l *ReviewEnterpriseLogic) ReviewEnterprise(in *user.ReviewEnterpriseReq) (
if insertEnterpriseErr != nil {
return insertEnterpriseErr
}
_, InsertWalletsTransErr := l.svcCtx.WalletsModel.InsertWalletsTrans(l.ctx, &model.Wallets{UserId: enterpriseAuth.UserId}, session)
if InsertWalletsTransErr != nil {
return InsertWalletsTransErr
}
_, createSecretErr := l.svcCtx.SecretRpc.CreateSecret(l.ctx, &secret.CreateSecretRequest{
UserId: enterpriseAuth.UserId,
})

View File

@ -0,0 +1,41 @@
package userlogic
import (
"context"
"errors"
"tianyuan-api/apps/user/internal/svc"
"tianyuan-api/apps/user/user"
"github.com/zeromicro/go-zero/core/logx"
)
type GetUserInfoLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewGetUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserInfoLogic {
return &GetUserInfoLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *GetUserInfoLogic) GetUserInfo(in *user.UserInfoReq) (*user.GetUserInfoResp, error) {
// 查询用户信息
users, err := l.svcCtx.UserModel.FindOne(l.ctx, in.UserId)
if err != nil {
return nil, errors.New("用户不存在")
}
// 正常返回用户和企业信息
return &user.GetUserInfoResp{
Username: users.Username,
Phone: users.Phone,
Disable: users.Disable,
QuotaExceeded: users.QuotaExceeded,
}, nil
}

View File

@ -0,0 +1,38 @@
package walletservicelogic
import (
"context"
"tianyuan-api/apps/user/internal/svc"
"tianyuan-api/apps/user/user"
"github.com/zeromicro/go-zero/core/logx"
)
type GetDeductionByTransactionIdLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewGetDeductionByTransactionIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetDeductionByTransactionIdLogic {
return &GetDeductionByTransactionIdLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *GetDeductionByTransactionIdLogic) GetDeductionByTransactionId(in *user.GetDeductionByTransactionIdRequest) (*user.GetDeductionByTransactionIdResponse, error) {
deduction, err := l.svcCtx.DeductionsModel.FindOneByTransactionId(l.ctx, in.TransactionId)
if err != nil {
return nil, err
}
return &user.GetDeductionByTransactionIdResponse{
Id: deduction.Id,
UserId: deduction.UserId,
Amount: deduction.Amount,
TransactionId: deduction.TransactionId,
CreatedAt: deduction.CreatedAt.Format("2006-01-02 15:04:05"),
}, nil
}

View File

@ -0,0 +1,31 @@
package walletservicelogic
import (
"context"
"tianyuan-api/apps/user/internal/svc"
"tianyuan-api/apps/user/user"
"github.com/zeromicro/go-zero/core/logx"
)
type GetDeductionsLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewGetDeductionsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetDeductionsLogic {
return &GetDeductionsLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
// 查询扣款记录
func (l *GetDeductionsLogic) GetDeductions(in *user.GetDeductionsRequest) (*user.GetDeductionsResponse, error) {
// todo: add your logic here and delete this line
return &user.GetDeductionsResponse{}, nil
}

View File

@ -0,0 +1,31 @@
package walletservicelogic
import (
"context"
"tianyuan-api/apps/user/internal/svc"
"tianyuan-api/apps/user/user"
"github.com/zeromicro/go-zero/core/logx"
)
type GetWalletLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewGetWalletLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetWalletLogic {
return &GetWalletLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
// 查询钱包信息
func (l *GetWalletLogic) GetWallet(in *user.GetWalletRequest) (*user.GetWalletResponse, error) {
// todo: add your logic here and delete this line
return &user.GetWalletResponse{}, nil
}

View File

@ -0,0 +1,114 @@
package walletservicelogic
import (
"context"
"database/sql"
"errors"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"tianyuan-api/apps/sentinel/client/product"
"tianyuan-api/apps/user/internal/model"
"time"
"tianyuan-api/apps/user/internal/svc"
"tianyuan-api/apps/user/user"
"github.com/zeromicro/go-zero/core/logx"
)
type UpdateWalletLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewUpdateWalletLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateWalletLogic {
return &UpdateWalletLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
const maxRetries = 5 // 最大重试次数
// 修改钱包余额
func (l *UpdateWalletLogic) UpdateWallet(in *user.UpdateWalletRequest) (*user.UpdateWalletResponse, error) {
if !in.Charge {
return nil, errors.New("不需要扣款")
}
// 获取产品信息
consumeProduct, getProductByCodeErr := l.svcCtx.ProductRpc.GetProductByCode(l.ctx, &product.GetRecordByCodeRequest{Code: in.ProductCode})
if getProductByCodeErr != nil {
return nil, getProductByCodeErr
}
// 检查是否已经扣款
deduction, FindOneByTransactionIdErr := l.svcCtx.DeductionsModel.FindOneByTransactionId(l.ctx, in.TransactionId)
if FindOneByTransactionIdErr != nil {
if FindOneByTransactionIdErr == sql.ErrNoRows {
} else {
// 其他错误,直接返回
return nil, FindOneByTransactionIdErr
}
} else if deduction != nil {
// 找到扣款记录,不能重复扣款
return nil, errors.New("该订单已扣款")
}
// 启动事务
err := l.svcCtx.WalletsModel.TransCtx(l.ctx, func(ctx context.Context, session sqlx.Session) error {
var err error
// 尝试多次更新余额(用于乐观锁)
for i := 0; i < maxRetries; i++ {
err = l.svcCtx.WalletsModel.UpdateBalance(session, l.ctx, in.UserId, -consumeProduct.ProductPrice)
if err == nil {
// 成功,退出循环
break
}
if errors.Is(err, model.ErrVersionMismatch) {
// 版本号不匹配,重试
time.Sleep(100 * time.Millisecond) // 重试前的延迟
continue
} else {
// 其他错误,直接返回
return err
}
}
if err != nil {
return err
}
// 更新余额成功后,插入扣款记录
_, err = l.svcCtx.DeductionsModel.InsertDeductionsTrans(ctx, &model.Deductions{
TransactionId: in.TransactionId,
UserId: in.UserId,
Amount: consumeProduct.ProductPrice,
}, session)
if err != nil {
return err
}
return nil
})
if err != nil {
return nil, err
}
// 获取最新的用户余额
wallet, err := l.svcCtx.WalletsModel.FindOneByUserId(l.ctx, in.UserId)
if err != nil {
return nil, err
}
// 如果余额小于 0更新 QuotaExceeded 字段为 1
if wallet.Balance < 0 {
users, findOneErr := l.svcCtx.UserModel.FindOne(l.ctx, in.UserId)
if findOneErr != nil {
return nil, findOneErr
}
users.QuotaExceeded = 1
updateErr := l.svcCtx.UserModel.Update(l.ctx, users)
if updateErr != nil {
return nil, updateErr
}
}
return &user.UpdateWalletResponse{}, nil
}

View File

@ -0,0 +1,27 @@
package model
import (
"github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/core/stores/sqlx"
)
var _ ApiRequestsModel = (*customApiRequestsModel)(nil)
type (
// ApiRequestsModel is an interface to be customized, add more methods here,
// and implement the added methods in customApiRequestsModel.
ApiRequestsModel interface {
apiRequestsModel
}
customApiRequestsModel struct {
*defaultApiRequestsModel
}
)
// NewApiRequestsModel returns a model for the database table.
func NewApiRequestsModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) ApiRequestsModel {
return &customApiRequestsModel{
defaultApiRequestsModel: newApiRequestsModel(conn, c, opts...),
}
}

View File

@ -0,0 +1,152 @@
// Code generated by goctl. DO NOT EDIT.
// versions:
// goctl version: 1.7.2
package model
import (
"context"
"database/sql"
"fmt"
"strings"
"time"
"github.com/zeromicro/go-zero/core/stores/builder"
"github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/core/stores/sqlc"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"github.com/zeromicro/go-zero/core/stringx"
)
var (
apiRequestsFieldNames = builder.RawFieldNames(&ApiRequests{})
apiRequestsRows = strings.Join(apiRequestsFieldNames, ",")
apiRequestsRowsExpectAutoSet = strings.Join(stringx.Remove(apiRequestsFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
apiRequestsRowsWithPlaceHolder = strings.Join(stringx.Remove(apiRequestsFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
cacheApiRequestsIdPrefix = "cache:apiRequests:id:"
cacheApiRequestsTransactionIdPrefix = "cache:apiRequests:transactionId:"
)
type (
apiRequestsModel interface {
Insert(ctx context.Context, data *ApiRequests) (sql.Result, error)
FindOne(ctx context.Context, id int64) (*ApiRequests, error)
FindOneByTransactionId(ctx context.Context, transactionId string) (*ApiRequests, error)
Update(ctx context.Context, data *ApiRequests) error
Delete(ctx context.Context, id int64) error
}
defaultApiRequestsModel struct {
sqlc.CachedConn
table string
}
ApiRequests struct {
Id int64 `db:"id"` // 请求记录ID
TransactionId string `db:"transaction_id"` // 交易ID
UserId int64 `db:"user_id"` // 用户ID
ProductCode string `db:"product_code"` // 产品编码
Status string `db:"status"` // 请求状态success=成功failed=失败
Charges int64 `db:"charges"` // 是否需要付费
Remark sql.NullString `db:"remark"` // 备注
Timestamp time.Time `db:"timestamp"` // 请求时间
}
)
func newApiRequestsModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *defaultApiRequestsModel {
return &defaultApiRequestsModel{
CachedConn: sqlc.NewConn(conn, c, opts...),
table: "`api_requests`",
}
}
func (m *defaultApiRequestsModel) Delete(ctx context.Context, id int64) error {
data, err := m.FindOne(ctx, id)
if err != nil {
return err
}
apiRequestsIdKey := fmt.Sprintf("%s%v", cacheApiRequestsIdPrefix, id)
apiRequestsTransactionIdKey := fmt.Sprintf("%s%v", cacheApiRequestsTransactionIdPrefix, data.TransactionId)
_, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
return conn.ExecCtx(ctx, query, id)
}, apiRequestsIdKey, apiRequestsTransactionIdKey)
return err
}
func (m *defaultApiRequestsModel) FindOne(ctx context.Context, id int64) (*ApiRequests, error) {
apiRequestsIdKey := fmt.Sprintf("%s%v", cacheApiRequestsIdPrefix, id)
var resp ApiRequests
err := m.QueryRowCtx(ctx, &resp, apiRequestsIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", apiRequestsRows, m.table)
return conn.QueryRowCtx(ctx, v, query, id)
})
switch err {
case nil:
return &resp, nil
case sqlc.ErrNotFound:
return nil, ErrNotFound
default:
return nil, err
}
}
func (m *defaultApiRequestsModel) FindOneByTransactionId(ctx context.Context, transactionId string) (*ApiRequests, error) {
apiRequestsTransactionIdKey := fmt.Sprintf("%s%v", cacheApiRequestsTransactionIdPrefix, transactionId)
var resp ApiRequests
err := m.QueryRowIndexCtx(ctx, &resp, apiRequestsTransactionIdKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) {
query := fmt.Sprintf("select %s from %s where `transaction_id` = ? limit 1", apiRequestsRows, m.table)
if err := conn.QueryRowCtx(ctx, &resp, query, transactionId); err != nil {
return nil, err
}
return resp.Id, nil
}, m.queryPrimary)
switch err {
case nil:
return &resp, nil
case sqlc.ErrNotFound:
return nil, ErrNotFound
default:
return nil, err
}
}
func (m *defaultApiRequestsModel) Insert(ctx context.Context, data *ApiRequests) (sql.Result, error) {
apiRequestsIdKey := fmt.Sprintf("%s%v", cacheApiRequestsIdPrefix, data.Id)
apiRequestsTransactionIdKey := fmt.Sprintf("%s%v", cacheApiRequestsTransactionIdPrefix, data.TransactionId)
ret, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?)", m.table, apiRequestsRowsExpectAutoSet)
return conn.ExecCtx(ctx, query, data.TransactionId, data.UserId, data.ProductCode, data.Status, data.Charges, data.Remark, data.Timestamp)
}, apiRequestsIdKey, apiRequestsTransactionIdKey)
return ret, err
}
func (m *defaultApiRequestsModel) Update(ctx context.Context, newData *ApiRequests) error {
data, err := m.FindOne(ctx, newData.Id)
if err != nil {
return err
}
apiRequestsIdKey := fmt.Sprintf("%s%v", cacheApiRequestsIdPrefix, data.Id)
apiRequestsTransactionIdKey := fmt.Sprintf("%s%v", cacheApiRequestsTransactionIdPrefix, data.TransactionId)
_, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, apiRequestsRowsWithPlaceHolder)
return conn.ExecCtx(ctx, query, newData.TransactionId, newData.UserId, newData.ProductCode, newData.Status, newData.Charges, newData.Remark, newData.Timestamp, newData.Id)
}, apiRequestsIdKey, apiRequestsTransactionIdKey)
return err
}
func (m *defaultApiRequestsModel) formatPrimary(primary any) string {
return fmt.Sprintf("%s%v", cacheApiRequestsIdPrefix, primary)
}
func (m *defaultApiRequestsModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary any) error {
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", apiRequestsRows, m.table)
return conn.QueryRowCtx(ctx, v, query, primary)
}
func (m *defaultApiRequestsModel) tableName() string {
return m.table
}

View File

@ -0,0 +1,52 @@
package model
import (
"context"
"database/sql"
"fmt"
"github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/core/stores/sqlx"
)
var _ DeductionsModel = (*customDeductionsModel)(nil)
type (
// DeductionsModel is an interface to be customized, add more methods here,
// and implement the added methods in customDeductionsModel.
DeductionsModel interface {
deductionsModel
InsertDeductionsTrans(ctx context.Context, deductions *Deductions, session sqlx.Session) (sql.Result, error)
}
customDeductionsModel struct {
*defaultDeductionsModel
}
)
// NewDeductionsModel returns a model for the database table.
func NewDeductionsModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) DeductionsModel {
return &customDeductionsModel{
defaultDeductionsModel: newDeductionsModel(conn, c, opts...),
}
}
func (m *customDeductionsModel) InsertDeductionsTrans(ctx context.Context, deductions *Deductions, session sqlx.Session) (sql.Result, error) {
query := fmt.Sprintf("INSERT INTO %s (%s) VALUES (?, ?, ?)", m.table, deductionsRowsExpectAutoSet)
ret, err := session.ExecCtx(ctx, query, deductions.UserId, deductions.Amount, deductions.TransactionId)
if err != nil {
return nil, err
}
deductionsTransactionIdKey := fmt.Sprintf("%s%v", cacheDeductionsTransactionIdPrefix, deductions.TransactionId)
// 2. 更新缓存,保证所有缓存操作成功
cacheKeys := []string{deductionsTransactionIdKey}
cacheErrors := make([]error, len(cacheKeys))
cacheErrors[0] = m.DelCacheCtx(ctx, deductionsTransactionIdKey)
// 3. 检查缓存操作是否全部成功
for _, cacheErr := range cacheErrors {
if cacheErr != nil {
return nil, cacheErr // 返回第一个缓存更新失败的错误
}
}
return ret, err
}

View File

@ -0,0 +1,149 @@
// Code generated by goctl. DO NOT EDIT.
// versions:
// goctl version: 1.7.2
package model
import (
"context"
"database/sql"
"fmt"
"strings"
"time"
"github.com/zeromicro/go-zero/core/stores/builder"
"github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/core/stores/sqlc"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"github.com/zeromicro/go-zero/core/stringx"
)
var (
deductionsFieldNames = builder.RawFieldNames(&Deductions{})
deductionsRows = strings.Join(deductionsFieldNames, ",")
deductionsRowsExpectAutoSet = strings.Join(stringx.Remove(deductionsFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
deductionsRowsWithPlaceHolder = strings.Join(stringx.Remove(deductionsFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
cacheDeductionsIdPrefix = "cache:deductions:id:"
cacheDeductionsTransactionIdPrefix = "cache:deductions:transactionId:"
)
type (
deductionsModel interface {
Insert(ctx context.Context, data *Deductions) (sql.Result, error)
FindOne(ctx context.Context, id int64) (*Deductions, error)
FindOneByTransactionId(ctx context.Context, transactionId string) (*Deductions, error)
Update(ctx context.Context, data *Deductions) error
Delete(ctx context.Context, id int64) error
}
defaultDeductionsModel struct {
sqlc.CachedConn
table string
}
Deductions struct {
Id int64 `db:"id"` // 扣款记录ID
UserId int64 `db:"user_id"` // 用户ID
Amount float64 `db:"amount"` // 扣款金额
TransactionId string `db:"transaction_id"` // 交易流水号
CreatedAt time.Time `db:"created_at"` // 扣款时间
}
)
func newDeductionsModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *defaultDeductionsModel {
return &defaultDeductionsModel{
CachedConn: sqlc.NewConn(conn, c, opts...),
table: "`deductions`",
}
}
func (m *defaultDeductionsModel) Delete(ctx context.Context, id int64) error {
data, err := m.FindOne(ctx, id)
if err != nil {
return err
}
deductionsIdKey := fmt.Sprintf("%s%v", cacheDeductionsIdPrefix, id)
deductionsTransactionIdKey := fmt.Sprintf("%s%v", cacheDeductionsTransactionIdPrefix, data.TransactionId)
_, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
return conn.ExecCtx(ctx, query, id)
}, deductionsIdKey, deductionsTransactionIdKey)
return err
}
func (m *defaultDeductionsModel) FindOne(ctx context.Context, id int64) (*Deductions, error) {
deductionsIdKey := fmt.Sprintf("%s%v", cacheDeductionsIdPrefix, id)
var resp Deductions
err := m.QueryRowCtx(ctx, &resp, deductionsIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", deductionsRows, m.table)
return conn.QueryRowCtx(ctx, v, query, id)
})
switch err {
case nil:
return &resp, nil
case sqlc.ErrNotFound:
return nil, ErrNotFound
default:
return nil, err
}
}
func (m *defaultDeductionsModel) FindOneByTransactionId(ctx context.Context, transactionId string) (*Deductions, error) {
deductionsTransactionIdKey := fmt.Sprintf("%s%v", cacheDeductionsTransactionIdPrefix, transactionId)
var resp Deductions
err := m.QueryRowIndexCtx(ctx, &resp, deductionsTransactionIdKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) {
query := fmt.Sprintf("select %s from %s where `transaction_id` = ? limit 1", deductionsRows, m.table)
if err := conn.QueryRowCtx(ctx, &resp, query, transactionId); err != nil {
return nil, err
}
return resp.Id, nil
}, m.queryPrimary)
switch err {
case nil:
return &resp, nil
case sqlc.ErrNotFound:
return nil, ErrNotFound
default:
return nil, err
}
}
func (m *defaultDeductionsModel) Insert(ctx context.Context, data *Deductions) (sql.Result, error) {
deductionsIdKey := fmt.Sprintf("%s%v", cacheDeductionsIdPrefix, data.Id)
deductionsTransactionIdKey := fmt.Sprintf("%s%v", cacheDeductionsTransactionIdPrefix, data.TransactionId)
ret, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?)", m.table, deductionsRowsExpectAutoSet)
return conn.ExecCtx(ctx, query, data.UserId, data.Amount, data.TransactionId)
}, deductionsIdKey, deductionsTransactionIdKey)
return ret, err
}
func (m *defaultDeductionsModel) Update(ctx context.Context, newData *Deductions) error {
data, err := m.FindOne(ctx, newData.Id)
if err != nil {
return err
}
deductionsIdKey := fmt.Sprintf("%s%v", cacheDeductionsIdPrefix, data.Id)
deductionsTransactionIdKey := fmt.Sprintf("%s%v", cacheDeductionsTransactionIdPrefix, data.TransactionId)
_, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, deductionsRowsWithPlaceHolder)
return conn.ExecCtx(ctx, query, newData.UserId, newData.Amount, newData.TransactionId, newData.Id)
}, deductionsIdKey, deductionsTransactionIdKey)
return err
}
func (m *defaultDeductionsModel) formatPrimary(primary any) string {
return fmt.Sprintf("%s%v", cacheDeductionsIdPrefix, primary)
}
func (m *defaultDeductionsModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary any) error {
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", deductionsRows, m.table)
return conn.QueryRowCtx(ctx, v, query, primary)
}
func (m *defaultDeductionsModel) tableName() string {
return m.table
}

View File

@ -45,13 +45,16 @@ type (
}
Users struct {
Id int64 `db:"id"` // 用户ID
Username string `db:"username"` // 用户名
Password string `db:"password"` // 用户密码
Phone string `db:"phone"` // 用户手机号
AuthStatus string `db:"auth_status"` // 认证状态unverified=未提交pending=待审核approved=审核通过rejected=审核拒绝
CreatedAt time.Time `db:"created_at"` // 用户创建时间
UpdatedAt time.Time `db:"updated_at"` // 用户更新时间
Id int64 `db:"id"` // 用户ID
Username string `db:"username"` // 用户名
Password string `db:"password"` // 用户密码
Phone string `db:"phone"` // 用户手机号
AuthStatus string `db:"auth_status"` // 认证状态unverified=未提交pending=待审核approved=审核通过rejected=审核拒绝
Disable int64 `db:"disable"` // 是否禁用0=未禁用1=禁用
Internal int64 `db:"internal"` // 是否内部人员0=否1=是
QuotaExceeded int64 `db:"quota_exceeded"` // 是否额度用完0=否1=是
CreatedAt time.Time `db:"created_at"` // 用户创建时间
UpdatedAt time.Time `db:"updated_at"` // 用户更新时间
}
)
@ -140,8 +143,8 @@ func (m *defaultUsersModel) Insert(ctx context.Context, data *Users) (sql.Result
usersPhoneKey := fmt.Sprintf("%s%v", cacheUsersPhonePrefix, data.Phone)
usersUsernameKey := fmt.Sprintf("%s%v", cacheUsersUsernamePrefix, data.Username)
ret, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?)", m.table, usersRowsExpectAutoSet)
return conn.ExecCtx(ctx, query, data.Username, data.Password, data.Phone, data.AuthStatus)
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?)", m.table, usersRowsExpectAutoSet)
return conn.ExecCtx(ctx, query, data.Username, data.Password, data.Phone, data.AuthStatus, data.Disable, data.Internal, data.QuotaExceeded)
}, usersIdKey, usersPhoneKey, usersUsernameKey)
return ret, err
}
@ -157,7 +160,7 @@ func (m *defaultUsersModel) Update(ctx context.Context, newData *Users) error {
usersUsernameKey := fmt.Sprintf("%s%v", cacheUsersUsernamePrefix, data.Username)
_, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, usersRowsWithPlaceHolder)
return conn.ExecCtx(ctx, query, newData.Username, newData.Password, newData.Phone, newData.AuthStatus, newData.Id)
return conn.ExecCtx(ctx, query, newData.Username, newData.Password, newData.Phone, newData.AuthStatus, newData.Disable, newData.Internal, newData.QuotaExceeded, newData.Id)
}, usersIdKey, usersPhoneKey, usersUsernameKey)
return err
}

View File

@ -0,0 +1,89 @@
package model
import (
"context"
"database/sql"
"errors"
"fmt"
"github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/core/stores/sqlx"
)
var _ WalletsModel = (*customWalletsModel)(nil)
type (
// WalletsModel is an interface to be customized, add more methods here,
// and implement the added methods in customWalletsModel.
WalletsModel interface {
walletsModel
InsertWalletsTrans(ctx context.Context, wallets *Wallets, session sqlx.Session) (sql.Result, error)
UpdateBalance(session sqlx.Session, ctx context.Context, userId int64, amount float64) error
TransCtx(ctx context.Context, fn func(ctx context.Context, session sqlx.Session) error) error
}
customWalletsModel struct {
*defaultWalletsModel
}
)
var ErrBalanceNotEnough = errors.New("余额不足")
var ErrVersionMismatch = errors.New("版本号不匹配,请重试")
// NewWalletsModel returns a model for the database table.
func NewWalletsModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) WalletsModel {
return &customWalletsModel{
defaultWalletsModel: newWalletsModel(conn, c, opts...),
}
}
func (m *customWalletsModel) TransCtx(ctx context.Context, fn func(ctx context.Context, session sqlx.Session) error) error {
// 使用带 ctx 的事务处理
err := m.TransactCtx(ctx, func(ctx context.Context, session sqlx.Session) error {
return fn(ctx, session)
})
return err
}
// 更新余额的方法
func (m *customWalletsModel) UpdateBalance(session sqlx.Session, ctx context.Context, userId int64, amount float64) error {
wallet, err := m.FindOneByUserId(ctx, userId)
if err != nil {
return err
}
// 检查余额是否足够
if wallet.Balance+amount < 0 {
return ErrBalanceNotEnough
}
// 使用乐观锁更新余额
result, err := session.Exec("UPDATE wallets SET balance = balance + ?, version = version + 1 WHERE user_id = ? AND version = ?", amount, userId, wallet.Version)
if err != nil {
return err
}
// 检查影响的行数,确保更新成功
rowsAffected, err := result.RowsAffected()
if err != nil {
return err
}
if rowsAffected == 0 {
return ErrVersionMismatch
}
walletsUserIdKey := fmt.Sprintf("%s%v", cacheWalletsUserIdPrefix, userId)
cacheErrors := m.DelCacheCtx(ctx, walletsUserIdKey)
if cacheErrors != nil {
return cacheErrors
}
return nil
}
func (m *customWalletsModel) InsertWalletsTrans(ctx context.Context, wallets *Wallets, session sqlx.Session) (sql.Result, error) {
query := fmt.Sprintf("INSERT INTO %s (%s) VALUES (?, ?, ?)", m.table, walletsRowsExpectAutoSet)
ret, err := session.ExecCtx(ctx, query, wallets.UserId, wallets.Balance, wallets.Version)
if err != nil {
return nil, err
}
return ret, err
}

View File

@ -0,0 +1,150 @@
// Code generated by goctl. DO NOT EDIT.
// versions:
// goctl version: 1.7.2
package model
import (
"context"
"database/sql"
"fmt"
"strings"
"time"
"github.com/zeromicro/go-zero/core/stores/builder"
"github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/core/stores/sqlc"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"github.com/zeromicro/go-zero/core/stringx"
)
var (
walletsFieldNames = builder.RawFieldNames(&Wallets{})
walletsRows = strings.Join(walletsFieldNames, ",")
walletsRowsExpectAutoSet = strings.Join(stringx.Remove(walletsFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",")
walletsRowsWithPlaceHolder = strings.Join(stringx.Remove(walletsFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?"
cacheWalletsIdPrefix = "cache:wallets:id:"
cacheWalletsUserIdPrefix = "cache:wallets:userId:"
)
type (
walletsModel interface {
Insert(ctx context.Context, data *Wallets) (sql.Result, error)
FindOne(ctx context.Context, id int64) (*Wallets, error)
FindOneByUserId(ctx context.Context, userId int64) (*Wallets, error)
Update(ctx context.Context, data *Wallets) error
Delete(ctx context.Context, id int64) error
}
defaultWalletsModel struct {
sqlc.CachedConn
table string
}
Wallets struct {
Id int64 `db:"id"` // 钱包ID
UserId int64 `db:"user_id"` // 用户ID
Balance float64 `db:"balance"` // 钱包余额
Version int64 `db:"version"` // 乐观锁版本号
CreatedAt time.Time `db:"created_at"` // 创建时间
UpdatedAt time.Time `db:"updated_at"` // 更新时间
}
)
func newWalletsModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) *defaultWalletsModel {
return &defaultWalletsModel{
CachedConn: sqlc.NewConn(conn, c, opts...),
table: "`wallets`",
}
}
func (m *defaultWalletsModel) Delete(ctx context.Context, id int64) error {
data, err := m.FindOne(ctx, id)
if err != nil {
return err
}
walletsIdKey := fmt.Sprintf("%s%v", cacheWalletsIdPrefix, id)
walletsUserIdKey := fmt.Sprintf("%s%v", cacheWalletsUserIdPrefix, data.UserId)
_, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
return conn.ExecCtx(ctx, query, id)
}, walletsIdKey, walletsUserIdKey)
return err
}
func (m *defaultWalletsModel) FindOne(ctx context.Context, id int64) (*Wallets, error) {
walletsIdKey := fmt.Sprintf("%s%v", cacheWalletsIdPrefix, id)
var resp Wallets
err := m.QueryRowCtx(ctx, &resp, walletsIdKey, func(ctx context.Context, conn sqlx.SqlConn, v any) error {
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", walletsRows, m.table)
return conn.QueryRowCtx(ctx, v, query, id)
})
switch err {
case nil:
return &resp, nil
case sqlc.ErrNotFound:
return nil, ErrNotFound
default:
return nil, err
}
}
func (m *defaultWalletsModel) FindOneByUserId(ctx context.Context, userId int64) (*Wallets, error) {
walletsUserIdKey := fmt.Sprintf("%s%v", cacheWalletsUserIdPrefix, userId)
var resp Wallets
err := m.QueryRowIndexCtx(ctx, &resp, walletsUserIdKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v any) (i any, e error) {
query := fmt.Sprintf("select %s from %s where `user_id` = ? limit 1", walletsRows, m.table)
if err := conn.QueryRowCtx(ctx, &resp, query, userId); err != nil {
return nil, err
}
return resp.Id, nil
}, m.queryPrimary)
switch err {
case nil:
return &resp, nil
case sqlc.ErrNotFound:
return nil, ErrNotFound
default:
return nil, err
}
}
func (m *defaultWalletsModel) Insert(ctx context.Context, data *Wallets) (sql.Result, error) {
walletsIdKey := fmt.Sprintf("%s%v", cacheWalletsIdPrefix, data.Id)
walletsUserIdKey := fmt.Sprintf("%s%v", cacheWalletsUserIdPrefix, data.UserId)
ret, err := m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?)", m.table, walletsRowsExpectAutoSet)
return conn.ExecCtx(ctx, query, data.UserId, data.Balance, data.Version)
}, walletsIdKey, walletsUserIdKey)
return ret, err
}
func (m *defaultWalletsModel) Update(ctx context.Context, newData *Wallets) error {
data, err := m.FindOne(ctx, newData.Id)
if err != nil {
return err
}
walletsIdKey := fmt.Sprintf("%s%v", cacheWalletsIdPrefix, data.Id)
walletsUserIdKey := fmt.Sprintf("%s%v", cacheWalletsUserIdPrefix, data.UserId)
_, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, walletsRowsWithPlaceHolder)
return conn.ExecCtx(ctx, query, newData.UserId, newData.Balance, newData.Version, newData.Id)
}, walletsIdKey, walletsUserIdKey)
return err
}
func (m *defaultWalletsModel) formatPrimary(primary any) string {
return fmt.Sprintf("%s%v", cacheWalletsIdPrefix, primary)
}
func (m *defaultWalletsModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary any) error {
query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", walletsRows, m.table)
return conn.QueryRowCtx(ctx, v, query, primary)
}
func (m *defaultWalletsModel) tableName() string {
return m.table
}

View File

@ -0,0 +1,42 @@
// Code generated by goctl. DO NOT EDIT.
// goctl 1.7.2
// Source: user.proto
package server
import (
"context"
"tianyuan-api/apps/user/internal/logic/apirequestservice"
"tianyuan-api/apps/user/internal/svc"
"tianyuan-api/apps/user/user"
)
type ApiRequestServiceServer struct {
svcCtx *svc.ServiceContext
user.UnimplementedApiRequestServiceServer
}
func NewApiRequestServiceServer(svcCtx *svc.ServiceContext) *ApiRequestServiceServer {
return &ApiRequestServiceServer{
svcCtx: svcCtx,
}
}
// 添加API请求记录
func (s *ApiRequestServiceServer) AddApiRequest(ctx context.Context, in *user.AddApiRequestRequest) (*user.AddApiRequestResponse, error) {
l := apirequestservicelogic.NewAddApiRequestLogic(ctx, s.svcCtx)
return l.AddApiRequest(in)
}
// 查询API请求记录
func (s *ApiRequestServiceServer) GetApiRequests(ctx context.Context, in *user.GetApiRequestsRequest) (*user.GetApiRequestsResponse, error) {
l := apirequestservicelogic.NewGetApiRequestsLogic(ctx, s.svcCtx)
return l.GetApiRequests(in)
}
// 查询API请求记录ByTransactionId
func (s *ApiRequestServiceServer) GetApiRequestByTransactionId(ctx context.Context, in *user.GetApiRequestByTransactionIdRequest) (*user.GetApiRequestByTransactionIdResponse, error) {
l := apirequestservicelogic.NewGetApiRequestByTransactionIdLogic(ctx, s.svcCtx)
return l.GetApiRequestByTransactionId(in)
}

View File

@ -29,6 +29,11 @@ func (s *UserServer) UserInfo(ctx context.Context, in *user.UserInfoReq) (*user.
return l.UserInfo(in)
}
func (s *UserServer) GetUserInfo(ctx context.Context, in *user.UserInfoReq) (*user.GetUserInfoResp, error) {
l := userlogic.NewGetUserInfoLogic(ctx, s.svcCtx)
return l.GetUserInfo(in)
}
func (s *UserServer) GetEnterpriseAuthStatus(ctx context.Context, in *user.GetEnterpriseAuthStatusReq) (*user.GetEnterpriseAuthStatusResp, error) {
l := userlogic.NewGetEnterpriseAuthStatusLogic(ctx, s.svcCtx)
return l.GetEnterpriseAuthStatus(in)

View File

@ -0,0 +1,47 @@
// Code generated by goctl. DO NOT EDIT.
// goctl 1.7.2
// Source: user.proto
package server
import (
"context"
"tianyuan-api/apps/user/internal/logic/walletservice"
"tianyuan-api/apps/user/internal/svc"
"tianyuan-api/apps/user/user"
)
type WalletServiceServer struct {
svcCtx *svc.ServiceContext
user.UnimplementedWalletServiceServer
}
func NewWalletServiceServer(svcCtx *svc.ServiceContext) *WalletServiceServer {
return &WalletServiceServer{
svcCtx: svcCtx,
}
}
// 修改钱包余额
func (s *WalletServiceServer) UpdateWallet(ctx context.Context, in *user.UpdateWalletRequest) (*user.UpdateWalletResponse, error) {
l := walletservicelogic.NewUpdateWalletLogic(ctx, s.svcCtx)
return l.UpdateWallet(in)
}
// 查询钱包信息
func (s *WalletServiceServer) GetWallet(ctx context.Context, in *user.GetWalletRequest) (*user.GetWalletResponse, error) {
l := walletservicelogic.NewGetWalletLogic(ctx, s.svcCtx)
return l.GetWallet(in)
}
// 查询扣款记录
func (s *WalletServiceServer) GetDeductions(ctx context.Context, in *user.GetDeductionsRequest) (*user.GetDeductionsResponse, error) {
l := walletservicelogic.NewGetDeductionsLogic(ctx, s.svcCtx)
return l.GetDeductions(in)
}
func (s *WalletServiceServer) GetDeductionByTransactionId(ctx context.Context, in *user.GetDeductionByTransactionIdRequest) (*user.GetDeductionByTransactionIdResponse, error) {
l := walletservicelogic.NewGetDeductionByTransactionIdLogic(ctx, s.svcCtx)
return l.GetDeductionByTransactionId(in)
}

View File

@ -15,7 +15,11 @@ type ServiceContext struct {
UserModel model.UsersModel // 用户表的模型
EnterpriseModel model.EnterpriseInfoModel
EnterpriseAuthModel model.EnterpriseAuthModel
WalletsModel model.WalletsModel
DeductionsModel model.DeductionsModel
ApiRequestsModel model.ApiRequestsModel
SecretRpc sentinel.SecretClient
ProductRpc sentinel.ProductClient
}
func NewServiceContext(c config.Config) *ServiceContext {
@ -34,6 +38,10 @@ func NewServiceContext(c config.Config) *ServiceContext {
UserModel: model.NewUsersModel(db, c.CacheRedis), // 注入UserModel
EnterpriseModel: model.NewEnterpriseInfoModel(db, c.CacheRedis),
EnterpriseAuthModel: model.NewEnterpriseAuthModel(db, c.CacheRedis),
WalletsModel: model.NewWalletsModel(db, c.CacheRedis),
DeductionsModel: model.NewDeductionsModel(db, c.CacheRedis),
ApiRequestsModel: model.NewApiRequestsModel(db, c.CacheRedis),
SecretRpc: sentinel.NewSecretClient(zrpc.MustNewClient(c.SentinelRpc).Conn()),
ProductRpc: sentinel.NewProductClient(zrpc.MustNewClient(c.SentinelRpc).Conn()),
}
}

View File

@ -6,9 +6,11 @@ import (
"os"
"tianyuan-api/apps/user/internal/config"
apirequestserviceServer "tianyuan-api/apps/user/internal/server/apirequestservice"
authServer "tianyuan-api/apps/user/internal/server/auth"
enterpriseServer "tianyuan-api/apps/user/internal/server/enterprise"
userServer "tianyuan-api/apps/user/internal/server/user"
walletserviceServer "tianyuan-api/apps/user/internal/server/walletservice"
"tianyuan-api/apps/user/internal/svc"
"tianyuan-api/apps/user/user"
@ -46,6 +48,8 @@ func main() {
user.RegisterEnterpriseServer(grpcServer, enterpriseServer.NewEnterpriseServer(ctx))
user.RegisterAuthServer(grpcServer, authServer.NewAuthServer(ctx))
user.RegisterUserServer(grpcServer, userServer.NewUserServer(ctx))
user.RegisterWalletServiceServer(grpcServer, walletserviceServer.NewWalletServiceServer(ctx))
user.RegisterApiRequestServiceServer(grpcServer, apirequestserviceServer.NewApiRequestServiceServer(ctx))
if c.Mode == service.DevMode || c.Mode == service.TestMode {
reflection.Register(grpcServer)

View File

@ -107,10 +107,18 @@ message UserInfoReq {
message UserInfoResp{
string username = 1;
string phone = 2;
string enterpriseAuthStatus = 3;
string enterpriseName = 4;
string creditCode = 5;
string legalPerson = 6;
int64 disable = 3;
int64 quotaExceeded = 4;
string enterpriseAuthStatus = 5;
string enterpriseName = 6;
string creditCode = 7;
string legalPerson = 8;
}
message GetUserInfoResp{
string username = 1;
string phone = 2;
int64 disable = 3;
int64 quotaExceeded = 4;
}
message GetEnterpriseAuthStatusReq {
@ -122,5 +130,142 @@ message GetEnterpriseAuthStatusResp {
service User {
//
rpc UserInfo(UserInfoReq) returns (UserInfoResp);
rpc GetUserInfo(UserInfoReq) returns (GetUserInfoResp);
rpc GetEnterpriseAuthStatus(GetEnterpriseAuthStatusReq) returns (GetEnterpriseAuthStatusResp);
}
//
service WalletService {
//
rpc UpdateWallet (UpdateWalletRequest) returns (UpdateWalletResponse);
//
rpc GetWallet (GetWalletRequest) returns (GetWalletResponse);
//
rpc GetDeductions (GetDeductionsRequest) returns (GetDeductionsResponse);
rpc GetDeductionByTransactionId (GetDeductionByTransactionIdRequest) returns (GetDeductionByTransactionIdResponse);
}
//
message UpdateWalletRequest {
int64 user_id = 1;
string transaction_id = 2;
string product_code = 3;
string remark = 4;
bool charge = 5;
}
message UpdateWalletResponse {
}
//
message GetWalletRequest {
int64 id = 1;
}
message GetWalletResponse {
int64 id = 1;
int64 user_id = 2;
double balance = 3;
int64 version = 4;
}
//
message GetDeductionsRequest {
int64 user_id = 1;
int64 page = 2;
int64 size = 3;
}
message GetDeductionsResponse {
repeated Deduction deductions = 1;
}
message GetDeductionByTransactionIdRequest {
string transaction_id = 1;
}
message GetDeductionByTransactionIdResponse {
int64 id = 1;
int64 user_id = 2;
double amount = 3;
string transaction_id = 4;
string created_at = 5;
}
message Deduction {
int64 id = 1;
int64 user_id = 2;
double amount = 3;
string transaction_id = 4;
string created_at = 5;
}
service ApiRequestService {
// API请求记录
rpc AddApiRequest (AddApiRequestRequest) returns (AddApiRequestResponse);
// API请求记录
rpc GetApiRequests (GetApiRequestsRequest) returns (GetApiRequestsResponse);
// API请求记录ByTransactionId
rpc GetApiRequestByTransactionId (GetApiRequestByTransactionIdRequest) returns (GetApiRequestByTransactionIdResponse);
}
// API请求记录
message AddApiRequestRequest {
string transaction_id = 1;
int64 user_id = 2;
string product_code = 3;
string status = 4;
bool charges = 5;
string remark = 6;
string timestamp = 7;
}
message AddApiRequestResponse {
bool success = 1;
}
// API请求记录
message GetApiRequestsRequest {
int64 user_id = 1;
int64 page = 2;
int64 size = 3;
}
message GetApiRequestsResponse {
repeated ApiRequest api_requests = 1;
}
message ApiRequest {
int64 id = 1;
string transaction_id = 2;
int64 user_id = 3;
string product_code = 4;
string status = 5;
double charges = 6;
string remark = 7;
string timestamp = 8;
}
// API请求记录
message GetApiRequestByTransactionIdRequest {
string transaction_id = 1;
}
message GetApiRequestByTransactionIdResponse {
int64 id = 1;
string transaction_id = 2;
int64 user_id = 3;
string product_code = 4;
string status = 5;
double charges = 6;
string remark = 7;
string timestamp = 8;
}

View File

@ -4,6 +4,9 @@ CREATE TABLE users (
password VARCHAR(100) NOT NULL COMMENT '用户密码',
phone VARCHAR(15) NOT NULL UNIQUE COMMENT '用户手机号',
auth_status ENUM('unverified','pending', 'approved', 'rejected') DEFAULT 'unverified' COMMENT '认证状态unverified=未提交pending=待审核approved=审核通过rejected=审核拒绝',
disable TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否禁用0=未禁用1=禁用',
internal TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否内部人员0=否1=是',
quota_exceeded TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否额度用完0=否1=是',
created_at DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '用户创建时间',
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '用户更新时间',
PRIMARY KEY (id)
@ -35,3 +38,35 @@ CREATE TABLE enterprise_auth (
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '认证更新时间',
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='企业认证表,存储企业认证的相关信息';
CREATE TABLE wallets (
id INT(11) NOT NULL AUTO_INCREMENT COMMENT '钱包ID',
user_id INT(11) NOT NULL UNIQUE COMMENT '用户ID',
balance DECIMAL(10, 2) DEFAULT 0.00 COMMENT '钱包余额',
version INT(11) DEFAULT 0 COMMENT '乐观锁版本号',
created_at DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='钱包表,存储用户的余额信息';
CREATE TABLE deductions (
id INT(11) NOT NULL AUTO_INCREMENT COMMENT '扣款记录ID',
user_id INT(11) NOT NULL COMMENT '用户ID',
amount DECIMAL(10, 2) NOT NULL COMMENT '扣款金额',
transaction_id VARCHAR(50) NOT NULL UNIQUE COMMENT '交易流水号',
created_at DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '扣款时间',
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='扣款记录表,存储用户的扣款历史记录';
CREATE TABLE api_requests (
id INT(11) NOT NULL AUTO_INCREMENT COMMENT '请求记录ID',
transaction_id VARCHAR(50) NOT NULL UNIQUE COMMENT '交易ID',
user_id INT(11) NOT NULL COMMENT '用户ID',
product_code VARCHAR(100) NOT NULL COMMENT '产品编码',
status ENUM('success', 'failed') DEFAULT 'success' COMMENT '请求状态success=成功failed=失败',
charges TINYINT(1) NOT NULL COMMENT '是否需要付费',
remark VARCHAR(255) COMMENT '备注',
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '请求时间',
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='API请求记录表存储API请求的相关信息';

File diff suppressed because it is too large Load Diff

View File

@ -374,6 +374,7 @@ var Auth_ServiceDesc = grpc.ServiceDesc{
const (
User_UserInfo_FullMethodName = "/User/UserInfo"
User_GetUserInfo_FullMethodName = "/User/GetUserInfo"
User_GetEnterpriseAuthStatus_FullMethodName = "/User/GetEnterpriseAuthStatus"
)
@ -383,6 +384,7 @@ const (
type UserClient interface {
// 获取用户信息
UserInfo(ctx context.Context, in *UserInfoReq, opts ...grpc.CallOption) (*UserInfoResp, error)
GetUserInfo(ctx context.Context, in *UserInfoReq, opts ...grpc.CallOption) (*GetUserInfoResp, error)
GetEnterpriseAuthStatus(ctx context.Context, in *GetEnterpriseAuthStatusReq, opts ...grpc.CallOption) (*GetEnterpriseAuthStatusResp, error)
}
@ -404,6 +406,16 @@ func (c *userClient) UserInfo(ctx context.Context, in *UserInfoReq, opts ...grpc
return out, nil
}
func (c *userClient) GetUserInfo(ctx context.Context, in *UserInfoReq, opts ...grpc.CallOption) (*GetUserInfoResp, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(GetUserInfoResp)
err := c.cc.Invoke(ctx, User_GetUserInfo_FullMethodName, in, out, cOpts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *userClient) GetEnterpriseAuthStatus(ctx context.Context, in *GetEnterpriseAuthStatusReq, opts ...grpc.CallOption) (*GetEnterpriseAuthStatusResp, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(GetEnterpriseAuthStatusResp)
@ -420,6 +432,7 @@ func (c *userClient) GetEnterpriseAuthStatus(ctx context.Context, in *GetEnterpr
type UserServer interface {
// 获取用户信息
UserInfo(context.Context, *UserInfoReq) (*UserInfoResp, error)
GetUserInfo(context.Context, *UserInfoReq) (*GetUserInfoResp, error)
GetEnterpriseAuthStatus(context.Context, *GetEnterpriseAuthStatusReq) (*GetEnterpriseAuthStatusResp, error)
mustEmbedUnimplementedUserServer()
}
@ -431,6 +444,9 @@ type UnimplementedUserServer struct {
func (UnimplementedUserServer) UserInfo(context.Context, *UserInfoReq) (*UserInfoResp, error) {
return nil, status.Errorf(codes.Unimplemented, "method UserInfo not implemented")
}
func (UnimplementedUserServer) GetUserInfo(context.Context, *UserInfoReq) (*GetUserInfoResp, error) {
return nil, status.Errorf(codes.Unimplemented, "method GetUserInfo not implemented")
}
func (UnimplementedUserServer) GetEnterpriseAuthStatus(context.Context, *GetEnterpriseAuthStatusReq) (*GetEnterpriseAuthStatusResp, error) {
return nil, status.Errorf(codes.Unimplemented, "method GetEnterpriseAuthStatus not implemented")
}
@ -465,6 +481,24 @@ func _User_UserInfo_Handler(srv interface{}, ctx context.Context, dec func(inter
return interceptor(ctx, in, info, handler)
}
func _User_GetUserInfo_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(UserInfoReq)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(UserServer).GetUserInfo(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: User_GetUserInfo_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(UserServer).GetUserInfo(ctx, req.(*UserInfoReq))
}
return interceptor(ctx, in, info, handler)
}
func _User_GetEnterpriseAuthStatus_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(GetEnterpriseAuthStatusReq)
if err := dec(in); err != nil {
@ -494,6 +528,10 @@ var User_ServiceDesc = grpc.ServiceDesc{
MethodName: "UserInfo",
Handler: _User_UserInfo_Handler,
},
{
MethodName: "GetUserInfo",
Handler: _User_GetUserInfo_Handler,
},
{
MethodName: "GetEnterpriseAuthStatus",
Handler: _User_GetEnterpriseAuthStatus_Handler,
@ -502,3 +540,391 @@ var User_ServiceDesc = grpc.ServiceDesc{
Streams: []grpc.StreamDesc{},
Metadata: "user.proto",
}
const (
WalletService_UpdateWallet_FullMethodName = "/WalletService/UpdateWallet"
WalletService_GetWallet_FullMethodName = "/WalletService/GetWallet"
WalletService_GetDeductions_FullMethodName = "/WalletService/GetDeductions"
WalletService_GetDeductionByTransactionId_FullMethodName = "/WalletService/GetDeductionByTransactionId"
)
// WalletServiceClient is the client API for WalletService service.
//
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
//
// 定义钱包服务
type WalletServiceClient interface {
// 修改钱包余额
UpdateWallet(ctx context.Context, in *UpdateWalletRequest, opts ...grpc.CallOption) (*UpdateWalletResponse, error)
// 查询钱包信息
GetWallet(ctx context.Context, in *GetWalletRequest, opts ...grpc.CallOption) (*GetWalletResponse, error)
// 查询扣款记录
GetDeductions(ctx context.Context, in *GetDeductionsRequest, opts ...grpc.CallOption) (*GetDeductionsResponse, error)
GetDeductionByTransactionId(ctx context.Context, in *GetDeductionByTransactionIdRequest, opts ...grpc.CallOption) (*GetDeductionByTransactionIdResponse, error)
}
type walletServiceClient struct {
cc grpc.ClientConnInterface
}
func NewWalletServiceClient(cc grpc.ClientConnInterface) WalletServiceClient {
return &walletServiceClient{cc}
}
func (c *walletServiceClient) UpdateWallet(ctx context.Context, in *UpdateWalletRequest, opts ...grpc.CallOption) (*UpdateWalletResponse, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(UpdateWalletResponse)
err := c.cc.Invoke(ctx, WalletService_UpdateWallet_FullMethodName, in, out, cOpts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *walletServiceClient) GetWallet(ctx context.Context, in *GetWalletRequest, opts ...grpc.CallOption) (*GetWalletResponse, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(GetWalletResponse)
err := c.cc.Invoke(ctx, WalletService_GetWallet_FullMethodName, in, out, cOpts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *walletServiceClient) GetDeductions(ctx context.Context, in *GetDeductionsRequest, opts ...grpc.CallOption) (*GetDeductionsResponse, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(GetDeductionsResponse)
err := c.cc.Invoke(ctx, WalletService_GetDeductions_FullMethodName, in, out, cOpts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *walletServiceClient) GetDeductionByTransactionId(ctx context.Context, in *GetDeductionByTransactionIdRequest, opts ...grpc.CallOption) (*GetDeductionByTransactionIdResponse, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(GetDeductionByTransactionIdResponse)
err := c.cc.Invoke(ctx, WalletService_GetDeductionByTransactionId_FullMethodName, in, out, cOpts...)
if err != nil {
return nil, err
}
return out, nil
}
// WalletServiceServer is the server API for WalletService service.
// All implementations must embed UnimplementedWalletServiceServer
// for forward compatibility
//
// 定义钱包服务
type WalletServiceServer interface {
// 修改钱包余额
UpdateWallet(context.Context, *UpdateWalletRequest) (*UpdateWalletResponse, error)
// 查询钱包信息
GetWallet(context.Context, *GetWalletRequest) (*GetWalletResponse, error)
// 查询扣款记录
GetDeductions(context.Context, *GetDeductionsRequest) (*GetDeductionsResponse, error)
GetDeductionByTransactionId(context.Context, *GetDeductionByTransactionIdRequest) (*GetDeductionByTransactionIdResponse, error)
mustEmbedUnimplementedWalletServiceServer()
}
// UnimplementedWalletServiceServer must be embedded to have forward compatible implementations.
type UnimplementedWalletServiceServer struct {
}
func (UnimplementedWalletServiceServer) UpdateWallet(context.Context, *UpdateWalletRequest) (*UpdateWalletResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method UpdateWallet not implemented")
}
func (UnimplementedWalletServiceServer) GetWallet(context.Context, *GetWalletRequest) (*GetWalletResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method GetWallet not implemented")
}
func (UnimplementedWalletServiceServer) GetDeductions(context.Context, *GetDeductionsRequest) (*GetDeductionsResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method GetDeductions not implemented")
}
func (UnimplementedWalletServiceServer) GetDeductionByTransactionId(context.Context, *GetDeductionByTransactionIdRequest) (*GetDeductionByTransactionIdResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method GetDeductionByTransactionId not implemented")
}
func (UnimplementedWalletServiceServer) mustEmbedUnimplementedWalletServiceServer() {}
// UnsafeWalletServiceServer may be embedded to opt out of forward compatibility for this service.
// Use of this interface is not recommended, as added methods to WalletServiceServer will
// result in compilation errors.
type UnsafeWalletServiceServer interface {
mustEmbedUnimplementedWalletServiceServer()
}
func RegisterWalletServiceServer(s grpc.ServiceRegistrar, srv WalletServiceServer) {
s.RegisterService(&WalletService_ServiceDesc, srv)
}
func _WalletService_UpdateWallet_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(UpdateWalletRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(WalletServiceServer).UpdateWallet(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: WalletService_UpdateWallet_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(WalletServiceServer).UpdateWallet(ctx, req.(*UpdateWalletRequest))
}
return interceptor(ctx, in, info, handler)
}
func _WalletService_GetWallet_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(GetWalletRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(WalletServiceServer).GetWallet(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: WalletService_GetWallet_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(WalletServiceServer).GetWallet(ctx, req.(*GetWalletRequest))
}
return interceptor(ctx, in, info, handler)
}
func _WalletService_GetDeductions_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(GetDeductionsRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(WalletServiceServer).GetDeductions(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: WalletService_GetDeductions_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(WalletServiceServer).GetDeductions(ctx, req.(*GetDeductionsRequest))
}
return interceptor(ctx, in, info, handler)
}
func _WalletService_GetDeductionByTransactionId_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(GetDeductionByTransactionIdRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(WalletServiceServer).GetDeductionByTransactionId(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: WalletService_GetDeductionByTransactionId_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(WalletServiceServer).GetDeductionByTransactionId(ctx, req.(*GetDeductionByTransactionIdRequest))
}
return interceptor(ctx, in, info, handler)
}
// WalletService_ServiceDesc is the grpc.ServiceDesc for WalletService service.
// It's only intended for direct use with grpc.RegisterService,
// and not to be introspected or modified (even as a copy)
var WalletService_ServiceDesc = grpc.ServiceDesc{
ServiceName: "WalletService",
HandlerType: (*WalletServiceServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "UpdateWallet",
Handler: _WalletService_UpdateWallet_Handler,
},
{
MethodName: "GetWallet",
Handler: _WalletService_GetWallet_Handler,
},
{
MethodName: "GetDeductions",
Handler: _WalletService_GetDeductions_Handler,
},
{
MethodName: "GetDeductionByTransactionId",
Handler: _WalletService_GetDeductionByTransactionId_Handler,
},
},
Streams: []grpc.StreamDesc{},
Metadata: "user.proto",
}
const (
ApiRequestService_AddApiRequest_FullMethodName = "/ApiRequestService/AddApiRequest"
ApiRequestService_GetApiRequests_FullMethodName = "/ApiRequestService/GetApiRequests"
ApiRequestService_GetApiRequestByTransactionId_FullMethodName = "/ApiRequestService/GetApiRequestByTransactionId"
)
// ApiRequestServiceClient is the client API for ApiRequestService service.
//
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
type ApiRequestServiceClient interface {
// 添加API请求记录
AddApiRequest(ctx context.Context, in *AddApiRequestRequest, opts ...grpc.CallOption) (*AddApiRequestResponse, error)
// 查询API请求记录
GetApiRequests(ctx context.Context, in *GetApiRequestsRequest, opts ...grpc.CallOption) (*GetApiRequestsResponse, error)
// 查询API请求记录ByTransactionId
GetApiRequestByTransactionId(ctx context.Context, in *GetApiRequestByTransactionIdRequest, opts ...grpc.CallOption) (*GetApiRequestByTransactionIdResponse, error)
}
type apiRequestServiceClient struct {
cc grpc.ClientConnInterface
}
func NewApiRequestServiceClient(cc grpc.ClientConnInterface) ApiRequestServiceClient {
return &apiRequestServiceClient{cc}
}
func (c *apiRequestServiceClient) AddApiRequest(ctx context.Context, in *AddApiRequestRequest, opts ...grpc.CallOption) (*AddApiRequestResponse, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(AddApiRequestResponse)
err := c.cc.Invoke(ctx, ApiRequestService_AddApiRequest_FullMethodName, in, out, cOpts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *apiRequestServiceClient) GetApiRequests(ctx context.Context, in *GetApiRequestsRequest, opts ...grpc.CallOption) (*GetApiRequestsResponse, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(GetApiRequestsResponse)
err := c.cc.Invoke(ctx, ApiRequestService_GetApiRequests_FullMethodName, in, out, cOpts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *apiRequestServiceClient) GetApiRequestByTransactionId(ctx context.Context, in *GetApiRequestByTransactionIdRequest, opts ...grpc.CallOption) (*GetApiRequestByTransactionIdResponse, error) {
cOpts := append([]grpc.CallOption{grpc.StaticMethod()}, opts...)
out := new(GetApiRequestByTransactionIdResponse)
err := c.cc.Invoke(ctx, ApiRequestService_GetApiRequestByTransactionId_FullMethodName, in, out, cOpts...)
if err != nil {
return nil, err
}
return out, nil
}
// ApiRequestServiceServer is the server API for ApiRequestService service.
// All implementations must embed UnimplementedApiRequestServiceServer
// for forward compatibility
type ApiRequestServiceServer interface {
// 添加API请求记录
AddApiRequest(context.Context, *AddApiRequestRequest) (*AddApiRequestResponse, error)
// 查询API请求记录
GetApiRequests(context.Context, *GetApiRequestsRequest) (*GetApiRequestsResponse, error)
// 查询API请求记录ByTransactionId
GetApiRequestByTransactionId(context.Context, *GetApiRequestByTransactionIdRequest) (*GetApiRequestByTransactionIdResponse, error)
mustEmbedUnimplementedApiRequestServiceServer()
}
// UnimplementedApiRequestServiceServer must be embedded to have forward compatible implementations.
type UnimplementedApiRequestServiceServer struct {
}
func (UnimplementedApiRequestServiceServer) AddApiRequest(context.Context, *AddApiRequestRequest) (*AddApiRequestResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method AddApiRequest not implemented")
}
func (UnimplementedApiRequestServiceServer) GetApiRequests(context.Context, *GetApiRequestsRequest) (*GetApiRequestsResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method GetApiRequests not implemented")
}
func (UnimplementedApiRequestServiceServer) GetApiRequestByTransactionId(context.Context, *GetApiRequestByTransactionIdRequest) (*GetApiRequestByTransactionIdResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method GetApiRequestByTransactionId not implemented")
}
func (UnimplementedApiRequestServiceServer) mustEmbedUnimplementedApiRequestServiceServer() {}
// UnsafeApiRequestServiceServer may be embedded to opt out of forward compatibility for this service.
// Use of this interface is not recommended, as added methods to ApiRequestServiceServer will
// result in compilation errors.
type UnsafeApiRequestServiceServer interface {
mustEmbedUnimplementedApiRequestServiceServer()
}
func RegisterApiRequestServiceServer(s grpc.ServiceRegistrar, srv ApiRequestServiceServer) {
s.RegisterService(&ApiRequestService_ServiceDesc, srv)
}
func _ApiRequestService_AddApiRequest_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(AddApiRequestRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(ApiRequestServiceServer).AddApiRequest(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: ApiRequestService_AddApiRequest_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(ApiRequestServiceServer).AddApiRequest(ctx, req.(*AddApiRequestRequest))
}
return interceptor(ctx, in, info, handler)
}
func _ApiRequestService_GetApiRequests_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(GetApiRequestsRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(ApiRequestServiceServer).GetApiRequests(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: ApiRequestService_GetApiRequests_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(ApiRequestServiceServer).GetApiRequests(ctx, req.(*GetApiRequestsRequest))
}
return interceptor(ctx, in, info, handler)
}
func _ApiRequestService_GetApiRequestByTransactionId_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(GetApiRequestByTransactionIdRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(ApiRequestServiceServer).GetApiRequestByTransactionId(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: ApiRequestService_GetApiRequestByTransactionId_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(ApiRequestServiceServer).GetApiRequestByTransactionId(ctx, req.(*GetApiRequestByTransactionIdRequest))
}
return interceptor(ctx, in, info, handler)
}
// ApiRequestService_ServiceDesc is the grpc.ServiceDesc for ApiRequestService service.
// It's only intended for direct use with grpc.RegisterService,
// and not to be introspected or modified (even as a copy)
var ApiRequestService_ServiceDesc = grpc.ServiceDesc{
ServiceName: "ApiRequestService",
HandlerType: (*ApiRequestServiceServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "AddApiRequest",
Handler: _ApiRequestService_AddApiRequest_Handler,
},
{
MethodName: "GetApiRequests",
Handler: _ApiRequestService_GetApiRequests_Handler,
},
{
MethodName: "GetApiRequestByTransactionId",
Handler: _ApiRequestService_GetApiRequestByTransactionId_Handler,
},
},
Streams: []grpc.StreamDesc{},
Metadata: "user.proto",
}

9
pkg/errs/api.go Normal file
View File

@ -0,0 +1,9 @@
package errs
// 常见错误
var (
ErrSystem = NewAppError(1001, "业务失败")
ErrParamDecryption = NewAppError(1002, "参数解密失败")
ErrParamValidation = NewAppError(1003, "校验参数不正确")
ErrDataSource = NewAppError(1004, "数据源异常")
)

20
pkg/errs/err.go Normal file
View File

@ -0,0 +1,20 @@
package errs
// 定义自定义错误类型
type AppError struct {
Code int // 错误码
Message string // 错误信息
}
// 实现 error 接口的 Error 方法
func (e *AppError) Error() string {
return e.Message
}
// 创建带有错误码和错误信息的 AppError
func NewAppError(code int, message string) *AppError {
return &AppError{
Code: code,
Message: message,
}
}

13
pkg/models/mqs.go Normal file
View File

@ -0,0 +1,13 @@
package models
import "time"
type ApiRequestMessage struct {
TransactionID string `json:"transactionID"`
UserId int64 `json:"userId"`
ProductCode string `json:"productCode"`
Status string `json:"status"` // 1. success 2. error
Charges bool `json:"charges"` // 是否扣费
Remark string `json:"remark"`
Timestamp time.Time `json:"timestamp"` // 添加时间戳
}

View File

@ -4,6 +4,7 @@ import (
"context"
"github.com/zeromicro/go-zero/rest/httpx"
"net/http"
"tianyuan-api/pkg/errs"
)
// 定义通用的响应结构
@ -13,41 +14,43 @@ type Response struct {
Message string `json:"message"`
}
type ResponseWithTransactionID struct {
Response
TransactionID string `json:"transaction_id"`
Code int `json:"code"`
Data interface{} `json:"data,omitempty"`
Message string `json:"message"`
TransactionID string `json:"transaction_id"`
}
// 发送响应(公用函数)
func sendResponse(ctx context.Context, w http.ResponseWriter, code int, message string, data interface{}) {
// 从上下文中获取 TransactionID
transactionID, _ := ctx.Value("transactionID").(string)
if transactionID != "" {
result := ResponseWithTransactionID{
Code: code,
Data: data,
Message: message,
TransactionID: transactionID,
}
httpx.OkJsonCtx(ctx, w, result)
} else {
result := Response{
Code: code,
Data: data,
Message: message,
}
httpx.OkJsonCtx(ctx, w, result)
}
}
// 响应成功
func Success(ctx context.Context, w http.ResponseWriter, data interface{}) {
// 从上下文中获取 TransactionID
transactionID := ctx.Value("TransactionID")
// 判断是否存在 TransactionID
if transactionID != nil {
result := ResponseWithTransactionID{
Response: Response{
Code: http.StatusOK,
Data: data,
Message: "success",
},
TransactionID: transactionID.(string), // 将 TransactionID 添加到响应
}
httpx.OkJsonCtx(ctx, w, result) // 返回带有 TransactionID 的响应
} else {
result := Response{
Code: http.StatusOK,
Data: data,
Message: "success",
}
httpx.OkJsonCtx(ctx, w, result) // 返回没有 TransactionID 的响应
}
ctx = context.WithValue(ctx, "status", "success")
sendResponse(ctx, w, 0, "success", data)
}
// 响应失败
func Fail(ctx context.Context, w http.ResponseWriter, err error) {
result := Response{
Code: -1,
Message: err.Error(),
}
httpx.OkJsonCtx(ctx, w, result)
func Fail(ctx context.Context, w http.ResponseWriter, err *errs.AppError) {
ctx = context.WithValue(ctx, "status", "failed")
ctx = context.WithValue(ctx, "remark", err.Message)
sendResponse(ctx, w, err.Code, err.Message, nil)
}

View File

@ -1,86 +0,0 @@
package schema
import (
"encoding/json"
"fmt"
"github.com/xeipuuv/gojsonschema"
"os"
"path/filepath"
"strings"
)
// ValidationResult 结构用于保存校验结果
type ValidationResult struct {
Valid bool
Data map[string]interface{}
Errors string
}
// 校验函数:接受 schema 文件路径和 JSON 数据
func ValidateJSONWithSchema(schemaFileName string, data []byte) (ValidationResult, error) {
// 获取项目根目录
rootPath, err := os.Getwd()
if err != nil {
return ValidationResult{}, fmt.Errorf("无法获取项目根目录: %v", err)
}
// 构建本地 Schema 文件路径
schemaPath := filepath.Join(rootPath, "internal", "schema", schemaFileName)
// 将文件路径转换为 file:// URI 格式
schemaURI := "file:///" + filepath.ToSlash(schemaPath)
// 读取 schema 文件,通过 URI 加载
schemaLoader := gojsonschema.NewReferenceLoader(schemaURI)
// 将传入的 []byte 数据转为 JSON Loader
jsonLoader := gojsonschema.NewBytesLoader(data)
// 执行校验
result, err := gojsonschema.Validate(schemaLoader, jsonLoader)
if err != nil {
return ValidationResult{}, fmt.Errorf("校验过程中出错: %v", err)
}
// 初始化返回结果
validationResult := ValidationResult{
Valid: result.Valid(),
Data: make(map[string]interface{}),
Errors: "",
}
// 如果校验失败,收集并自定义错误信息
if !result.Valid() {
errorMessages := collectErrors(result.Errors())
validationResult.Errors = formatErrors(errorMessages)
return validationResult, nil
}
// 校验成功,解析 JSON
if err := json.Unmarshal(data, &validationResult.Data); err != nil {
return validationResult, fmt.Errorf("JSON 解析出错: %v", err)
}
return validationResult, nil
}
// collectErrors 自定义处理错误信息
func collectErrors(errors []gojsonschema.ResultError) []string {
var errorMessages []string
for _, err := range errors {
// 从 Details() 中获取真正的字段名
details := err.Details()
fieldName, ok := details["property"].(string)
if !ok {
fieldName = err.Field() // 默认使用 err.Field(),如果 property 不存在
}
errorMessages = append(errorMessages, fmt.Sprintf("%s: %s", fieldName, err.Description()))
}
return errorMessages
}
// formatErrors 将错误列表格式化为美观的字符串
func formatErrors(errors []string) string {
return strings.Join(errors, ", ") // 用换行符连接每个错误信息
}