系统设置页现已支持设置Google analysis、Microsoft Clarify

*这两个分析服务的实际应用会在后续加入
This commit is contained in:
Chenx221 2024-04-01 14:08:01 +08:00
parent 6c35ec4170
commit 96c60d7bad
Signed by: chenx221
GPG Key ID: D7A9EC07024C3021
3 changed files with 51 additions and 5 deletions

View File

@ -10,7 +10,6 @@ use yii\base\Model;
* Class SiteConfig * Class SiteConfig
* 配置信息 * 配置信息
* 少数配置不在列表中,如数据库配置等 * 少数配置不在列表中,如数据库配置等
* TODO: 实现Google analysis、Microsoft Clarity统计
*/ */
class SiteConfig extends Model class SiteConfig extends Model
{ {
@ -27,13 +26,18 @@ class SiteConfig extends Model
public string $turnstileSecret; // Turnstile Secret public string $turnstileSecret; // Turnstile Secret
public bool $enableIpinfo; // 启用 ipinfo.io 查询 public bool $enableIpinfo; // 启用 ipinfo.io 查询
public string $ipinfoToken; // IPinfo Token public string $ipinfoToken; // IPinfo Token
public bool $clarityEnabled; // 启用 Clarity
public string $clarityId; // Clarity ID
public bool $gaEnabled; // 启用 Google Analytics
public string $gaId; // Google Analytics ID
public function rules(): array public function rules(): array
{ {
return [ return [
[['siteTitle', 'domain'], 'required'], [['siteTitle', 'domain'], 'required'],
[['siteTitle', 'domain', 'verifyProvider', 'recaptchaSiteKey', 'recaptchaSecret', 'hcaptchaSiteKey', 'hcaptchaSecret', 'turnstileSiteKey', 'turnstileSecret', 'ipinfoToken'], 'string'], [['siteTitle', 'domain', 'verifyProvider', 'recaptchaSiteKey', 'recaptchaSecret', 'hcaptchaSiteKey', 'hcaptchaSecret', 'turnstileSiteKey', 'turnstileSecret', 'ipinfoToken', 'clarityId', 'gaId'], 'string'],
[['registrationEnabled', 'enableIpinfo'], 'boolean'], [['registrationEnabled', 'enableIpinfo', 'clarityEnabled', 'gaEnabled'], 'boolean'],
['verifyProvider', 'in', 'range' => ['reCAPTCHA', 'hCaptcha', 'Turnstile', 'None']], ['verifyProvider', 'in', 'range' => ['reCAPTCHA', 'hCaptcha', 'Turnstile', 'None']],
[['recaptchaSiteKey', 'recaptchaSecret'], 'required', 'when' => function ($model) { [['recaptchaSiteKey', 'recaptchaSecret'], 'required', 'when' => function ($model) {
return $model->verifyProvider == 'reCAPTCHA'; return $model->verifyProvider == 'reCAPTCHA';
@ -55,6 +59,16 @@ class SiteConfig extends Model
}, 'whenClient' => "function (attribute, value) { }, 'whenClient' => "function (attribute, value) {
return $('#siteconfig-enableipinfo').is(':checked'); return $('#siteconfig-enableipinfo').is(':checked');
}"], }"],
['clarityId', 'required', 'when' => function ($model) {
return $model->clarityEnabled;
}, 'whenClient' => "function (attribute, value) {
return $('#siteconfig-clarityenabled').is(':checked');
}"],
['gaId', 'required', 'when' => function ($model) {
return $model->gaEnabled;
}, 'whenClient' => "function (attribute, value) {
return $('#siteconfig-gaenabled').is(':checked');
}"],
]; ];
} }
@ -73,8 +87,13 @@ class SiteConfig extends Model
'turnstileSecret' => 'Turnstile Secret', 'turnstileSecret' => 'Turnstile Secret',
'enableIpinfo' => '启用 ipinfo.io 查询', 'enableIpinfo' => '启用 ipinfo.io 查询',
'ipinfoToken' => 'IPinfo Token', 'ipinfoToken' => 'IPinfo Token',
'clarityEnabled' => '启用 Clarity',
'clarityId' => 'Clarity ID',
'gaEnabled' => '启用 Google Analytics',
'gaId' => 'Google Analytics ID',
]; ];
} }
public function attributeHelpTexts(): array public function attributeHelpTexts(): array
{ {
return [ return [
@ -90,8 +109,13 @@ class SiteConfig extends Model
'turnstileSecret' => '请在这里填入Turnstile Secret', 'turnstileSecret' => '请在这里填入Turnstile Secret',
'enableIpinfo' => '是否使用<a href=\'https://ipinfo.io/\' target=\'_blank\'>ipinfo.io</a>查询站点上的ip信息', 'enableIpinfo' => '是否使用<a href=\'https://ipinfo.io/\' target=\'_blank\'>ipinfo.io</a>查询站点上的ip信息',
'ipinfoToken' => '请在这里填入IPinfo Token', 'ipinfoToken' => '请在这里填入IPinfo Token',
'clarityEnabled' => '是否启用<a href=\'https://clarity.microsoft.com/\' target=\'_blank\'>Microsoft Clarity</a>',
'clarityId' => '请在这里填入Clarity ID',
'gaEnabled' => '是否启用<a href=\'https://analytics.google.com/\' target=\'_blank\'>Google Analytics</a>',
'gaId' => '请在这里填入Google Analytics ID',
]; ];
} }
/** /**
* 读取配置信息 * 读取配置信息
* @return bool * @return bool
@ -111,6 +135,10 @@ class SiteConfig extends Model
$this->turnstileSecret = $_ENV['TURNSTILE_SECRET']; $this->turnstileSecret = $_ENV['TURNSTILE_SECRET'];
$this->enableIpinfo = $_ENV['ENABLE_IPINFO'] === 'true'; $this->enableIpinfo = $_ENV['ENABLE_IPINFO'] === 'true';
$this->ipinfoToken = $_ENV['IPINFO_TOKEN']; $this->ipinfoToken = $_ENV['IPINFO_TOKEN'];
$this->clarityEnabled = $_ENV['CLARITY_ENABLED'] === 'true';
$this->clarityId = $_ENV['CLARITY_ID'];
$this->gaEnabled = $_ENV['GA_ENABLED'] === 'true';
$this->gaId = $_ENV['GA_ID'];
return true; return true;
} catch (Exception) { } catch (Exception) {
return false; return false;
@ -140,6 +168,10 @@ class SiteConfig extends Model
$env['TURNSTILE_SECRET'] = $this->turnstileSecret; $env['TURNSTILE_SECRET'] = $this->turnstileSecret;
$env['ENABLE_IPINFO'] = $this->enableIpinfo ? 'true' : 'false'; $env['ENABLE_IPINFO'] = $this->enableIpinfo ? 'true' : 'false';
$env['IPINFO_TOKEN'] = $this->ipinfoToken; $env['IPINFO_TOKEN'] = $this->ipinfoToken;
$env['CLARITY_ENABLED'] = $this->clarityEnabled ? 'true' : 'false';
$env['CLARITY_ID'] = $this->clarityId;
$env['GA_ENABLED'] = $this->gaEnabled ? 'true' : 'false';
$env['GA_ID'] = $this->gaId;
$data = array_map(function ($key, $value) { $data = array_map(function ($key, $value) {
return "$key=$value"; return "$key=$value";
}, array_keys($env), $env); }, array_keys($env), $env);

View File

@ -28,7 +28,7 @@ $this->title = '系统设置';
foreach ($attributes as $attribute) { foreach ($attributes as $attribute) {
echo '<tr id="tr-' . $attribute . '">'; echo '<tr id="tr-' . $attribute . '">';
echo '<th style="width: 200px"><p>' . $siteConfig->attributeLabels()[$attribute] . '</p></th>'; echo '<th style="width: 200px"><p>' . $siteConfig->attributeLabels()[$attribute] . '</p></th>';
if ($attribute == 'registrationEnabled' || $attribute == 'enableIpinfo') { if ($attribute == 'registrationEnabled' || $attribute == 'enableIpinfo' || $attribute == 'clarityEnabled' || $attribute == 'gaEnabled') {
echo '<td>' . $form->field($siteConfig, $attribute)->checkbox(['class' => 'form-check-input'], false)->label(false) . '</td>'; echo '<td>' . $form->field($siteConfig, $attribute)->checkbox(['class' => 'form-check-input'], false)->label(false) . '</td>';
} elseif ($attribute == 'verifyProvider') { } elseif ($attribute == 'verifyProvider') {
echo '<td>' . $form->field($siteConfig, $attribute)->dropDownList(['None' => 'None', 'reCAPTCHA' => 'reCAPTCHA', 'hCaptcha' => 'hCaptcha', 'Turnstile' => 'Turnstile'], ['class' => 'form-select form-select-sm', 'style' => 'width:25em'])->label(false) . '</td>'; echo '<td>' . $form->field($siteConfig, $attribute)->dropDownList(['None' => 'None', 'reCAPTCHA' => 'reCAPTCHA', 'hCaptcha' => 'hCaptcha', 'Turnstile' => 'Turnstile'], ['class' => 'form-select form-select-sm', 'style' => 'width:25em'])->label(false) . '</td>';

View File

@ -6,12 +6,20 @@ $(document).ready(function () {
$('#siteconfig-enableipinfo').change(function () { $('#siteconfig-enableipinfo').change(function () {
updateTableRowVisibility(); updateTableRowVisibility();
}); });
$('#siteconfig-clarityenabled').change(function () {
updateTableRowVisibility();
});
$('#siteconfig-gaenabled').change(function () {
updateTableRowVisibility();
});
}); });
function updateTableRowVisibility() { function updateTableRowVisibility() {
let currentValue = $('#siteconfig-verifyprovider').val(); let currentValue = $('#siteconfig-verifyprovider').val();
let ipinfoEnable = $('#siteconfig-enableipinfo').prop('checked'); let ipinfoEnable = $('#siteconfig-enableipinfo').prop('checked');
$('#tr-recaptchaSiteKey, #tr-recaptchaSecret, #tr-hcaptchaSiteKey, #tr-hcaptchaSecret, #tr-turnstileSiteKey, #tr-turnstileSecret,#tr-ipinfoToken').hide(); let clarityEnable = $('#siteconfig-clarityenabled').prop('checked');
let gaEnable = $('#siteconfig-gaenabled').prop('checked');
$('#tr-recaptchaSiteKey, #tr-recaptchaSecret, #tr-hcaptchaSiteKey, #tr-hcaptchaSecret, #tr-turnstileSiteKey, #tr-turnstileSecret,#tr-ipinfoToken,#tr-clarityId,#tr-gaId').hide();
if (currentValue === 'reCAPTCHA') { if (currentValue === 'reCAPTCHA') {
$('#tr-recaptchaSiteKey, #tr-recaptchaSecret').show(); $('#tr-recaptchaSiteKey, #tr-recaptchaSecret').show();
} else if (currentValue === 'hCaptcha') { } else if (currentValue === 'hCaptcha') {
@ -22,4 +30,10 @@ function updateTableRowVisibility() {
if (ipinfoEnable) { if (ipinfoEnable) {
$('#tr-ipinfoToken').show(); $('#tr-ipinfoToken').show();
} }
if (clarityEnable) {
$('#tr-clarityId').show();
}
if (gaEnable) {
$('#tr-gaId').show();
}
} }