|
|
@@ -1,18 +1,24 @@
|
|
|
package main
|
|
|
|
|
|
import (
|
|
|
+ "Vali-Tools/handlers" // 根据实际项目路径调整
|
|
|
+ "Vali-Tools/utils"
|
|
|
"context"
|
|
|
"fmt"
|
|
|
- "github.com/wailsapp/wails/v2/pkg/runtime"
|
|
|
- "io"
|
|
|
"os"
|
|
|
- "path/filepath"
|
|
|
"strings"
|
|
|
)
|
|
|
|
|
|
// App struct
|
|
|
type App struct {
|
|
|
- ctx context.Context
|
|
|
+ ctx context.Context
|
|
|
+ Token string
|
|
|
+ Env string
|
|
|
+ directoryHandler *handlers.DirectoryHandler
|
|
|
+ dialogHandler *handlers.DialogHandler
|
|
|
+ UrlHost string // 根据实际项目路径调整
|
|
|
+ HomePage string //首次要打开得页面路径
|
|
|
+ handlerRequest *handlers.HandlerRequests
|
|
|
}
|
|
|
|
|
|
// NewApp creates a new App application struct
|
|
|
@@ -20,215 +26,191 @@ func NewApp() *App {
|
|
|
return &App{}
|
|
|
}
|
|
|
|
|
|
-// startup is called when the app starts. The context is saved
|
|
|
-// so we can call the runtime methods
|
|
|
+// startup is called when the app starts
|
|
|
func (a *App) startup(ctx context.Context) {
|
|
|
a.ctx = ctx
|
|
|
+ a.directoryHandler = handlers.NewDirectoryHandler(ctx)
|
|
|
+ a.dialogHandler = handlers.NewDialogHandler(ctx)
|
|
|
+ fmt.Printf("All startup args: %v\n", os.Args)
|
|
|
+ args := os.Args
|
|
|
+ if len(args) > 1 {
|
|
|
+ Args := args[1]
|
|
|
+ paramsMap := a.parsArguments(Args)
|
|
|
+ a.Token = paramsMap["token"]
|
|
|
+ env := paramsMap["env"]
|
|
|
+ page := paramsMap["page"]
|
|
|
+ urlHost := "https://dev2.pubdata.cn"
|
|
|
+ if env != "dev" && env != "" {
|
|
|
+ urlHost = "https://dev2.valimart.net"
|
|
|
+ }
|
|
|
+ a.UrlHost = urlHost
|
|
|
+ a.HomePage = page
|
|
|
+ println("Token:", a.Token)
|
|
|
+ println("UrlHost", a.UrlHost)
|
|
|
+ a.handlerRequest = handlers.NewHandlerRequests(ctx, a.Token, a.UrlHost)
|
|
|
+ fmt.Printf("获取到了Token信息: %s\n", a.Token)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-// Greet returns a greeting for the given name
|
|
|
-func (a *App) Greet(name string) string {
|
|
|
- return fmt.Sprintf("Hello %s, It's show time!", name)
|
|
|
-}
|
|
|
+// 解析参数
|
|
|
+func (a *App) parsArguments(params string) map[string]string {
|
|
|
+ result := make(map[string]string)
|
|
|
+ pairs := strings.Split(params, "&")
|
|
|
|
|
|
-// SelectDirectory 目录选择方法
|
|
|
-func (a *App) SelectDirectory() (string, error) {
|
|
|
- //目录选择方法
|
|
|
- options := runtime.OpenDialogOptions{
|
|
|
- Title: "选择目录",
|
|
|
- Filters: []runtime.FileFilter{},
|
|
|
- DefaultDirectory: "",
|
|
|
- DefaultFilename: "", // 允许选择目录
|
|
|
- }
|
|
|
-
|
|
|
- result, err := runtime.OpenDirectoryDialog(a.ctx, options)
|
|
|
- if err != nil {
|
|
|
- return "", err
|
|
|
+ for _, pair := range pairs {
|
|
|
+ kv := strings.Split(pair, "=")
|
|
|
+ if len(kv) == 2 {
|
|
|
+ result[kv[0]] = kv[1]
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- if result == "" {
|
|
|
- return "", nil // 用户取消选择
|
|
|
- }
|
|
|
+ return result
|
|
|
+}
|
|
|
|
|
|
- return result, nil
|
|
|
+type UserInfo struct {
|
|
|
+ AccountName string `json:"account_name"`
|
|
|
+ CompanyName string `json:"brand_company_name"`
|
|
|
+ RealName string `json:"real_name"`
|
|
|
}
|
|
|
|
|
|
-// HandlerDirectory 处理目录复制的主要方法
|
|
|
-func (a *App) HandlerDirectory(sourceDir, targetDir string) error {
|
|
|
- // 检查源目录是否存在
|
|
|
- if _, err := os.Stat(sourceDir); os.IsNotExist(err) {
|
|
|
- return fmt.Errorf("源目录不存在: %s", sourceDir)
|
|
|
+// GetAppArgument 获取APP传递得参数
|
|
|
+func (a *App) GetAppArgument() interface{} {
|
|
|
+ if a.handlerRequest == nil {
|
|
|
+ fmt.Printf("handlerRequest未初始化\n")
|
|
|
+ return nil
|
|
|
}
|
|
|
-
|
|
|
- // 检查目标目录,如果不存在则创建
|
|
|
- if _, err := os.Stat(targetDir); os.IsNotExist(err) {
|
|
|
- // 源目录不存在时不需要创建目录,但目标目录可以创建
|
|
|
- err := os.MkdirAll(targetDir, os.ModePerm)
|
|
|
- if err != nil {
|
|
|
- return fmt.Errorf("创建目标目录失败: %v", err)
|
|
|
+ info, err := a.handlerRequest.GetUserInfo()
|
|
|
+ if err != nil {
|
|
|
+ fmt.Printf("获取用户信息失败: %v\n", err)
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+ // 将 map 转换为 JSON 再解析为结构体
|
|
|
+ // 安全地提取用户信息数据
|
|
|
+ var userInfo interface{}
|
|
|
+ if infoMap, ok := info.(map[string]interface{}); ok {
|
|
|
+ if data, exists := infoMap["data"]; exists {
|
|
|
+ userInfo = data
|
|
|
+ } else {
|
|
|
+ userInfo = infoMap
|
|
|
}
|
|
|
+ } else {
|
|
|
+ userInfo = info
|
|
|
+ }
|
|
|
+ return map[string]interface {
|
|
|
+ }{
|
|
|
+ "token": a.Token,
|
|
|
+ "user_info": userInfo,
|
|
|
+ "home_page": a.HomePage,
|
|
|
}
|
|
|
-
|
|
|
- // 执行具体的处理逻辑
|
|
|
- return a.processDirectories(sourceDir, targetDir)
|
|
|
}
|
|
|
|
|
|
-// ProcessResult 处理结果结构体
|
|
|
-type ProcessResult struct {
|
|
|
- Success bool `json:"success"`
|
|
|
- Message string `json:"message"`
|
|
|
- Progress int `json:"progress"`
|
|
|
+// SelectDirectory 目录选择方法
|
|
|
+func (a *App) SelectDirectory() (string, error) {
|
|
|
+ return a.dialogHandler.SelectDirectory()
|
|
|
}
|
|
|
|
|
|
-// isValidImageFile 检查文件是否为有效的图片文件
|
|
|
-func (a *App) isValidImageFile(filename string) bool {
|
|
|
- validExtensions := []string{".avif", ".bmp", ".png", ".jpg", ".jpeg"}
|
|
|
- ext := strings.ToLower(filepath.Ext(filename))
|
|
|
+// HandlerDirectory 处理目录复制的主要方法
|
|
|
+func (a *App) HandlerDirectory(sourceDir, targetDir string) error {
|
|
|
+ return a.directoryHandler.HandlerDirectory(sourceDir, targetDir)
|
|
|
+}
|
|
|
|
|
|
- for _, validExt := range validExtensions {
|
|
|
- if ext == validExt {
|
|
|
- return true
|
|
|
- }
|
|
|
+// HandlerOutPutDirectory 处理输出目录
|
|
|
+func (a *App) HandlerOutPutDirectory() []handlers.ImageResult {
|
|
|
+ applicationDirectory, err := a.directoryHandler.GetApplicationDirectory()
|
|
|
+ if err != nil {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+ directory, err := a.directoryHandler.HandlerOutPutDirectory(applicationDirectory + "/output")
|
|
|
+ if err != nil {
|
|
|
+ return []handlers.ImageResult{}
|
|
|
}
|
|
|
- return false
|
|
|
+ return directory
|
|
|
}
|
|
|
|
|
|
-// ProcessCallback 处理过程回调函数类型
|
|
|
-type ProcessCallback func(result ProcessResult)
|
|
|
-
|
|
|
-// processDirectories 实际处理目录的逻辑
|
|
|
-func (a *App) processDirectories(sourceDir, targetDir string) error {
|
|
|
- // 发送初始进度
|
|
|
- a.sendProgress(ProcessResult{
|
|
|
- Success: true,
|
|
|
- Message: "开始处理目录...",
|
|
|
- Progress: 0,
|
|
|
- })
|
|
|
+type PostJsonData struct {
|
|
|
+ ImageUrl string `json:"image_url"`
|
|
|
+ GoodsArtNo string `json:"goods_art_no"`
|
|
|
+ Category string `json:"category"`
|
|
|
+ Description string `json:"description"`
|
|
|
+ Price string `json:"price"`
|
|
|
+}
|
|
|
|
|
|
- // 遍历和处理逻辑
|
|
|
- err := filepath.Walk(sourceDir, func(path string, info os.FileInfo, err error) error {
|
|
|
+func (a *App) MakeProducts(imageProduct []handlers.ImageResult) map[string]interface{} {
|
|
|
+ // 生成产品册
|
|
|
+ var postData []PostJsonData
|
|
|
+ for _, product := range imageProduct {
|
|
|
+ result, err := a.handlerRequest.MakeFileRequest(utils.UploadImages, product.ImagePath, "file")
|
|
|
if err != nil {
|
|
|
- a.sendProgress(ProcessResult{
|
|
|
- Success: false,
|
|
|
- Message: fmt.Sprintf("访问路径出错: %v", err),
|
|
|
- Progress: 0,
|
|
|
- })
|
|
|
- return err
|
|
|
+ fmt.Printf("上传图片失败: %v\n", err)
|
|
|
+ continue
|
|
|
}
|
|
|
- fmt.Printf("正在处理: %s\n", path)
|
|
|
- // 处理 800x800 目录的逻辑
|
|
|
- if info.IsDir() && strings.Contains(info.Name(), "800x800") {
|
|
|
- fmt.Printf("👍👍👍符合条件: %s\n", path)
|
|
|
- // 获取上级目录名称
|
|
|
- parentDirName := filepath.Base(filepath.Dir(path))
|
|
|
-
|
|
|
- // 在目标目录中创建以父目录命名的子目录
|
|
|
- newTargetDir := filepath.Join(targetDir, parentDirName)
|
|
|
- // 检查目标目录,如果不存在则创建
|
|
|
- if _, err := os.Stat(newTargetDir); os.IsNotExist(err) {
|
|
|
- // 源目录不存在时不需要创建目录,但目标目录可以创建
|
|
|
- err := os.MkdirAll(newTargetDir, os.ModePerm)
|
|
|
- if err != nil {
|
|
|
- return fmt.Errorf("创建目标目录失败: %v", err)
|
|
|
+ if resultMap, ok := result.(map[string]interface{}); ok {
|
|
|
+ // 安全获取 data 字段
|
|
|
+ dataValue, exists := resultMap["data"]
|
|
|
+ if !exists {
|
|
|
+ fmt.Println("响应中未找到 data 字段")
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ // 类型断言为 map[string]interface{}
|
|
|
+ reqData, ok := dataValue.(map[string]interface{})
|
|
|
+ if !ok {
|
|
|
+ fmt.Printf("data字段类型不正确,期望map[string]interface{},实际类型: %T\n", dataValue)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ var imageUrl string
|
|
|
+ if urlValue, exists := reqData["url"]; exists {
|
|
|
+ // 安全转换为字符串
|
|
|
+ switch v := urlValue.(type) {
|
|
|
+ case string:
|
|
|
+ imageUrl = v
|
|
|
+ case fmt.Stringer:
|
|
|
+ imageUrl = v.String()
|
|
|
+ default:
|
|
|
+ imageUrl = fmt.Sprintf("%v", v)
|
|
|
}
|
|
|
}
|
|
|
- a.sendProgress(ProcessResult{
|
|
|
- Success: true,
|
|
|
- Message: fmt.Sprintf("找到目录: %s,将在目标目录创建子目录: %s", info.Name(), parentDirName),
|
|
|
- Progress: 50,
|
|
|
+ // 安全获取各个字段并进行类型转换
|
|
|
+ goodsArtNo := product.GoodsArtNo
|
|
|
+ category := product.Category
|
|
|
+ description := product.Description
|
|
|
+ priceValue := product.Price
|
|
|
+ // 添加到 postData
|
|
|
+ postData = append(postData, PostJsonData{
|
|
|
+ ImageUrl: imageUrl,
|
|
|
+ GoodsArtNo: goodsArtNo,
|
|
|
+ Category: category,
|
|
|
+ Description: description,
|
|
|
+ Price: priceValue,
|
|
|
})
|
|
|
-
|
|
|
- // 执行复制操作到新创建的子目录
|
|
|
- err := a.copyFilesFromDir(path, newTargetDir)
|
|
|
- if err != nil {
|
|
|
- a.sendProgress(ProcessResult{
|
|
|
- Success: false,
|
|
|
- Message: fmt.Sprintf("复制文件到:%v 失败: %v", newTargetDir, err),
|
|
|
- Progress: 50,
|
|
|
- })
|
|
|
- return err
|
|
|
- }
|
|
|
}
|
|
|
-
|
|
|
- return nil
|
|
|
- })
|
|
|
-
|
|
|
- // 发送完成进度
|
|
|
- if err != nil {
|
|
|
- a.sendProgress(ProcessResult{
|
|
|
- Success: false,
|
|
|
- Message: fmt.Sprintf("处理失败: %v", err),
|
|
|
- Progress: 100,
|
|
|
- })
|
|
|
- } else {
|
|
|
- a.sendProgress(ProcessResult{
|
|
|
- Success: true,
|
|
|
- Message: "处理完成",
|
|
|
- Progress: 100,
|
|
|
- })
|
|
|
}
|
|
|
-
|
|
|
- return err
|
|
|
-}
|
|
|
-
|
|
|
-// copyFilesFromDir 从指定目录复制所有文件到目标目录
|
|
|
-func (a *App) copyFilesFromDir(sourceDir, targetDir string) error {
|
|
|
- entries, err := os.ReadDir(sourceDir)
|
|
|
+ HLMData := map[string]interface{}{
|
|
|
+ "data": postData,
|
|
|
+ }
|
|
|
+ result, err := a.handlerRequest.MakePostRequest(utils.CreateProduct, HLMData)
|
|
|
if err != nil {
|
|
|
- return fmt.Errorf("读取目录失败 %s: %v", sourceDir, err)
|
|
|
+ fmt.Printf("request err:%v\n", err)
|
|
|
+ return nil
|
|
|
}
|
|
|
-
|
|
|
- for _, entry := range entries {
|
|
|
- // 只处理文件,跳过子目录
|
|
|
- if entry.IsDir() {
|
|
|
- continue // 忽略目录
|
|
|
+ if resultMap, ok := result.(map[string]interface{}); ok {
|
|
|
+ dataValue, exists := resultMap["data"]
|
|
|
+ if !exists {
|
|
|
+ return nil
|
|
|
}
|
|
|
-
|
|
|
- // 检查是否为有效的图片文件
|
|
|
- if !a.isValidImageFile(entry.Name()) {
|
|
|
- continue // 跳过非图片文件
|
|
|
+ reqData, _ := dataValue.(map[string]interface{})
|
|
|
+ //a.UrlHost
|
|
|
+ productIndex, _ := reqData["product_index"].(string)
|
|
|
+ urlShuffix := "/product_catalog/index.html?product_index="
|
|
|
+ // 检查 URL 是否包含特定域名
|
|
|
+ HtmlUrl := "https://b2b2.valimart.net"
|
|
|
+ if strings.Contains(a.UrlHost, "pubdata.cn") {
|
|
|
+ // 处理 pubdata.cn 域名的逻辑
|
|
|
+ HtmlUrl = "https://b2b2.pubdata.cn"
|
|
|
}
|
|
|
-
|
|
|
- sourcePath := filepath.Join(sourceDir, entry.Name())
|
|
|
- targetPath := filepath.Join(targetDir, entry.Name())
|
|
|
-
|
|
|
- err := a.copyFile(sourcePath, targetPath)
|
|
|
- if err != nil {
|
|
|
- return fmt.Errorf("复制文件失败 %s 到 %s: %v", sourcePath, targetPath, err)
|
|
|
+ returnUrl := HtmlUrl + urlShuffix + productIndex
|
|
|
+ return map[string]interface{}{
|
|
|
+ "url": returnUrl,
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
return nil
|
|
|
}
|
|
|
-
|
|
|
-// copyFile 复制单个文件
|
|
|
-func (a *App) copyFile(src, dst string) error {
|
|
|
- sourceFile, err := os.Open(src)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- defer sourceFile.Close()
|
|
|
-
|
|
|
- // 创建目标文件
|
|
|
- destFile, err := os.Create(dst)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
- defer destFile.Close()
|
|
|
-
|
|
|
- // 复制文件内容
|
|
|
- _, err = io.Copy(destFile, sourceFile)
|
|
|
- if err != nil {
|
|
|
- return err
|
|
|
- }
|
|
|
-
|
|
|
- // 同步文件内容到磁盘
|
|
|
- return destFile.Sync()
|
|
|
-}
|
|
|
-
|
|
|
-// sendProgress 发送进度更新
|
|
|
-func (a *App) sendProgress(result ProcessResult) {
|
|
|
- // 这里可以通过事件系统发送进度更新
|
|
|
- // 具体实现在下一步中说明
|
|
|
- runtime.EventsEmit(a.ctx, "copy800-progress", result)
|
|
|
-}
|