改造核心框架

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.baomidou.mybatisplus.core.metadata.IPage;
import com.gxwebsoft.common.core.Constants; import com.gxwebsoft.common.core.Constants;
import com.gxwebsoft.common.core.exception.BusinessException; 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.entity.User;
import com.gxwebsoft.common.system.mapper.CompanyMapper;
import com.gxwebsoft.common.system.service.CompanyService;
import com.gxwebsoft.common.system.service.UserService; import com.gxwebsoft.common.system.service.UserService;
import org.springframework.beans.propertyeditors.StringTrimmerEditor; import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder; import org.springframework.web.bind.annotation.InitBinder;
@@ -25,199 +29,221 @@ import java.util.List;
* @since 2017-06-10 10:10:19 * @since 2017-06-10 10:10:19
*/ */
public class BaseController { public class BaseController {
@Resource @Resource
private HttpServletRequest request; private HttpServletRequest request;
@Resource @Resource
private StringRedisTemplate stringRedisTemplate; private StringRedisTemplate stringRedisTemplate;
@Resource @Resource
private UserService userService; private UserService userService;
@Resource
private CompanyService companyService;
/** /**
* 获取当前登录的user * 获取当前登录的user
* *
* @return User * @return User
*/ */
public User getLoginUser() { public User getLoginUser() {
try { try {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null) { if (authentication != null) {
Object object = authentication.getPrincipal(); Object object = authentication.getPrincipal();
if (object instanceof User) { if (object instanceof User) {
return (User) object; return (User) object;
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
} }
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 } catch (Exception e) {
if(StrUtil.isNotBlank(request.getHeader("tenantId"))){ System.out.println(e.getMessage());
return Integer.valueOf(request.getHeader("tenantId"));
}
return null;
} }
return null;
}
/** /**
* 返回成功 * 获取当前登录的userId
* *
* @return ApiResult * @return userId
*/ */
public ApiResult<?> success() { public Integer getLoginUserId() {
return new ApiResult<>(Constants.RESULT_OK_CODE, Constants.RESULT_OK_MSG); User loginUser = getLoginUser();
} return loginUser == null ? null : loginUser.getUserId();
}
/** /**
* 返回成功 * 获取当前登录的tenantId
* *
* @param message 状态信息 * @return tenantId
* @return ApiResult */
*/ public Integer getTenantId() {
public ApiResult<?> success(String message) { // 从登录用户拿tenantId
return success().setMessage(message); 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 Company
* @return ApiResult */
*/ public Company getCompany() {
public <T> ApiResult<T> success(T data) { List<Company> list = companyService.list(new LambdaQueryWrapper<Company>().eq(Company::getAuthoritative, 1));
return new ApiResult<>(Constants.RESULT_OK_CODE, Constants.RESULT_OK_MSG, data); if (!CollectionUtils.isEmpty(list)) {
final Company company = list.get(0);
return company;
} }
return null;
}
/** public Integer getCompanyId() {
* 返回成功 Company company = getCompany();
* return company.getCompanyId();
* @param message 状态信息 }
* @return ApiResult
*/
public <T> ApiResult<T> success(String message, T data) {
return success(data).setMessage(message);
}
/** /**
* 返回分页查询数据 * 返回成功
* *
* @param list 当前页数据 * @return ApiResult
* @param count 总数量 */
* @return ApiResult public ApiResult<?> success() {
*/ return new ApiResult<>(Constants.RESULT_OK_CODE, Constants.RESULT_OK_MSG);
public <T> ApiResult<PageResult<T>> success(List<T> list, Long count) { }
return success(new PageResult<>(list, count));
}
/** /**
* 返回分页查询数据 * 返回成功
* *
* @param iPage IPage * @param message 状态信息
* @return ApiResult * @return ApiResult
*/ */
public <T> ApiResult<PageResult<T>> success(IPage<T> iPage) { public ApiResult<?> success(String message) {
return success(iPage.getRecords(), iPage.getTotal()); return success().setMessage(message);
} }
/** /**
* 返回失败 * 返回成功
* *
* @return ApiResult * @param data 返回数据
*/ * @return ApiResult
public ApiResult<?> fail() { */
return new ApiResult<>(Constants.RESULT_ERROR_CODE, Constants.RESULT_ERROR_MSG); public <T> ApiResult<T> success(T data) {
} return new ApiResult<>(Constants.RESULT_OK_CODE, Constants.RESULT_OK_MSG, data);
}
/** /**
* 返回失败 * 返回成功
* *
* @param message 状态信息 * @param message 状态信息
* @return ApiResult * @return ApiResult
*/ */
public ApiResult<?> fail(String message) { public <T> ApiResult<T> success(String message, T data) {
return fail().setMessage(message); return success(data).setMessage(message);
} }
/** /**
* 返回失败 * 返回分页查询数据
* *
* @param data 返回数据 * @param list 当前页数据
* @return ApiResult * @param count 总数量
*/ * @return ApiResult
public <T> ApiResult<T> fail(T data) { */
return fail(Constants.RESULT_ERROR_MSG, data); public <T> ApiResult<PageResult<T>> success(List<T> list, Long count) {
} return success(new PageResult<>(list, count));
}
/** /**
* 返回失败 * 返回分页查询数据
* *
* @param message 状态信息 * @param iPage IPage
* @param data 返回数据 * @return ApiResult
* @return ApiResult */
*/ public <T> ApiResult<PageResult<T>> success(IPage<T> iPage) {
public <T> ApiResult<T> fail(String message, T data) { return success(iPage.getRecords(), iPage.getTotal());
return new ApiResult<>(Constants.RESULT_ERROR_CODE, message, data); }
}
/** /**
* 请求参数的空字符串转为null * 返回失败
*/ *
@InitBinder * @return ApiResult
public void initBinder(WebDataBinder binder) { */
binder.registerCustomEditor(String.class, new StringTrimmerEditor(true)); 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 *
*/ * @param message 状态信息
public Integer getUserIdByUsername(String username, Integer tenantId){ * @param data 返回数据
// 按账号搜素 * @return ApiResult
User user = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, username).eq(User::getTenantId,tenantId)); */
if (user != null && user.getUserId() > 0) { public <T> ApiResult<T> fail(String message, T data) {
return user.getUserId(); return new ApiResult<>(Constants.RESULT_ERROR_CODE, message, data);
} }
// 按手机号码搜索
User userByPhone = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getPhone, username).eq(User::getTenantId, tenantId)); /**
if (userByPhone != null && userByPhone.getUserId() > 0) { * 请求参数的空字符串转为null
return userByPhone.getUserId(); */
} @InitBinder
// 按邮箱搜索 public void initBinder(WebDataBinder binder) {
User userByEmail = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getEmail, username).eq(User::getTenantId, tenantId)); binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
if (userByEmail != null && userByEmail.getUserId() > 0) { }
return userByEmail.getUserId();
} // 自定义函数
throw new BusinessException("找不到该用户"); 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 title = "恭喜!您的账号已注册成功";
String content = "租户ID".concat(tenant.getTenantId().toString()).concat("\r\n名称" + tenantName).concat("\r\n账号" + phone).concat("\r\n密码" + password); 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"); 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(); emailRecordService.sendEmail(title,content.concat(adminUrl),email);
emailRecord.setTitle(title); return success("注册成功",tenant.getTenantId());
emailRecord.setContent(content);
emailRecord.setReceiver(receiver);
emailRecord.setCreateUserId(42);
emailRecordService.sendTextEmail(title,content,receiver.split(","));
emailRecordService.save(emailRecord);
} }
// 缓存租户信息 // 缓存租户信息

View File

@@ -1,13 +1,12 @@
package com.gxwebsoft.common.system.controller; 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.annotation.OperationLog;
import com.gxwebsoft.common.core.web.*; 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.Menu;
import com.gxwebsoft.common.system.entity.Plug;
import com.gxwebsoft.common.system.param.MenuParam; 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.MenuService;
import com.gxwebsoft.common.system.service.PlugService;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
@@ -29,7 +28,7 @@ public class MenuController extends BaseController {
@Resource @Resource
private MenuService menuService; private MenuService menuService;
@Resource @Resource
private PlugService plugService; private CompanyService companyService;
@PreAuthorize("hasAuthority('sys:menu:list')") @PreAuthorize("hasAuthority('sys:menu:list')")
@OperationLog @OperationLog
@@ -134,7 +133,12 @@ public class MenuController extends BaseController {
@PostMapping("/clone") @PostMapping("/clone")
public ApiResult<?> onClone(@RequestBody MenuParam param){ public ApiResult<?> onClone(@RequestBody MenuParam param){
if(menuService.cloneMenu(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("克隆失败"); return fail("克隆失败");
} }

View File

@@ -133,6 +133,9 @@ public class Company implements Serializable {
@ApiModelProperty(value = "排序") @ApiModelProperty(value = "排序")
private Integer sortNumber; private Integer sortNumber;
@ApiModelProperty(value = "当前使用的租户模板")
private Integer planId;
@ApiModelProperty(value = "用户ID") @ApiModelProperty(value = "用户ID")
private Integer userId; private Integer userId;
@@ -170,4 +173,8 @@ public class Company implements Serializable {
@TableField(exist = false) @TableField(exist = false)
private String nickname; 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; private Integer organizationId;
@ApiModelProperty("客户ID") @ApiModelProperty("客户ID")
@TableField(exist = false)
private Integer customerId; private Integer customerId;
@ApiModelProperty("企业ID") @ApiModelProperty("企业ID")
@TableField(exist = false)
private Integer companyId; private Integer companyId;
@ApiModelProperty("注册来源客户端") @ApiModelProperty("注册来源客户端")
@@ -191,6 +193,7 @@ public class User implements UserDetails {
private Date updateTime; private Date updateTime;
@ApiModelProperty("公司名称") @ApiModelProperty("公司名称")
@TableField(exist = false)
private String companyName; private String companyName;
@ApiModelProperty("是否已实名认证") @ApiModelProperty("是否已实名认证")

View File

@@ -65,4 +65,8 @@ public class MenuParam extends BaseParam {
@QueryField(type = QueryType.EQ) @QueryField(type = QueryType.EQ)
private Integer tenantId; 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) void sendHtmlEmail(String title, String path, Map<String, Object> map, String[] toEmails)
throws MessagingException, IOException; 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.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
@@ -69,4 +70,17 @@ public class EmailRecordServiceImpl extends ServiceImpl<EmailRecordMapper, Email
sendFullTextEmail(title, html, toEmails); // 发送邮件 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.entity.UserRole;
import com.gxwebsoft.common.system.mapper.UserMapper; import com.gxwebsoft.common.system.mapper.UserMapper;
import com.gxwebsoft.common.system.param.UserParam; 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.RoleMenuService;
import com.gxwebsoft.common.system.service.UserRoleService; import com.gxwebsoft.common.system.service.UserRoleService;
import com.gxwebsoft.common.system.service.UserService; import com.gxwebsoft.common.system.service.UserService;
@@ -46,6 +47,8 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
@Resource @Resource
private BCryptPasswordEncoder bCryptPasswordEncoder; private BCryptPasswordEncoder bCryptPasswordEncoder;
@Resource @Resource
private CompanyService companyService;
@Resource
private RedisUtil redisUtil; private RedisUtil redisUtil;
@Override @Override
@@ -79,24 +82,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
user.setAuthorities(roleMenuService.listMenuByUserId(user.getUserId(), null)); user.setAuthorities(roleMenuService.listMenuByUserId(user.getUserId(), null));
// 系统配置信息 // 系统配置信息
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
// 1)基本信息 // 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)云存储
String key = "setting:upload:" + user.getTenantId(); String key = "setting:upload:" + user.getTenantId();
final String upload = redisUtil.get(key); final String upload = redisUtil.get(key);
if(upload != null){ if(upload != null){
@@ -107,10 +93,11 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
user.setSystem(map); user.setSystem(map);
} }
// 企业信息 // 企业信息
String key2 = "company:authoritative:" + user.getTenantId(); final Company company = companyService.getByTenantIdRel(user.getTenantId());
final Company company = redisUtil.get(key2, Company.class); if (company != null) {
user.setCompanyInfo(company); user.setCompanyId(company.getCompanyId());
user.setSystem(map); user.setCompanyInfo(company);
}
} }
return user; return user;
} }

View File

@@ -54,8 +54,8 @@ public class SysGenerator {
// "sys_user_grade" // "sys_user_grade"
// "sys_user_referee" // "sys_user_referee"
// "sys_notice" // "sys_notice"
"sys_plug" // "sys_plug"
"sys_plug_record"
}; };
// 需要去除的表前缀 // 需要去除的表前缀
private static final String[] TABLE_PREFIX = new String[]{ private static final String[] TABLE_PREFIX = new String[]{