@@ -0,0 +1,95 @@
|
||||
# 生成演示数据(便于看效果、试用手感;可对空库反复运行)
|
||||
# 用法: powershell -NoProfile -ExecutionPolicy Bypass -File scripts\seed.ps1 -Base http://127.0.0.1:18080
|
||||
param(
|
||||
[string]$Base = 'http://127.0.0.1:18080',
|
||||
[string]$User = 'admin',
|
||||
[string]$Pass = 'daili123'
|
||||
)
|
||||
|
||||
$ErrorActionPreference = 'Stop'
|
||||
$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
|
||||
|
||||
function Send {
|
||||
param([string]$Method, [string]$Path, $Body)
|
||||
$p = @{ Uri = "$Base$Path"; WebSession = $session; UseBasicParsing = $true; MaximumRedirection = 5; Method = $Method }
|
||||
if ($null -ne $Body) { $p.Body = $Body }
|
||||
try { Invoke-WebRequest @p } catch [System.Net.WebException] { $null }
|
||||
}
|
||||
|
||||
$now = Get-Date
|
||||
function D([int]$offset) { $now.AddDays($offset).ToString('yyyy-MM-dd') }
|
||||
|
||||
# 登录
|
||||
$r = Send 'GET' '/setup'
|
||||
if ($r -and $r.Content -match '创建第一个管理员') {
|
||||
Send 'POST' '/setup' @{ username = $User; display_name = '老板'; password = $Pass; password2 = $Pass } | Out-Null
|
||||
} else {
|
||||
Send 'POST' '/login' @{ username = $User; password = $Pass; next = '/calendar' } | Out-Null
|
||||
}
|
||||
|
||||
# 套餐(1、2 已存在则跳过创建)
|
||||
$pkgs = @(
|
||||
@{ name = '包月日常'; unit = 'day'; amount = 30; price = 300; color = '#2F5D50'; note = '每天日常 + 周常' },
|
||||
@{ name = '深渊十次'; unit = 'count'; amount = 10; price = 180; color = '#6B4E9B'; note = '按次结算,打完扣次' },
|
||||
@{ name = '赛季深渊'; unit = 'day'; amount = 90; price = 900; color = '#B45309'; note = '整赛季包干' },
|
||||
@{ name = '每周副本'; unit = 'count'; amount = 5; price = 120; color = '#41556B'; note = '每周 5 次' }
|
||||
)
|
||||
foreach ($p in $pkgs) {
|
||||
Send 'POST' '/packages' @{
|
||||
name = $p.name; unit = $p.unit; default_amount = "$($p.amount)"
|
||||
price = "$($p.price)"; color = $p.color; note = $p.note; sort_order = '1'
|
||||
} | Out-Null
|
||||
}
|
||||
|
||||
# 客户 + 订阅
|
||||
$demo = @(
|
||||
@{ tail = '1204'; name = '小林'; pkg = 1; start = -12; days = 30; note = '日常 + 周常' },
|
||||
@{ tail = '5533'; name = '阿飞'; pkg = 1; start = -28; days = 30; note = '快到期,提醒续费' },
|
||||
@{ tail = '9088'; name = '老王'; pkg = 1; start = -30; days = 30; note = '今天到期' },
|
||||
@{ tail = '4321'; name = '妹妹'; pkg = 3; start = -60; days = 90; note = '整赛季' },
|
||||
@{ tail = '7788'; name = '大鹏'; pkg = 2; start = -5; count = 10; end = 3; note = '按次' },
|
||||
@{ tail = '6677'; name = '猫猫'; pkg = 4; start = -2; count = 5; end = 20; note = '每周副本' },
|
||||
@{ tail = '3321'; name = '阿豪'; pkg = 1; start = -40; days = 30; note = '已过期两周' },
|
||||
@{ tail = '9900'; name = '小雨'; pkg = 3; start = 2; days = 90; note = '还没开始' }
|
||||
)
|
||||
|
||||
foreach ($d in $demo) {
|
||||
$r = Send 'POST' '/customers' @{
|
||||
tail = $d.tail; name = $d.name; game_name = '王者荣耀'; game_server = '安卓微信区'
|
||||
contact = ('wx: ' + $d.tail); tags = '老客户'; source = '朋友介绍'; note = $d.note
|
||||
}
|
||||
$path = $r.BaseResponse.ResponseUri.AbsolutePath
|
||||
if ($path -notlike '/customers/*') { continue }
|
||||
$id = ($path -split '/')[-1]
|
||||
if ($d.days) {
|
||||
Send 'POST' "/customers/$id/subscriptions" @{
|
||||
package_id = "$($d.pkg)"; start_date = (D $d.start); days = "$($d.days)"; note = $d.note
|
||||
} | Out-Null
|
||||
} else {
|
||||
Send 'POST' "/customers/$id/subscriptions" @{
|
||||
package_id = "$($d.pkg)"; start_date = (D $d.start); total_count = "$($d.count)"; end_date = (D $d.end); note = $d.note
|
||||
} | Out-Null
|
||||
}
|
||||
}
|
||||
|
||||
# 今天的两条打卡
|
||||
$r = Send 'GET' '/customers?q=1204'
|
||||
if ($r.Content -match '/customers/(\d+)') {
|
||||
$cid = $matches[1]
|
||||
$r2 = Send 'GET' "/customers/$cid"
|
||||
$ids = [regex]::Matches($r2.Content, 'id="sub-(\d+)"') | ForEach-Object { $_.Groups[1].Value }
|
||||
if ($ids.Count -gt 0) {
|
||||
Send 'POST' "/subscriptions/$($ids[0])/checkin" @{ date = (D 0); status = 'done'; note = '今日已打'; back = '/calendar' } | Out-Null
|
||||
}
|
||||
}
|
||||
$r = Send 'GET' '/customers?q=4321'
|
||||
if ($r.Content -match '/customers/(\d+)') {
|
||||
$cid = $matches[1]
|
||||
$r2 = Send 'GET' "/customers/$cid"
|
||||
$ids = [regex]::Matches($r2.Content, 'id="sub-(\d+)"') | ForEach-Object { $_.Groups[1].Value }
|
||||
if ($ids.Count -gt 0) {
|
||||
Send 'POST' "/subscriptions/$($ids[0])/checkin" @{ date = (D 0); status = 'done'; note = '深渊打完'; back = '/calendar' } | Out-Null
|
||||
}
|
||||
}
|
||||
|
||||
Write-Host '演示数据已写入。' -ForegroundColor Green
|
||||
Reference in New Issue
Block a user