Browse Source

feat:小程序登录

zhanghui 3 months ago
parent
commit
bc1b18a15d

+ 24 - 28
src/main/java/org/springblade/modules/auth/granter/WeChatTokenGranter.java

@@ -57,40 +57,36 @@ public class WeChatTokenGranter implements ITokenGranter {
 		// 获取openId
 		JSONObject jsonObject = userWeChatService.getopenId(code);
 		String openId = jsonObject.getString("openid");
+		String unionId = jsonObject.getString("unionid");
 		User user = new User();
 		UserWeChat userWeChat = userWeChatService.lambdaQuery().eq(UserWeChat::getOpenId, openId).one();
 
 		if (userWeChat == null){
-			User byAccount = userService.getByAccount(phoneNumber);
-			if (byAccount == null){
-				//该微信号未注册 注册用户
-				user.setName("微信用户");
-				user.setAccount(phoneNumber);
-				user.setPhone(phoneNumber);
-				user.setRealName("用户" + phoneNumber.substring(7));
-				user.setSex(1);
-				user.setUserType(2);//app
-				userService.save(user);
-
-				UserApp userApp = new UserApp();
-				userApp.setUserId(user.getId());
-				userApp.setState(YesOrNoEnum.YES.getValue());
-				userApp.setNowMoney(new BigDecimal(0));
-				userApp.setLivingHall(YesOrNoEnum.NO.getValue());
-				userApp.setIsStore(YesOrNoEnum.NO.getValue());
-				userAppService.save(userApp);
-				userWeChat = new UserWeChat();
-				userWeChat.setOpenId(openId);
-				userWeChat.setUnionId(jsonObject.getString("unionid"));
-				userWeChat.setUserId(user.getId());
-			}else{
-				//手机号已被注册。给该用户绑定openId
-				userWeChat = new UserWeChat();
-				userWeChat.setOpenId(openId);
-				userWeChat.setUnionId(jsonObject.getString("unionid"));
-				userWeChat.setUserId(byAccount.getId());
+//			User byAccount = userService.getByAccount(phoneNumber);
+			User byAccount = userService.getByAccountAndType(phoneNumber,UserEnum.APP.getCategory());
+			if (Objects.nonNull(byAccount)){
+				throw new ServiceException("您提供的手机号已注册,请更换手机号!");
 			}
+			//注册用户
+			user.setName("微信用户");
+			user.setAccount(phoneNumber);
+			user.setPhone(phoneNumber);
+			user.setRealName("用户" + phoneNumber.substring(7));
+			user.setSex(1);
+			user.setUserType(UserEnum.APP.getCategory());//app
+			userService.save(user);
 
+			UserApp userApp = new UserApp();
+			userApp.setUserId(user.getId());
+			userApp.setState(YesOrNoEnum.YES.getValue());
+			userApp.setNowMoney(new BigDecimal(0));
+			userApp.setLivingHall(YesOrNoEnum.NO.getValue());
+			userApp.setIsStore(YesOrNoEnum.NO.getValue());
+			userAppService.save(userApp);
+			userWeChat = new UserWeChat();
+			userWeChat.setOpenId(openId);
+			userWeChat.setUnionId(unionId);
+			userWeChat.setUserId(user.getId());
 		}
 		user = userService.getById(userWeChat.getUserId());
 		userWeChat.setSessionKey(jsonObject.getString("session_key"));

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

@@ -316,4 +316,12 @@ public interface IUserService extends BaseService<User> {
 	 * @param userId
 	 */
 	void removeRoleByRoleName(Long userId, String roleName);
+
+	/**
+	 * 根据账号和用户类型查询用户
+	 * @param phoneNumber 账号
+	 * @param category 类型
+	 * @return
+	 */
+	User getByAccountAndType(String phoneNumber, int category);
 }

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

@@ -688,4 +688,12 @@ public class UserServiceImpl extends BaseServiceImpl<UserMapper, User> implement
 		}
 		updateById(user);
 	}
+
+	@Override
+	public User getByAccountAndType(String phoneNumber, int category) {
+		User user = getOne(Wrappers.lambdaQuery(User.class)
+				.eq(User::getAccount, phoneNumber)
+				.eq(User::getUserType, category));
+		return user;
+	}
 }