diff --git a/mallchat-common/src/main/java/com/abin/mallchat/common/common/exception/BusinessErrorEnum.java b/mallchat-common/src/main/java/com/abin/mallchat/common/common/exception/BusinessErrorEnum.java index c082a45..191bca2 100644 --- a/mallchat-common/src/main/java/com/abin/mallchat/common/common/exception/BusinessErrorEnum.java +++ b/mallchat-common/src/main/java/com/abin/mallchat/common/common/exception/BusinessErrorEnum.java @@ -12,7 +12,7 @@ import lombok.Getter; @Getter public enum BusinessErrorEnum implements ErrorEnum { //==================================common================================== - BUSINESS_ERROR(1001, "{}"), + BUSINESS_ERROR(1001, "{0}"), //==================================user================================== //==================================chat================================== SYSTEM_ERROR(1001, "系统出小差了,请稍后再试哦~~"), diff --git a/mallchat-common/src/main/java/com/abin/mallchat/common/common/utils/AssertUtil.java b/mallchat-common/src/main/java/com/abin/mallchat/common/common/utils/AssertUtil.java index 0ffc966..14e0a53 100644 --- a/mallchat-common/src/main/java/com/abin/mallchat/common/common/utils/AssertUtil.java +++ b/mallchat-common/src/main/java/com/abin/mallchat/common/common/utils/AssertUtil.java @@ -7,6 +7,7 @@ import com.abin.mallchat.common.common.exception.ErrorEnum; import com.abin.mallchat.common.user.domain.entity.UserBackpack; import org.apache.commons.lang3.StringUtils; +import java.text.MessageFormat; import java.util.Objects; /** @@ -71,7 +72,7 @@ public class AssertUtil { if (Objects.isNull(errorEnum)) { errorEnum = BusinessErrorEnum.BUSINESS_ERROR; } - throw new BusinessException(errorEnum.getErrorCode(), String.format(errorEnum.getErrorMsg(), arg)); + throw new BusinessException(errorEnum.getErrorCode(), MessageFormat.format(errorEnum.getErrorMsg(), arg)); } diff --git a/mallchat-common/src/main/java/com/abin/mallchat/common/user/dao/UserBackpackDao.java b/mallchat-common/src/main/java/com/abin/mallchat/common/user/dao/UserBackpackDao.java index 5e57ab0..c7ea47a 100644 --- a/mallchat-common/src/main/java/com/abin/mallchat/common/user/dao/UserBackpackDao.java +++ b/mallchat-common/src/main/java/com/abin/mallchat/common/user/dao/UserBackpackDao.java @@ -40,9 +40,9 @@ public class UserBackpackDao extends ServiceImpl { update.setItemId(badgeId); updateById(update); } + + public User getByName(String name) { + return lambdaQuery().eq(User::getName, name).one(); + } } diff --git a/mallchat-common/src/main/resources/application.yml b/mallchat-common/src/main/resources/application.yml index 4fc6110..7080519 100644 --- a/mallchat-common/src/main/resources/application.yml +++ b/mallchat-common/src/main/resources/application.yml @@ -20,6 +20,11 @@ spring: username: ${mallchat.mysql.username} password: ${mallchat.mysql.password} driver-class-name: com.mysql.cj.jdbc.Driver + hikari: + minimum-idle: 3 + maximum-pool-size: 10 + max-lifetime: 30000 #不能小于30秒,否则默认回到1800秒 + connection-test-query: SELECT 1 mvc: pathmatch: matching-strategy: ANT_PATH_MATCHER diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/WxMsgService.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/WxMsgService.java index 207f964..5be4d89 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/WxMsgService.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/WxMsgService.java @@ -102,15 +102,14 @@ public class WxMsgService { for (int i = 0; i < 5; i++) { try { userDao.updateById(update); - update.setName("名字重置" + RandomUtil.randomInt(100000)); return; } catch (DuplicateKeyException e) { log.info("fill userInfo duplicate uid:{},info:{}", uid, userInfo); } catch (Exception e) { log.error("fill userInfo fail uid:{},info:{}", uid, userInfo); } + update.setName("名字重置" + RandomUtil.randomInt(100000)); } - } private void login(Long uid, Integer eventKey) { @@ -119,6 +118,5 @@ public class WxMsgService { String token = loginService.login(uid); //推送前端登录成功 webSocketService.scanLoginSuccess(eventKey, user, token); - } } diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/adapter/UserAdapter.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/adapter/UserAdapter.java index 88a46ae..4f169a1 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/adapter/UserAdapter.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/adapter/UserAdapter.java @@ -2,6 +2,7 @@ package com.abin.mallchat.custom.user.service.adapter; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.util.ObjectUtil; +import cn.hutool.core.util.RandomUtil; import com.abin.mallchat.common.common.domain.enums.YesOrNoEnum; import com.abin.mallchat.common.user.domain.entity.ItemConfig; import com.abin.mallchat.common.user.domain.entity.User; @@ -37,6 +38,11 @@ public class UserAdapter { user.setAvatar(userInfo.getHeadImgUrl()); user.setName(userInfo.getNickname()); user.setSex(userInfo.getSex()); + if(userInfo.getNickname().length()>6){ + user.setName("名字过长"+RandomUtil.randomInt(100000)); + }else { + user.setName(userInfo.getNickname()); + } return user; } diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/impl/UserServiceImpl.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/impl/UserServiceImpl.java index c8b34f8..44d2d26 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/impl/UserServiceImpl.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/impl/UserServiceImpl.java @@ -62,21 +62,26 @@ public class UserServiceImpl implements UserService { @Override @Transactional public void modifyName(Long uid, ModifyNameReq req) { + //判断名字是不是重复 + User oldUser = userDao.getByName(req.getName()); + AssertUtil.isEmpty(oldUser,"名字已经被抢占了,请换一个哦~~"); //判断改名卡够不够 UserBackpack firstValidItem = userBackpackDao.getFirstValidItem(uid, ItemEnum.MODIFY_NAME_CARD.getId()); AssertUtil.isNotEmpty(firstValidItem, "改名次数不够了,等后续活动送改名卡哦"); //使用改名卡 - boolean useSuccess = userBackpackDao.invalidItem(firstValidItem.getItemId()); + boolean useSuccess = userBackpackDao.invalidItem(firstValidItem.getId()); if (useSuccess) {//用乐观锁,就不用分布式锁了 //改名 userDao.modifyName(uid, req.getName()); + //删除缓存 + userCache.delUserInfo(uid); } } @Override public List badges(Long uid) { //查询所有徽章 - List itemConfigs =itemCache.getByType(ItemTypeEnum.BADGE.getType()); + List itemConfigs = itemCache.getByType(ItemTypeEnum.BADGE.getType()); //查询用户拥有的徽章 List backpacks = userBackpackDao.getByItemIds(uid, itemConfigs.stream().map(ItemConfig::getId).collect(Collectors.toList())); //查询用户当前佩戴的标签 @@ -101,6 +106,6 @@ public class UserServiceImpl implements UserService { public void register(String openId) { User insert = User.builder().openId(openId).build(); userDao.save(insert); - applicationEventPublisher.publishEvent(new UserRegisterEvent(this,insert)); + applicationEventPublisher.publishEvent(new UserRegisterEvent(this, insert)); } }