fix: store public source from blade homepage

master
weizong song 4 months ago
parent 88be92c1d6
commit b40bcceebb

@ -1,9 +1,5 @@
@php
$publicSourceCode = trim((string) request()->query('src', ''));
$signupUrl = 'admin/c/xxf/login';
if (preg_match('/^[A-Za-z0-9_-]{2,64}$/', $publicSourceCode) === 1) {
$signupUrl .= '?src='.rawurlencode($publicSourceCode);
}
@endphp
<!doctype html>
<html lang="zh-CN">
@ -1990,6 +1986,14 @@
}
}
</style>
@if ($publicSourceCode)
<script>
try {
localStorage.setItem('cxxfds_public_source_code', @json($publicSourceCode));
localStorage.setItem('cxxfds_public_source_checked_at', String(Date.now()));
} catch (e) {}
</script>
@endif
</head>
<body>
<div class="site">

@ -2,6 +2,7 @@
use App\Http\Controllers\Api\ApplicationFileController;
use App\Http\Controllers\ChannelEntryController;
use App\Models\PublicSourceChannel;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Route;
@ -17,7 +18,22 @@ use Illuminate\Support\Facades\Route;
*/
Route::get('/', function () {
return view('welcome');
$sourceCode = trim((string) request()->query('src', ''));
$publicSourceCode = null;
if (preg_match('/^[A-Za-z0-9_-]{2,64}$/', $sourceCode) === 1) {
try {
$publicSourceCode = PublicSourceChannel::query()
->where('source_code', $sourceCode)
->where('status', PublicSourceChannel::STATUS_ENABLED)
->value('source_code');
} catch (Throwable) {
$publicSourceCode = null;
}
}
return view('welcome', [
'publicSourceCode' => $publicSourceCode,
]);
});
Route::get('/channel-entry', ChannelEntryController::class)->name('channel.entry');

@ -2,7 +2,11 @@
namespace Tests\Feature;
use App\Models\PublicSourceChannel;
// use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Tests\TestCase;
class ExampleTest extends TestCase
@ -22,13 +26,48 @@ class ExampleTest extends TestCase
public function test_homepage_signup_link_preserves_public_source(): void
{
config([
'database.default' => 'sqlite',
'database.connections.sqlite' => [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
'foreign_key_constraints' => false,
],
]);
DB::purge();
DB::reconnect();
Schema::create('public_source_channels', function (Blueprint $table) {
$table->id();
$table->string('source_code', 64)->unique();
$table->string('source_name', 100);
$table->string('status')->default(PublicSourceChannel::STATUS_ENABLED);
$table->timestamps();
});
PublicSourceChannel::query()->create([
'source_code' => 'poster_A',
'source_name' => '海报 A',
'status' => PublicSourceChannel::STATUS_ENABLED,
]);
PublicSourceChannel::query()->create([
'source_code' => 'closed_src',
'source_name' => '停用渠道',
'status' => PublicSourceChannel::STATUS_DISABLED,
]);
$this->get('/?src=poster_A')
->assertOk()
->assertSee('href="admin/c/xxf/login?src=poster_A"', false);
->assertSee('href="admin/c/xxf/login"', false)
->assertSee("localStorage.setItem('cxxfds_public_source_code'", false)
->assertSee('poster_A');
$this->get('/?src=bad%20code')
->assertOk()
->assertSee('href="admin/c/xxf/login"', false)
->assertDontSee('href="admin/c/xxf/login?src=', false);
->assertDontSee("localStorage.setItem('cxxfds_public_source_code'", false);
$this->get('/?src=closed_src')
->assertOk()
->assertDontSee("localStorage.setItem('cxxfds_public_source_code'", false);
}
}

Loading…
Cancel
Save