Commit 6c28ff99 by 1539136324@qq.com

提交多租户登录授权相关接口

1 parent db2c68f7
package com.linkwechat.web.controller.system;
import cn.hutool.core.collection.CollectionUtil;
import com.linkwechat.common.constant.Constants;
import com.linkwechat.common.core.domain.AjaxResult;
import com.linkwechat.common.core.domain.entity.SysMenu;
......@@ -52,6 +53,9 @@ public class SysLoginController
@Autowired
private WeAccessTokenClient weAccessTokenClient;
// @Autowired
// private IWeGroupCodeService weGroupCodeService;
......@@ -156,7 +160,30 @@ public class SysLoginController
}
return ajax;
}
/**
* 通过企业id和企业密钥登录
* @param corpId
* @param corpSecret
* @return
*/
@GetMapping("/corpLogin")
public AjaxResult corpLogin(String corpId,String corpSecret){
List<WeCorpAccount> weCorpAccounts = iWxCorpAccountService.selectWeCorpAccountList(WeCorpAccount.builder()
.corpId(corpId)
.corpSecret(corpSecret)
.delFlag(Constants.NORMAL_CODE)
.build());
if(CollectionUtil.isEmpty(weCorpAccounts)){
return AjaxResult.error("当前企业id与企业密码不匹配或不存在");
}
return AjaxResult.success(
loginService.noPwdLogin(weCorpAccounts.get(0).getCropAccount())
);
}
}
......@@ -87,4 +87,7 @@ public class SecurityUtils
{
return userId != null && 1L == userId;
}
}
......@@ -10,8 +10,11 @@ import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerIntercept
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import com.github.pagehelper.PageInterceptor;
import com.linkwechat.common.config.WeComeConfig;
import com.linkwechat.common.utils.SecurityUtils;
import com.linkwechat.wecom.service.IWeAccessTokenService;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import net.sf.jsqlparser.expression.StringValue;
import org.apache.ibatis.plugin.Interceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
......@@ -32,6 +35,9 @@ public class MyBatisPlusConfig
@Autowired
WeComeConfig weComeConfig;
@Autowired
IWeAccessTokenService iWeAccessTokenService;
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
......@@ -46,7 +52,8 @@ public class MyBatisPlusConfig
// 获取租户 ID 值表达式,只支持单个 ID 值
@Override
public Expression getTenantId() {
return new LongValue(1088248166370832385L);
return new StringValue(SecurityUtils.getLoginUser().getUser().getWeCorpAccount().getCorpId());
}
// 这是 default 方法,默认返回 false 表示所有表都需要拼多租户条件,
// 这里设置 role表不需要该条件
......
......@@ -126,6 +126,8 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
.antMatchers("/wecom/portrait/**").anonymous()
.antMatchers("/wecom/findAddaddEmployes").anonymous()
.antMatchers("/corpLogin").anonymous()
// 除上面外的所有请求全部需要鉴权认证
.anyRequest().authenticated()
.and()
......
......@@ -27,6 +27,10 @@
<artifactId>spring-test</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>com.linkwechat</groupId>
<artifactId>linkwe-system</artifactId>
</dependency>
</dependencies>
......
......@@ -2,9 +2,17 @@ package com.linkwechat.wecom.service.impl;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import cn.hutool.core.util.ArrayUtil;
import com.linkwechat.common.config.RuoYiConfig;
import com.linkwechat.common.constant.Constants;
import com.linkwechat.common.constant.WeConstans;
import com.linkwechat.common.core.domain.entity.SysRole;
import com.linkwechat.common.core.domain.entity.SysUser;
import com.linkwechat.common.utils.SecurityUtils;
import com.linkwechat.system.mapper.SysRoleMapper;
import com.linkwechat.system.service.ISysUserService;
import com.linkwechat.wecom.service.IWeAccessTokenService;
import com.linkwechat.wecom.service.IWeCorpAccountService;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -21,13 +29,25 @@ import com.linkwechat.common.core.domain.entity.WeCorpAccount;
@Service
public class WeCorpAccountServiceImpl implements IWeCorpAccountService {
@Autowired
private WeCorpAccountMapper weCorpAccountMapper;
@Autowired
private IWeAccessTokenService iWeAccessTokenService;
@Autowired
private IWeCorpAccountService iWeCorpAccountService;
@Autowired
private ISysUserService iSysUserService;
@Autowired
private RuoYiConfig ruoYiConfig;
@Autowired
private SysRoleMapper roleMapper;
/**
* 查询企业id相关配置
*
......@@ -37,7 +57,7 @@ public class WeCorpAccountServiceImpl implements IWeCorpAccountService {
@Override
public WeCorpAccount selectWeCorpAccountById(Long id)
{
return weCorpAccountMapper.selectWeCorpAccountById(id);
return iWeCorpAccountService.selectWeCorpAccountById(id);
}
/**
......@@ -49,7 +69,7 @@ public class WeCorpAccountServiceImpl implements IWeCorpAccountService {
@Override
public List<WeCorpAccount> selectWeCorpAccountList(WeCorpAccount wxCorpAccount)
{
return weCorpAccountMapper.selectWeCorpAccountList(wxCorpAccount);
return iWeCorpAccountService.selectWeCorpAccountList(wxCorpAccount);
}
/**
......@@ -61,8 +81,23 @@ public class WeCorpAccountServiceImpl implements IWeCorpAccountService {
@Override
public int insertWeCorpAccount(WeCorpAccount wxCorpAccount)
{
int returnCode = iWeCorpAccountService.insertWeCorpAccount(wxCorpAccount);
if(Constants.SERVICE_STATUS_ERROR<returnCode){
iSysUserService.insertUser(
SysUser.builder()
.userName(wxCorpAccount.getCropAccount())
.nickName(wxCorpAccount.getCompanyName())
.userType(Constants.USER_TYOE_CORP_ADMIN)
.roleIds(ArrayUtil.toArray(roleMapper.selectRoleList(SysRole.builder()
.roleKey(Constants.DEFAULT_WECOME_CORP_ADMIN)
.build()).stream().map(SysRole::getRoleId).collect(Collectors.toList()), Long.class))
.password(SecurityUtils.encryptPassword(ruoYiConfig.getWeUserDefaultPwd()))
.build()
);
}
return weCorpAccountMapper.insertWeCorpAccount(wxCorpAccount);
return returnCode;
}
/**
......@@ -75,7 +110,7 @@ public class WeCorpAccountServiceImpl implements IWeCorpAccountService {
public int updateWeCorpAccount(WeCorpAccount wxCorpAccount)
{
int returnCode = weCorpAccountMapper.updateWeCorpAccount(wxCorpAccount);
int returnCode = iWeCorpAccountService.updateWeCorpAccount(wxCorpAccount);
if(Constants.SERVICE_RETURN_SUCCESS_CODE<returnCode){
......@@ -95,7 +130,7 @@ public class WeCorpAccountServiceImpl implements IWeCorpAccountService {
*/
@Override
public WeCorpAccount findValidWeCorpAccount() {
return weCorpAccountMapper.findValidWeCorpAccount();
return iWeCorpAccountService.findValidWeCorpAccount();
}
......@@ -106,7 +141,7 @@ public class WeCorpAccountServiceImpl implements IWeCorpAccountService {
@Override
public int startVailWeCorpAccount(String corpId) {
int returnCode = weCorpAccountMapper.startVailWeCorpAccount(corpId);
int returnCode = iWeCorpAccountService.startVailWeCorpAccount(corpId);
if(Constants.SERVICE_RETURN_SUCCESS_CODE<returnCode){
......@@ -123,12 +158,12 @@ public class WeCorpAccountServiceImpl implements IWeCorpAccountService {
public int startCustomerChurnNoticeSwitch(String status) {
WeCorpAccount validWeCorpAccount = findValidWeCorpAccount();
validWeCorpAccount.setCustomerChurnNoticeSwitch(status);
return weCorpAccountMapper.updateWeCorpAccount(validWeCorpAccount);
return iWeCorpAccountService.updateWeCorpAccount(validWeCorpAccount);
}
@Override
public String getCustomerChurnNoticeSwitch() {
WeCorpAccount validWeCorpAccount = weCorpAccountMapper.findValidWeCorpAccount();
WeCorpAccount validWeCorpAccount = iWeCorpAccountService.findValidWeCorpAccount();
String noticeSwitch = Optional.ofNullable(validWeCorpAccount).map(WeCorpAccount::getCustomerChurnNoticeSwitch)
.orElse(WeConstans.DEL_FOLLOW_USER_SWITCH_CLOSE);
return noticeSwitch;
......@@ -137,7 +172,7 @@ public class WeCorpAccountServiceImpl implements IWeCorpAccountService {
@Override
public WeCorpAccount findWeCorpByAccount(String corpAccount) {
return weCorpAccountMapper.findWeCorpByAccount(corpAccount);
return iWeCorpAccountService.findWeCorpByAccount(corpAccount);
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!