From 175218e29fc7f9c29e46124dd5d5745b25f6fe3a Mon Sep 17 00:00:00 2001 From: gxwebsoft Date: Mon, 16 Oct 2023 17:49:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E9=80=A0=E6=A0=B8=E5=BF=83=E6=A1=86?= =?UTF-8?q?=E6=9E=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/core/web/BaseController.java | 372 ++++++++++-------- .../system/controller/MainController.java | 15 +- .../system/controller/MenuController.java | 14 +- .../common/system/entity/Company.java | 7 + .../gxwebsoft/common/system/entity/User.java | 3 + .../common/system/param/MenuParam.java | 4 + .../system/service/EmailRecordService.java | 1 + .../service/impl/EmailRecordServiceImpl.java | 14 + .../system/service/impl/UserServiceImpl.java | 31 +- .../com/gxwebsoft/generator/SysGenerator.java | 4 +- 10 files changed, 250 insertions(+), 215 deletions(-) diff --git a/src/main/java/com/gxwebsoft/common/core/web/BaseController.java b/src/main/java/com/gxwebsoft/common/core/web/BaseController.java index e86d5a4..4e9c3a5 100644 --- a/src/main/java/com/gxwebsoft/common/core/web/BaseController.java +++ b/src/main/java/com/gxwebsoft/common/core/web/BaseController.java @@ -5,12 +5,16 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.gxwebsoft.common.core.Constants; import com.gxwebsoft.common.core.exception.BusinessException; +import com.gxwebsoft.common.system.entity.Company; import com.gxwebsoft.common.system.entity.User; +import com.gxwebsoft.common.system.mapper.CompanyMapper; +import com.gxwebsoft.common.system.service.CompanyService; import com.gxwebsoft.common.system.service.UserService; import org.springframework.beans.propertyeditors.StringTrimmerEditor; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.util.CollectionUtils; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.InitBinder; @@ -25,199 +29,221 @@ import java.util.List; * @since 2017-06-10 10:10:19 */ public class BaseController { - @Resource - private HttpServletRequest request; - @Resource - private StringRedisTemplate stringRedisTemplate; - @Resource - private UserService userService; + @Resource + private HttpServletRequest request; + @Resource + private StringRedisTemplate stringRedisTemplate; + @Resource + private UserService userService; + @Resource + private CompanyService companyService; - /** - * 获取当前登录的user - * - * @return User - */ - public User getLoginUser() { - try { - Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); - if (authentication != null) { - Object object = authentication.getPrincipal(); - if (object instanceof User) { - return (User) object; - } - } - } catch (Exception e) { - System.out.println(e.getMessage()); + /** + * 获取当前登录的user + * + * @return User + */ + public User getLoginUser() { + try { + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + if (authentication != null) { + Object object = authentication.getPrincipal(); + if (object instanceof User) { + return (User) object; } - return null; - } - - /** - * 获取当前登录的userId - * - * @return userId - */ - public Integer getLoginUserId() { - User loginUser = getLoginUser(); - return loginUser == null ? null : loginUser.getUserId(); - } - - /** - * 获取当前登录的tenantId - * - * @return tenantId - */ - public Integer getTenantId() { - // 从登录用户拿tenantId - User loginUser = getLoginUser(); - if (loginUser != null) { - return loginUser.getTenantId(); } - // 从请求头拿tenantId - if(StrUtil.isNotBlank(request.getHeader("tenantId"))){ - return Integer.valueOf(request.getHeader("tenantId")); - } - return null; + } catch (Exception e) { + System.out.println(e.getMessage()); } + return null; + } - /** - * 返回成功 - * - * @return ApiResult - */ - public ApiResult success() { - return new ApiResult<>(Constants.RESULT_OK_CODE, Constants.RESULT_OK_MSG); - } + /** + * 获取当前登录的userId + * + * @return userId + */ + public Integer getLoginUserId() { + User loginUser = getLoginUser(); + return loginUser == null ? null : loginUser.getUserId(); + } - /** - * 返回成功 - * - * @param message 状态信息 - * @return ApiResult - */ - public ApiResult success(String message) { - return success().setMessage(message); + /** + * 获取当前登录的tenantId + * + * @return tenantId + */ + public Integer getTenantId() { + // 从登录用户拿tenantId + User loginUser = getLoginUser(); + if (loginUser != null) { + return loginUser.getTenantId(); } + // 从请求头拿tenantId + if (StrUtil.isNotBlank(request.getHeader("tenantId"))) { + return Integer.valueOf(request.getHeader("tenantId")); + } + return null; + } - /** - * 返回成功 - * - * @param data 返回数据 - * @return ApiResult - */ - public ApiResult success(T data) { - return new ApiResult<>(Constants.RESULT_OK_CODE, Constants.RESULT_OK_MSG, data); + /** + * 获取当前登录的企业信息 + * + * @return Company + */ + public Company getCompany() { + List list = companyService.list(new LambdaQueryWrapper().eq(Company::getAuthoritative, 1)); + if (!CollectionUtils.isEmpty(list)) { + final Company company = list.get(0); + return company; } + return null; + } - /** - * 返回成功 - * - * @param message 状态信息 - * @return ApiResult - */ - public ApiResult success(String message, T data) { - return success(data).setMessage(message); - } + public Integer getCompanyId() { + Company company = getCompany(); + return company.getCompanyId(); + } - /** - * 返回分页查询数据 - * - * @param list 当前页数据 - * @param count 总数量 - * @return ApiResult - */ - public ApiResult> success(List list, Long count) { - return success(new PageResult<>(list, count)); - } + /** + * 返回成功 + * + * @return ApiResult + */ + public ApiResult success() { + return new ApiResult<>(Constants.RESULT_OK_CODE, Constants.RESULT_OK_MSG); + } - /** - * 返回分页查询数据 - * - * @param iPage IPage - * @return ApiResult - */ - public ApiResult> success(IPage iPage) { - return success(iPage.getRecords(), iPage.getTotal()); - } + /** + * 返回成功 + * + * @param message 状态信息 + * @return ApiResult + */ + public ApiResult success(String message) { + return success().setMessage(message); + } - /** - * 返回失败 - * - * @return ApiResult - */ - public ApiResult fail() { - return new ApiResult<>(Constants.RESULT_ERROR_CODE, Constants.RESULT_ERROR_MSG); - } + /** + * 返回成功 + * + * @param data 返回数据 + * @return ApiResult + */ + public ApiResult success(T data) { + return new ApiResult<>(Constants.RESULT_OK_CODE, Constants.RESULT_OK_MSG, data); + } - /** - * 返回失败 - * - * @param message 状态信息 - * @return ApiResult - */ - public ApiResult fail(String message) { - return fail().setMessage(message); - } + /** + * 返回成功 + * + * @param message 状态信息 + * @return ApiResult + */ + public ApiResult success(String message, T data) { + return success(data).setMessage(message); + } - /** - * 返回失败 - * - * @param data 返回数据 - * @return ApiResult - */ - public ApiResult fail(T data) { - return fail(Constants.RESULT_ERROR_MSG, data); - } + /** + * 返回分页查询数据 + * + * @param list 当前页数据 + * @param count 总数量 + * @return ApiResult + */ + public ApiResult> success(List list, Long count) { + return success(new PageResult<>(list, count)); + } - /** - * 返回失败 - * - * @param message 状态信息 - * @param data 返回数据 - * @return ApiResult - */ - public ApiResult fail(String message, T data) { - return new ApiResult<>(Constants.RESULT_ERROR_CODE, message, data); - } + /** + * 返回分页查询数据 + * + * @param iPage IPage + * @return ApiResult + */ + public ApiResult> success(IPage iPage) { + return success(iPage.getRecords(), iPage.getTotal()); + } - /** - * 请求参数的空字符串转为null - */ - @InitBinder - public void initBinder(WebDataBinder binder) { - binder.registerCustomEditor(String.class, new StringTrimmerEditor(true)); - } + /** + * 返回失败 + * + * @return ApiResult + */ + public ApiResult fail() { + return new ApiResult<>(Constants.RESULT_ERROR_CODE, Constants.RESULT_ERROR_MSG); + } - // 自定义函数 - public String getAuthorization(){ - return request.getHeader("Authorization"); - } + /** + * 返回失败 + * + * @param message 状态信息 + * @return ApiResult + */ + public ApiResult fail(String message) { + return fail().setMessage(message); + } - public String getSign() { - return request.getParameter("sign"); - } + /** + * 返回失败 + * + * @param data 返回数据 + * @return ApiResult + */ + public ApiResult fail(T data) { + return fail(Constants.RESULT_ERROR_MSG, data); + } - /** - * 根据账号|手机号码|邮箱查找用户ID - * @return userId - */ - public Integer getUserIdByUsername(String username, Integer tenantId){ - // 按账号搜素 - User user = userService.getOne(new LambdaQueryWrapper().eq(User::getUsername, username).eq(User::getTenantId,tenantId)); - if (user != null && user.getUserId() > 0) { - return user.getUserId(); - } - // 按手机号码搜索 - User userByPhone = userService.getOne(new LambdaQueryWrapper().eq(User::getPhone, username).eq(User::getTenantId, tenantId)); - if (userByPhone != null && userByPhone.getUserId() > 0) { - return userByPhone.getUserId(); - } - // 按邮箱搜索 - User userByEmail = userService.getOne(new LambdaQueryWrapper().eq(User::getEmail, username).eq(User::getTenantId, tenantId)); - if (userByEmail != null && userByEmail.getUserId() > 0) { - return userByEmail.getUserId(); - } - throw new BusinessException("找不到该用户"); + /** + * 返回失败 + * + * @param message 状态信息 + * @param data 返回数据 + * @return ApiResult + */ + public ApiResult fail(String message, T data) { + return new ApiResult<>(Constants.RESULT_ERROR_CODE, message, data); + } + + /** + * 请求参数的空字符串转为null + */ + @InitBinder + public void initBinder(WebDataBinder binder) { + binder.registerCustomEditor(String.class, new StringTrimmerEditor(true)); + } + + // 自定义函数 + public String getAuthorization() { + return request.getHeader("Authorization"); + } + + public String getSign() { + return request.getParameter("sign"); + } + + /** + * 根据账号|手机号码|邮箱查找用户ID + * + * @return userId + */ + public Integer getUserIdByUsername(String username, Integer tenantId) { + // 按账号搜素 + User user = userService.getOne(new LambdaQueryWrapper().eq(User::getUsername, username).eq(User::getTenantId, tenantId)); + if (user != null && user.getUserId() > 0) { + return user.getUserId(); } + // 按手机号码搜索 + User userByPhone = userService.getOne(new LambdaQueryWrapper().eq(User::getPhone, username).eq(User::getTenantId, tenantId)); + if (userByPhone != null && userByPhone.getUserId() > 0) { + return userByPhone.getUserId(); + } + // 按邮箱搜索 + User userByEmail = userService.getOne(new LambdaQueryWrapper().eq(User::getEmail, username).eq(User::getTenantId, tenantId)); + if (userByEmail != null && userByEmail.getUserId() > 0) { + return userByEmail.getUserId(); + } + throw new BusinessException("找不到该用户"); + } } diff --git a/src/main/java/com/gxwebsoft/common/system/controller/MainController.java b/src/main/java/com/gxwebsoft/common/system/controller/MainController.java index 9b50eb3..225d4ac 100644 --- a/src/main/java/com/gxwebsoft/common/system/controller/MainController.java +++ b/src/main/java/com/gxwebsoft/common/system/controller/MainController.java @@ -926,20 +926,9 @@ public class MainController extends BaseController { String title = "恭喜!您的账号已注册成功"; String content = "租户ID:".concat(tenant.getTenantId().toString()).concat("\r\n名称:" + tenantName).concat("\r\n账号:" + phone).concat("\r\n密码:" + password); String adminUrl = "\r\n后台管理:".concat("https://admin.gxwebsoft.com"); - sendEmail(title,content.concat(adminUrl),email); - return success("注册成功",tenant.getTenantId()); - } - - - private void sendEmail(String title, String content, String receiver) { // 发送邮件通知 - EmailRecord emailRecord = new EmailRecord(); - emailRecord.setTitle(title); - emailRecord.setContent(content); - emailRecord.setReceiver(receiver); - emailRecord.setCreateUserId(42); - emailRecordService.sendTextEmail(title,content,receiver.split(",")); - emailRecordService.save(emailRecord); + emailRecordService.sendEmail(title,content.concat(adminUrl),email); + return success("注册成功",tenant.getTenantId()); } // 缓存租户信息 diff --git a/src/main/java/com/gxwebsoft/common/system/controller/MenuController.java b/src/main/java/com/gxwebsoft/common/system/controller/MenuController.java index b2d7dbf..9b57669 100644 --- a/src/main/java/com/gxwebsoft/common/system/controller/MenuController.java +++ b/src/main/java/com/gxwebsoft/common/system/controller/MenuController.java @@ -1,13 +1,12 @@ package com.gxwebsoft.common.system.controller; -import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.gxwebsoft.common.core.annotation.OperationLog; import com.gxwebsoft.common.core.web.*; +import com.gxwebsoft.common.system.entity.Company; import com.gxwebsoft.common.system.entity.Menu; -import com.gxwebsoft.common.system.entity.Plug; import com.gxwebsoft.common.system.param.MenuParam; +import com.gxwebsoft.common.system.service.CompanyService; import com.gxwebsoft.common.system.service.MenuService; -import com.gxwebsoft.common.system.service.PlugService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.security.access.prepost.PreAuthorize; @@ -29,7 +28,7 @@ public class MenuController extends BaseController { @Resource private MenuService menuService; @Resource - private PlugService plugService; + private CompanyService companyService; @PreAuthorize("hasAuthority('sys:menu:list')") @OperationLog @@ -134,7 +133,12 @@ public class MenuController extends BaseController { @PostMapping("/clone") public ApiResult onClone(@RequestBody MenuParam param){ if(menuService.cloneMenu(param)){ - return success("克隆成功,请刷新"); + Integer companyId = getCompanyId(); + Company company = new Company(); + company.setCompanyId(companyId); + company.setPlanId(param.getTenantId()); + companyService.updateById(company); + return success("克隆成功"); } return fail("克隆失败"); } diff --git a/src/main/java/com/gxwebsoft/common/system/entity/Company.java b/src/main/java/com/gxwebsoft/common/system/entity/Company.java index 854019f..17715b1 100644 --- a/src/main/java/com/gxwebsoft/common/system/entity/Company.java +++ b/src/main/java/com/gxwebsoft/common/system/entity/Company.java @@ -133,6 +133,9 @@ public class Company implements Serializable { @ApiModelProperty(value = "排序") private Integer sortNumber; + @ApiModelProperty(value = "当前使用的租户模板") + private Integer planId; + @ApiModelProperty(value = "用户ID") private Integer userId; @@ -170,4 +173,8 @@ public class Company implements Serializable { @TableField(exist = false) private String nickname; + @ApiModelProperty(value = "是否已安装") + @TableField(exist = false) + private Boolean installed; + } diff --git a/src/main/java/com/gxwebsoft/common/system/entity/User.java b/src/main/java/com/gxwebsoft/common/system/entity/User.java index f1efda6..5d82c2a 100644 --- a/src/main/java/com/gxwebsoft/common/system/entity/User.java +++ b/src/main/java/com/gxwebsoft/common/system/entity/User.java @@ -123,9 +123,11 @@ public class User implements UserDetails { private Integer organizationId; @ApiModelProperty("客户ID") + @TableField(exist = false) private Integer customerId; @ApiModelProperty("企业ID") + @TableField(exist = false) private Integer companyId; @ApiModelProperty("注册来源客户端") @@ -191,6 +193,7 @@ public class User implements UserDetails { private Date updateTime; @ApiModelProperty("公司名称") + @TableField(exist = false) private String companyName; @ApiModelProperty("是否已实名认证") diff --git a/src/main/java/com/gxwebsoft/common/system/param/MenuParam.java b/src/main/java/com/gxwebsoft/common/system/param/MenuParam.java index 6596d0a..1cb7c7c 100644 --- a/src/main/java/com/gxwebsoft/common/system/param/MenuParam.java +++ b/src/main/java/com/gxwebsoft/common/system/param/MenuParam.java @@ -65,4 +65,8 @@ public class MenuParam extends BaseParam { @QueryField(type = QueryType.EQ) private Integer tenantId; + @ApiModelProperty("企业ID") + @QueryField(type = QueryType.EQ) + private Integer companyId; + } diff --git a/src/main/java/com/gxwebsoft/common/system/service/EmailRecordService.java b/src/main/java/com/gxwebsoft/common/system/service/EmailRecordService.java index 6b100c7..b99a195 100644 --- a/src/main/java/com/gxwebsoft/common/system/service/EmailRecordService.java +++ b/src/main/java/com/gxwebsoft/common/system/service/EmailRecordService.java @@ -47,4 +47,5 @@ public interface EmailRecordService extends IService { void sendHtmlEmail(String title, String path, Map map, String[] toEmails) throws MessagingException, IOException; + void sendEmail(String title, String content, String receiver); } diff --git a/src/main/java/com/gxwebsoft/common/system/service/impl/EmailRecordServiceImpl.java b/src/main/java/com/gxwebsoft/common/system/service/impl/EmailRecordServiceImpl.java index a939a3d..13ecae2 100644 --- a/src/main/java/com/gxwebsoft/common/system/service/impl/EmailRecordServiceImpl.java +++ b/src/main/java/com/gxwebsoft/common/system/service/impl/EmailRecordServiceImpl.java @@ -12,6 +12,7 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.MimeMessageHelper; +import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import javax.annotation.Resource; @@ -69,4 +70,17 @@ public class EmailRecordServiceImpl extends ServiceImpl implements Us @Resource private BCryptPasswordEncoder bCryptPasswordEncoder; @Resource + private CompanyService companyService; + @Resource private RedisUtil redisUtil; @Override @@ -79,24 +82,7 @@ public class UserServiceImpl extends ServiceImpl implements Us user.setAuthorities(roleMenuService.listMenuByUserId(user.getUserId(), null)); // 系统配置信息 Map map = new HashMap<>(); - // 1)基本信息 - final String setting = redisUtil.get("setting:setting:" + user.getTenantId()); - if(setting != null){ - final JSONObject site = JSONObject.parseObject(setting); - map.put("siteName",site.getString("siteName")); - map.put("remarks",site.getString("remarks")); - map.put("keyword",site.getString("keyword")); - map.put("icp",site.getString("icp")); - map.put("copyright",site.getString("copyright")); - map.put("company",site.getString("company")); - map.put("address",site.getString("address")); - map.put("phone",site.getString("phone")); - map.put("email",site.getString("email")); - map.put("domain",site.getString("domain")); - map.put("support",site.getString("support")); - map.put("logo",site.getString("logo")); - } - // 2)云存储 + // 1)云存储 String key = "setting:upload:" + user.getTenantId(); final String upload = redisUtil.get(key); if(upload != null){ @@ -107,10 +93,11 @@ public class UserServiceImpl extends ServiceImpl implements Us user.setSystem(map); } // 企业信息 - String key2 = "company:authoritative:" + user.getTenantId(); - final Company company = redisUtil.get(key2, Company.class); - user.setCompanyInfo(company); - user.setSystem(map); + final Company company = companyService.getByTenantIdRel(user.getTenantId()); + if (company != null) { + user.setCompanyId(company.getCompanyId()); + user.setCompanyInfo(company); + } } return user; } diff --git a/src/test/java/com/gxwebsoft/generator/SysGenerator.java b/src/test/java/com/gxwebsoft/generator/SysGenerator.java index 8d1b0dd..c353351 100644 --- a/src/test/java/com/gxwebsoft/generator/SysGenerator.java +++ b/src/test/java/com/gxwebsoft/generator/SysGenerator.java @@ -54,8 +54,8 @@ public class SysGenerator { // "sys_user_grade" // "sys_user_referee" // "sys_notice" - "sys_plug" - +// "sys_plug" + "sys_plug_record" }; // 需要去除的表前缀 private static final String[] TABLE_PREFIX = new String[]{