app.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. package main
  2. import (
  3. "Vali-Tools/handlers" // 根据实际项目路径调整
  4. "Vali-Tools/utils"
  5. "context"
  6. "fmt"
  7. "os"
  8. "strings"
  9. )
  10. // App struct
  11. type App struct {
  12. ctx context.Context
  13. Token string
  14. Env string
  15. directoryHandler *handlers.DirectoryHandler
  16. dialogHandler *handlers.DialogHandler
  17. UrlHost string // 根据实际项目路径调整
  18. HomePage string //首次要打开得页面路径
  19. handlerRequest *handlers.HandlerRequests
  20. }
  21. // NewApp creates a new App application struct
  22. func NewApp() *App {
  23. return &App{}
  24. }
  25. // startup is called when the app starts
  26. func (a *App) startup(ctx context.Context) {
  27. a.ctx = ctx
  28. a.directoryHandler = handlers.NewDirectoryHandler(ctx)
  29. a.dialogHandler = handlers.NewDialogHandler(ctx)
  30. fmt.Printf("All startup args: %v\n", os.Args)
  31. args := os.Args
  32. if len(args) > 1 {
  33. Args := args[1]
  34. paramsMap := a.parsArguments(Args)
  35. a.Token = paramsMap["token"]
  36. env := paramsMap["env"]
  37. page := paramsMap["page"]
  38. urlHost := "https://dev2.pubdata.cn"
  39. if env != "dev" && env != "" {
  40. urlHost = "https://dev2.valimart.net"
  41. }
  42. a.UrlHost = urlHost
  43. a.HomePage = page
  44. println("Token:", a.Token)
  45. println("UrlHost", a.UrlHost)
  46. a.handlerRequest = handlers.NewHandlerRequests(ctx, a.Token, a.UrlHost)
  47. fmt.Printf("获取到了Token信息: %s\n", a.Token)
  48. }
  49. }
  50. // 解析参数
  51. func (a *App) parsArguments(params string) map[string]string {
  52. result := make(map[string]string)
  53. pairs := strings.Split(params, "&")
  54. for _, pair := range pairs {
  55. kv := strings.Split(pair, "=")
  56. if len(kv) == 2 {
  57. result[kv[0]] = kv[1]
  58. }
  59. }
  60. return result
  61. }
  62. type UserInfo struct {
  63. AccountName string `json:"account_name"`
  64. CompanyName string `json:"brand_company_name"`
  65. RealName string `json:"real_name"`
  66. }
  67. // GetAppArgument 获取APP传递得参数
  68. func (a *App) GetAppArgument() interface{} {
  69. if a.handlerRequest == nil {
  70. fmt.Printf("handlerRequest未初始化\n")
  71. return nil
  72. }
  73. info, err := a.handlerRequest.GetUserInfo()
  74. if err != nil {
  75. fmt.Printf("获取用户信息失败: %v\n", err)
  76. return nil
  77. }
  78. // 将 map 转换为 JSON 再解析为结构体
  79. // 安全地提取用户信息数据
  80. var userInfo interface{}
  81. if infoMap, ok := info.(map[string]interface{}); ok {
  82. if data, exists := infoMap["data"]; exists {
  83. userInfo = data
  84. } else {
  85. userInfo = infoMap
  86. }
  87. } else {
  88. userInfo = info
  89. }
  90. return map[string]interface {
  91. }{
  92. "token": a.Token,
  93. "user_info": userInfo,
  94. "home_page": a.HomePage,
  95. }
  96. }
  97. // SelectDirectory 目录选择方法
  98. func (a *App) SelectDirectory() (string, error) {
  99. return a.dialogHandler.SelectDirectory()
  100. }
  101. // HandlerDirectory 处理目录复制的主要方法
  102. func (a *App) HandlerDirectory(sourceDir, targetDir string) error {
  103. return a.directoryHandler.HandlerDirectory(sourceDir, targetDir)
  104. }
  105. // HandlerOutPutDirectory 处理输出目录
  106. func (a *App) HandlerOutPutDirectory() []handlers.ImageResult {
  107. applicationDirectory, err := a.directoryHandler.GetApplicationDirectory()
  108. if err != nil {
  109. return nil
  110. }
  111. directory, err := a.directoryHandler.HandlerOutPutDirectory(applicationDirectory + "/output")
  112. if err != nil {
  113. return []handlers.ImageResult{}
  114. }
  115. return directory
  116. }
  117. type PostJsonData struct {
  118. ImageUrl string `json:"image_url"`
  119. GoodsArtNo string `json:"goods_art_no"`
  120. Category string `json:"category"`
  121. Description string `json:"description"`
  122. Price string `json:"price"`
  123. }
  124. func (a *App) MakeProducts(imageProduct []handlers.ImageResult) map[string]interface{} {
  125. // 生成产品册
  126. var postData []PostJsonData
  127. for _, product := range imageProduct {
  128. result, err := a.handlerRequest.MakeFileRequest(utils.UploadImages, product.ImagePath, "file")
  129. if err != nil {
  130. fmt.Printf("上传图片失败: %v\n", err)
  131. continue
  132. }
  133. if resultMap, ok := result.(map[string]interface{}); ok {
  134. // 安全获取 data 字段
  135. dataValue, exists := resultMap["data"]
  136. if !exists {
  137. fmt.Println("响应中未找到 data 字段")
  138. continue
  139. }
  140. // 类型断言为 map[string]interface{}
  141. reqData, ok := dataValue.(map[string]interface{})
  142. if !ok {
  143. fmt.Printf("data字段类型不正确,期望map[string]interface{},实际类型: %T\n", dataValue)
  144. continue
  145. }
  146. var imageUrl string
  147. if urlValue, exists := reqData["url"]; exists {
  148. // 安全转换为字符串
  149. switch v := urlValue.(type) {
  150. case string:
  151. imageUrl = v
  152. case fmt.Stringer:
  153. imageUrl = v.String()
  154. default:
  155. imageUrl = fmt.Sprintf("%v", v)
  156. }
  157. }
  158. // 安全获取各个字段并进行类型转换
  159. goodsArtNo := product.GoodsArtNo
  160. category := product.Category
  161. description := product.Description
  162. priceValue := product.Price
  163. // 添加到 postData
  164. postData = append(postData, PostJsonData{
  165. ImageUrl: imageUrl,
  166. GoodsArtNo: goodsArtNo,
  167. Category: category,
  168. Description: description,
  169. Price: priceValue,
  170. })
  171. }
  172. }
  173. HLMData := map[string]interface{}{
  174. "data": postData,
  175. }
  176. result, err := a.handlerRequest.MakePostRequest(utils.CreateProduct, HLMData)
  177. if err != nil {
  178. fmt.Printf("request err:%v\n", err)
  179. return nil
  180. }
  181. if resultMap, ok := result.(map[string]interface{}); ok {
  182. dataValue, exists := resultMap["data"]
  183. if !exists {
  184. return nil
  185. }
  186. reqData, _ := dataValue.(map[string]interface{})
  187. //a.UrlHost
  188. productIndex, _ := reqData["product_index"].(string)
  189. urlShuffix := "/product_catalog/index.html?product_index="
  190. // 检查 URL 是否包含特定域名
  191. HtmlUrl := "https://b2b2.valimart.net"
  192. if strings.Contains(a.UrlHost, "pubdata.cn") {
  193. // 处理 pubdata.cn 域名的逻辑
  194. HtmlUrl = "https://b2b2.pubdata.cn"
  195. }
  196. returnUrl := HtmlUrl + urlShuffix + productIndex
  197. return map[string]interface{}{
  198. "url": returnUrl,
  199. }
  200. }
  201. return nil
  202. }