From 3985b97f2e8d62717de855e53c9270e06be22742 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Thu, 20 Oct 2022 10:02:53 +0800 Subject: [PATCH] feat(web/user): qq avatar --- db/app_uoj233.sql | 1 + web/app/controllers/user_info_edit.php | 20 ++++++++++----- web/app/libs/uoj-form-lib.php | 34 +++++++++++++++++++++++++ web/app/locale/user/en.php | 1 + web/app/locale/user/zh-cn.php | 1 + web/app/models/HTML.php | 14 ++++++++++ web/app/upgrade/6_user_info_v2/down.sql | 2 ++ web/app/upgrade/6_user_info_v2/up.sql | 1 + 8 files changed, 67 insertions(+), 7 deletions(-) diff --git a/db/app_uoj233.sql b/db/app_uoj233.sql index fce837f..aa59101 100644 --- a/db/app_uoj233.sql +++ b/db/app_uoj233.sql @@ -819,6 +819,7 @@ CREATE TABLE `user_info` ( `codeforces_handle` 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 '', + `avatar_source` varchar(10) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'gravatar', PRIMARY KEY (`username`), KEY `ac_num` (`ac_num`,`username`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4; diff --git a/web/app/controllers/user_info_edit.php b/web/app/controllers/user_info_edit.php index b1354b9..1ab650e 100644 --- a/web/app/controllers/user_info_edit.php +++ b/web/app/controllers/user_info_edit.php @@ -44,17 +44,22 @@ $update_profile_form = new UOJForm('update_profile'); $username = UOJLocale::get('username'); $avatar = UOJLocale::get('avatar'); - $change_avatar_help = UOJLocale::get('change avatar help'); $update_profile_form->appendHTML(<<
用户名不能被修改。
-
-
$avatar
-
$change_avatar_help
-
+EOD); + $update_profile_form->addVCheckboxes('avatar_source', [ + 'gravatar' => 'Gravatar', + 'qq' => 'QQ', + ], UOJLocale::get('user::avatar source'), $user['avatar_source']); + $change_avatar_help = UOJLocale::get('change avatar help'); + $update_profile_form->appendHTML(<< + $change_avatar_help + EOD); $update_profile_form->addVInput('email', 'email', UOJLocale::get('email'), $user['email'], function($email, &$vdata) { @@ -103,7 +108,7 @@ EOD); EOD); } - $update_profile_form->addVSelect('sex', [ + $update_profile_form->addVCheckboxes('sex', [ 'U' => UOJLocale::get('refuse to answer'), 'M' => UOJLocale::get('male'), 'F' => UOJLocale::get('female'), @@ -146,6 +151,7 @@ EOD); $esc_motto = DB::escape($vdata['motto']); $esc_codeforces_handle = DB::escape($vdata['codeforces_handle']); $esc_website = DB::escape($vdata['website']); + $esc_avatar_source = DB::escape($_POST['avatar_source']); if (isSuperUser($myUser)) { $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 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['text'] = '更新'; diff --git a/web/app/libs/uoj-form-lib.php b/web/app/libs/uoj-form-lib.php index 16d272a..897111a 100644 --- a/web/app/libs/uoj-form-lib.php +++ b/web/app/libs/uoj-form-lib.php @@ -225,6 +225,40 @@ EOD; EOD; $this->addNoVal($name, $html); } + public function addVCheckboxes($name, $options, $label_text, $default_value) { + $default_value = htmlspecialchars($default_value); + $html = << + +EOD; + foreach ($options as $opt_name => $opt_label) { + $html .= << +EOD; + if ($opt_name != $default_value) { + $html .= << +EOD; + } else { + $html .= << +EOD; + } + $html .= <<$opt_label + +EOD; + } + $html .= << +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) { $default_value = htmlspecialchars($default_value); global $REQUIRE_LIB; diff --git a/web/app/locale/user/en.php b/web/app/locale/user/en.php index 281cb57..0d00af9 100644 --- a/web/app/locale/user/en.php +++ b/web/app/locale/user/en.php @@ -1,5 +1,6 @@ 'Avatar source', 'website' => 'Website', 'user type' => 'User type', 'admin' => 'Admin', diff --git a/web/app/locale/user/zh-cn.php b/web/app/locale/user/zh-cn.php index 449014c..0a6da0d 100644 --- a/web/app/locale/user/zh-cn.php +++ b/web/app/locale/user/zh-cn.php @@ -1,5 +1,6 @@ '头像来源', 'website' => '网址', 'user type' => '用户类型', 'admin' => '管理员', diff --git a/web/app/models/HTML.php b/web/app/models/HTML.php index 6bc9981..547e678 100644 --- a/web/app/models/HTML.php +++ b/web/app/models/HTML.php @@ -8,6 +8,20 @@ class HTML { return strip_tags($str); } 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&s=$size"; } diff --git a/web/app/upgrade/6_user_info_v2/down.sql b/web/app/upgrade/6_user_info_v2/down.sql index c6767c4..ea11898 100644 --- a/web/app/upgrade/6_user_info_v2/down.sql +++ b/web/app/upgrade/6_user_info_v2/down.sql @@ -1,2 +1,4 @@ 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 `website`; +ALTER TABLE `user_info` DROP COLUMN IF EXISTS `avatar_source`; diff --git a/web/app/upgrade/6_user_info_v2/up.sql b/web/app/upgrade/6_user_info_v2/up.sql index 416ffbf..830ca6f 100644 --- a/web/app/upgrade/6_user_info_v2/up.sql +++ b/web/app/upgrade/6_user_info_v2/up.sql @@ -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 `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 `avatar_source` varchar(10) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'gravatar';