原型里每行末尾有「详情 / 目录」两个操作,实现时漏掉了整个操作列。 - 详情:按店铺地区拼 Shopee 商品页地址并用系统浏览器打开。 地区来自 erp/shop/all 的 region 字段,在前端建 平台店铺ID -> 地区 的映射表;认不出地区时置灰,不拼一个打不开的地址。 各站点域名集中在 shopeeDomains 里,不要在别处硬编码。 - 目录:打开本地视频所在文件夹。视频尚未下载,先置灰并提示 「下载视频后可用」,等 R4(#5 MVP2)实现下载后解开禁用即可, 届时不用再动表格结构。 app.go 新增 OpenInBrowser,只允许 http/https,避免变成可以 启动任意程序的入口。 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LbdtsD3ohhSMy3KPoCgARq
306 lines
9.6 KiB
Go
306 lines
9.6 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"cmsp/internal/config"
|
|
"cmsp/internal/huohanhan"
|
|
"cmsp/internal/logx"
|
|
"cmsp/internal/store"
|
|
|
|
"github.com/wailsapp/wails/v2/pkg/runtime"
|
|
)
|
|
|
|
// App 是暴露给前端的对象。
|
|
//
|
|
// 重要约定(会决定将来能不能顺利升级到 Wails v3):
|
|
//
|
|
// - 只有 main.go 和 app.go 这两个文件允许 import wails 包。
|
|
// internal/ 下面的业务代码一律不许依赖 wails,这样将来换框架
|
|
// 只用改这两个文件。
|
|
//
|
|
// - 本文件里的方法只做三件事:校验参数、调用 internal 里的业务代码、
|
|
// 把结果转成前端能用的形式。不要在这里写业务逻辑。
|
|
//
|
|
// - 每个公开方法都会被 Wails 生成成 JavaScript 函数给前端调用,
|
|
// 所以方法名和参数类型要稳定,改名等于改接口。
|
|
type App struct {
|
|
ctx context.Context
|
|
|
|
cfgPath string
|
|
cfg config.Config
|
|
|
|
db *store.Store
|
|
log *logx.Logger
|
|
}
|
|
|
|
// NewApp 创建应用对象。真正的初始化在 startup 里做。
|
|
func NewApp() *App {
|
|
return &App{
|
|
cfgPath: config.DefaultPath(),
|
|
cfg: config.Default(),
|
|
log: logx.New(2000),
|
|
}
|
|
}
|
|
|
|
// startup 在窗口创建后被 Wails 调用。
|
|
//
|
|
// 这里做的事要尽量少、尽量不出错:这时候界面已经显示了,
|
|
// 抛错的话使用者只会看到一个空白窗口,不知道发生了什么。
|
|
// 所以出错时记录到日志,让界面能打开,由使用者去「参数设置」里修。
|
|
func (a *App) startup(ctx context.Context) {
|
|
a.ctx = ctx
|
|
|
|
// 日志写入后推给前端,「运行日志」窗口就能实时刷新。
|
|
a.log.SetHandler(func(e logx.Entry) {
|
|
runtime.EventsEmit(ctx, "log:entry", e)
|
|
})
|
|
|
|
a.log.Info("cmsp 启动")
|
|
|
|
cfg, err := config.Load(a.cfgPath)
|
|
if err != nil {
|
|
// 配置读不出来不算致命:用默认配置继续,让使用者能进设置页修。
|
|
a.log.Error("读取配置失败,已使用默认配置:%v", err)
|
|
cfg = config.Default()
|
|
}
|
|
a.cfg = cfg
|
|
a.log.Info("配置文件:%s", a.cfgPath)
|
|
|
|
dbPath := store.DefaultPath()
|
|
db, err := store.Open(dbPath)
|
|
if err != nil {
|
|
// 数据库打不开就没法干活了,但仍然让窗口显示,
|
|
// 并把原因写在日志里,比直接闪退好排查。
|
|
a.log.Error("打开数据库失败:%v", err)
|
|
return
|
|
}
|
|
a.db = db
|
|
a.log.Success("数据库就绪:%s", dbPath)
|
|
}
|
|
|
|
// shutdown 在窗口关闭时被 Wails 调用,用来收尾。
|
|
func (a *App) shutdown(ctx context.Context) {
|
|
if a.db != nil {
|
|
if err := a.db.Close(); err != nil {
|
|
a.log.Warn("关闭数据库出错:%v", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------- 配置
|
|
|
|
// GetConfig 返回当前配置给「参数设置」页显示。
|
|
//
|
|
// 注意这里返回的是含密码的完整配置:设置页本来就要显示密码框,
|
|
// 前端不会把它写进日志。真正需要脱敏的是日志和工单。
|
|
func (a *App) GetConfig() config.Config {
|
|
return a.cfg
|
|
}
|
|
|
|
// SaveConfig 校验并保存配置。
|
|
//
|
|
// 返回的 error 会变成前端的 Promise reject,界面直接把消息弹出来,
|
|
// 所以 Validate 里的错误信息必须是能看懂的中文。
|
|
func (a *App) SaveConfig(cfg config.Config) error {
|
|
if err := config.Save(a.cfgPath, cfg); err != nil {
|
|
a.log.Error("保存配置失败:%v", err)
|
|
return err
|
|
}
|
|
a.cfg = cfg
|
|
a.log.Success("配置已保存")
|
|
return nil
|
|
}
|
|
|
|
// ResetConfig 把配置还原成默认值,但不写文件。
|
|
// 使用者还要点「保存设置」才会真正生效,避免手滑丢掉已填的账号。
|
|
func (a *App) ResetConfig() config.Config {
|
|
return config.Default()
|
|
}
|
|
|
|
// TestHuohanhanLogin 使用当前设置重新登录并在线验证认证状态。
|
|
// 设置页用它确认账号、密码、网址和 OCR 服务可以协同工作。
|
|
func (a *App) TestHuohanhanLogin() error {
|
|
if a.db == nil {
|
|
return fmt.Errorf("数据库未就绪,请查看运行日志")
|
|
}
|
|
ctx := a.ctx
|
|
if ctx == nil {
|
|
ctx = context.Background()
|
|
}
|
|
manager := huohanhan.NewAuthManager(a.cfg.Huohanhan, a.db, a.log, huohanhan.AuthOptions{})
|
|
_, err := manager.ForceLogin(ctx)
|
|
return err
|
|
}
|
|
|
|
// ---------------------------------------------------------------- 路径选择
|
|
|
|
// PickChromeExe 弹出文件选择框让使用者挑 chrome.exe。
|
|
//
|
|
// 用文件选择框而不是目录选择框,因为这个配置项指向的是文件本身。
|
|
// 使用者点了取消时返回空字符串,前端要判断一下再覆盖原值。
|
|
func (a *App) PickChromeExe() (string, error) {
|
|
return runtime.OpenFileDialog(a.ctx, runtime.OpenDialogOptions{
|
|
Title: "选择 chrome.exe",
|
|
DefaultDirectory: `C:\Program Files\Google\Chrome\Application`,
|
|
Filters: []runtime.FileFilter{
|
|
{DisplayName: "Chrome 可执行文件 (chrome.exe)", Pattern: "chrome.exe"},
|
|
{DisplayName: "可执行文件 (*.exe)", Pattern: "*.exe"},
|
|
},
|
|
})
|
|
}
|
|
|
|
// PickDirectory 弹出目录选择框,用于用户数据目录和视频保存目录。
|
|
func (a *App) PickDirectory(title string) (string, error) {
|
|
return runtime.OpenDirectoryDialog(a.ctx, runtime.OpenDialogOptions{
|
|
Title: title,
|
|
})
|
|
}
|
|
|
|
// ---------------------------------------------------------------- 商品
|
|
|
|
// ListProducts 按条件分页查询本地商品。
|
|
//
|
|
// 注意这里查的是本地 SQLite,不会去请求货憨憨。
|
|
// 只有点「下载数据」时才会真正联网。
|
|
func (a *App) ListProducts(query store.ProductQuery) (store.ProductPage, error) {
|
|
if a.db == nil {
|
|
return store.ProductPage{}, fmt.Errorf("数据库未就绪,请查看运行日志")
|
|
}
|
|
return a.db.ListProducts(query)
|
|
}
|
|
|
|
// CountProducts 返回本地商品总数。
|
|
func (a *App) CountProducts() (int, error) {
|
|
if a.db == nil {
|
|
return 0, fmt.Errorf("数据库未就绪,请查看运行日志")
|
|
}
|
|
return a.db.CountProducts()
|
|
}
|
|
|
|
// OpenInBrowser 用系统默认浏览器打开一个网址。
|
|
//
|
|
// 只允许 http/https,避免以后有人把本地文件路径或自定义协议传进来
|
|
// 变成一个可以启动任意程序的入口。
|
|
func (a *App) OpenInBrowser(rawURL string) error {
|
|
if !strings.HasPrefix(rawURL, "http://") && !strings.HasPrefix(rawURL, "https://") {
|
|
return fmt.Errorf("只能打开 http 或 https 地址")
|
|
}
|
|
runtime.BrowserOpenURL(a.ctx, rawURL)
|
|
return nil
|
|
}
|
|
|
|
// ---------------------------------------------------------------- 运行日志
|
|
|
|
// GetLogs 返回当前保留的全部日志,「运行日志」窗口打开时调用一次。
|
|
// 之后的新日志靠 log:entry 事件推送,不用轮询。
|
|
func (a *App) GetLogs() []logx.Entry {
|
|
return a.log.Entries()
|
|
}
|
|
|
|
// ClearLogs 清空日志。
|
|
func (a *App) ClearLogs() {
|
|
a.log.Clear()
|
|
}
|
|
|
|
// ExportLogs 让使用者选个位置,把日志存成 txt。
|
|
func (a *App) ExportLogs() (string, error) {
|
|
name := fmt.Sprintf("cmsp-日志-%s.txt", time.Now().Format("20060102-150405"))
|
|
path, err := runtime.SaveFileDialog(a.ctx, runtime.SaveDialogOptions{
|
|
Title: "导出运行日志",
|
|
DefaultFilename: name,
|
|
Filters: []runtime.FileFilter{
|
|
{DisplayName: "文本文件 (*.txt)", Pattern: "*.txt"},
|
|
},
|
|
})
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if path == "" {
|
|
// 使用者点了取消,不算错误。
|
|
return "", nil
|
|
}
|
|
if err := writeTextFile(path, a.log.Text()); err != nil {
|
|
a.log.Error("导出日志失败:%v", err)
|
|
return "", err
|
|
}
|
|
a.log.Success("日志已导出:%s", path)
|
|
return path, nil
|
|
}
|
|
|
|
// ---------------------------------------------------------------- 商品操作
|
|
|
|
// DownloadProductData 从货憨憨拉取商品列表到本地(需求 R1)。
|
|
func (a *App) DownloadProductData(platformShopID string) error {
|
|
if a.db == nil {
|
|
return fmt.Errorf("数据库未就绪,请查看运行日志")
|
|
}
|
|
if platformShopID == "" {
|
|
return fmt.Errorf("请先选择店铺")
|
|
}
|
|
|
|
client, err := a.newHuohanhanClient()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ctx := a.ctx
|
|
if ctx == nil {
|
|
ctx = context.Background()
|
|
}
|
|
a.log.Info("开始下载所选店铺的商品数据")
|
|
products, err := client.DownloadAllProducts(ctx, platformShopID, func(current, total int) {
|
|
a.log.Info("已拉取 %d/%d 页", current, total)
|
|
})
|
|
if err != nil {
|
|
a.log.Error("下载商品数据失败:%v", err)
|
|
return err
|
|
}
|
|
if err := a.db.UpsertProducts(products, time.Now().Format("2006-01-02 15:04:05")); err != nil {
|
|
a.log.Error("保存商品数据失败:%v", err)
|
|
return err
|
|
}
|
|
a.log.Success("商品数据下载完成,共拉取 %d 条", len(products))
|
|
return nil
|
|
}
|
|
|
|
// DownloadVideos 对选中的商品搜同款并下载视频(需求 R3、R4)。
|
|
func (a *App) DownloadVideos(productIDs []string) error {
|
|
return fmt.Errorf("「下载视频」还没实现,见工单 R3 和 R4")
|
|
}
|
|
|
|
// UploadVideos 把本地视频上传回货憨憨(需求 R5)。
|
|
func (a *App) UploadVideos(productIDs []string) error {
|
|
return fmt.Errorf("「上传数据」还没实现,见工单 R5")
|
|
}
|
|
|
|
// ListShops 返回当前账号可用的店铺(需求 R1)。
|
|
func (a *App) ListShops() ([]huohanhan.Shop, error) {
|
|
if a.db == nil {
|
|
return nil, fmt.Errorf("数据库未就绪,请查看运行日志")
|
|
}
|
|
client, err := a.newHuohanhanClient()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
ctx := a.ctx
|
|
if ctx == nil {
|
|
ctx = context.Background()
|
|
}
|
|
shops, err := client.ListShops(ctx)
|
|
if err != nil {
|
|
a.log.Error("加载店铺列表失败:%v", err)
|
|
return nil, err
|
|
}
|
|
return shops, nil
|
|
}
|
|
|
|
// newHuohanhanClient 使用当前配置创建业务客户端。
|
|
// 每次创建可确保设置页刚保存的账号或网址立即生效。
|
|
func (a *App) newHuohanhanClient() (*huohanhan.Client, error) {
|
|
manager := huohanhan.NewAuthManager(a.cfg.Huohanhan, a.db, a.log, huohanhan.AuthOptions{})
|
|
return huohanhan.NewClient(a.cfg.Huohanhan, manager, a.log, nil)
|
|
}
|