55 lines
1.9 KiB
PHP
55 lines
1.9 KiB
PHP
<?php
|
||
|
||
namespace Database\Seeders;
|
||
|
||
use App\Models\WxAppModel;
|
||
use Illuminate\Database\Seeder;
|
||
|
||
/**
|
||
* 小程序应用配置引导(nl_wx_app)
|
||
*
|
||
* 用途:单品牌部署首次上线时,把 .env 里声明的 AppID 引导落库一条记录,
|
||
* 免得运维还要进后台手工新建。AppSecret 不能从 .env 读,留空由后台填。
|
||
*
|
||
* 执行:php artisan db:seed --class=WxAppSeeder
|
||
* 不挂 DatabaseSeeder::run(),避免 CI / 自动测试里污染库表。
|
||
*/
|
||
class WxAppSeeder extends Seeder
|
||
{
|
||
public function run(): void
|
||
{
|
||
$code = trim((string) env('WX_DEFAULT_APP_CODE', ''));
|
||
$appId = trim((string) env('WX_APP_ID', ''));
|
||
$name = trim((string) env('WX_APP_NAME', ''));
|
||
|
||
// .env 没配就不跑,避免插空记录
|
||
if ($code === '' || $appId === '') {
|
||
$this->command->warn('WX_DEFAULT_APP_CODE / WX_APP_ID 未配置,跳过 WxAppSeeder。');
|
||
return;
|
||
}
|
||
|
||
// 用 firstOrCreate 防重复:已存在相同 code 的记录就只更新 AppID/名称,不动密钥
|
||
// 不直接写 app_secret —— 密钥必须经后台加密入库
|
||
$row = WxAppModel::where('code', $code)->where('deleted_at', 0)->first();
|
||
if (!empty($row)) {
|
||
$row->update([
|
||
'app_id' => $appId,
|
||
'name' => $name !== '' ? $name : $row->name,
|
||
'updated_at' => time(),
|
||
]);
|
||
$this->command->info("已更新品牌 [{$code}] 的 AppID 为 {$appId},AppSecret 未改动。");
|
||
return;
|
||
}
|
||
|
||
WxAppModel::create([
|
||
'code' => $code,
|
||
'name' => $name,
|
||
'app_id' => $appId,
|
||
'status' => 0,
|
||
'created_at' => time(),
|
||
'updated_at' => time(),
|
||
]);
|
||
$this->command->info("已新增品牌 [{$code}] 应用记录,请进后台「小程序应用配置」填写 AppSecret。");
|
||
}
|
||
}
|