diff --git a/yudao-framework/yudao-spring-boot-starter-biz-weixin/src/main/java/cn/iocoder/yudao/config/WxMaConfiguration.java b/yudao-framework/yudao-spring-boot-starter-biz-weixin/src/main/java/cn/iocoder/yudao/config/WxMaConfiguration.java new file mode 100644 index 00000000..b1c394c9 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-weixin/src/main/java/cn/iocoder/yudao/config/WxMaConfiguration.java @@ -0,0 +1,42 @@ +package cn.iocoder.yudao.config; + + +import cn.binarywang.wx.miniapp.api.WxMaService; +import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl; +import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Scope; +import org.springframework.context.annotation.ScopedProxyMode; + + +/** + * Created by kellen on 2020/5/3. 微信小程序服务配置 + */ +@Configuration +@Slf4j +public class WxMaConfiguration { + + @Value("${wxma.app_id}") + private String appId; + @Value("${wxma.app_secret}") + private String appSecret; + + @Bean + @Scope(value = "prototype", proxyMode = ScopedProxyMode.TARGET_CLASS) + public WxMaService wxMaService() { + + WxMaDefaultConfigImpl wxMaDefaultConfig = new WxMaDefaultConfigImpl(); + wxMaDefaultConfig.setAppid(appId); + wxMaDefaultConfig.setSecret(appSecret); + + WxMaServiceImpl wxMaService = new WxMaServiceImpl(); + wxMaService.setWxMaConfig(wxMaDefaultConfig); + + return wxMaService; + + } + +} \ No newline at end of file diff --git a/yudao-framework/yudao-spring-boot-starter-biz-weixin/src/main/resources/META-INF/spring.factories b/yudao-framework/yudao-spring-boot-starter-biz-weixin/src/main/resources/META-INF/spring.factories index 65dc80b8..e0b48aa7 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-weixin/src/main/resources/META-INF/spring.factories +++ b/yudao-framework/yudao-spring-boot-starter-biz-weixin/src/main/resources/META-INF/spring.factories @@ -1,2 +1,3 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ - cn.iocoder.yudao.config.WxCpConfigure + cn.iocoder.yudao.config.WxCpConfigure,\ + cn.iocoder.yudao.config.WxMaConfiguration diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/auth/AuthController.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/auth/AuthController.java index 36126333..7b9d826e 100644 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/auth/AuthController.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/auth/AuthController.java @@ -1,5 +1,7 @@ package cn.iocoder.yudao.module.system.controller.admin.auth; +import cn.binarywang.wx.miniapp.api.WxMaService; +import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; import cn.hutool.core.util.ObjectUtil; import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; @@ -69,7 +71,8 @@ public class AuthController { private CpUserService cpUserService; @Resource private WxCpService wxCpService; - + @Resource + private WxMaService wxMaService; @Resource private UserSessionService userSessionService; @@ -94,8 +97,17 @@ public class AuthController { if ("wxcp".equals(dto.getCode())){ cpuser = cpUserService.getByUserId("test"); }else{ - WxCpMaJsCode2SessionResult session = wxCpService.jsCode2Session(dto.getCode()); - cpuser = cpUserService.getByUserId(session.getUserId()); + if (dto.getPlatform().equalsIgnoreCase("WX")){ + //普通微信 + WxMaJscode2SessionResult result = wxMaService.jsCode2SessionInfo(dto.getCode()); + log.info("微信登录信息,{}", result); + cpuser = cpUserService.getByUserId(result.getUnionid()); + }else{ + //企业微信 + WxCpMaJsCode2SessionResult session = wxCpService.jsCode2Session(dto.getCode()); + log.info("企业微信登录信息:{}", session); + cpuser = cpUserService.getByUserId(session.getUserId()); + } } if (ObjectUtil.isEmpty(cpuser)){ @@ -106,19 +118,21 @@ public class AuthController { if (ObjectUtil.isEmpty(user)){ CommonResult.error(ErrorCodeConstants.USER_NOT_EXISTS.getCode(),"未发现绑定的企业用户,暂时无法登录,请联系管理员"); }else { - user.setCpUserId(cpuser.getUserId()); - userService.updateUser(UserConvert.INSTANCE.convertFromEntity(user)); + if (dto.getPlatform().equalsIgnoreCase("WX")){ + //暂时用微信小程序登录 + }else{ + user.setCpUserId(cpuser.getUserId()); + userService.updateUser(UserConvert.INSTANCE.convertFromEntity(user)); + } } - LoginUser login1 = AuthConvert.INSTANCE.convert(user); - String token1 = userSessionService.createUserSession(login1, getClientIP(), getUserAgent()); - AuthLoginReqVO login = AuthLoginReqVO.builder() .username(cpuser.getUserId()) .password("123456") .platform("wxcp").build(); String token = authService.login(login, getClientIP(), getUserAgent()); + log.info("TOKEN:::{}",token); AuthLoginRespVO vo = AuthLoginRespVO.builder().token(token).user(cpuser).build(); return CommonResult.success(vo); } diff --git a/yudao-server/src/main/resources/application.yaml b/yudao-server/src/main/resources/application.yaml index 9af11a91..d231bb15 100644 --- a/yudao-server/src/main/resources/application.yaml +++ b/yudao-server/src/main/resources/application.yaml @@ -150,5 +150,10 @@ smms: wxcp: corpId: wwb9f9734e8e124761 - farmAppid: 1000033 - farmSecret: UDSKsn0_LAPYqSwjH9E-AfY_X40lq0sormfe1yV_6Gc \ No newline at end of file + farmAppid: 1000036 + farmSecret: UDSKsn0_LAPYqSwjH9E-AfY_X40lq0sormfe1yV_6Gc + +#农场小程序 +wxma: + app_id: wx4c3f7b2dd907f34e + app_secret: b2496a7a77483f819c6074841250119b