Files
dpl/plugins/VipLoyalty/Settings.php
T
hellcat 0501711267 feat(fuc-xb): prepare isolated staging theme and VIP auto upgrades
Port the Fuc 0.12.0 frontend and default-off VIP plugin. Include the previously approved password-reset token revocation. Deploy only to an independent FUC-XB test instance; no production cutover.
2026-11-04 13:44:33 +08:00

32 lines
1.8 KiB
PHP

<?php
namespace Plugin\VipLoyalty;
use App\Models\Plugin;
use Illuminate\Support\Facades\Validator;
final class Settings {
public const DEFAULTS=['enabled'=>false,'tier1_amount'=>100,'tier1_discount'=>8,'tier2_amount'=>200,'tier2_discount'=>12,'tier3_amount'=>360,'tier3_discount'=>15];
public function values(): array {
$plugin=Plugin::where('code','vip_loyalty')->first();
$values=array_replace(self::DEFAULTS,json_decode($plugin?->config??'{}',true)?:[]);
$values['enabled']=(bool)($plugin?->is_enabled) && filter_var($values['enabled'],FILTER_VALIDATE_BOOL);
return $values['enabled']?$this->validate($values):$values;
}
public function validate(array $values): array {
$values=array_replace(self::DEFAULTS,$values);
$rules=['enabled'=>'required|boolean'];
for($i=1;$i<=3;$i++) {
$rules['tier'.$i.'_amount']='required|numeric|min:0.01|max:100000000|decimal:0,2';
$rules['tier'.$i.'_discount']='required|integer|min:0|max:100';
}
Validator::make($values,$rules)->after(function($validator)use($values){
if($validator->errors()->isNotEmpty())return;
for($i=2;$i<=3;$i++)if((float)$values['tier'.$i.'_amount']<=(float)$values['tier'.($i-1).'_amount'] || (int)$values['tier'.$i.'_discount']<(int)$values['tier'.($i-1).'_discount'])$validator->errors()->add('tiers','VIP门槛须递增,优惠不能降低');
})->validate();
return array_intersect_key($values,self::DEFAULTS);
}
public function levels(?array $values=null): array {
$values??=$this->values();$levels=[];
for($i=1;$i<=3;$i++)$levels[]=['level'=>$i,'name'=>[1=>'小福',2=>'中福',3=>'大福'][$i],'threshold_cents'=>(int)round((float)$values['tier'.$i.'_amount']*100),'discount'=>(int)$values['tier'.$i.'_discount']];
return $levels;
}
}