Browse Source

feat:判断微信用户是否注册过,支持静默登录

zhanghui 1 month ago
parent
commit
25b8854b82

+ 2 - 0
src/main/java/org/springblade/Application.java

@@ -32,6 +32,8 @@ public class Application {
 
 
 	public static void main(String[] args) {
 	public static void main(String[] args) {
 		BladeApplication.run(CommonConstant.APPLICATION_NAME, Application.class, args);
 		BladeApplication.run(CommonConstant.APPLICATION_NAME, Application.class, args);
+
+		System.out.println("++++++++++++++++++项目启动成功!!!++++++++++++++++++++");
 	}
 	}
 
 
 }
 }

+ 10 - 1
src/main/java/org/springblade/modules/api/controller/LoginController.java

@@ -236,7 +236,7 @@ public class LoginController extends BladeController {
 	@ApiOperationSupport(order = 2)
 	@ApiOperationSupport(order = 2)
 	@ApiOperation(value = "微信授权登录", tags = "通过 getPhoneNumberCode 获取手机号")
 	@ApiOperation(value = "微信授权登录", tags = "通过 getPhoneNumberCode 获取手机号")
 	public R wxAuthLogin(@ApiParam(value = "微信code", required = true) @RequestParam String code,
 	public R wxAuthLogin(@ApiParam(value = "微信code", required = true) @RequestParam String code,
-						 @ApiParam(value = "获取手机号的code,五分钟有效", required = true) @RequestParam String getPhoneNumberCode) {
+						 @ApiParam(value = "获取手机号的code,五分钟有效", required = false) @RequestParam(required = false) String getPhoneNumberCode) {
 
 
 		String tenantId = WebUtil.getRequest().getHeader(TokenUtil.TENANT_HEADER_KEY);
 		String tenantId = WebUtil.getRequest().getHeader(TokenUtil.TENANT_HEADER_KEY);
 		TokenParameter tokenParameter = new TokenParameter();
 		TokenParameter tokenParameter = new TokenParameter();
@@ -258,6 +258,15 @@ public class LoginController extends BladeController {
 		return R.data(TokenUtil.createAuthInfo(userInfo));
 		return R.data(TokenUtil.createAuthInfo(userInfo));
 	}
 	}
 
 
+	@ApiLog("判断微信用户是否注册过")
+	@PostMapping("/login/isUser")
+	@ApiOperationSupport(order = 2)
+	@ApiOperation(value = "判断微信用户是否注册过")
+	public R<Boolean> isUser(@ApiParam(value = "微信code", required = true) @RequestParam String code) {
+		Boolean b = userService.isUser(code);
+		return R.data(b);
+	}
+
 	@ApiLog("登录用户验证-手机验证码登录")
 	@ApiLog("登录用户验证-手机验证码登录")
 	@PostMapping("/login_phone")
 	@PostMapping("/login_phone")
 	@ApiOperationSupport(order = 3)
 	@ApiOperationSupport(order = 3)

+ 9 - 1
src/main/java/org/springblade/modules/auth/granter/WeChatTokenGranter.java

@@ -51,8 +51,13 @@ public class WeChatTokenGranter implements ITokenGranter {
 		String getPhoneNumberCode = tokenParameter.getArgs().getStr("getPhoneNumberCode");
 		String getPhoneNumberCode = tokenParameter.getArgs().getStr("getPhoneNumberCode");
 		String code = tokenParameter.getArgs().getStr("code");
 		String code = tokenParameter.getArgs().getStr("code");
 
 
+
 		// 获取用户手机号
 		// 获取用户手机号
-		String phoneNumber = userWeChatService.getPhoneNumber(getPhoneNumberCode);
+		String phoneNumber = null;
+		if (Objects.nonNull(getPhoneNumberCode)){
+			 phoneNumber = userWeChatService.getPhoneNumber(getPhoneNumberCode);
+		}
+
 
 
 		// 获取openId
 		// 获取openId
 		JSONObject jsonObject = userWeChatService.getopenId(code);
 		JSONObject jsonObject = userWeChatService.getopenId(code);
@@ -62,6 +67,9 @@ public class WeChatTokenGranter implements ITokenGranter {
 		UserWeChat userWeChat = userWeChatService.lambdaQuery().eq(UserWeChat::getOpenId, openId).one();
 		UserWeChat userWeChat = userWeChatService.lambdaQuery().eq(UserWeChat::getOpenId, openId).one();
 
 
 		if (userWeChat == null){
 		if (userWeChat == null){
+			if (Objects.isNull(getPhoneNumberCode)){
+				throw new ServiceException("该用户没有注册!");
+			}
 //			User byAccount = userService.getByAccount(phoneNumber);
 //			User byAccount = userService.getByAccount(phoneNumber);
 			User byAccount = userService.getByAccountAndType(phoneNumber,UserEnum.APP.getCategory());
 			User byAccount = userService.getByAccountAndType(phoneNumber,UserEnum.APP.getCategory());
 			if (Objects.nonNull(byAccount)){
 			if (Objects.nonNull(byAccount)){

+ 2 - 0
src/main/java/org/springblade/modules/system/service/IUserService.java

@@ -324,4 +324,6 @@ public interface IUserService extends BaseService<User> {
 	 * @return
 	 * @return
 	 */
 	 */
 	User getByAccountAndType(String phoneNumber, int category);
 	User getByAccountAndType(String phoneNumber, int category);
+
+    Boolean isUser(String code);
 }
 }

+ 11 - 0
src/main/java/org/springblade/modules/system/service/impl/UserServiceImpl.java

@@ -19,6 +19,7 @@ package org.springblade.modules.system.service.impl;
 
 
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.util.StrUtil;
 import cn.hutool.core.util.StrUtil;
+import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.Wrapper;
 import com.baomidou.mybatisplus.core.conditions.Wrapper;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.core.metadata.IPage;
@@ -85,6 +86,8 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, User> implement
 	private UserAppService userAppService;
 	private UserAppService userAppService;
 	@Resource
 	@Resource
 	private IUserMerchantService iUserMerchantService;
 	private IUserMerchantService iUserMerchantService;
+	@Resource
+	private  IUserWeChatService userWeChatService;
 
 
 	@Override
 	@Override
 	@Transactional(rollbackFor = Exception.class)
 	@Transactional(rollbackFor = Exception.class)
@@ -696,4 +699,12 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, User> implement
 				.eq(User::getUserType, category));
 				.eq(User::getUserType, category));
 		return user;
 		return user;
 	}
 	}
+
+	@Override
+	public Boolean isUser(String code) {
+		JSONObject jsonObject = userWeChatService.getopenId(code);
+		String openId = jsonObject.getString("openid");
+		UserWeChat userWeChat = userWeChatService.lambdaQuery().eq(UserWeChat::getOpenId, openId).one();
+		return userWeChat != null;
+	}
 }
 }