重构远程接口,替换为Baiyin SDK & 添加实名认证处理逻辑

This commit is contained in:
2025-04-16 10:41:04 +08:00
parent 0cf9b98059
commit f1456d01ea
11 changed files with 319 additions and 88 deletions

42
pkg/env/env.go vendored
View File

@@ -139,21 +139,45 @@ func loadLog() {
// region remote
var (
RemoteAddr = "http://103.139.212.110:9989"
RemoteToken string
BaiyinAddr = "http://103.139.212.110:9989"
BaiyinTokenUrl string
)
var (
IdenCallbackUrl string
IdenAccessKey string
IdenSecretKey string
)
func loadRemote() {
_RemoteAddr := os.Getenv("REMOTE_ADDR")
if _RemoteAddr != "" {
RemoteAddr = _RemoteAddr
_BaiyinAddr := os.Getenv("BAIYIN_ADDR")
if _BaiyinAddr != "" {
BaiyinAddr = _BaiyinAddr
}
_RemoteToken := os.Getenv("REMOTE_TOKEN")
if _RemoteToken == "" {
panic("环境变量 REMOTE_TOKEN 的值不能为空")
_BaiyinTokenUrl := os.Getenv("BAIYIN_TOKEN_URL")
if _BaiyinTokenUrl == "" {
panic("环境变量 BAIYIN_TOKEN_URL 的值不能为空")
}
RemoteToken = _RemoteToken
BaiyinTokenUrl = _BaiyinTokenUrl
_IdenCallbackUrl := os.Getenv("IDEN_CALLBACK_URL")
if _IdenCallbackUrl == "" {
panic("环境变量 IDEN_CALLBACK_URL 的值不能为空")
}
IdenCallbackUrl = _IdenCallbackUrl
_IdenAccessKey := os.Getenv("IDEN_ACCESS_KEY")
if _IdenAccessKey == "" {
panic("环境变量 IDEN_ACCESS_KEY 的值不能为空")
}
IdenAccessKey = _IdenAccessKey
_IdenSecretKey := os.Getenv("IDEN_SECRET_KEY")
if _IdenSecretKey == "" {
panic("环境变量 IDEN_SECRET_KEY 的值不能为空")
}
IdenSecretKey = _IdenSecretKey
}
// endregion

View File

@@ -1,4 +1,4 @@
package remote
package baiyin
import (
"context"
@@ -38,7 +38,7 @@ var Cloud CloudClient
func Init() {
Cloud = &cloud{
url: env.RemoteAddr,
url: env.BaiyinAddr,
}
}
@@ -311,7 +311,7 @@ func (c *cloud) token(refresh bool) (string, error) {
}
// redis 获取失败,重新获取
resp, err := http.Get(env.RemoteToken)
resp, err := http.Get(env.BaiyinTokenUrl)
if err != nil {
return "", err
}

View File

@@ -1,7 +1,7 @@
package testutil
import (
"platform/pkg/remote"
"platform/pkg/sdks/baiyin"
"sync"
"testing"
)
@@ -9,15 +9,15 @@ import (
// MockCloudClient 是CloudClient接口的测试实现
type MockCloudClient struct {
// 存储预期结果的字段
EdgesMock func(param remote.CloudEdgesReq) (*remote.CloudEdgesResp, error)
ConnectMock func(param remote.CloudConnectReq) error
DisconnectMock func(param remote.CloudDisconnectReq) (int, error)
AutoQueryMock func() (remote.CloudConnectResp, error)
EdgesMock func(param baiyin.CloudEdgesReq) (*baiyin.CloudEdgesResp, error)
ConnectMock func(param baiyin.CloudConnectReq) error
DisconnectMock func(param baiyin.CloudDisconnectReq) (int, error)
AutoQueryMock func() (baiyin.CloudConnectResp, error)
// 记录调用历史
EdgesCalls []remote.CloudEdgesReq
ConnectCalls []remote.CloudConnectReq
DisconnectCalls []remote.CloudDisconnectReq
EdgesCalls []baiyin.CloudEdgesReq
ConnectCalls []baiyin.CloudConnectReq
DisconnectCalls []baiyin.CloudDisconnectReq
AutoQueryCalls int
// 用于并发安全
@@ -25,19 +25,19 @@ type MockCloudClient struct {
}
// 确保MockCloudClient实现了CloudClient接口
var _ remote.CloudClient = (*MockCloudClient)(nil)
var _ baiyin.CloudClient = (*MockCloudClient)(nil)
func (m *MockCloudClient) CloudEdges(param remote.CloudEdgesReq) (*remote.CloudEdgesResp, error) {
func (m *MockCloudClient) CloudEdges(param baiyin.CloudEdgesReq) (*baiyin.CloudEdgesResp, error) {
m.mu.Lock()
defer m.mu.Unlock()
m.EdgesCalls = append(m.EdgesCalls, param)
if m.EdgesMock != nil {
return m.EdgesMock(param)
}
return &remote.CloudEdgesResp{}, nil
return &baiyin.CloudEdgesResp{}, nil
}
func (m *MockCloudClient) CloudConnect(param remote.CloudConnectReq) error {
func (m *MockCloudClient) CloudConnect(param baiyin.CloudConnectReq) error {
m.mu.Lock()
defer m.mu.Unlock()
m.ConnectCalls = append(m.ConnectCalls, param)
@@ -47,7 +47,7 @@ func (m *MockCloudClient) CloudConnect(param remote.CloudConnectReq) error {
return nil
}
func (m *MockCloudClient) CloudDisconnect(param remote.CloudDisconnectReq) (int, error) {
func (m *MockCloudClient) CloudDisconnect(param baiyin.CloudDisconnectReq) (int, error) {
m.mu.Lock()
defer m.mu.Unlock()
m.DisconnectCalls = append(m.DisconnectCalls, param)
@@ -57,33 +57,33 @@ func (m *MockCloudClient) CloudDisconnect(param remote.CloudDisconnectReq) (int,
return 0, nil
}
func (m *MockCloudClient) CloudAutoQuery() (remote.CloudConnectResp, error) {
func (m *MockCloudClient) CloudAutoQuery() (baiyin.CloudConnectResp, error) {
m.mu.Lock()
defer m.mu.Unlock()
m.AutoQueryCalls++
if m.AutoQueryMock != nil {
return m.AutoQueryMock()
}
return remote.CloudConnectResp{}, nil
return baiyin.CloudConnectResp{}, nil
}
// SetupCloudClientMock 替换全局CloudClient为测试实现并在测试完成后恢复
func SetupCloudClientMock(t *testing.T) *MockCloudClient {
mock := &MockCloudClient{
EdgesMock: func(param remote.CloudEdgesReq) (*remote.CloudEdgesResp, error) {
EdgesMock: func(param baiyin.CloudEdgesReq) (*baiyin.CloudEdgesResp, error) {
panic("not implemented")
},
ConnectMock: func(param remote.CloudConnectReq) error {
ConnectMock: func(param baiyin.CloudConnectReq) error {
panic("not implemented")
},
DisconnectMock: func(param remote.CloudDisconnectReq) (int, error) {
DisconnectMock: func(param baiyin.CloudDisconnectReq) (int, error) {
panic("not implemented")
},
AutoQueryMock: func() (remote.CloudConnectResp, error) {
AutoQueryMock: func() (baiyin.CloudConnectResp, error) {
panic("not implemented")
},
}
remote.Cloud = mock
baiyin.Cloud = mock
return mock
}
@@ -94,9 +94,9 @@ type MockGatewayClient struct {
}
// 确保MockGatewayClient实现了GatewayClient接口
var _ remote.GatewayClient = (*MockGatewayClient)(nil)
var _ baiyin.GatewayClient = (*MockGatewayClient)(nil)
func (m *MockGatewayClient) GatewayPortConfigs(params []remote.PortConfigsReq) error {
func (m *MockGatewayClient) GatewayPortConfigs(params []baiyin.PortConfigsReq) error {
testGatewayBase.mu.Lock()
defer testGatewayBase.mu.Unlock()
testGatewayBase.PortConfigsCalls = append(testGatewayBase.PortConfigsCalls, params)
@@ -106,42 +106,42 @@ func (m *MockGatewayClient) GatewayPortConfigs(params []remote.PortConfigsReq) e
return nil
}
func (m *MockGatewayClient) GatewayPortActive(param ...remote.PortActiveReq) (map[string]remote.PortData, error) {
func (m *MockGatewayClient) GatewayPortActive(param ...baiyin.PortActiveReq) (map[string]baiyin.PortData, error) {
testGatewayBase.mu.Lock()
defer testGatewayBase.mu.Unlock()
testGatewayBase.PortActiveCalls = append(testGatewayBase.PortActiveCalls, param)
if testGatewayBase.PortActiveMock != nil {
return testGatewayBase.PortActiveMock(m, param...)
}
return map[string]remote.PortData{}, nil
return map[string]baiyin.PortData{}, nil
}
type GatewayClientIns struct {
// 存储预期结果的字段
PortConfigsMock func(c *MockGatewayClient, params []remote.PortConfigsReq) error
PortActiveMock func(c *MockGatewayClient, param ...remote.PortActiveReq) (map[string]remote.PortData, error)
PortConfigsMock func(c *MockGatewayClient, params []baiyin.PortConfigsReq) error
PortActiveMock func(c *MockGatewayClient, param ...baiyin.PortActiveReq) (map[string]baiyin.PortData, error)
// 记录调用历史
PortConfigsCalls [][]remote.PortConfigsReq
PortActiveCalls [][]remote.PortActiveReq
PortConfigsCalls [][]baiyin.PortConfigsReq
PortActiveCalls [][]baiyin.PortActiveReq
// 用于并发安全
mu sync.Mutex
}
var testGatewayBase = &GatewayClientIns{
PortConfigsMock: func(c *MockGatewayClient, params []remote.PortConfigsReq) error {
PortConfigsMock: func(c *MockGatewayClient, params []baiyin.PortConfigsReq) error {
panic("not implemented")
},
PortActiveMock: func(c *MockGatewayClient, param ...remote.PortActiveReq) (map[string]remote.PortData, error) {
PortActiveMock: func(c *MockGatewayClient, param ...baiyin.PortActiveReq) (map[string]baiyin.PortData, error) {
panic("not implemented")
},
}
// SetupGatewayClientMock 创建一个MockGatewayClient并提供替换函数
func SetupGatewayClientMock(t *testing.T) *GatewayClientIns {
remote.GatewayInitializer = func(url, username, password string) remote.GatewayClient {
baiyin.GatewayInitializer = func(url, username, password string) baiyin.GatewayClient {
return &MockGatewayClient{
Host: url,
}