From 3e6bc5ce4cd6feedc64b56a1d6cc6f89b716d4ed Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=98=93=E5=B0=8F=E6=AF=85?= <quchuanping@qq.com>
Date: Tue, 18 Mar 2025 15:02:02 +0800
Subject: [PATCH] ikucun

---
 upstream/ikucun/ikucun.go       | 74 +++++++++++++++++++++++++++++++++
 upstream/ikucun/ikucun_goods.go |  1 +
 upstream/upstream.go            |  7 ++++
 3 files changed, 82 insertions(+)
 create mode 100644 upstream/ikucun/ikucun.go
 create mode 100644 upstream/ikucun/ikucun_goods.go

diff --git a/upstream/ikucun/ikucun.go b/upstream/ikucun/ikucun.go
new file mode 100644
index 0000000..6316536
--- /dev/null
+++ b/upstream/ikucun/ikucun.go
@@ -0,0 +1,74 @@
+package ikucun
+
+import (
+	"context"
+	"github.com/gogf/gf/crypto/gsha1"
+	"github.com/gogf/gf/encoding/gjson"
+	"github.com/gogf/gf/frame/g"
+	"github.com/gogf/gf/os/gtime"
+	"github.com/gogf/gf/util/gconv"
+	"math/rand"
+	"time"
+)
+
+var server *Config
+
+const pkgName = "ikucun"
+
+type Config struct {
+	AppId     string
+	AppSecret string
+	ApiUrl    string
+}
+
+func makeNonceStr() string {
+	str := "abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789"
+	rand.Seed(time.Now().UnixNano()) // 初始化随机数种子
+
+	// 打乱字符串
+	shuffledStr := shuffleString(str)
+
+	// 随机起始位置
+	startIndex := rand.Intn(len(shuffledStr) - 10)
+
+	// 截取 10 位字符
+	return shuffledStr[startIndex : startIndex+10]
+}
+
+// 打乱字符串
+func shuffleString(str string) string {
+	strRunes := []rune(str)
+	rand.Shuffle(len(strRunes), func(i, j int) {
+		strRunes[i], strRunes[j] = strRunes[j], strRunes[i]
+	})
+	return string(strRunes)
+}
+
+func sign(req string) string {
+	res := gsha1.Encrypt(req)
+	return res
+}
+
+func post(ctx context.Context, method string, req interface{}) (res string, err error) {
+	Start := gtime.TimestampMilli()
+	reqMap := gconv.Map(req)
+	reqMap["appId"] = server.AppId
+	param := gjson.New(reqMap)
+	Url := server.ApiUrl + method
+	Request := g.Client()
+	Request.SetHeader("Content-Type", "application/json")
+	Request.SetHeader("sign", sign(param.MustToJsonString()))
+	resp, err := Request.Timeout(time.Second*5).Post(Url, param.MustToJsonString())
+	defer func() {
+		_ = resp.Close()
+		ctx = context.WithValue(ctx, "Method", "POST")
+		ctx = context.WithValue(ctx, "URI", Url)
+		if err != nil {
+			g.Log().Ctx(ctx).Cat(pkgName).Cat("error").Infof("参数【%v】错误【%v】响应时间【%v ms】", param.MustToJsonString(), err.Error(), gtime.TimestampMilli()-Start)
+		} else {
+			g.Log().Ctx(ctx).Cat(pkgName).Infof("参数【%v】响应【%v】响应时间【%v ms】", param.MustToJsonString(), res, gtime.TimestampMilli()-Start)
+		}
+	}()
+	res = resp.ReadAllString()
+	return
+}
diff --git a/upstream/ikucun/ikucun_goods.go b/upstream/ikucun/ikucun_goods.go
new file mode 100644
index 0000000..e823cb9
--- /dev/null
+++ b/upstream/ikucun/ikucun_goods.go
@@ -0,0 +1 @@
+package ikucun
diff --git a/upstream/upstream.go b/upstream/upstream.go
index a01c366..1173fbc 100644
--- a/upstream/upstream.go
+++ b/upstream/upstream.go
@@ -23,6 +23,7 @@ const (
 	Tmv3     = 20 //天猫优选
 	Suning   = 21 //苏宁有货
 	Yonghui  = 22 //永辉
+	Ikucun   = 23 //爱库存
 )
 
 var (
@@ -116,6 +117,10 @@ func GetUpstreamList() (res interface{}, err error) {
 			"key":  Yonghui,
 			"name": GetUpstreamName(Yonghui),
 		},
+		g.Map{
+			"key":  Ikucun,
+			"name": GetUpstreamName(Ikucun),
+		},
 	}
 	return
 }
@@ -158,6 +163,8 @@ func GetUpstreamName(source int) string {
 		return "苏宁易购"
 	case Yonghui:
 		return "永辉"
+	case Ikucun:
+		return "爱库存"
 	default:
 		return "未知来源"
 	}
-- 
2.18.1