chore: 初始开源发布
iotlcm-app:小程序端 + 用户端服务。 - app/:uni-app 小程序前端(基于 unibest,MIT) - 微信 AppID → YOUR_WX_APPID,uni-app AppID → __UNI__YOURAPPID - 内网后端地址 → admin.iotlcm.example.com - 应用标题与 unibest 模板 README 重写(保留上游致谢与 MIT 声明) - server/:GoFrame 用户端服务(基于 XiuAdmin,Apache-2.0) - module xiuadmin → git.5iot.com/iotlcm-public/iotlcm-app(360 文件) - 支付回调地址 → app.iotlcm.example.com,商户名与应用名泛化 - 真实手机号 → 合法 11 位编造号(保留号段前 3 位 + 后 4 位) - 依赖号码尾部的断言同步更新 - 补 server/LICENSE(Apache-2.0) - 移除内部资产:CI 触发器 - 补根 README,LF 行尾 + .gitattributes 已知:TestSysDept_RefreshDeptAncestors 在源仓同一提交上同样失败,属既有缺陷。
This commit is contained in:
@@ -0,0 +1,225 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/gclient"
|
||||
"github.com/gogf/gf/v2/net/gsvc"
|
||||
)
|
||||
|
||||
type appUpstreamDefaultConfig struct {
|
||||
ClientName string `json:"clientName"`
|
||||
Timeout string `json:"timeout"`
|
||||
}
|
||||
|
||||
type appUpstreamSimKey struct {
|
||||
ChanKey string `json:"chanKey,omitempty"`
|
||||
ICCID string `json:"iccid,omitempty"`
|
||||
IMSI string `json:"imsi,omitempty"`
|
||||
MSISDN string `json:"msisdn,omitempty"`
|
||||
}
|
||||
|
||||
type appUpstreamSimQueryInput struct {
|
||||
SimKeys []*appUpstreamSimKey `json:"simKeys"`
|
||||
ReqData any `json:"reqData"`
|
||||
}
|
||||
|
||||
type appUpstreamSimQueryItem struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
SimKey *appUpstreamSimKey `json:"simKey"`
|
||||
ResData json.RawMessage `json:"resData"`
|
||||
}
|
||||
|
||||
type appUpstreamSimQueryResponse struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data []appUpstreamSimQueryItem `json:"data"`
|
||||
}
|
||||
|
||||
func queryAppCardRealtimeSession(ctx context.Context, row *appCardQueryRow) (*appOneLinkMockSimSessionItem, string, error) {
|
||||
simKey, err := appUpstreamSimKeyFromRow(row)
|
||||
if err != nil {
|
||||
return nil, "upstream", err
|
||||
}
|
||||
return queryAppCardRealtimeSessionWithKey(ctx, row, simKey)
|
||||
}
|
||||
|
||||
func queryAppCardRealtimeSessionWithKey(ctx context.Context, row *appCardQueryRow, simKey *appUpstreamSimKey) (*appOneLinkMockSimSessionItem, string, error) {
|
||||
session, err := queryAppCardRealtimeSessionByUpstreamWithKey(ctx, simKey)
|
||||
if err == nil {
|
||||
return session, "upstream", nil
|
||||
}
|
||||
if !appUpstreamMockFallbackEnabled(ctx) {
|
||||
return nil, "upstream", err
|
||||
}
|
||||
g.Log().Debugf(ctx, "app card session upstream query failed, fallback to onelink mock, iccid:%s, err:%+v", appCardQueryLogIccid(row), err)
|
||||
|
||||
session, mockErr := queryAppCardRealtimeSessionByMock(ctx, row)
|
||||
if mockErr != nil {
|
||||
return nil, "onelink_mock", gerror.Wrapf(mockErr, "查询OneLink挡板会话失败,上游网关错误:%v", err)
|
||||
}
|
||||
return session, "onelink_mock", nil
|
||||
}
|
||||
|
||||
func queryAppCardRealtimeSessionByUpstream(ctx context.Context, row *appCardQueryRow) (*appOneLinkMockSimSessionItem, error) {
|
||||
simKey, err := appUpstreamSimKeyFromRow(row)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return queryAppCardRealtimeSessionByUpstreamWithKey(ctx, simKey)
|
||||
}
|
||||
|
||||
func queryAppCardRealtimeSessionByUpstreamWithKey(ctx context.Context, simKey *appUpstreamSimKey) (*appOneLinkMockSimSessionItem, error) {
|
||||
items, err := queryAppUpstreamSimWithKey(ctx, simKey, "/sim/query/sim-session", map[string]any{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, item := range items {
|
||||
if item.Code != 0 || len(item.ResData) == 0 {
|
||||
continue
|
||||
}
|
||||
session := appOneLinkMockSimSessionItem{}
|
||||
if err = json.Unmarshal(item.ResData, &session); err != nil {
|
||||
return nil, gerror.Wrap(err, "解析上游会话响应失败")
|
||||
}
|
||||
if !session.isZero() {
|
||||
return &session, nil
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func queryAppCardRealtimeSessionByMock(ctx context.Context, row *appCardQueryRow) (*appOneLinkMockSimSessionItem, error) {
|
||||
client, err := newAppOneLinkMockClient(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return client.QuerySimSession(ctx, row)
|
||||
}
|
||||
|
||||
func appUpstreamMockFallbackEnabled(ctx context.Context) bool {
|
||||
return g.Cfg().MustGet(ctx, "upstream.onelinkMock.enabled", false).Bool()
|
||||
}
|
||||
|
||||
func appCardQueryLogIccid(row *appCardQueryRow) string {
|
||||
if row == nil {
|
||||
return ""
|
||||
}
|
||||
return row.Iccid
|
||||
}
|
||||
|
||||
func queryAppUpstreamSim(ctx context.Context, row *appCardQueryRow, path string, reqData any) ([]appUpstreamSimQueryItem, error) {
|
||||
simKey, err := appUpstreamSimKeyFromRow(row)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return queryAppUpstreamSimWithKey(ctx, simKey, path, reqData)
|
||||
}
|
||||
|
||||
func queryAppUpstreamSimWithKey(ctx context.Context, simKey *appUpstreamSimKey, path string, reqData any) ([]appUpstreamSimQueryItem, error) {
|
||||
if simKey == nil {
|
||||
return nil, gerror.New("卡片信息为空,不能查询上游")
|
||||
}
|
||||
cleanKey := &appUpstreamSimKey{
|
||||
ChanKey: strings.TrimSpace(simKey.ChanKey),
|
||||
ICCID: strings.TrimSpace(simKey.ICCID),
|
||||
IMSI: strings.TrimSpace(simKey.IMSI),
|
||||
MSISDN: strings.TrimSpace(simKey.MSISDN),
|
||||
}
|
||||
if cleanKey.ICCID == "" && cleanKey.IMSI == "" && cleanKey.MSISDN == "" {
|
||||
return nil, gerror.New("卡片缺少上游查询标识")
|
||||
}
|
||||
cfg := appUpstreamDefaultConfig{}
|
||||
if err := g.Cfg().MustGet(ctx, "upstream.default").Scan(&cfg); err != nil {
|
||||
return nil, gerror.Wrap(err, "读取上游代理配置失败")
|
||||
}
|
||||
cfg.ClientName = strings.TrimSpace(cfg.ClientName)
|
||||
cfg.Timeout = strings.TrimSpace(cfg.Timeout)
|
||||
if cfg.ClientName == "" {
|
||||
return nil, gerror.New("上游代理地址未配置")
|
||||
}
|
||||
|
||||
timeout := 10 * time.Second
|
||||
if cfg.Timeout != "" {
|
||||
if parsed, err := time.ParseDuration(cfg.Timeout); err == nil && parsed > 0 {
|
||||
timeout = parsed
|
||||
}
|
||||
}
|
||||
|
||||
if reqData == nil {
|
||||
reqData = map[string]any{}
|
||||
}
|
||||
input := appUpstreamSimQueryInput{
|
||||
SimKeys: []*appUpstreamSimKey{cleanKey},
|
||||
ReqData: reqData,
|
||||
}
|
||||
body, err := json.Marshal(input)
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "组装上游查询参数失败")
|
||||
}
|
||||
|
||||
requestURL := appUpstreamURL(cfg.ClientName, path)
|
||||
client := g.Client()
|
||||
client.SetDiscovery(gsvc.GetRegistry())
|
||||
appSetUpstreamClientTimeout(client, timeout)
|
||||
client.SetHeader("Content-Type", "application/json")
|
||||
resp, err := client.Post(ctx, requestURL, string(body))
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "请求上游信息失败")
|
||||
}
|
||||
defer resp.Close()
|
||||
respBody := resp.ReadAll()
|
||||
|
||||
output := appUpstreamSimQueryResponse{}
|
||||
if err = json.Unmarshal(respBody, &output); err != nil {
|
||||
return nil, gerror.Wrap(err, "解析上游响应失败")
|
||||
}
|
||||
if output.Code != 0 {
|
||||
message := strings.TrimSpace(output.Message)
|
||||
if message == "" {
|
||||
message = "上游查询失败"
|
||||
}
|
||||
return nil, gerror.New(message)
|
||||
}
|
||||
return output.Data, nil
|
||||
}
|
||||
|
||||
func appSetUpstreamClientTimeout(client *gclient.Client, timeout time.Duration) {
|
||||
client.SetTimeout(timeout)
|
||||
if transport, ok := client.Transport.(*http.Transport); ok {
|
||||
transport.ResponseHeaderTimeout = timeout
|
||||
}
|
||||
}
|
||||
|
||||
func appUpstreamSimKeyFromRow(row *appCardQueryRow) (*appUpstreamSimKey, error) {
|
||||
if row == nil {
|
||||
return nil, gerror.New("卡片信息为空,不能查询上游")
|
||||
}
|
||||
simKey := &appUpstreamSimKey{
|
||||
ChanKey: strings.TrimSpace(row.ChanKey),
|
||||
ICCID: strings.TrimSpace(row.Iccid),
|
||||
IMSI: strings.TrimSpace(row.Imsi),
|
||||
MSISDN: strings.TrimSpace(row.Msisdn),
|
||||
}
|
||||
if simKey.ICCID == "" && simKey.IMSI == "" && simKey.MSISDN == "" {
|
||||
return nil, gerror.New("卡片缺少上游查询标识")
|
||||
}
|
||||
return simKey, nil
|
||||
}
|
||||
|
||||
func appUpstreamURL(clientName string, path string) string {
|
||||
clientName = strings.TrimRight(strings.TrimSpace(clientName), "/")
|
||||
path = "/" + strings.TrimLeft(path, "/")
|
||||
if strings.HasPrefix(clientName, "http://") || strings.HasPrefix(clientName, "https://") {
|
||||
return clientName + path
|
||||
}
|
||||
return fmt.Sprintf("http://%s%s", clientName, path)
|
||||
}
|
||||
Reference in New Issue
Block a user