Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
3676395e9f | ||
![]() |
2fd998b52d | ||
![]() |
2e8f0cb3f2 | ||
![]() |
992f39109d | ||
![]() |
db27554374 | ||
![]() |
e2d9685db3 |
@@ -19,11 +19,10 @@ type HuaChenIpClient struct {
|
|||||||
cache ICacheAdapter
|
cache ICacheAdapter
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHuaChenIpClient(appCode string, cache ICacheAdapter) *HuaChenIpClient {
|
func NewHuaChenIpClient(appCode string) *HuaChenIpClient {
|
||||||
return &HuaChenIpClient{
|
return &HuaChenIpClient{
|
||||||
AppCode: appCode,
|
AppCode: appCode,
|
||||||
Host: "https://c2ba.api.huachen.cn",
|
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
|
return &apiResult, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *HuaChenIpClient) GetIpInfoFormCache(ctx context.Context, ip string, ttl time.Duration) (res *ApiResult, err error) {
|
func (h *HuaChenIpClient) Set(cache ICacheAdapter) {
|
||||||
getCache, err := h.cache.Get(ctx, h.ipKey(ip))
|
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 {
|
if err != nil {
|
||||||
return nil, errors.Wrapf(err, "获取缓存失败,ip:%s", ip)
|
return nil, errors.Wrapf(err, "获取缓存失败,ip:%s", ip)
|
||||||
}
|
}
|
||||||
if getCache != nil {
|
if res != nil {
|
||||||
return getCache, nil
|
return res, nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
res, err = h.GetIpInfo(ip)
|
res, err = h.GetIpInfo(ip)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -109,10 +114,12 @@ func (h *HuaChenIpClient) GetIpInfoFormCache(ctx context.Context, ip string, ttl
|
|||||||
return nil, errors.Wrapf(err, "无法将IP信息转换为JSON,ip:%s", ip)
|
return nil, errors.Wrapf(err, "无法将IP信息转换为JSON,ip:%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 {
|
if err != nil {
|
||||||
return nil, errors.Wrapf(err, "缓存ip:%s信息失败,", ip)
|
return nil, errors.Wrapf(err, "缓存ip:%s信息失败,", ip)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -9,4 +9,5 @@ type ICacheAdapter interface {
|
|||||||
Get(ctx context.Context, key string) (value interface{}, err error)
|
Get(ctx context.Context, key string) (value interface{}, err error)
|
||||||
Set(ctx context.Context, key string, value interface{}, expire time.Duration) (err error)
|
Set(ctx context.Context, key string, value interface{}, expire time.Duration) (err error)
|
||||||
Del(ctx context.Context, key string) (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)
|
||||||
}
|
}
|
||||||
|
@@ -8,7 +8,6 @@ import (
|
|||||||
dysmsapi20170525 "github.com/alibabacloud-go/dysmsapi-20170525/v5/client"
|
dysmsapi20170525 "github.com/alibabacloud-go/dysmsapi-20170525/v5/client"
|
||||||
util "github.com/alibabacloud-go/tea-utils/v2/service"
|
util "github.com/alibabacloud-go/tea-utils/v2/service"
|
||||||
"github.com/alibabacloud-go/tea/tea"
|
"github.com/alibabacloud-go/tea/tea"
|
||||||
"github.com/redis/go-redis/v9"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -71,7 +70,15 @@ func (c *SmsClient) GetCode(ctx context.Context, key string) (code string, err e
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
if value == nil {
|
||||||
|
return "", errors.New("验证码不存在,请重新发送")
|
||||||
|
}
|
||||||
|
switch value.(type) {
|
||||||
|
case string:
|
||||||
return value.(string), nil
|
return value.(string), nil
|
||||||
|
default:
|
||||||
|
return "", errors.New("验证码类型错误,请联系管理员")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SaveCode 保存验证码
|
// SaveCode 保存验证码
|
||||||
@@ -81,10 +88,7 @@ func (c *SmsClient) SaveCode(ctx context.Context, key string, code string, expir
|
|||||||
}
|
}
|
||||||
//保存code
|
//保存code
|
||||||
err = c.Cache.Set(ctx, key, code, expire)
|
err = c.Cache.Set(ctx, key, code, expire)
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteCode 删除验证码
|
// DeleteCode 删除验证码
|
||||||
@@ -102,34 +106,18 @@ func (c *SmsClient) SaveCodeWithFrequency(ctx context.Context, key string, code
|
|||||||
return errors.New("缓存不能为空")
|
return errors.New("缓存不能为空")
|
||||||
}
|
}
|
||||||
frequencyKey := fmt.Sprintf(CodeFrequencyKey, key)
|
frequencyKey := fmt.Sprintf(CodeFrequencyKey, key)
|
||||||
if frequency != 0 {
|
if frequency <= 0 {
|
||||||
// 判断验证码是否频繁
|
return errors.New("频率不能小于0")
|
||||||
frequencyValue, err := c.Cache.Get(ctx, frequencyKey)
|
}
|
||||||
|
ok, err := c.Cache.SetNX(ctx, frequencyKey, CodeFrequencyValue, frequency)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, redis.Nil) {
|
|
||||||
// 缓存中没有数据
|
|
||||||
fmt.Printf("缓存中没有数据")
|
|
||||||
} else {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
if !ok {
|
||||||
if frequencyValue != nil && frequencyValue.(string) == CodeFrequencyValue {
|
|
||||||
return errors.New("code发送频繁,请稍后再试")
|
return errors.New("code发送频繁,请稍后再试")
|
||||||
}
|
}
|
||||||
}
|
|
||||||
//保存code
|
|
||||||
err = c.SaveCode(ctx, key, code, expire)
|
err = c.SaveCode(ctx, key, code, expire)
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
|
||||||
if frequency != 0 {
|
|
||||||
// 保存验证码发送频率
|
|
||||||
err = c.Cache.Set(ctx, frequencyKey, CodeFrequencyValue, frequency)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// VerifyCode 校验验证码
|
// VerifyCode 校验验证码
|
||||||
|
@@ -51,6 +51,14 @@ func (r *RedisCacheAdapter) Del(ctx context.Context, key string) error {
|
|||||||
return r.client.Del(ctx, key).Err()
|
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 (
|
var (
|
||||||
//SMS
|
//SMS
|
||||||
accessKeyId = os.Getenv("SMS_ALIBABA_CLOUD_ACCESS_KEY_ID")
|
accessKeyId = os.Getenv("SMS_ALIBABA_CLOUD_ACCESS_KEY_ID")
|
||||||
|
@@ -33,7 +33,7 @@ func (w *WeiPinShangClient) GetManyPostage(getManyPostageReq *GetManyPostageReq)
|
|||||||
fmt.Println("getManyPostageReq", getManyPostageReq)
|
fmt.Println("getManyPostageReq", getManyPostageReq)
|
||||||
paramMap := make(map[string]any)
|
paramMap := make(map[string]any)
|
||||||
|
|
||||||
paramMap["goodInfo"] = getManyPostageReq.GoodsInfo
|
paramMap["goodsInfo"] = getManyPostageReq.GoodsInfo
|
||||||
paramMap["address"] = getManyPostageReq.Address
|
paramMap["address"] = getManyPostageReq.Address
|
||||||
paramMap["province"] = getManyPostageReq.Province
|
paramMap["province"] = getManyPostageReq.Province
|
||||||
paramMap["county"] = getManyPostageReq.County
|
paramMap["county"] = getManyPostageReq.County
|
||||||
|
@@ -29,7 +29,7 @@ func TestWeiPinShangClient_GetManyPostage(t *testing.T) {
|
|||||||
Key: "f654ea5bde7635c3f46191191e5c4c8e",
|
Key: "f654ea5bde7635c3f46191191e5c4c8e",
|
||||||
},
|
},
|
||||||
args: GetManyPostageReq{
|
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: "广东省",
|
Province: "广东省",
|
||||||
Address: "奥园",
|
Address: "奥园",
|
||||||
City: "广州市",
|
City: "广州市",
|
||||||
@@ -80,14 +80,14 @@ func TestWeiPinShangClient_PreOrder(t *testing.T) {
|
|||||||
Key: "f654ea5bde7635c3f46191191e5c4c8e",
|
Key: "f654ea5bde7635c3f46191191e5c4c8e",
|
||||||
},
|
},
|
||||||
args: PreOrderReq{
|
args: PreOrderReq{
|
||||||
GoodsInfo: "[{\"goodsId\":\"WPS2_1231155626421463\",\"goodSpecId\":\"WPS2_12311556265677476\",\"num\":1}]",
|
GoodsInfo: "[{\"goodsId\":\"WPS427_adf0008\",\"goodSpecId\":\"WPS427_0715110641454716\",\"num\":1}]",
|
||||||
Province: "广东省",
|
Province: "广东省",
|
||||||
Address: "奥园",
|
Address: "奥园",
|
||||||
City: "广州市",
|
City: "广州市",
|
||||||
Area: "番禺区",
|
Area: "番禺区",
|
||||||
ConsigneePhone: "15375390426",
|
ConsigneePhone: "15375390426",
|
||||||
ConsigneeContacts: "张三",
|
ConsigneeContacts: "张三",
|
||||||
LockCode: "L100000004",
|
LockCode: "L100000005",
|
||||||
Source: "AILEHUI",
|
Source: "AILEHUI",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
Reference in New Issue
Block a user