Commit 40fd4f56 by giaogiao

微信登陆相关.短信验证码

parent 8f51bb00
...@@ -20,6 +20,7 @@ import io.geekidea.springbootplus.framework.util.PrintApplicationInfo; ...@@ -20,6 +20,7 @@ import io.geekidea.springbootplus.framework.util.PrintApplicationInfo;
import org.mybatis.spring.annotation.MapperScan; import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.ServletComponentScan; import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
...@@ -38,8 +39,9 @@ import org.springframework.transaction.annotation.EnableTransactionManagement; ...@@ -38,8 +39,9 @@ import org.springframework.transaction.annotation.EnableTransactionManagement;
@EnableTransactionManagement @EnableTransactionManagement
@EnableConfigurationProperties @EnableConfigurationProperties
@ServletComponentScan @ServletComponentScan
@ConfigurationPropertiesScan("com.sien.common.config")
@MapperScan({"io.geekidea.springbootplus.**.mapper", "com.sien.**.mapper"}) @MapperScan({"io.geekidea.springbootplus.**.mapper", "com.sien.**.mapper"})
@SpringBootApplication(scanBasePackages = {"io.geekidea.springbootplus", "com.sien"}) @SpringBootApplication(scanBasePackages = {"io.geekidea.springbootplus", "com.sien", "com.sien.common", "com.sien.common.config"})
public class SpringBootPlusApplication { public class SpringBootPlusApplication {
public static void main(String[] args) { public static void main(String[] args) {
......
package io.geekidea.springbootplus.test; package io.geekidea.springbootplus.test;
import com.sien.common.factory.PushFactory; import com.sien.common.factory.PushFactory;
import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
import me.chanjar.weixin.mp.api.WxMpService;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -9,11 +12,14 @@ import org.springframework.test.context.junit4.SpringRunner; ...@@ -9,11 +12,14 @@ import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@SpringBootTest @SpringBootTest
public class PushTest { public class SpiringPlusTest {
@Autowired @Autowired
private PushFactory pushFactory; private PushFactory pushFactory;
@Autowired
private WxMpService wxMpService;
/** /**
* app推送:1.您有一条新消息(客服回复推送) * app推送:1.您有一条新消息(客服回复推送)
* 2.您的订单已报价(商家端报价完后推送) * 2.您的订单已报价(商家端报价完后推送)
...@@ -22,7 +28,7 @@ public class PushTest { ...@@ -22,7 +28,7 @@ public class PushTest {
* @throws Exception * @throws Exception
*/ */
@Test @Test
public void pu() throws Exception { public void push() throws Exception {
pushFactory.getService(1).unicast("Ar4opHQcGn-UA1-fZ7O6PAeNSJs9rCEEI5XRvS_0rQke", "2", "您的订单已报价"); pushFactory.getService(1).unicast("Ar4opHQcGn-UA1-fZ7O6PAeNSJs9rCEEI5XRvS_0rQke", "2", "您的订单已报价");
...@@ -30,4 +36,19 @@ public class PushTest { ...@@ -30,4 +36,19 @@ public class PushTest {
} }
@Test
public void wxGetOpenId() throws Exception {
String code = "071iQYGa1jwhCA06vtFa1OHuHE2iQYGq";
WxOAuth2AccessToken accessToken = wxMpService.getOAuth2Service().getAccessToken(code);
// 获取用户头像
WxOAuth2UserInfo userInfo = wxMpService.getOAuth2Service().getUserInfo(accessToken, null);
int ii = 1;
}
} }
...@@ -22,6 +22,14 @@ ...@@ -22,6 +22,14 @@
</dependency> </dependency>
<!-- 公众号(包括订阅号和服务号):weixin-java-mp -->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>4.0.0</version>
</dependency>
<!-- fastbootWeixin的核心依赖 --> <!-- fastbootWeixin的核心依赖 -->
<dependency> <dependency>
<groupId>com.mxixm</groupId> <groupId>com.mxixm</groupId>
......
package com.sien.common.config;
import io.geekidea.springbootplus.config.properties.WxMpProperties;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.List;
import java.util.stream.Collectors;
/**
* wechat mp configuration
*
* @author Binary Wang(https://github.com/binarywang)
*/
@Configuration
//@EnableConfigurationProperties(WxMpProperties.class)
public class WxMpConfiguration {
// private final LogHandler logHandler;
// private final NullHandler nullHandler;
// private final KfSessionHandler kfSessionHandler;
// private final StoreCheckNotifyHandler storeCheckNotifyHandler;
// private final LocationHandler locationHandler;
// private final MenuHandler menuHandler;
// private final MsgHandler msgHandler;
// private final UnsubscribeHandler unsubscribeHandler;
// private final SubscribeHandler subscribeHandler;
// private final ScanHandler scanHandler;
// private final WxMpProperties properties;
@Autowired
private WxMpProperties properties;
@Bean
public WxMpService wxMpService() {
// 代码里 getConfigs()处报错的同学,请注意仔细阅读项目说明,你的IDE需要引入lombok插件!!!!
final List<WxMpProperties.MpConfig> configs = this.properties.getConfigs();
if (configs == null) {
throw new RuntimeException("添加下相关配置,注意别配错了!");
}
WxMpService service = new WxMpServiceImpl();
service.setMultiConfigStorages(configs
.stream().map(a -> {
WxMpDefaultConfigImpl configStorage;
// if (this.properties.isUseRedis()) {
// final WxMpProperties.RedisConfig redisConfig = this.properties.getRedisConfig();
// JedisPool jedisPool = new JedisPool(redisConfig.getHost(), redisConfig.getPort());
// configStorage = new WxMpRedisConfigImpl(new JedisWxRedisOps(jedisPool), a.getAppId());
// } else {
configStorage = new WxMpDefaultConfigImpl();
// }
configStorage.setAppId(a.getAppId());
configStorage.setSecret(a.getSecret());
configStorage.setToken(a.getToken());
configStorage.setAesKey(a.getAesKey());
return configStorage;
}).collect(Collectors.toMap(WxMpDefaultConfigImpl::getAppId, a -> a, (o, n) -> o)));
return service;
}
// @Bean
// public WxMpMessageRouter messageRouter(WxMpService wxMpService) {
// final WxMpMessageRouter newRouter = new WxMpMessageRouter(wxMpService);
//
// // 记录所有事件的日志 (异步执行)
// newRouter.rule().handler(this.logHandler).next();
//
// // 接收客服会话管理事件
// newRouter.rule().async(false).msgType(EVENT).event(KF_CREATE_SESSION)
// .handler(this.kfSessionHandler).end();
// newRouter.rule().async(false).msgType(EVENT).event(KF_CLOSE_SESSION)
// .handler(this.kfSessionHandler).end();
// newRouter.rule().async(false).msgType(EVENT).event(KF_SWITCH_SESSION)
// .handler(this.kfSessionHandler).end();
//
// // 门店审核事件
// newRouter.rule().async(false).msgType(EVENT).event(POI_CHECK_NOTIFY).handler(this.storeCheckNotifyHandler).end();
//
// // 自定义菜单事件
// newRouter.rule().async(false).msgType(EVENT).event(EventType.CLICK).handler(this.menuHandler).end();
//
// // 点击菜单连接事件
// newRouter.rule().async(false).msgType(EVENT).event(EventType.VIEW).handler(this.nullHandler).end();
//
// // 关注事件
// newRouter.rule().async(false).msgType(EVENT).event(SUBSCRIBE).handler(this.subscribeHandler).end();
//
// // 取消关注事件
// newRouter.rule().async(false).msgType(EVENT).event(UNSUBSCRIBE).handler(this.unsubscribeHandler).end();
//
// // 上报地理位置事件
// newRouter.rule().async(false).msgType(EVENT).event(EventType.LOCATION).handler(this.locationHandler).end();
//
// // 接收地理位置消息
// newRouter.rule().async(false).msgType(XmlMsgType.LOCATION).handler(this.locationHandler).end();
//
// // 扫码事件
// newRouter.rule().async(false).msgType(EVENT).event(EventType.SCAN).handler(this.scanHandler).end();
//
// // 默认
// newRouter.rule().async(false).handler(this.msgHandler).end();
//
// return newRouter;
// }
}
...@@ -2,6 +2,7 @@ package com.sien.common.controller; ...@@ -2,6 +2,7 @@ package com.sien.common.controller;
import com.sien.common.service.AppUserService; import com.sien.common.service.AppUserService;
import com.sien.common.vo.AppUserQueryVo; import com.sien.common.vo.AppUserQueryVo;
import com.sien.common.vo.app.LoginAppUserTokenVo;
import io.geekidea.springbootplus.framework.common.api.ApiResult; import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.common.controller.BaseController; import io.geekidea.springbootplus.framework.common.controller.BaseController;
import io.geekidea.springbootplus.framework.log.annotation.Module; import io.geekidea.springbootplus.framework.log.annotation.Module;
...@@ -13,6 +14,7 @@ import lombok.extern.slf4j.Slf4j; ...@@ -13,6 +14,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
/** /**
...@@ -36,12 +38,12 @@ public class WechatUserController extends BaseController { ...@@ -36,12 +38,12 @@ public class WechatUserController extends BaseController {
*/ */
@GetMapping("/check/") @GetMapping("/check/")
@OperationLog(name = "根据微信重定向code查询用户是否绑定", type = OperationLogType.INFO) @OperationLog(name = "根据微信重定向code查询用户是否绑定", type = OperationLogType.INFO)
@ApiOperation(value = "根据微信重定向code查询用户是否绑定", response = AppUserQueryVo.class) @ApiOperation(value = "根据微信重定向code查询用户是否绑定", notes = "用户每次从微信公众号菜单页跳转,都必须调用此接口,来判断用户是否已经绑定用户," +
public ApiResult<AppUserQueryVo> getAppUser() throws Exception { " 如果已经绑定就直接返回绑定用户的token,如没有绑定则需要用户进行注册,", response = AppUserQueryVo.class)
AppUserQueryVo appUserQueryVo = appUserService.getMyInfo(); public ApiResult<LoginAppUserTokenVo> checkAppUser(@RequestParam("code") String code) throws Exception {
return ApiResult.ok(appUserQueryVo);
}
return appUserService.checkWechatUserBinding(code);
}
} }
package com.sien.common.factory;
import io.geekidea.springbootplus.config.properties.WxMpProperties;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import java.util.stream.Collectors;
public class GetWx {
private static WxMpProperties properties;
public static WxMpService getWxMpService() {
// 代码里 getConfigs()处报错的同学,请注意仔细阅读项目说明,你的IDE需要引入lombok插件!!!!
final List<WxMpProperties.MpConfig> configs = properties.getConfigs();
if (configs == null) {
throw new RuntimeException("添加下相关配置,注意别配错了!");
}
WxMpService service = new WxMpServiceImpl();
service.setMultiConfigStorages(configs
.stream().map(a -> {
WxMpDefaultConfigImpl configStorage;
//
configStorage = new WxMpDefaultConfigImpl();
configStorage.setAppId(a.getAppId());
configStorage.setSecret(a.getSecret());
configStorage.setToken(a.getToken());
configStorage.setAesKey(a.getAesKey());
return configStorage;
}).collect(Collectors.toMap(WxMpDefaultConfigImpl::getAppId, a -> a, (o, n) -> o)));
return service;
}
@Autowired
public void setProperties(WxMpProperties properties) {
this.properties = properties;
}
}
...@@ -44,8 +44,8 @@ public class AppSmsRegisterParam implements Serializable { ...@@ -44,8 +44,8 @@ public class AppSmsRegisterParam implements Serializable {
@ApiModelProperty(value = "短信验证码", example = "666666", required = true) @ApiModelProperty(value = "短信验证码", example = "666666", required = true)
private String smsCode; private String smsCode;
@ApiModelProperty(value = "微信id", example = "adfadlsfadsf", required = false) @ApiModelProperty(value = "微信重定向地址中的code", example = "adfadlsfadsf", required = false)
private String openid; private String code;
@ApiModelProperty("姓名") @ApiModelProperty("姓名")
private String nickname; private String nickname;
......
...@@ -101,6 +101,12 @@ public interface AppUserService extends BaseService<AppUser> { ...@@ -101,6 +101,12 @@ public interface AppUserService extends BaseService<AppUser> {
/** /**
* 查询用户是否绑定微信
*/
ApiResult<LoginAppUserTokenVo> checkWechatUserBinding(String code) ;
/**
* 获取分页对象 * 获取分页对象
* *
* @param appUserPageParam * @param appUserPageParam
......
package com.sien.common.service.impl; package com.sien.common.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sien.common.entity.AppUser; import com.sien.common.entity.AppUser;
import com.sien.common.param.app.AppSmsRegisterParam; import com.sien.common.param.app.AppSmsRegisterParam;
...@@ -10,9 +9,15 @@ import com.sien.common.service.AppUserApiService; ...@@ -10,9 +9,15 @@ import com.sien.common.service.AppUserApiService;
import com.sien.common.service.AppUserService; import com.sien.common.service.AppUserService;
import com.sien.common.vo.AppUserQueryVo; import com.sien.common.vo.AppUserQueryVo;
import com.sien.common.vo.app.LoginAppUserTokenVo; import com.sien.common.vo.app.LoginAppUserTokenVo;
import io.geekidea.springbootplus.config.properties.WxMpProperties;
import io.geekidea.springbootplus.framework.common.api.ApiCode; import io.geekidea.springbootplus.framework.common.api.ApiCode;
import io.geekidea.springbootplus.framework.common.api.ApiResult; import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.shiro.jwt.JwtToken; import io.geekidea.springbootplus.framework.shiro.jwt.JwtToken;
import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.apache.shiro.SecurityUtils; import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -20,6 +25,7 @@ import org.springframework.stereotype.Service; ...@@ -20,6 +25,7 @@ import org.springframework.stereotype.Service;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
@Service @Service
public class AppUserApiServiceImpl implements AppUserApiService { public class AppUserApiServiceImpl implements AppUserApiService {
...@@ -28,7 +34,32 @@ public class AppUserApiServiceImpl implements AppUserApiService { ...@@ -28,7 +34,32 @@ public class AppUserApiServiceImpl implements AppUserApiService {
private AppSmsService appSmsService; private AppSmsService appSmsService;
@Autowired @Autowired
private AppUserService appUserService; private AppUserService appUserService;
// @Autowired
// private WxMpService wxMpService;
@Autowired
private WxMpProperties properties;
private WxMpService getWxMpService() {
// 代码里 getConfigs()处报错的同学,请注意仔细阅读项目说明,你的IDE需要引入lombok插件!!!!
final List<WxMpProperties.MpConfig> configs = this.properties.getConfigs();
if (configs == null) {
throw new RuntimeException("添加下相关配置,注意别配错了!");
}
WxMpService service = new WxMpServiceImpl();
service.setMultiConfigStorages(configs
.stream().map(a -> {
WxMpDefaultConfigImpl configStorage;
//
configStorage = new WxMpDefaultConfigImpl();
configStorage.setAppId(a.getAppId());
configStorage.setSecret(a.getSecret());
configStorage.setToken(a.getToken());
configStorage.setAesKey(a.getAesKey());
return configStorage;
}).collect(Collectors.toMap(WxMpDefaultConfigImpl::getAppId, a -> a, (o, n) -> o)));
return service;
}
@Override @Override
public ApiResult<Boolean> updatePhone(String phoneArea, String phone, String code) throws Exception { public ApiResult<Boolean> updatePhone(String phoneArea, String phone, String code) throws Exception {
...@@ -90,11 +121,18 @@ public class AppUserApiServiceImpl implements AppUserApiService { ...@@ -90,11 +121,18 @@ public class AppUserApiServiceImpl implements AppUserApiService {
return appUserService.login(loginParam, language, true); return appUserService.login(loginParam, language, true);
} }
WxOAuth2AccessToken accessToken = getWxMpService().getOAuth2Service().getAccessToken(loginParam.getCode());
// 从微信获取用户信息
WxOAuth2UserInfo wechatUserInfo = getWxMpService().getOAuth2Service().getUserInfo(accessToken, null);
// 没注册则先保存到数据库 // 没注册则先保存到数据库
AppUser appUser = new AppUser(); AppUser appUser = new AppUser();
appUser.setNickname(wechatUserInfo.getNickname());
appUser.setHead(wechatUserInfo.getHeadImgUrl());
appUser.setLocation(wechatUserInfo.getCountry() + wechatUserInfo.getProvince() + wechatUserInfo.getCity());
appUser.setPhoneArea(loginParam.getPhoneArea()); appUser.setPhoneArea(loginParam.getPhoneArea());
appUser.setPhone(loginParam.getPhone()); appUser.setPhone(loginParam.getPhone());
appUser.setWechatOpenId(loginParam.getOpenid()); appUser.setWechatOpenId(wechatUserInfo.getOpenid());
boolean isDbOk = appUserService.saveAppUser(appUser); boolean isDbOk = appUserService.saveAppUser(appUser);
if (!isDbOk) { if (!isDbOk) {
return ApiResult.fail(ApiCode.SPRING_BOOT_PLUS_EXCEPTION, new LoginAppUserTokenVo()); return ApiResult.fail(ApiCode.SPRING_BOOT_PLUS_EXCEPTION, new LoginAppUserTokenVo());
......
...@@ -15,6 +15,7 @@ import com.sien.common.vo.AppUserQueryVo; ...@@ -15,6 +15,7 @@ import com.sien.common.vo.AppUserQueryVo;
import com.sien.common.vo.app.LoginAppUserTokenVo; import com.sien.common.vo.app.LoginAppUserTokenVo;
import io.geekidea.springbootplus.config.properties.JwtProperties; import io.geekidea.springbootplus.config.properties.JwtProperties;
import io.geekidea.springbootplus.config.properties.SpringBootPlusProperties; import io.geekidea.springbootplus.config.properties.SpringBootPlusProperties;
import io.geekidea.springbootplus.config.properties.WxMpProperties;
import io.geekidea.springbootplus.framework.common.api.ApiCode; import io.geekidea.springbootplus.framework.common.api.ApiCode;
import io.geekidea.springbootplus.framework.common.api.ApiResult; import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.common.service.impl.BaseServiceImpl; import io.geekidea.springbootplus.framework.common.service.impl.BaseServiceImpl;
...@@ -27,6 +28,11 @@ import io.geekidea.springbootplus.framework.shiro.util.JwtUtil; ...@@ -27,6 +28,11 @@ import io.geekidea.springbootplus.framework.shiro.util.JwtUtil;
import io.geekidea.springbootplus.framework.shiro.util.SaltUtil; import io.geekidea.springbootplus.framework.shiro.util.SaltUtil;
import io.geekidea.springbootplus.framework.shiro.vo.LoginUserVo; import io.geekidea.springbootplus.framework.shiro.vo.LoginUserVo;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.codec.digest.DigestUtils;
import org.apache.shiro.SecurityUtils; import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject; import org.apache.shiro.subject.Subject;
...@@ -38,7 +44,9 @@ import org.springframework.stereotype.Service; ...@@ -38,7 +44,9 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.time.Duration; import java.time.Duration;
import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
; ;
...@@ -53,6 +61,9 @@ import java.util.concurrent.TimeUnit; ...@@ -53,6 +61,9 @@ import java.util.concurrent.TimeUnit;
public class AppUserServiceImpl extends BaseServiceImpl<AppUserMapper, AppUser> implements AppUserService { public class AppUserServiceImpl extends BaseServiceImpl<AppUserMapper, AppUser> implements AppUserService {
@Autowired @Autowired
private WxMpProperties properties;
@Autowired
private RedisTemplate redisTemplate; private RedisTemplate redisTemplate;
@Lazy @Lazy
...@@ -69,6 +80,7 @@ public class AppUserServiceImpl extends BaseServiceImpl<AppUserMapper, AppUser> ...@@ -69,6 +80,7 @@ public class AppUserServiceImpl extends BaseServiceImpl<AppUserMapper, AppUser>
@Autowired @Autowired
private AppUserMapper appUserMapper; private AppUserMapper appUserMapper;
@Override @Override
public boolean hasUserByPhoneNumer(String phoneArea, String phone) { public boolean hasUserByPhoneNumer(String phoneArea, String phone) {
Integer selectCount = appUserMapper.selectCount(new QueryWrapper<AppUser>( Integer selectCount = appUserMapper.selectCount(new QueryWrapper<AppUser>(
...@@ -104,6 +116,12 @@ public class AppUserServiceImpl extends BaseServiceImpl<AppUserMapper, AppUser> ...@@ -104,6 +116,12 @@ public class AppUserServiceImpl extends BaseServiceImpl<AppUserMapper, AppUser>
return ApiResult.fail(ApiCode.USER_NOT_FOUND, language); return ApiResult.fail(ApiCode.USER_NOT_FOUND, language);
} }
LoginAppUserTokenVo loginAppUserTokenVo = getLoginAppUserTokenVo(hasRegister, appUser);
return ApiResult.ok(loginAppUserTokenVo);
}
private LoginAppUserTokenVo getLoginAppUserTokenVo(Boolean hasRegister, AppUser appUser) {
// 将系统用户对象转换成登录用户对象 // 将系统用户对象转换成登录用户对象
LoginUserVo loginSysUserVo = new LoginUserVo(); LoginUserVo loginSysUserVo = new LoginUserVo();
...@@ -145,8 +163,7 @@ public class AppUserServiceImpl extends BaseServiceImpl<AppUserMapper, AppUser> ...@@ -145,8 +163,7 @@ public class AppUserServiceImpl extends BaseServiceImpl<AppUserMapper, AppUser>
loginAppUserTokenVo.setToken(token); loginAppUserTokenVo.setToken(token);
// loginAppUserTokenVo.setId(appUser.getId()); // loginAppUserTokenVo.setId(appUser.getId());
loginAppUserTokenVo.setHasRegister(hasRegister); loginAppUserTokenVo.setHasRegister(hasRegister);
return ApiResult.ok(loginAppUserTokenVo); return loginAppUserTokenVo;
} }
...@@ -197,11 +214,59 @@ public class AppUserServiceImpl extends BaseServiceImpl<AppUserMapper, AppUser> ...@@ -197,11 +214,59 @@ public class AppUserServiceImpl extends BaseServiceImpl<AppUserMapper, AppUser>
return appUserById; return appUserById;
} }
private WxMpService getWxMpService() {
// 代码里 getConfigs()处报错的同学,请注意仔细阅读项目说明,你的IDE需要引入lombok插件!!!!
final List<WxMpProperties.MpConfig> configs = this.properties.getConfigs();
if (configs == null) {
throw new RuntimeException("添加下相关配置,注意别配错了!");
}
WxMpService service = new WxMpServiceImpl();
service.setMultiConfigStorages(configs
.stream().map(a -> {
WxMpDefaultConfigImpl configStorage;
//
configStorage = new WxMpDefaultConfigImpl();
configStorage.setAppId(a.getAppId());
configStorage.setSecret(a.getSecret());
configStorage.setToken(a.getToken());
configStorage.setAesKey(a.getAesKey());
return configStorage;
}).collect(Collectors.toMap(WxMpDefaultConfigImpl::getAppId, a -> a, (o, n) -> o)));
return service;
}
@Override
public ApiResult<LoginAppUserTokenVo> checkWechatUserBinding(String code) {
WxOAuth2AccessToken accessToken = null;
try {
accessToken = getWxMpService().getOAuth2Service().getAccessToken(code);
} catch (WxErrorException e) {
e.printStackTrace();
}
// 查询openId是否被绑定
String openId = accessToken.getOpenId();
AppUser appUser = super.getOne(new QueryWrapper<AppUser>().lambda().eq(AppUser::getWechatOpenId, openId));
if (appUser == null) {
// 返回告诉前端没有绑定过
return ApiResult.ok(new LoginAppUserTokenVo().setHasRegister(Boolean.FALSE));
} else {
// 有绑定直接返回用户token
LoginAppUserTokenVo loginAppUserTokenVo = getLoginAppUserTokenVo(Boolean.TRUE, appUser);
return ApiResult.ok(loginAppUserTokenVo);
}
}
@Override @Override
public Paging<AppUserQueryVo> getAppUserPageList(AppUserPageParam appUserPageParam) throws Exception { public Paging<AppUserQueryVo> getAppUserPageList(AppUserPageParam appUserPageParam) throws Exception {
Page<AppUserQueryVo> page = new PageInfo<>(appUserPageParam, OrderItem.desc(getLambdaColumn(AppUser::getCreateTime))); Page<AppUserQueryVo> page = new PageInfo<>(appUserPageParam, OrderItem.desc(getLambdaColumn(AppUser::getCreateTime)));
IPage<AppUserQueryVo> iPage = appUserMapper.getAppUserPageList(page, appUserPageParam); IPage<AppUserQueryVo> iPage = appUserMapper.getAppUserPageList(page, appUserPageParam);
return new Paging<AppUserQueryVo>(iPage); return new Paging<AppUserQueryVo>(iPage);
} }
} }
...@@ -34,10 +34,9 @@ public class LoginAppUserTokenVo implements LoginToken { ...@@ -34,10 +34,9 @@ public class LoginAppUserTokenVo implements LoginToken {
@ApiModelProperty("token") @ApiModelProperty("token")
private String token; private String token;
@ApiModelProperty("是否已经注册, true = 已经注册, false = 没有注册") @ApiModelProperty("是否已经注册, true = 已经绑定, false = 没有绑定,需要跳转到注册页")
private Boolean hasRegister; private Boolean hasRegister;
@ApiModelProperty("用户名") @ApiModelProperty("用户名")
private String username; private String username;
......
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2019-2029 geekidea(https://github.com/geekidea)
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
......
package io.geekidea.springbootplus.config.properties;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* wechat mp properties
*
* @author Binary Wang(https://github.com/binarywang)
*/
@Data
@Component
@ConfigurationProperties(prefix = "wx.mp")
public class WxMpProperties {
/**
* 是否使用redis存储access token
*/
private boolean useRedis;
/**
* redis 配置
*/
private RedisConfig redisConfig;
@Data
public static class RedisConfig {
/**
* redis服务器 主机地址
*/
private String host;
/**
* redis服务器 端口号
*/
private Integer port;
}
/**
* 多个公众号配置信息
*/
private List<MpConfig> configs;
@Data
public static class MpConfig {
/**
* 设置微信公众号的appid
*/
private String appId;
/**
* 设置微信公众号的app secret
*/
private String secret;
/**
* 设置微信公众号的token
*/
private String token;
/**
* 设置微信公众号的EncodingAESKey
*/
private String aesKey;
}
//
// @Override
// public String toString() {
// return JsonUtils.toJson(this);
// }
}
...@@ -13,13 +13,29 @@ spring-boot-plus: ...@@ -13,13 +13,29 @@ spring-boot-plus:
response-log-format: false response-log-format: false
wx: wx:
token: sDsdaSDADad # token: sDsdaSDADad
# 测试号的appid,测试号管理界面有 # # 测试号的appid,测试号管理界面有
appid: wx7aac805012428dc5 # appid: wx7aac805012428dc5
# 测试号的appsecret,测试号管理界面有 # # 测试号的appsecret,测试号管理界面有
appsecret: # appsecret:
# =服务调用的url地址,用于微信web的oauth2授权回调等,若没有可为空。 # # =服务调用的url地址,用于微信web的oauth2授权回调等,若没有可为空。
callback-url: wx7aac805012428dc5 # callback-url: wx7aac805012428dc5
#
mp:
useRedis: false
redisConfig:
host: 127.0.0.1
port: 6379
configs:
- appId: wxe4a696e431b0456b # 第一个公众号的appid
secret: 87dbca535a713cd505be2f928fa84c29 # 公众号的appsecret
token: 111 # 接口配置里的Token值
aesKey: 111 # 接口配置里的EncodingAESKey值
- appId: 2222 # 第二个公众号的appid,以下同上
secret: 1111
token: 111
aesKey: 111
spring: spring:
datasource: datasource:
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
<version>2.0</version> <version>2.0</version>
<packaging>pom</packaging> <packaging>pom</packaging>
<name>Jumeirah</name> <name>SiEn</name>
<url>https://github.com/geekidea/spring-boot-plus</url> <url>https://github.com/geekidea/spring-boot-plus</url>
<properties> <properties>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment