改造核心框架

This commit is contained in:
gxwebsoft
2023-10-16 17:49:10 +08:00
parent 3352569518
commit 175218e29f
10 changed files with 250 additions and 215 deletions

View File

@@ -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 <T> ApiResult<T> success(T data) {
return new ApiResult<>(Constants.RESULT_OK_CODE, Constants.RESULT_OK_MSG, data);
/**
* 获取当前登录的企业信息
*
* @return Company
*/
public Company getCompany() {
List<Company> list = companyService.list(new LambdaQueryWrapper<Company>().eq(Company::getAuthoritative, 1));
if (!CollectionUtils.isEmpty(list)) {
final Company company = list.get(0);
return company;
}
return null;
}
/**
* 返回成功
*
* @param message 状态信息
* @return ApiResult
*/
public <T> ApiResult<T> 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 <T> ApiResult<PageResult<T>> success(List<T> 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 <T> ApiResult<PageResult<T>> success(IPage<T> 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 <T> ApiResult<T> 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 <T> ApiResult<T> success(String message, T data) {
return success(data).setMessage(message);
}
/**
* 返回失败
*
* @param data 返回数据
* @return ApiResult
*/
public <T> ApiResult<T> fail(T data) {
return fail(Constants.RESULT_ERROR_MSG, data);
}
/**
* 返回分页查询数据
*
* @param list 当前页数据
* @param count 总数量
* @return ApiResult
*/
public <T> ApiResult<PageResult<T>> success(List<T> list, Long count) {
return success(new PageResult<>(list, count));
}
/**
* 返回失败
*
* @param message 状态信息
* @param data 返回数据
* @return ApiResult
*/
public <T> ApiResult<T> fail(String message, T data) {
return new ApiResult<>(Constants.RESULT_ERROR_CODE, message, data);
}
/**
* 返回分页查询数据
*
* @param iPage IPage
* @return ApiResult
*/
public <T> ApiResult<PageResult<T>> success(IPage<T> 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 <T> ApiResult<T> 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<User>().eq(User::getUsername, username).eq(User::getTenantId,tenantId));
if (user != null && user.getUserId() > 0) {
return user.getUserId();
}
// 按手机号码搜索
User userByPhone = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getPhone, username).eq(User::getTenantId, tenantId));
if (userByPhone != null && userByPhone.getUserId() > 0) {
return userByPhone.getUserId();
}
// 按邮箱搜索
User userByEmail = userService.getOne(new LambdaQueryWrapper<User>().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 <T> ApiResult<T> 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<User>().eq(User::getUsername, username).eq(User::getTenantId, tenantId));
if (user != null && user.getUserId() > 0) {
return user.getUserId();
}
// 按手机号码搜索
User userByPhone = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getPhone, username).eq(User::getTenantId, tenantId));
if (userByPhone != null && userByPhone.getUserId() > 0) {
return userByPhone.getUserId();
}
// 按邮箱搜索
User userByEmail = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getEmail, username).eq(User::getTenantId, tenantId));
if (userByEmail != null && userByEmail.getUserId() > 0) {
return userByEmail.getUserId();
}
throw new BusinessException("找不到该用户");
}
}

View File

@@ -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());
}
// 缓存租户信息

View File

@@ -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("克隆失败");
}

View File

@@ -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;
}

View File

@@ -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("是否已实名认证")

View File

@@ -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;
}

View File

@@ -47,4 +47,5 @@ public interface EmailRecordService extends IService<EmailRecord> {
void sendHtmlEmail(String title, String path, Map<String, Object> map, String[] toEmails)
throws MessagingException, IOException;
void sendEmail(String title, String content, String receiver);
}

View File

@@ -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<EmailRecordMapper, Email
sendFullTextEmail(title, html, toEmails); // 发送邮件
}
@Async
@Override
public void sendEmail(String title, String content, String receiver) {
// 发送邮件通知
EmailRecord emailRecord = new EmailRecord();
emailRecord.setTitle(title);
emailRecord.setContent(content);
emailRecord.setReceiver(receiver);
emailRecord.setCreateUserId(42);
sendTextEmail(title,content,receiver.split(","));
save(emailRecord);
}
}

View File

@@ -15,6 +15,7 @@ import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.common.system.entity.UserRole;
import com.gxwebsoft.common.system.mapper.UserMapper;
import com.gxwebsoft.common.system.param.UserParam;
import com.gxwebsoft.common.system.service.CompanyService;
import com.gxwebsoft.common.system.service.RoleMenuService;
import com.gxwebsoft.common.system.service.UserRoleService;
import com.gxwebsoft.common.system.service.UserService;
@@ -46,6 +47,8 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
@Resource
private BCryptPasswordEncoder bCryptPasswordEncoder;
@Resource
private CompanyService companyService;
@Resource
private RedisUtil redisUtil;
@Override
@@ -79,24 +82,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
user.setAuthorities(roleMenuService.listMenuByUserId(user.getUserId(), null));
// 系统配置信息
Map<String, Object> 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<UserMapper, User> 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;
}

View File

@@ -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[]{