app.go 7.8 KB

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