feat(website): 完善应用类型管理和标识符唯一性校验
- 移除邀请用户接口的权限注解和操作日志注解 - 更新CmsWebsite实体类中的类型描述,支持多种应用类型 - 在保存和修改网站信息时增加websiteCode唯一性校验逻辑 - 实现create方法中根据应用类型自动设置管理后台地址和类型名称 - 添加generateUniqueCode方法确保websiteCode全局唯一 - 优化代码结构和业务逻辑处理
This commit is contained in:
@@ -67,8 +67,6 @@ public class AppUserController extends BaseController {
|
||||
return fail("添加失败");
|
||||
}
|
||||
|
||||
@PreAuthorize("hasAuthority('app:appUser:save')")
|
||||
@OperationLog
|
||||
@Operation(summary = "邀请用户成为应用成员")
|
||||
@PostMapping("/invite")
|
||||
public ApiResult<?> invite(@RequestBody AppUser appUser) {
|
||||
|
||||
@@ -96,6 +96,16 @@ public class CmsWebsiteController extends BaseController {
|
||||
@Operation(summary = "添加网站信息记录表")
|
||||
@PostMapping()
|
||||
public ApiResult<?> save(@RequestBody CmsWebsite cmsWebsite) {
|
||||
// 前端若指定了 websiteCode,先做唯一性校验
|
||||
if (StrUtil.isNotBlank(cmsWebsite.getWebsiteCode())) {
|
||||
long cnt = cmsWebsiteService.count(
|
||||
new LambdaQueryWrapper<CmsWebsite>()
|
||||
.eq(CmsWebsite::getWebsiteCode, cmsWebsite.getWebsiteCode())
|
||||
);
|
||||
if (cnt > 0) {
|
||||
return fail("应用标识 [" + cmsWebsite.getWebsiteCode() + "] 已存在,请更换");
|
||||
}
|
||||
}
|
||||
// 记录当前登录用户id
|
||||
User loginUser = getLoginUser();
|
||||
if (loginUser != null) {
|
||||
@@ -109,6 +119,20 @@ public class CmsWebsiteController extends BaseController {
|
||||
@Operation(summary = "修改网站信息记录表")
|
||||
@PutMapping()
|
||||
public ApiResult<?> update(@RequestBody CmsWebsite cmsWebsite) {
|
||||
// websiteCode 全局唯一,有值时校验是否与其他记录冲突(排除自身)
|
||||
if (StrUtil.isNotBlank(cmsWebsite.getWebsiteCode())) {
|
||||
long cnt = cmsWebsiteService.count(
|
||||
new LambdaQueryWrapper<CmsWebsite>()
|
||||
.eq(CmsWebsite::getWebsiteCode, cmsWebsite.getWebsiteCode())
|
||||
.ne(CmsWebsite::getWebsiteId, cmsWebsite.getWebsiteId())
|
||||
);
|
||||
if (cnt > 0) {
|
||||
return fail("应用标识 [" + cmsWebsite.getWebsiteCode() + "] 已存在,请更换");
|
||||
}
|
||||
} else {
|
||||
// 不允许通过此接口清空或修改 websiteCode(设为 null 则 MP 不更新该字段)
|
||||
cmsWebsite.setWebsiteCode(null);
|
||||
}
|
||||
if (cmsWebsiteService.updateById(cmsWebsite)) {
|
||||
return success("修改成功");
|
||||
}
|
||||
|
||||
@@ -66,7 +66,7 @@ public class CmsWebsite implements Serializable {
|
||||
@Schema(description = "网站截图")
|
||||
private String files;
|
||||
|
||||
@Schema(description = "网站类型 10企业官网 20微信小程序 30APP 40其他")
|
||||
@Schema(description = "应用类型 10=web(Web应用) 20=miniprogram(小程序) 30=mobile(移动App) 40=api(API服务) 50=internal(内部工具)")
|
||||
private Integer type;
|
||||
|
||||
@Schema(description = "网站关键词")
|
||||
|
||||
@@ -145,15 +145,45 @@ public class CmsWebsiteServiceImpl extends ServiceImpl<CmsWebsiteMapper, CmsWebs
|
||||
@Override
|
||||
public CmsWebsite create(CmsWebsite website) {
|
||||
final User loginUser = website.getLoginUser();
|
||||
// 创建站点
|
||||
// website.setWebsiteName("网站名称");
|
||||
website.setWebsiteCode("site-".concat(loginUser.getTenantId().toString()));
|
||||
// if (StrUtil.isBlank(website.getWebsiteLogo())) {
|
||||
// website.setWebsiteLogo("https://oss.wsdns.cn/20240822/0252ad4ed46449cdafe12f8d3d96c2ea.svg");
|
||||
// }
|
||||
// 生成全局唯一 websiteCode:若调用方已指定则保留,否则自动生成
|
||||
if (StrUtil.isBlank(website.getWebsiteCode())) {
|
||||
website.setWebsiteCode(generateUniqueCode("template-" + loginUser.getTenantId()));
|
||||
}
|
||||
website.setWebsiteIcon("/favicon.ico");
|
||||
website.setWebsiteType("云·企业官网");
|
||||
website.setAdminUrl("site.websoft.top");
|
||||
// 根据应用类型设置管理后台地址和 websiteType 字符串
|
||||
// 10=web(Web应用) | 20=miniprogram(小程序) | 30=mobile(移动App) | 40=api(API服务) | 50=internal(内部工具)
|
||||
Integer siteType = website.getType();
|
||||
if (siteType == null) siteType = 10;
|
||||
String adminUrl;
|
||||
String websiteTypeName;
|
||||
switch (siteType) {
|
||||
case 20:
|
||||
adminUrl = "https://mp.websoft.top";
|
||||
websiteTypeName = "miniprogram";
|
||||
break;
|
||||
case 30:
|
||||
adminUrl = "https://app.websoft.top";
|
||||
websiteTypeName = "mobile";
|
||||
break;
|
||||
case 40:
|
||||
adminUrl = "https://api.websoft.top";
|
||||
websiteTypeName = "api";
|
||||
break;
|
||||
case 50:
|
||||
adminUrl = "https://oa.websoft.top";
|
||||
websiteTypeName = "internal";
|
||||
break;
|
||||
case 10:
|
||||
default:
|
||||
adminUrl = "https://site.websoft.top";
|
||||
websiteTypeName = "web";
|
||||
break;
|
||||
}
|
||||
website.setAdminUrl(adminUrl);
|
||||
// 若前端已传 websiteType 则优先使用,否则用 type 推导值
|
||||
if (StrUtil.isBlank(website.getWebsiteType())) {
|
||||
website.setWebsiteType(websiteTypeName);
|
||||
}
|
||||
website.setVersion(10);
|
||||
website.setExpirationTime(java.util.Date.from(LocalDateTime.now().plusMonths(1)
|
||||
.atZone(java.time.ZoneId.systemDefault()).toInstant()));
|
||||
@@ -174,7 +204,7 @@ public class CmsWebsiteServiceImpl extends ServiceImpl<CmsWebsiteMapper, CmsWebs
|
||||
|
||||
// 将网站创建者的userId做为查询条件 10257(4716),10324(6978),10398(26564)
|
||||
Integer websiteUserId = website.getTemplateId();
|
||||
|
||||
|
||||
// 只有当templateId存在时才执行复制操作
|
||||
if (websiteUserId != null && websiteUserId > 0) {
|
||||
// TODO 国际化
|
||||
@@ -432,4 +462,22 @@ public class CmsWebsiteServiceImpl extends ServiceImpl<CmsWebsiteMapper, CmsWebs
|
||||
List<CmsNavigation> bottomNavs = cmsNavigationService.listRel(navigationParam);
|
||||
website.setBottomNavs(bottomNavs);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成全局唯一的 websiteCode。
|
||||
* <p>以 baseCode 为基础,若已存在则追加 -2、-3 … 直到找到空闲值。</p>
|
||||
*
|
||||
* @param baseCode 期望的 code 前缀,如 "site-10398"
|
||||
* @return 全局唯一的 websiteCode
|
||||
*/
|
||||
private String generateUniqueCode(String baseCode) {
|
||||
String candidate = baseCode;
|
||||
int suffix = 2;
|
||||
while (count(new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<CmsWebsite>()
|
||||
.eq(CmsWebsite::getWebsiteCode, candidate)) > 0) {
|
||||
candidate = baseCode + "-" + suffix;
|
||||
suffix++;
|
||||
}
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user