14 Commits

Author SHA1 Message Date
yuguojian
754a8910f2 写死请求地址,换接口,返回结果也会变,不用配置 2025-06-26 16:24:21 +08:00
吕翠丽
587fd91338 实名认证接口 2025-06-26 16:20:14 +08:00
吕翠丽
0c1bce5d8c 实名认证接口 2025-06-26 16:13:15 +08:00
yuguojian
0237a95a43 Merge remote-tracking branch 'origin/feature/sms_tool' 2025-06-26 11:12:29 +08:00
lzh
f1d35cabba sms_tool 2025-06-26 11:09:45 +08:00
zhongqiang
f7a1a30203 新增物流接口 2025-06-23 16:59:47 +08:00
zhongqiang
9eecda8c5f 新增物流接口 2025-06-23 16:00:54 +08:00
zhongqiang
12db943012 新增物流接口 2025-06-20 18:03:39 +08:00
yuguojian
3676395e9f Merge remote-tracking branch 'origin/feature/对接新供应链-唯品尚' 2025-06-20 15:53:30 +08:00
zhongqiang
2fd998b52d 对接新供应链-唯品尚 2025-06-20 15:51:48 +08:00
lzh
2e8f0cb3f2 sms_tool 2025-06-20 14:49:45 +08:00
yuguojian
992f39109d 优化ip缓存 2025-06-18 18:06:45 +08:00
yuguojian
db27554374 Merge branch 'feature/sms_tool' 2025-06-18 17:41:21 +08:00
lzh
e2d9685db3 sms_tool 2025-06-18 17:37:21 +08:00
11 changed files with 492 additions and 45 deletions

View File

@@ -0,0 +1,161 @@
package ali_cloud_tool
import (
"context"
"encoding/json"
"fmt"
"github.com/pkg/errors"
"io"
"log"
"net/http"
"net/url"
"time"
)
type AliCloudExpressClient struct {
AppCode string
Host string
cache ICacheAdapter
}
func NewAliCloudExpressClient(appCode string) *AliCloudExpressClient {
return &AliCloudExpressClient{
AppCode: appCode,
Host: "https://qryexpress.market.alicloudapi.com/lundear/expressTracking",
}
}
func (a *AliCloudExpressClient) GetLogisticsInfo(mobile, number string) (res *ExpressRes, err error) {
if mobile == "" || number == "" {
return nil, errors.New("请输入手机号和物流单号")
}
// 设置参数
params := url.Values{}
params.Add("mobile", mobile)
params.Add("number", number)
// 拼接URL
var fullURL string
fullURL, err = url.JoinPath(a.Host)
if err != nil {
return nil, errors.Wrapf(err, "查询物流信息失败, 拼接路径错误! 手机号:%s, 物流单号:%s", mobile, number)
}
// 拼接参数
fullURL = fmt.Sprintf("%s?%s", fullURL, params.Encode())
// 创建HTTP客户端
client := &http.Client{}
// 创建请求
var req *http.Request
req, err = http.NewRequest(http.MethodGet, fullURL, nil)
if err != nil {
return nil, errors.Wrapf(err, "查询物流信息失败, 创建请求失败! 手机号:%s, 物流单号:%s", mobile, number)
}
// 设置请求头
req.Header.Add("Authorization", "APPCODE "+a.AppCode)
// 发送请求
client.Timeout = 2 * time.Second
var resp *http.Response
resp, err = client.Do(req)
if err != nil {
return nil, errors.Wrapf(err, "查询物流信息失败, 发送请求失败! 手机号:%s, 物流单号:%s", mobile, number)
}
defer func(Body io.ReadCloser) {
err = Body.Close()
if err != nil {
log.Printf("查询物流信息失败, 关闭响应体失败! 手机号:%s, 物流单号:%s , %+v\n", mobile, number, err)
}
}(resp.Body)
// 读取响应体
var body []byte
body, err = io.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrapf(err, "查询物流信息失败, 读取响应体失败! 手机号:%s, 物流单号:%s", mobile, number)
}
//log.Printf("查询物流信息成功! %s\n", string(body))
// 解析JSON响应
var expressRes ExpressRes
err = json.Unmarshal(body, &expressRes)
if err != nil {
return nil, errors.Wrapf(err, "查询物流信息失败, 解析JSON响应失败, %s! 手机号:%s, 物流单号:%s", string(body), mobile, number)
}
if expressRes.Code != 0 {
return &expressRes, errors.Wrapf(err, "查询物流信息失败! expressRes:%+v 手机号:%s, 物流单号:%s", expressRes, mobile, number)
}
if expressRes.Data == nil {
return &expressRes, errors.Wrapf(err, "查询物流信息失败,没有查询到物流信息! expressRes:%+v 手机号:%s, 物流单号:%s", expressRes, mobile, number)
}
return &expressRes, nil
}
func (a *AliCloudExpressClient) Set(cache ICacheAdapter) {
a.cache = cache
}
func (a *AliCloudExpressClient) GetLogisticsInfoFormCache(ctx context.Context, mobile, prefix, number string, opt ...time.Duration) (res *ExpressRes, err error) {
if a.cache != nil {
res, err = a.cache.Get(ctx, a.numberKey(prefix, number))
if err != nil {
return nil, errors.Wrapf(err, "获取缓存失败, number:%s", number)
}
if res != nil {
return res, nil
}
}
res, err = a.GetLogisticsInfo(mobile, number)
if err != nil {
return nil, errors.Wrapf(err, "获取物流信息失败, number:%s", number)
}
var infoJson []byte
infoJson, err = json.Marshal(res)
if err != nil {
return nil, errors.Wrapf(err, "无法将物流信息转换为JSON, number:%s", number)
}
if len(opt) > 0 {
err = a.cache.Set(ctx, a.numberKey(prefix, number), string(infoJson), opt[0])
if err != nil {
return nil, errors.Wrapf(err, "缓存物流信息失败, number:%s", number)
}
}
return
}
func (a *AliCloudExpressClient) DeleteLogisticsInfoCache(ctx context.Context, prefix, number string) (err error) {
if a.cache == nil {
return errors.New("缓存不能为空")
}
err = a.cache.Del(ctx, a.numberKey(prefix, number))
return err
}
// ipKey 生成Redis key
func (a *AliCloudExpressClient) numberKey(prefix, number string) string {
return fmt.Sprintf("%s:number:%s", prefix, number)
}
type ExpressRes struct {
Code int `json:"code"`
Desc string `json:"desc"`
Data *Data `json:"data"`
}
type Data struct {
State int `json:"state" dc:"物流状态【1在途中2派件中3已签收4派送失败5揽收6退回7转单8疑难9退签10待清关11清关中12已清关13清关异常】"`
Name string `json:"name" dc:"物流名"`
Com string `json:"com"`
Number string `json:"number" dc:"单号"`
Logo string `json:"logo" dc:"图标地址"`
List []*List `json:"list"`
}
type List struct {
Time string `json:"time"`
Status string `json:"status"`
}

View File

@@ -0,0 +1,130 @@
package ali_cloud_tool
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/redis/go-redis/v9"
"log"
"testing"
"time"
)
func TestAliCloudExpressClient_GetLogisticsInfo(t *testing.T) {
type fields struct {
AppCode string
Host string
}
type args struct {
mobile string
number string
}
tests := []struct {
name string
fields fields
args args
wantRes *ExpressRes
wantErr bool
}{
{
name: "test1",
fields: fields{
AppCode: "",
Host: "",
},
args: args{
mobile: "",
number: "",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := &AliCloudExpressClient{
AppCode: tt.fields.AppCode,
Host: tt.fields.Host,
}
gotRes, err := a.GetLogisticsInfo(tt.args.mobile, tt.args.number)
log.Println(gotRes, err)
})
}
}
func TestAliCloudExpressClient_GetLogisticsInfoFormCache(t *testing.T) {
rdb := redis.NewClient(&redis.Options{
Addr: "",
Password: "",
DB: 1,
})
// 创建缓存实例
cache := NewRedisCache(rdb)
a := &AliCloudExpressClient{
AppCode: "",
Host: "",
cache: cache,
}
gotRes, err := a.GetLogisticsInfoFormCache(context.Background(), "", "", "", time.Minute)
log.Println(gotRes, err)
}
func TestAliCloudExpressClient_DeleteLogisticsInfoCache(t *testing.T) {
rdb := redis.NewClient(&redis.Options{
Addr: "",
Password: "",
DB: 1,
})
// 创建缓存实例
cache := NewRedisCache(rdb)
a := &AliCloudExpressClient{
AppCode: "",
Host: "",
cache: cache,
}
err := a.DeleteLogisticsInfoCache(context.Background(), "", "")
log.Println(err)
}
type RedisCache struct {
client *redis.Client
}
func (r *RedisCache) Del(ctx context.Context, number string) error {
return r.client.Del(ctx, number).Err()
}
func NewRedisCache(client *redis.Client) *RedisCache {
return &RedisCache{client: client}
}
func (r *RedisCache) Set(ctx context.Context, number string, res string, ttl time.Duration) error {
if number == "" {
return errors.New("number不能为空")
}
return r.client.Set(ctx, number, res, ttl).Err()
}
// Get 从Redis获取IP信息
func (r *RedisCache) Get(ctx context.Context, number string) (*ExpressRes, error) {
if number == "" {
return nil, errors.New("number不能为空")
}
data, err := r.client.Get(ctx, number).Bytes()
if err != nil {
if errors.Is(err, redis.Nil) {
return nil, nil // 键不存在返回nil而不是错误
}
return nil, fmt.Errorf("无法获取物流信息: %w", err)
}
var info ExpressRes
if err = json.Unmarshal(data, &info); err != nil {
return nil, fmt.Errorf("无法解析物流信息: %w", err)
}
return &info, nil
}

View File

@@ -0,0 +1,116 @@
package ali_cloud_tool
import (
"context"
"encoding/json"
"fmt"
"github.com/pkg/errors"
"io"
"log"
"net/http"
"net/url"
"strings"
"time"
)
type AliCloudRealNameClient struct {
AppCode string
Host string
}
func NewAliCloudRealNameClient(appCode string) *AliCloudRealNameClient {
return &AliCloudRealNameClient{
AppCode: appCode,
Host: "https://zidv2.market.alicloudapi.com/idcheck/Post",
}
}
// 调用实名认证API
func (s *AliCloudRealNameClient) CallRealNameAuthAPI(ctx context.Context, name, idCard string) (*RealNameAuthData, error) {
// 验证输入
if strings.TrimSpace(name) == "" || strings.TrimSpace(idCard) == "" {
return nil, errors.New("姓名和身份证号不能为空")
}
// 设置参数
params := url.Values{}
params.Add("realName", name)
params.Add("cardNo", idCard)
// 构建完整URL
fullURL, err := url.JoinPath(s.Host)
if err != nil {
return nil, fmt.Errorf("构建实名认证URL失败: %w, 姓名:%s, 身份证号:%s", err, name, idCard)
}
// 创建HTTP客户端
client := &http.Client{
Timeout: 2 * time.Second,
}
// 创建POST请求
req, err := http.NewRequestWithContext(ctx, http.MethodPost, fullURL, strings.NewReader(params.Encode()))
if err != nil {
return nil, fmt.Errorf("创建实名认证请求失败: %w, 姓名:%s, 身份证号:%s", err, name, idCard)
}
// 设置请求头
req.Header.Set("Authorization", "APPCODE "+s.AppCode)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
// 发送请求
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("发送实名认证请求失败: %w, 姓名:%s, 身份证号:%s", err, name, idCard)
}
defer func() {
if closeErr := resp.Body.Close(); closeErr != nil {
log.Printf("关闭实名认证响应体失败: %v, 姓名:%s, 身份证号:%s", closeErr, name, idCard)
}
}()
// 读取响应体
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("读取实名认证响应失败: %w, 姓名:%s, 身份证号:%s", err, name, idCard)
}
// 解析JSON响应
var result RealNameAuthData
if err = json.Unmarshal(body, &result); err != nil {
return nil, fmt.Errorf("解析实名认证响应失败: %w, 响应内容:%s, 姓名:%s, 身份证号:%s", err, string(body), name, idCard)
}
// 检查API返回的错误码
if result.ErrorCode != 0 {
return &result, fmt.Errorf("实名认证接口返回错误, 错误码:%d, 原因:%s, 姓名:%s, 身份证号:%s",
result.ErrorCode, result.Reason, name, idCard)
}
// 检查认证结果
if !result.Result.Isok {
return &result, fmt.Errorf("实名认证不通过, 原因:%s, 姓名:%s, 身份证号:%s",
result.Reason, name, idCard)
}
return &result, nil
}
type RealNameAuthData struct {
ErrorCode int `json:"error_code"`
Reason string `json:"reason"`
Result struct {
Realname string `json:"realname"`
Idcard string `json:"idcard"`
Isok bool `json:"isok"`
IdCardInfor struct {
Province string `json:"province"`
City string `json:"city"`
District string `json:"district"`
Area string `json:"area"`
Sex string `json:"sex"`
Birthday string `json:"birthday"`
} `json:"IdCardInfor"`
} `json:"result"`
Sn string `json:"sn"`
}

View File

@@ -0,0 +1,20 @@
package ali_cloud_tool
import (
"context"
"fmt"
"testing"
)
func TestAliCloudExpressClient_CallRealNameAuthAPI(t *testing.T) {
s := &AliCloudRealNameClient{
AppCode: "",
Host: "",
}
got, err := s.CallRealNameAuthAPI(context.Background(), "", "")
if err != nil {
t.Errorf("CallRealNameAuthAPI() error = %v", err)
return
}
fmt.Println("=====", got)
}

View File

@@ -0,0 +1,12 @@
package ali_cloud_tool
import (
"context"
"time"
)
type ICacheAdapter interface {
Set(ctx context.Context, key string, res string, ttl time.Duration) error
Get(ctx context.Context, key string) (*ExpressRes, error)
Del(ctx context.Context, key string) error
}

View File

@@ -19,11 +19,10 @@ type HuaChenIpClient struct {
cache ICacheAdapter
}
func NewHuaChenIpClient(appCode string, cache ICacheAdapter) *HuaChenIpClient {
func NewHuaChenIpClient(appCode string) *HuaChenIpClient {
return &HuaChenIpClient{
AppCode: appCode,
Host: "https://c2ba.api.huachen.cn",
cache: cache,
}
}
@@ -90,13 +89,19 @@ func (h *HuaChenIpClient) GetIpInfo(ip string) (res *ApiResult, err error) {
return &apiResult, nil
}
func (h *HuaChenIpClient) GetIpInfoFormCache(ctx context.Context, ip string, ttl time.Duration) (res *ApiResult, err error) {
getCache, err := h.cache.Get(ctx, h.ipKey(ip))
func (h *HuaChenIpClient) Set(cache ICacheAdapter) {
h.cache = cache
}
func (h *HuaChenIpClient) GetIpInfoFormCache(ctx context.Context, ip string, opt ...time.Duration) (res *ApiResult, err error) {
if h.cache != nil {
res, err = h.cache.Get(ctx, h.ipKey(ip))
if err != nil {
return nil, errors.Wrapf(err, "获取缓存失败ip:%s", ip)
}
if getCache != nil {
return getCache, nil
if res != nil {
return res, nil
}
}
res, err = h.GetIpInfo(ip)
if err != nil {
@@ -109,10 +114,12 @@ func (h *HuaChenIpClient) GetIpInfoFormCache(ctx context.Context, ip string, ttl
return nil, errors.Wrapf(err, "无法将IP信息转换为JSONip:%s", ip)
}
err = h.cache.Set(ctx, h.ipKey(ip), string(infoJson), ttl)
if len(opt) == 0 {
err = h.cache.Set(ctx, h.ipKey(ip), string(infoJson), opt[0])
if err != nil {
return nil, errors.Wrapf(err, "缓存ip:%s信息失败", ip)
}
}
return
}

View File

@@ -9,4 +9,5 @@ type ICacheAdapter interface {
Get(ctx context.Context, key string) (value interface{}, err error)
Set(ctx context.Context, key string, value interface{}, expire time.Duration) (err error)
Del(ctx context.Context, key string) (err error)
SetNX(ctx context.Context, key string, value interface{}, expire time.Duration) (ok bool, err error)
}

View File

@@ -8,7 +8,6 @@ import (
dysmsapi20170525 "github.com/alibabacloud-go/dysmsapi-20170525/v5/client"
util "github.com/alibabacloud-go/tea-utils/v2/service"
"github.com/alibabacloud-go/tea/tea"
"github.com/redis/go-redis/v9"
"time"
)
@@ -71,7 +70,19 @@ func (c *SmsClient) GetCode(ctx context.Context, key string) (code string, err e
if err != nil {
return "", err
}
if value == nil {
return "", errors.New("验证码不存在,请重新发送")
}
switch value.(type) {
case string:
return value.(string), nil
case float64:
return fmt.Sprintf("%d", int(value.(float64))), nil
case int64:
return fmt.Sprintf("%d", int(value.(int64))), nil
default:
return "", errors.New("验证码类型错误,请联系管理员")
}
}
// SaveCode 保存验证码
@@ -81,10 +92,7 @@ func (c *SmsClient) SaveCode(ctx context.Context, key string, code string, expir
}
//保存code
err = c.Cache.Set(ctx, key, code, expire)
if err != nil {
return err
}
return nil
}
// DeleteCode 删除验证码
@@ -102,34 +110,18 @@ func (c *SmsClient) SaveCodeWithFrequency(ctx context.Context, key string, code
return errors.New("缓存不能为空")
}
frequencyKey := fmt.Sprintf(CodeFrequencyKey, key)
if frequency != 0 {
// 判断验证码是否频繁
frequencyValue, err := c.Cache.Get(ctx, frequencyKey)
if frequency <= 0 {
return errors.New("频率不能小于0")
}
ok, err := c.Cache.SetNX(ctx, frequencyKey, CodeFrequencyValue, frequency)
if err != nil {
if errors.Is(err, redis.Nil) {
// 缓存中没有数据
fmt.Printf("缓存中没有数据")
} else {
return err
}
}
if frequencyValue != nil && frequencyValue.(string) == CodeFrequencyValue {
if !ok {
return errors.New("code发送频繁请稍后再试")
}
}
//保存code
err = c.SaveCode(ctx, key, code, expire)
if err != nil {
return err
}
if frequency != 0 {
// 保存验证码发送频率
err = c.Cache.Set(ctx, frequencyKey, CodeFrequencyValue, frequency)
if err != nil {
return err
}
}
return nil
}
// VerifyCode 校验验证码

View File

@@ -51,6 +51,14 @@ func (r *RedisCacheAdapter) Del(ctx context.Context, key string) error {
return r.client.Del(ctx, key).Err()
}
func (r *RedisCacheAdapter) SetNX(ctx context.Context, key string, value interface{}, expire time.Duration) (ok bool, err error) {
data, err := json.Marshal(value)
if err != nil {
return false, err
}
return r.client.SetNX(ctx, key, data, expire).Result()
}
var (
//SMS
accessKeyId = os.Getenv("SMS_ALIBABA_CLOUD_ACCESS_KEY_ID")

View File

@@ -33,7 +33,7 @@ func (w *WeiPinShangClient) GetManyPostage(getManyPostageReq *GetManyPostageReq)
fmt.Println("getManyPostageReq", getManyPostageReq)
paramMap := make(map[string]any)
paramMap["goodInfo"] = getManyPostageReq.GoodsInfo
paramMap["goodsInfo"] = getManyPostageReq.GoodsInfo
paramMap["address"] = getManyPostageReq.Address
paramMap["province"] = getManyPostageReq.Province
paramMap["county"] = getManyPostageReq.County

View File

@@ -29,7 +29,7 @@ func TestWeiPinShangClient_GetManyPostage(t *testing.T) {
Key: "f654ea5bde7635c3f46191191e5c4c8e",
},
args: GetManyPostageReq{
GoodsInfo: "[{\"goodsId\":\"WPS592_00019\",\"goodSpecId\":\"WPS592_1105165115160944\",\"num\":1},{\"goodsId\":\"WPS505_00007\",\"goodSpecId\":\"WPS505_1007111249857536\",\"num\":1}]",
GoodsInfo: "[{\"goodsId\":\"WPS427_adf0008\",\"goodSpecId\":\"WPS427_0715110641454716\",\"num\":1}]",
Province: "广东省",
Address: "奥园",
City: "广州市",
@@ -80,14 +80,14 @@ func TestWeiPinShangClient_PreOrder(t *testing.T) {
Key: "f654ea5bde7635c3f46191191e5c4c8e",
},
args: PreOrderReq{
GoodsInfo: "[{\"goodsId\":\"WPS2_1231155626421463\",\"goodSpecId\":\"WPS2_12311556265677476\",\"num\":1}]",
GoodsInfo: "[{\"goodsId\":\"WPS427_adf0008\",\"goodSpecId\":\"WPS427_0715110641454716\",\"num\":1}]",
Province: "广东省",
Address: "奥园",
City: "广州市",
Area: "番禺区",
ConsigneePhone: "15375390426",
ConsigneeContacts: "张三",
LockCode: "L100000004",
LockCode: "L100000005",
Source: "AILEHUI",
},
},