diff --git a/pom.xml b/pom.xml index 9425a2df..76d350af 100644 --- a/pom.xml +++ b/pom.xml @@ -35,7 +35,8 @@ 8.2.2 4.1.2 2.5.1 - 2.7.0 + 2.7.0 + 4.5.5.B 2.14.2 @@ -156,7 +157,14 @@ com.github.binarywang weixin-java-mp - ${weixin.version} + ${weixin.mp.version} + + + + + com.github.binarywang + weixin-java-miniapp + ${weixin.miniapp.version} diff --git a/yanzhu-api/yanzhu-api-system/src/main/java/com/yanzhu/system/api/RemoteProService.java b/yanzhu-api/yanzhu-api-system/src/main/java/com/yanzhu/system/api/RemoteProService.java index d68d3785..6af00b26 100644 --- a/yanzhu-api/yanzhu-api-system/src/main/java/com/yanzhu/system/api/RemoteProService.java +++ b/yanzhu-api/yanzhu-api-system/src/main/java/com/yanzhu/system/api/RemoteProService.java @@ -3,11 +3,10 @@ package com.yanzhu.system.api; import com.yanzhu.common.core.constant.SecurityConstants; import com.yanzhu.common.core.constant.ServiceNameConstants; import com.yanzhu.common.core.domain.R; +import com.yanzhu.system.api.domain.SysUser; import com.yanzhu.system.api.factory.RemoteFileFallbackFactory; import org.springframework.cloud.openfeign.FeignClient; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.*; import java.util.List; import java.util.Map; @@ -20,6 +19,10 @@ import java.util.Map; @FeignClient(contextId = "remoteProService", value = ServiceNameConstants.MANAGE_SERVICE, fallbackFactory = RemoteFileFallbackFactory.class) public interface RemoteProService { + + @GetMapping("/wxCallBack/maLogin/{code}") + public R> maLogin(@PathVariable("code") String code, @RequestHeader(SecurityConstants.FROM_SOURCE) String source); + /** * 项目详情 * diff --git a/yanzhu-api/yanzhu-api-system/src/main/java/com/yanzhu/system/api/factory/RemoteProFallbackFactory.java b/yanzhu-api/yanzhu-api-system/src/main/java/com/yanzhu/system/api/factory/RemoteProFallbackFactory.java index b1a7a03a..2b8084f3 100644 --- a/yanzhu-api/yanzhu-api-system/src/main/java/com/yanzhu/system/api/factory/RemoteProFallbackFactory.java +++ b/yanzhu-api/yanzhu-api-system/src/main/java/com/yanzhu/system/api/factory/RemoteProFallbackFactory.java @@ -26,6 +26,12 @@ public class RemoteProFallbackFactory implements FallbackFactory> maLogin(String code, String source) + { + return R.fail("小程序登录失败:" + throwable.getMessage()); + } + @Override public R> info(Long id, String source) { diff --git a/yanzhu-auth/src/main/java/com/yanzhu/auth/controller/TokenController.java b/yanzhu-auth/src/main/java/com/yanzhu/auth/controller/TokenController.java index 020a1350..7c79d4ea 100644 --- a/yanzhu-auth/src/main/java/com/yanzhu/auth/controller/TokenController.java +++ b/yanzhu-auth/src/main/java/com/yanzhu/auth/controller/TokenController.java @@ -43,6 +43,11 @@ public class TokenController @Autowired private RemoteUserService remoteUserService; + /** + * 账号密码登录 + * @param form + * @return + */ @PostMapping("login") public R login(@RequestBody LoginBody form) { @@ -52,13 +57,18 @@ public class TokenController 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 result = sysLoginService.maLogin(form.getCode(), form.getAppId()); // 获取登录token - return R.ok(tokenService.createToken(userInfo)); + return R.ok(result); } /** diff --git a/yanzhu-auth/src/main/java/com/yanzhu/auth/service/SysLoginService.java b/yanzhu-auth/src/main/java/com/yanzhu/auth/service/SysLoginService.java index 3b3b6960..25ca24f9 100644 --- a/yanzhu-auth/src/main/java/com/yanzhu/auth/service/SysLoginService.java +++ b/yanzhu-auth/src/main/java/com/yanzhu/auth/service/SysLoginService.java @@ -1,5 +1,6 @@ package com.yanzhu.auth.service; +import com.yanzhu.system.api.RemoteProService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.yanzhu.common.core.constant.CacheConstants; @@ -30,6 +31,9 @@ import java.util.Objects; @Component public class SysLoginService { + @Autowired + private RemoteProService remoteProService; + @Autowired private RemoteUserService remoteUserService; @@ -120,81 +124,12 @@ public class SysLoginService } /** - * 登录 + * 微信小程序登录 */ - public LoginUser wxLogin(String code, String appId) + public Map maLogin(String code, String appId) { - - // 用户名或密码为空 错误 - 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 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; + Map loginResult = remoteProService.maLogin(code,SecurityConstants.INNER).getData(); + return loginResult; } public void logout(String loginName) diff --git a/yanzhu-common/yanzhu-common-core/pom.xml b/yanzhu-common/yanzhu-common-core/pom.xml index 41c376f9..3a262e56 100644 --- a/yanzhu-common/yanzhu-common-core/pom.xml +++ b/yanzhu-common/yanzhu-common-core/pom.xml @@ -119,11 +119,17 @@ pinyin4j - + com.github.binarywang weixin-java-mp + + + com.github.binarywang + weixin-java-miniapp + + diff --git a/yanzhu-modules/yanzhu-manage/src/main/java/com/yanzhu/manage/config/WeChatConfig.java b/yanzhu-modules/yanzhu-manage/src/main/java/com/yanzhu/manage/config/WeChatConfig.java index a5199571..437b9811 100644 --- a/yanzhu-modules/yanzhu-manage/src/main/java/com/yanzhu/manage/config/WeChatConfig.java +++ b/yanzhu-modules/yanzhu-manage/src/main/java/com/yanzhu/manage/config/WeChatConfig.java @@ -1,6 +1,8 @@ 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.WxMpService; import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl; @@ -21,17 +23,39 @@ public class WeChatConfig { private static final Logger log = LoggerFactory.getLogger(WeChatConfig.class); + /** + * 公众号 + * @return + */ @Bean public WxMpService wxMpService() { WxMpInMemoryConfigStorage config = new WxMpInMemoryConfigStorage(); - log.info("weChatProperties.wxAppId...{}",weChatProperties.getWxAppId()); - log.info("weChatProperties.wxAppSecret...{}",weChatProperties.getWxAppSecret()); - config.setAppId(weChatProperties.getWxAppId()); - config.setSecret(weChatProperties.getWxAppSecret()); + log.info("weChatProperties.mpAppId...{}",weChatProperties.getMpAppId()); + log.info("weChatProperties.mpAppSecret...{}",weChatProperties.getMpAppSecret()); + config.setAppId(weChatProperties.getMpAppId()); + config.setSecret(weChatProperties.getMpAppSecret()); WxMpServiceImpl wxMpService = new WxMpServiceImpl(); wxMpService.setWxMpConfigStorage(config); 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; + } } diff --git a/yanzhu-modules/yanzhu-manage/src/main/java/com/yanzhu/manage/config/WeChatProperties.java b/yanzhu-modules/yanzhu-manage/src/main/java/com/yanzhu/manage/config/WeChatProperties.java index f754686d..dbaa4695 100644 --- a/yanzhu-modules/yanzhu-manage/src/main/java/com/yanzhu/manage/config/WeChatProperties.java +++ b/yanzhu-modules/yanzhu-manage/src/main/java/com/yanzhu/manage/config/WeChatProperties.java @@ -21,6 +21,6 @@ public class WeChatProperties { /** * 小程序 */ - private String wxAppId; - private String wxAppSecret; + private String maAppId; + private String maAppSecret; } diff --git a/yanzhu-modules/yanzhu-manage/src/main/java/com/yanzhu/manage/controller/WxCallBackController.java b/yanzhu-modules/yanzhu-manage/src/main/java/com/yanzhu/manage/controller/WxCallBackController.java new file mode 100644 index 00000000..c594970b --- /dev/null +++ b/yanzhu-modules/yanzhu-manage/src/main/java/com/yanzhu/manage/controller/WxCallBackController.java @@ -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> 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("小程序登录异常!!!"); + } + } + +}