提交代码

dev_xd
姜玉琦 2024-10-13 13:21:41 +08:00
parent 62b7274548
commit 17ae99df01
9 changed files with 128 additions and 90 deletions

12
pom.xml
View File

@ -35,7 +35,8 @@
<minio.version>8.2.2</minio.version> <minio.version>8.2.2</minio.version>
<poi.version>4.1.2</poi.version> <poi.version>4.1.2</poi.version>
<pinyin4j.version>2.5.1</pinyin4j.version> <pinyin4j.version>2.5.1</pinyin4j.version>
<weixin.version>2.7.0</weixin.version> <weixin.mp.version>2.7.0</weixin.mp.version>
<weixin.miniapp.version>4.5.5.B</weixin.miniapp.version>
<transmittable-thread-local.version>2.14.2</transmittable-thread-local.version> <transmittable-thread-local.version>2.14.2</transmittable-thread-local.version>
</properties> </properties>
@ -156,7 +157,14 @@
<dependency> <dependency>
<groupId>com.github.binarywang</groupId> <groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId> <artifactId>weixin-java-mp</artifactId>
<version>${weixin.version}</version> <version>${weixin.mp.version}</version>
</dependency>
<!-- 微信小程序 -->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-miniapp</artifactId>
<version>${weixin.miniapp.version}</version>
</dependency> </dependency>
<!-- 核心模块 --> <!-- 核心模块 -->

View File

@ -3,11 +3,10 @@ package com.yanzhu.system.api;
import com.yanzhu.common.core.constant.SecurityConstants; import com.yanzhu.common.core.constant.SecurityConstants;
import com.yanzhu.common.core.constant.ServiceNameConstants; import com.yanzhu.common.core.constant.ServiceNameConstants;
import com.yanzhu.common.core.domain.R; import com.yanzhu.common.core.domain.R;
import com.yanzhu.system.api.domain.SysUser;
import com.yanzhu.system.api.factory.RemoteFileFallbackFactory; import com.yanzhu.system.api.factory.RemoteFileFallbackFactory;
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -20,6 +19,10 @@ import java.util.Map;
@FeignClient(contextId = "remoteProService", value = ServiceNameConstants.MANAGE_SERVICE, fallbackFactory = RemoteFileFallbackFactory.class) @FeignClient(contextId = "remoteProService", value = ServiceNameConstants.MANAGE_SERVICE, fallbackFactory = RemoteFileFallbackFactory.class)
public interface RemoteProService public interface RemoteProService
{ {
@GetMapping("/wxCallBack/maLogin/{code}")
public R<Map<String,Object>> maLogin(@PathVariable("code") String code, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
/** /**
* *
* *

View File

@ -26,6 +26,12 @@ public class RemoteProFallbackFactory implements FallbackFactory<RemoteProServic
log.error("项目服务调用失败:{}", throwable.getMessage()); log.error("项目服务调用失败:{}", throwable.getMessage());
return new RemoteProService() return new RemoteProService()
{ {
@Override
public R<Map<String,Object>> maLogin(String code, String source)
{
return R.fail("小程序登录失败:" + throwable.getMessage());
}
@Override @Override
public R<Map<String,Object>> info(Long id, String source) public R<Map<String,Object>> info(Long id, String source)
{ {

View File

@ -43,6 +43,11 @@ public class TokenController
@Autowired @Autowired
private RemoteUserService remoteUserService; private RemoteUserService remoteUserService;
/**
*
* @param form
* @return
*/
@PostMapping("login") @PostMapping("login")
public R<?> login(@RequestBody LoginBody form) public R<?> login(@RequestBody LoginBody form)
{ {
@ -52,13 +57,18 @@ public class TokenController
return R.ok(tokenService.createToken(userInfo)); return R.ok(tokenService.createToken(userInfo));
} }
@PostMapping("wxLogin") /**
public R<?> wxLogin(@RequestBody WxLoginBody form) *
* @param form
* @return
*/
@PostMapping("maLogin")
public R<?> maLogin(@RequestBody WxLoginBody form)
{ {
// 用户登录 // 用户登录
LoginUser userInfo = sysLoginService.wxLogin(form.getCode(), form.getAppId()); Map<String,Object> result = sysLoginService.maLogin(form.getCode(), form.getAppId());
// 获取登录token // 获取登录token
return R.ok(tokenService.createToken(userInfo)); return R.ok(result);
} }
/** /**

View File

@ -1,5 +1,6 @@
package com.yanzhu.auth.service; package com.yanzhu.auth.service;
import com.yanzhu.system.api.RemoteProService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import com.yanzhu.common.core.constant.CacheConstants; import com.yanzhu.common.core.constant.CacheConstants;
@ -30,6 +31,9 @@ import java.util.Objects;
@Component @Component
public class SysLoginService public class SysLoginService
{ {
@Autowired
private RemoteProService remoteProService;
@Autowired @Autowired
private RemoteUserService remoteUserService; private RemoteUserService remoteUserService;
@ -120,81 +124,12 @@ public class SysLoginService
} }
/** /**
* *
*/ */
public LoginUser wxLogin(String code, String appId) public Map<String,Object> maLogin(String code, String appId)
{ {
Map<String,Object> loginResult = remoteProService.maLogin(code,SecurityConstants.INNER).getData();
// 用户名或密码为空 错误 return loginResult;
if (StringUtils.isAnyBlank(username, password))
{
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户/密码必须填写");
throw new ServiceException("用户/密码必须填写");
}
// 密码如果不在指定范围内 错误
if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
|| password.length() > UserConstants.PASSWORD_MAX_LENGTH)
{
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户密码不在指定范围");
throw new ServiceException("用户密码不在指定范围");
}
// 用户名不在指定范围内 错误
if (username.length() < UserConstants.USERNAME_MIN_LENGTH
|| username.length() > UserConstants.USERNAME_MAX_LENGTH)
{
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户名不在指定范围");
throw new ServiceException("用户名不在指定范围");
}
// IP黑名单校验
String blackStr = Convert.toStr(redisService.getCacheObject(CacheConstants.SYS_LOGIN_BLACKIPLIST));
if (IpUtils.isMatchedIp(blackStr, IpUtils.getIpAddr()))
{
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "很遗憾访问IP已被列入系统黑名单");
throw new ServiceException("很遗憾访问IP已被列入系统黑名单");
}
// 查询用户信息
R<LoginUser> userResult = remoteUserService.getUserInfo(username, SecurityConstants.INNER);
if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData()))
{
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "登录用户不存在");
throw new ServiceException("登录用户:" + username + " 不存在");
}
if (R.FAIL == userResult.getCode())
{
throw new ServiceException(userResult.getMsg());
}
LoginUser userInfo = userResult.getData();
SysUser user = userResult.getData().getSysUser();
if (UserStatus.DELETED.getCode().equals(user.getDelFlag()))
{
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "对不起,您的账号已被删除");
throw new ServiceException("对不起,您的账号:" + username + " 已被删除");
}
if (UserStatus.DISABLE.getCode().equals(user.getStatus()))
{
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户已停用,请联系管理员");
throw new ServiceException("对不起,您的账号:" + username + " 已停用");
}
passwordService.validate(user, password);
recordLogService.recordLogininfor(username, Constants.LOGIN_SUCCESS, "登录成功");
// 补充用户项目信息
if(Objects.nonNull(user.getActiveComId())){
userInfo.setProjectDeptId(user.getActiveComId());
}
if(Objects.nonNull(user.getActiveComName())){
userInfo.setProjectDeptName(user.getActiveComName());
}
if(Objects.nonNull(user.getActiveProjectId())){
userInfo.setProjectId(user.getActiveProjectId());
}
if(Objects.nonNull(user.getActiveProjectName())){
userInfo.setProjectName(user.getActiveProjectName());
}
return userInfo;
} }
public void logout(String loginName) public void logout(String loginName)

View File

@ -119,11 +119,17 @@
<artifactId>pinyin4j</artifactId> <artifactId>pinyin4j</artifactId>
</dependency> </dependency>
<!-- 微信小程序 --> <!-- 微信公众号 -->
<dependency> <dependency>
<groupId>com.github.binarywang</groupId> <groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId> <artifactId>weixin-java-mp</artifactId>
</dependency> </dependency>
<!-- 微信小程序 -->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-miniapp</artifactId>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -1,6 +1,8 @@
package com.yanzhu.manage.config; package com.yanzhu.manage.config;
import com.yanzhu.manage.service.impl.ProProjectInfoSubdeptsServiceImpl; import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage; import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl; import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
@ -21,17 +23,39 @@ public class WeChatConfig {
private static final Logger log = LoggerFactory.getLogger(WeChatConfig.class); private static final Logger log = LoggerFactory.getLogger(WeChatConfig.class);
/**
*
* @return
*/
@Bean @Bean
public WxMpService wxMpService() { public WxMpService wxMpService() {
WxMpInMemoryConfigStorage config = new WxMpInMemoryConfigStorage(); WxMpInMemoryConfigStorage config = new WxMpInMemoryConfigStorage();
log.info("weChatProperties.wxAppId...{}",weChatProperties.getWxAppId()); log.info("weChatProperties.mpAppId...{}",weChatProperties.getMpAppId());
log.info("weChatProperties.wxAppSecret...{}",weChatProperties.getWxAppSecret()); log.info("weChatProperties.mpAppSecret...{}",weChatProperties.getMpAppSecret());
config.setAppId(weChatProperties.getWxAppId()); config.setAppId(weChatProperties.getMpAppId());
config.setSecret(weChatProperties.getWxAppSecret()); config.setSecret(weChatProperties.getMpAppSecret());
WxMpServiceImpl wxMpService = new WxMpServiceImpl(); WxMpServiceImpl wxMpService = new WxMpServiceImpl();
wxMpService.setWxMpConfigStorage(config); wxMpService.setWxMpConfigStorage(config);
return wxMpService; return wxMpService;
} }
/**
*
* @return
*/
@Bean
public WxMaService wxMaService() {
WxMaDefaultConfigImpl wxMaConfig = new WxMaDefaultConfigImpl();
log.info("weChatProperties.maAppId...{}",weChatProperties.getMaAppId());
log.info("weChatProperties.maAppSecret...{}",weChatProperties.getMaAppSecret());
wxMaConfig.setAppid(weChatProperties.getMaAppId());
wxMaConfig.setSecret(weChatProperties.getMaAppSecret());
WxMaService wxMaService = new WxMaServiceImpl();
wxMaService.setWxMaConfig(wxMaConfig);
return wxMaService;
}
} }

View File

@ -21,6 +21,6 @@ public class WeChatProperties {
/** /**
* *
*/ */
private String wxAppId; private String maAppId;
private String wxAppSecret; private String maAppSecret;
} }

View File

@ -0,0 +1,46 @@
package com.yanzhu.manage.controller;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.yanzhu.common.core.domain.R;
import com.yanzhu.common.core.utils.bean.BeanUtils;
import com.yanzhu.common.security.annotation.InnerAuth;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
/**
*
*/
@RestController
@RequestMapping("/wxCallBack")
public class WxCallBackController {
@Autowired
private WxMaService wxMaService;
private static final Logger log = LoggerFactory.getLogger(WxCallBackController.class);
/**
*
*/
@InnerAuth
@GetMapping("/maLogin/{code}")
public R<Map<String, Object>> maLogin(@PathVariable("code") String code) {
WxMaJscode2SessionResult sessionInfo =
wxMaService.getUserService().getSessionInfo(code);
log.info("wxLogin==>{}...{}",code,sessionInfo.getOpenid());
try {
return R.ok(BeanUtils.beanToMap(sessionInfo));
}catch (Exception e){
return R.fail("小程序登录异常!!!");
}
}
}