feat(web/user): qq avatar

This commit is contained in:
Baoshuo Ren 2022-10-20 10:02:53 +08:00
parent 6ee3ed2490
commit 3985b97f2e
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A
8 changed files with 67 additions and 7 deletions

View File

@ -819,6 +819,7 @@ CREATE TABLE `user_info` (
`codeforces_handle` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', `codeforces_handle` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`github` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', `github` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`website` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', `website` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`avatar_source` varchar(10) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'gravatar',
PRIMARY KEY (`username`), PRIMARY KEY (`username`),
KEY `ac_num` (`ac_num`,`username`) KEY `ac_num` (`ac_num`,`username`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4; ) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4;

View File

@ -44,17 +44,22 @@
$update_profile_form = new UOJForm('update_profile'); $update_profile_form = new UOJForm('update_profile');
$username = UOJLocale::get('username'); $username = UOJLocale::get('username');
$avatar = UOJLocale::get('avatar'); $avatar = UOJLocale::get('avatar');
$change_avatar_help = UOJLocale::get('change avatar help');
$update_profile_form->appendHTML(<<<EOD $update_profile_form->appendHTML(<<<EOD
<div class="mb-3"> <div class="mb-3">
<label for="input-username" class="form-label">$username</label> <label for="input-username" class="form-label">$username</label>
<input type="text" class="form-control" id="input-username" aria-describedby="help-username" value="{$user['username']}" disabled> <input type="text" class="form-control" id="input-username" aria-describedby="help-username" value="{$user['username']}" disabled>
<div id="help-username" class="form-text">用户名不能被修改。</div> <div id="help-username" class="form-text">用户名不能被修改。</div>
</div> </div>
<div class="mb-3"> EOD);
<div>$avatar</div> $update_profile_form->addVCheckboxes('avatar_source', [
<div class="mt-1 small text-muted">$change_avatar_help</div> 'gravatar' => 'Gravatar',
</div> 'qq' => 'QQ',
], UOJLocale::get('user::avatar source'), $user['avatar_source']);
$change_avatar_help = UOJLocale::get('change avatar help');
$update_profile_form->appendHTML(<<<EOD
<div style="margin-top: -1.25rem;" class="mb-3 small text-muted">
$change_avatar_help
</div>
EOD); EOD);
$update_profile_form->addVInput('email', 'email', UOJLocale::get('email'), $user['email'], $update_profile_form->addVInput('email', 'email', UOJLocale::get('email'), $user['email'],
function($email, &$vdata) { function($email, &$vdata) {
@ -103,7 +108,7 @@ EOD);
</div> </div>
EOD); EOD);
} }
$update_profile_form->addVSelect('sex', [ $update_profile_form->addVCheckboxes('sex', [
'U' => UOJLocale::get('refuse to answer'), 'U' => UOJLocale::get('refuse to answer'),
'M' => UOJLocale::get('male'), 'M' => UOJLocale::get('male'),
'F' => UOJLocale::get('female'), 'F' => UOJLocale::get('female'),
@ -146,6 +151,7 @@ EOD);
$esc_motto = DB::escape($vdata['motto']); $esc_motto = DB::escape($vdata['motto']);
$esc_codeforces_handle = DB::escape($vdata['codeforces_handle']); $esc_codeforces_handle = DB::escape($vdata['codeforces_handle']);
$esc_website = DB::escape($vdata['website']); $esc_website = DB::escape($vdata['website']);
$esc_avatar_source = DB::escape($_POST['avatar_source']);
if (isSuperUser($myUser)) { if (isSuperUser($myUser)) {
$esc_school = DB::escape($vdata['school']); $esc_school = DB::escape($vdata['school']);
@ -153,7 +159,7 @@ EOD);
DB::update("UPDATE user_info SET school = '$esc_school' WHERE username = '{$user['username']}'"); DB::update("UPDATE user_info SET school = '$esc_school' WHERE username = '{$user['username']}'");
} }
DB::update("UPDATE user_info SET email = '$esc_email', qq = '$esc_qq', sex = '$esc_sex', motto = '$esc_motto', codeforces_handle = '$esc_codeforces_handle', github = '$esc_github', website = '$esc_website' WHERE username = '{$user['username']}'"); DB::update("UPDATE user_info SET email = '$esc_email', qq = '$esc_qq', sex = '$esc_sex', motto = '$esc_motto', codeforces_handle = '$esc_codeforces_handle', github = '$esc_github', website = '$esc_website', avatar_source = '$esc_avatar_source' WHERE username = '{$user['username']}'");
}; };
$update_profile_form->submit_button_config['margin_class'] = 'mt-3'; $update_profile_form->submit_button_config['margin_class'] = 'mt-3';
$update_profile_form->submit_button_config['text'] = '更新'; $update_profile_form->submit_button_config['text'] = '更新';

View File

@ -225,6 +225,40 @@ EOD;
EOD; EOD;
$this->addNoVal($name, $html); $this->addNoVal($name, $html);
} }
public function addVCheckboxes($name, $options, $label_text, $default_value) {
$default_value = htmlspecialchars($default_value);
$html = <<<EOD
<div id="div-$name" class="input-group mb-3">
<label class="form-label me-3">$label_text</label>
EOD;
foreach ($options as $opt_name => $opt_label) {
$html .= <<<EOD
<div class="form-check ms-2">
EOD;
if ($opt_name != $default_value) {
$html .= <<<EOD
<input class="form-check-input" type="radio" id="input-$name-$opt_name" name="$name" value="$opt_name">
EOD;
} else {
$html .= <<<EOD
<input class="form-check-input" type="radio" id="input-$name-$opt_name" name="$name" value="$opt_name" checked="checked">
EOD;
}
$html .= <<<EOD
<label class="form-check-label" for="input-$name-$opt_name">$opt_label</label>
</div>
EOD;
}
$html .= <<<EOD
</div>
EOD;
$this->add($name, $html,
function($opt) use ($options) {
return isset($options[$opt]) ? '' : "无效选项";
},
null
);
}
public function addCKEditor($name, $label_text, $default_value, $validator_php, $validator_js) { public function addCKEditor($name, $label_text, $default_value, $validator_php, $validator_js) {
$default_value = htmlspecialchars($default_value); $default_value = htmlspecialchars($default_value);
global $REQUIRE_LIB; global $REQUIRE_LIB;

View File

@ -1,5 +1,6 @@
<?php <?php
return [ return [
'avatar source' => 'Avatar source',
'website' => 'Website', 'website' => 'Website',
'user type' => 'User type', 'user type' => 'User type',
'admin' => 'Admin', 'admin' => 'Admin',

View File

@ -1,5 +1,6 @@
<?php <?php
return [ return [
'avatar source' => '头像来源',
'website' => '网址', 'website' => '网址',
'user type' => '用户类型', 'user type' => '用户类型',
'admin' => '管理员', 'admin' => '管理员',

View File

@ -8,6 +8,20 @@ class HTML {
return strip_tags($str); return strip_tags($str);
} }
public static function avatar_addr($user, $size) { public static function avatar_addr($user, $size) {
if ($user['avatar_source'] == 'qq' && $user['qq']) {
$s = '5';
if ($size <= 40) {
$s = '2';
} elseif ($size <= 100) {
$s = '3';
} elseif ($size <= 140) {
$s = '4';
}
return "https://q1.qlogo.cn/g?b=qq&nk={$user['qq']}&s=$s";
}
return '//gravatar.loli.net/avatar/' . md5(strtolower(trim($user['email']))) . "?d=mm&amp;s=$size"; return '//gravatar.loli.net/avatar/' . md5(strtolower(trim($user['email']))) . "?d=mm&amp;s=$size";
} }

View File

@ -1,2 +1,4 @@
ALTER TABLE `user_info` DROP COLUMN IF EXISTS `codeforces_handle`; ALTER TABLE `user_info` DROP COLUMN IF EXISTS `codeforces_handle`;
ALTER TABLE `user_info` DROP COLUMN IF EXISTS `github`; ALTER TABLE `user_info` DROP COLUMN IF EXISTS `github`;
ALTER TABLE `user_info` DROP COLUMN IF EXISTS `website`;
ALTER TABLE `user_info` DROP COLUMN IF EXISTS `avatar_source`;

View File

@ -1,3 +1,4 @@
ALTER TABLE `user_info` ADD COLUMN `codeforces_handle` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT ''; ALTER TABLE `user_info` ADD COLUMN `codeforces_handle` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '';
ALTER TABLE `user_info` ADD COLUMN `github` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT ''; ALTER TABLE `user_info` ADD COLUMN `github` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '';
ALTER TABLE `user_info` ADD COLUMN `website` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT ''; ALTER TABLE `user_info` ADD COLUMN `website` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '';
ALTER TABLE `user_info` ADD COLUMN `avatar_source` varchar(10) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'gravatar';