feat(avatar): 学校默认头像样式端点三件套(默认头像轮后端)

- SchSettingsController:GET schAvatarStyle(登录可读,null=未配置)/ POST saveSchAvatarStyle(校管理员)/ POST clearSchAvatarStyle
- SchSettings 实体 + sql/sch_settings_avatar_style.sql:avatar_style 列(DDL 待人工执行)
前端配套=UserHome/front e2c937f(登录写 localStorage[yh-avatar-sch-default] + 设置页 avatar pane)
This commit is contained in:
chenxing
2026-10-06 20:21:02 +08:00
parent a3aa6d917e
commit 564403b5ca
3 changed files with 72 additions and 0 deletions
@@ -31,6 +31,9 @@ import java.util.Optional;
* logo 等字段不能删行,置空后读侧返回 null,效果等同"未配置")。存储形态:theme_name 列存紧凑四段编码
* {@code theme|sideDeep01|gradOn01|sideFollow01}(如 teal|0|1|1;不拆列、零 DDL,列宽未确认故不存整份 JSON;
* 旧格式值/非法值读侧一律视同未配置)。</p>
* <p>默认头像轮(2026-09-30)同构新增学校默认头像样式三接口:schAvatarStyle 读(可空不兜底);
* saveSchAvatarStyle 写(仅学校管理员/超管,值只接受 text|silhouette);clearSchAvatarStyle 清空(置空
* avatar_style 字段)。</p>
*/
@RestController
@RequestMapping(value = Config.API_PATH)
@@ -39,6 +42,9 @@ public class SchSettingsController {
/** 合法档位(与通道 THEME_NAMES 一致;防旧库脏值意外生效) */
private static final String[] THEME_KEYS = {"blue", "ocean", "ink", "violet", "teal"};
/** 合法头像样式(与前端 avatar.ts 的 AvatarStyle 一致;写入侧从严,对齐 encodeThemeState 做法) */
private static final String[] AVATAR_STYLES = {"text", "silhouette"};
@Autowired
private ISchSettingsService schSettingsService;
@@ -117,6 +123,62 @@ public class SchSettingsController {
return false;
}
private static boolean isAvatarStyle(String style) {
if (style == null) return false;
for (String k : AVATAR_STYLES) {
if (k.equals(style)) return true;
}
return false;
}
/**
* 学校默认头像样式(默认头像轮 2026-09-30,已登录可读):{avatarStyle: "text"|"silhouette"|null, schId}。
* 未配置/脏值 → avatarStyle=null(读侧可空不兜底,对齐 schTheme 语义;前端"不写键、不报错")。
*/
@GetMapping("schAvatarStyle")
public Map<String, Object> schAvatarStyle(BoaUser authUserWithDept) {
Integer schId = resolveThemeSchId(authUserWithDept, null);
String avatarStyle = schSettingsService.findBySchIdRaw(schId)
.map(SchSettings::getAvatarStyle)
.filter(SchSettingsController::isAvatarStyle)
.orElse(null);
Map<String, Object> out = new HashMap<>();
out.put("avatarStyle", avatarStyle);
out.put("schId", schId);
return out;
}
/** 保存学校默认头像样式(仅学校管理员/超管):值只接受 "text"/"silhouette",非法抛 DException。 */
@PostMapping("saveSchAvatarStyle")
public void saveSchAvatarStyle(BoaUser authUserWithDept,
@RequestParam("avatarStyle") String avatarStyle,
@RequestParam(name = "schId", required = false) Integer schId) {
requireThemeAdmin(authUserWithDept);
if (!isAvatarStyle(avatarStyle)) {
throw new DException("头像样式不合法: " + avatarStyle);
}
Integer targetSchId = resolveThemeSchId(authUserWithDept, schId);
SchSettings schSettings = schSettingsService.findBySchIdRaw(targetSchId).orElseGet(SchSettings::new);
schSettings.setCDate(new Date());
schSettings.setSchId(targetSchId);
schSettings.setCId(authUserWithDept.getId());
schSettings.setAvatarStyle(avatarStyle);
schSettingsService.save(schSettings);
}
/** 清空学校默认头像样式(仅管理员):置空 avatar_style 字段(行还承载 logo/theme 等配置不能删行,置空后读侧返回 null)。 */
@PostMapping("clearSchAvatarStyle")
public void clearSchAvatarStyle(BoaUser authUserWithDept,
@RequestParam(name = "schId", required = false) Integer schId) {
requireThemeAdmin(authUserWithDept);
Integer targetSchId = resolveThemeSchId(authUserWithDept, schId);
Optional<SchSettings> existing = schSettingsService.findBySchIdRaw(targetSchId);
if (!existing.isPresent()) return;
SchSettings schSettings = existing.get();
schSettings.setAvatarStyle(null);
schSettingsService.save(schSettings);
}
/**
* 学校默认主题档(已登录可读):{themeState: "<ThemeState JSON>" | null, schId}。
* 未配置/旧格式值/脏数据 → themeState=null(前端"不写键、不报错")。
@@ -30,6 +30,10 @@ public class SchSettings {
@Column(name = "theme_name")
private String themeName;
/** 学校默认头像样式(默认头像轮 2026-09-30):text|silhouette;null=未配置(读侧可空不兜底) */
@Column(name = "avatar_style")
private String avatarStyle;
@Column(name = "logo_url")
private String logoUrl;
@@ -0,0 +1,6 @@
-- 学校默认头像样式(默认头像轮 2026-09-30):sch_settings 新增 avatar_style 列。
-- dev 环境由 JPA ddl-auto=update 自动加列;测试/生产等 ddl-auto 关闭的环境手工执行本文件。
ALTER TABLE sch_settings ADD COLUMN avatar_style VARCHAR(16) NULL COMMENT '学校默认头像样式 text|silhouette';
-- 回滚:
-- ALTER TABLE sch_settings DROP COLUMN avatar_style;