# uniapp小程序分包一键处理脚本
# 此脚本将完成: 1.创建目录 2.移动文件 3.更新路径

$projectRoot = "D:\zhongshuweilai\zswl.zip\zswl\code\charge_miniapp\charge_miniapp"

Write-Host "=" * 60 -ForegroundColor Cyan
Write-Host "uniapp小程序分包处理脚本" -ForegroundColor Cyan  
Write-Host "=" * 60 -ForegroundColor Cyan

# ========== 第一步: 创建分包目录 ==========
Write-Host "`n[1/3] 开始创建分包目录结构..." -ForegroundColor Green

$subPackagesRoot = Join-Path $projectRoot "subPackages"
if (!(Test-Path $subPackagesRoot)) {
    New-Item -ItemType Directory -Path $subPackagesRoot -Force | Out-Null
    Write-Host "  ✓ 创建目录: subPackages" -ForegroundColor Yellow
}

$subpackages = @("charging", "order", "coupon", "other")
foreach ($pkg in $subpackages) {
    $path = Join-Path $subPackagesRoot $pkg
    if (!(Test-Path $path)) {
        New-Item -ItemType Directory -Path $path -Force | Out-Null
        Write-Host "  ✓ 创建目录: subPackages\$pkg" -ForegroundColor Yellow
    }
}

# ========== 第二步: 移动页面文件 ==========
Write-Host "`n[2/3] 开始移动页面文件..." -ForegroundColor Green

$moveMap = @{
    "charging" = @("site", "site-more", "charging", "terminal", "new-site")
    "order" = @("order", "order-detail")
    "coupon" = @("coupon-buy", "coupons", "recharge-log")
    "other" = @("feedback", "feedback-reply", "search", "web", "banner-page")
}

$movedCount = 0
foreach ($package in $moveMap.Keys) {
    foreach ($pageName in $moveMap[$package]) {
        $sourcePath = Join-Path $projectRoot "pages\$pageName"
        $targetPath = Join-Path $subPackagesRoot "$package\$pageName"
        
        if (Test-Path $sourcePath) {
            Move-Item -Path $sourcePath -Destination $targetPath -Force
            Write-Host "  ✓ 移动: $pageName -> subPackages\$package\$pageName" -ForegroundColor Cyan
            $movedCount++
        } else {
            Write-Host "  ✗ 警告: 源目录不存在 pages\$pageName" -ForegroundColor Red
        }
    }
}

Write-Host "  完成移动 $movedCount 个页面目录" -ForegroundColor Green

# ========== 第三步: 更新路径引用 ==========
Write-Host "`n[3/3] 开始更新路径引用..." -ForegroundColor Green

# 定义路径映射关系
$pathMapping = @{
    "/pages/site/" = "/subPackages/charging/site/"
    "'/pages/site/" = "'/subPackages/charging/site/"
    
    "/pages/site-more/" = "/subPackages/charging/site-more/"
    "'/pages/site-more/" = "'/subPackages/charging/site-more/"
    
    "/pages/charging/" = "/subPackages/charging/charging/"
    "'/pages/charging/" = "'/subPackages/charging/charging/"
    
    "/pages/terminal/" = "/subPackages/charging/terminal/"
    "'/pages/terminal/" = "'/subPackages/charging/terminal/"
    
    "/pages/new-site/" = "/subPackages/charging/new-site/"
    "'/pages/new-site/" = "'/subPackages/charging/new-site/"
    
    "/pages/order/" = "/subPackages/order/order/"
    "'/pages/order/" = "'/subPackages/order/order/"
    
    "/pages/order-detail/" = "/subPackages/order/order-detail/"
    "'/pages/order-detail/" = "'/subPackages/order/order-detail/"
    
    "/pages/coupon-buy/" = "/subPackages/coupon/coupon-buy/"
    "'/pages/coupon-buy/" = "'/subPackages/coupon/coupon-buy/"
    
    "/pages/coupons/" = "/subPackages/coupon/coupons/"
    "'/pages/coupons/" = "'/subPackages/coupon/coupons/"
    
    "/pages/recharge-log/" = "/subPackages/coupon/recharge-log/"
    "'/pages/recharge-log/" = "'/subPackages/coupon/recharge-log/"
    
    "/pages/feedback/" = "/subPackages/other/feedback/"
    "'/pages/feedback/" = "'/subPackages/other/feedback/"
    
    "/pages/feedback-reply/" = "/subPackages/other/feedback-reply/"
    "'/pages/feedback-reply/" = "'/subPackages/other/feedback-reply/"
    
    "/pages/search/" = "/subPackages/other/search/"
    "'/pages/search/" = "'/subPackages/other/search/"
    
    "/pages/web/" = "/subPackages/other/web/"
    "'/pages/web/" = "'/subPackages/other/web/"
    
    "/pages/banner-page/" = "/subPackages/other/banner-page/"
    "'/pages/banner-page/" = "'/subPackages/other/banner-page/"
}

# 获取所有vue文件 (pages, subPackages, components目录)
$searchPaths = @("pages", "subPackages", "components")
$vueFiles = @()
foreach ($searchPath in $searchPaths) {
    $fullPath = Join-Path $projectRoot $searchPath
    if (Test-Path $fullPath) {
        $vueFiles += Get-ChildItem -Path $fullPath -Filter "*.vue" -Recurse
    }
}

$totalFiles = $vueFiles.Count
$modifiedFiles = 0

foreach ($file in $vueFiles) {
    $content = Get-Content -Path $file.FullName -Raw -Encoding UTF8
    $originalContent = $content
    $fileModified = $false
    
    # 替换所有路径
    foreach ($oldPath in $pathMapping.Keys) {
        if ($content -like "*$oldPath*") {
            $content = $content -replace [regex]::Escape($oldPath), $pathMapping[$oldPath]
            $fileModified = $true
        }
    }
    
    # 如果内容有变化,写回文件
    if ($fileModified) {
        Set-Content -Path $file.FullName -Value $content -Encoding UTF8 -NoNewline
        $relativePath = $file.FullName.Replace($projectRoot, "").TrimStart("\")
        Write-Host "  ✓ 已修改: $relativePath" -ForegroundColor Cyan
        $modifiedFiles++
    }
}

Write-Host "  共检查 $totalFiles 个文件,修改了 $modifiedFiles 个文件" -ForegroundColor Green

# ========== 完成 ==========
Write-Host "`n" + ("=" * 60) -ForegroundColor Cyan
Write-Host "分包处理完成!" -ForegroundColor Green
Write-Host ("=" * 60) -ForegroundColor Cyan

Write-Host "`n处理总结:" -ForegroundColor Yellow
Write-Host "  ✓ 创建了 4 个分包目录" -ForegroundColor White
Write-Host "  ✓ 移动了 $movedCount 个页面" -ForegroundColor White
Write-Host "  ✓ 更新了 $modifiedFiles 个文件的路径引用" -ForegroundColor White

Write-Host "`n下一步操作:" -ForegroundColor Yellow
Write-Host "  1. 使用HBuilderX或微信开发者工具编译项目" -ForegroundColor White
Write-Host "  2. 检查是否有编译错误" -ForegroundColor White
Write-Host "  3. 测试各个页面跳转是否正常" -ForegroundColor White
Write-Host "  4. 查看包体积是否满足微信小程序上传要求" -ForegroundColor White

Write-Host "`n按任意键退出..." -ForegroundColor Gray
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
