feat(国际化): 新增国际化

This commit is contained in:
gx_ma
2026-03-24 11:41:41 +08:00
parent abe46baf18
commit e70c198071
97 changed files with 10802 additions and 5302 deletions

View File

@@ -59,7 +59,7 @@ public interface ISocialLoginService {
* @param loginId 登录id
* @return
*/
AjaxResult socialLogin(String loginId);
AjaxResult socialLogin(String loginId, String language);
/**
* 绑定登录api
@@ -67,7 +67,7 @@ public interface ISocialLoginService {
* @param bindLoginBody 绑定账户参数
* @return
*/
AjaxResult bindLogin(BindLoginBody bindLoginBody);
AjaxResult bindLogin(BindLoginBody bindLoginBody, String language);
/**
* 注册绑定api
@@ -75,7 +75,7 @@ public interface ISocialLoginService {
* @param bindRegisterBody
* @return
*/
AjaxResult bindRegister(BindRegisterBody bindRegisterBody);
AjaxResult bindRegister(BindRegisterBody bindRegisterBody, String language);
String genBindId(AuthUser authUser);

View File

@@ -4,6 +4,7 @@ import com.fastbee.common.core.domain.AjaxResult;
import com.fastbee.common.core.domain.entity.SysRole;
import com.fastbee.common.core.domain.entity.SysUser;
import com.fastbee.common.utils.DateUtils;
import com.fastbee.common.utils.MessageUtils;
import com.fastbee.iot.domain.Category;
import com.fastbee.iot.mapper.CategoryMapper;
import com.fastbee.iot.model.IdAndName;
@@ -124,12 +125,12 @@ public class CategoryServiceImpl implements ICategoryService
{
int productCount=categoryMapper.productCountInCategorys(categoryIds);
if(productCount>0){
return AjaxResult.error("删除失败,请先删除对应分类下的产品");
return AjaxResult.error(MessageUtils.message("delete.fail.please.delete.category.product"));
}
if(categoryMapper.deleteCategoryByCategoryIds(categoryIds)>0){
return AjaxResult.success("删除成功");
return AjaxResult.success(MessageUtils.message("delete.success"));
}
return AjaxResult.error("删除失败");
return AjaxResult.error(MessageUtils.message("delete.fail"));
}
/**

View File

@@ -18,6 +18,7 @@ import com.fastbee.common.enums.DeviceStatus;
import com.fastbee.common.enums.ThingsModelType;
import com.fastbee.common.exception.ServiceException;
import com.fastbee.common.utils.DateUtils;
import com.fastbee.common.utils.MessageUtils;
import com.fastbee.common.utils.StringUtils;
import com.fastbee.common.utils.http.HttpUtils;
import com.fastbee.common.utils.ip.IpUtils;
@@ -678,8 +679,7 @@ public class DeviceServiceImpl implements IDeviceService {
// 设备编号唯一检查
Device existDevice = deviceMapper.selectDeviceBySerialNumber(device.getSerialNumber());
if (existDevice != null) {
log.error("设备编号:" + device.getSerialNumber() + "已经存在了,新增设备失败");
throw new ServiceException("设备编号:" + device.getSerialNumber() + " 已经存在,新增失败");
throw new ServiceException(MessageUtils.message("device.insert.fail.device.number.already.exist", device.getSerialNumber()));
}
SysUser sysUser = getLoginUser().getUser();
//添加设备
@@ -747,7 +747,7 @@ public class DeviceServiceImpl implements IDeviceService {
Device existDevice = deviceMapper.selectDeviceBySerialNumber(deviceRelateUserInput.getDeviceNumberAndProductIds().get(i).getDeviceNumber());
if (existDevice != null) {
if (existDevice.getUserId().longValue() == deviceRelateUserInput.getUserId().longValue()) {
return AjaxResult.error("用户已经拥有设备:" + existDevice.getDeviceName() + ", 设备编号:" + existDevice.getSerialNumber());
return AjaxResult.error(MessageUtils.message("now.user.belong.device.can.not.repeat.share", existDevice.getSerialNumber()));
}
// 先删除设备的所有用户
deviceUserMapper.deleteDeviceUserByDeviceId(new UserIdDeviceIdModel(null, existDevice.getDeviceId()));
@@ -774,11 +774,11 @@ public class DeviceServiceImpl implements IDeviceService {
deviceRelateUserInput.getUserId(),
deviceRelateUserInput.getDeviceNumberAndProductIds().get(i).getProductId());
if (result == 0) {
return AjaxResult.error("设备不存在,自动添加设备时失败,请检查产品编号是否正确");
return AjaxResult.error(MessageUtils.message("device.not.exist.add.fail.please.check.product.id.is.correct"));
}
}
}
return AjaxResult.success("添加设备成功");
return AjaxResult.success(MessageUtils.message("device.add.success"));
}
/**
@@ -1240,7 +1240,7 @@ public class DeviceServiceImpl implements IDeviceService {
DeviceMqttConnectVO connectVO = new DeviceMqttConnectVO();
DeviceMqttVO deviceMqttVO = deviceMapper.selectMqttConnectData(deviceId);
if (deviceMqttVO == null) {
throw new ServiceException("获取设备MQTT连接参数失败");
throw new ServiceException(MessageUtils.message("device.get.mqtt.connection.param.fail"));
}
// 不管认证方式,目前就只返回简单认证方式
String password;
@@ -1248,7 +1248,7 @@ public class DeviceServiceImpl implements IDeviceService {
// 查询产品授权码
List<ProductAuthorize> productAuthorizeList = productAuthorizeService.listByProductId(deviceMqttVO.getProductId());
if (CollectionUtils.isEmpty(productAuthorizeList)) {
throw new ServiceException("产品已启用授权,获取设备授权码失败,请先配置授权码");
throw new ServiceException(MessageUtils.message("device.get.authorization.fail.please.config"));
}
List<ProductAuthorize> collect = productAuthorizeList.stream().filter(p -> p.getProductId().equals(deviceMqttVO.getDeviceId())).collect(Collectors.toList());
ProductAuthorize productAuthorize = CollectionUtils.isEmpty(collect) ? productAuthorizeList.get(0) : collect.get(0);

View File

@@ -4,6 +4,7 @@ import java.util.List;
import com.fastbee.common.core.domain.AjaxResult;
import com.fastbee.common.utils.DateUtils;
import com.fastbee.common.utils.MessageUtils;
import com.fastbee.iot.model.IdAndName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@@ -95,12 +96,12 @@ public class NewsCategoryServiceImpl implements INewsCategoryService
{
int productCount=newsCategoryMapper.newsCountInCategorys(categoryIds);
if(productCount>0){
return AjaxResult.error("删除失败,请先删除对应分类下的新闻资讯");
return AjaxResult.error(MessageUtils.message("newsCategory.delete.fail.please.delete.category.info"));
}
if(newsCategoryMapper.deleteNewsCategoryByCategoryIds(categoryIds)>0){
return AjaxResult.success("删除成功");
return AjaxResult.success(MessageUtils.message("delete.success"));
}
return AjaxResult.error("删除失败");
return AjaxResult.error(MessageUtils.message("delete.fail"));
}
/**

View File

@@ -6,6 +6,7 @@ import com.fastbee.common.core.domain.entity.SysUser;
import com.fastbee.common.core.redis.RedisCache;
import com.fastbee.common.core.redis.RedisKeyBuilder;
import com.fastbee.common.utils.DateUtils;
import com.fastbee.common.utils.MessageUtils;
import com.fastbee.iot.domain.Product;
import com.fastbee.iot.domain.ThingsModelTemplate;
import com.fastbee.iot.mapper.DeviceMapper;
@@ -196,7 +197,7 @@ public class ProductServiceImpl implements IProductService
public AjaxResult changeProductStatus(ChangeProductStatusModel model)
{
if(model.getStatus()!=1 && model.getStatus()!=2){
return AjaxResult.error("状态更新失败,状态值有误");
return AjaxResult.error(MessageUtils.message("product.status.update.fail.value.fail"));
}
if(model.getStatus()==2){
// 产品下必须包含物模型
@@ -214,9 +215,9 @@ public class ProductServiceImpl implements IProductService
//}
}
if(productMapper.changeProductStatus(model)>0){
return AjaxResult.success("操作成功");
return AjaxResult.success(MessageUtils.message("operate.success"));
}
return AjaxResult.error("状态更新失败");
return AjaxResult.error(MessageUtils.message("product.status.update.fail"));
}
/***
@@ -250,7 +251,7 @@ public class ProductServiceImpl implements IProductService
// 产品下不能有设备
int deviceCount=productMapper.deviceCountInProducts(productIds);
if(deviceCount>0){
return AjaxResult.error("删除失败,请先删除对应产品下的设备");
return AjaxResult.error(MessageUtils.message("delete.fail.please.delete.product.device"));
}
// 删除产品物模型
productMapper.deleteProductThingsModelByProductIds(productIds);
@@ -258,9 +259,9 @@ public class ProductServiceImpl implements IProductService
productAuthorizeMapper.deleteProductAuthorizeByProductIds(productIds);
// 删除产品
if(productMapper.deleteProductByProductIds(productIds)>0){
return AjaxResult.success("删除成功");
return AjaxResult.success(MessageUtils.message("delete.success"));
}
return AjaxResult.error("删除失败");
return AjaxResult.error(MessageUtils.message("delete.fail"));
}

View File

@@ -10,6 +10,7 @@ import com.fastbee.common.core.domain.model.LoginUser;
import com.fastbee.common.core.redis.RedisCache;
import com.fastbee.common.exception.ServiceException;
import com.fastbee.common.utils.DateUtils;
import com.fastbee.common.utils.MessageUtils;
import com.fastbee.common.utils.SecurityUtils;
import com.fastbee.common.utils.StringUtils;
import com.fastbee.common.utils.sign.Md5Utils;
@@ -220,13 +221,13 @@ public class SocialLoginServiceImpl implements ISocialLoginService {
}
@Override
public AjaxResult socialLogin(String loginId) {
public AjaxResult socialLogin(String loginId, String language) {
AjaxResult ajax = AjaxResult.success();
String loginKey = LOGIN_SOCIAL_REDIS_KEY + loginId;
LoginIdValue loginIdValue = redisCache.getCacheObject(loginKey);
if (loginIdValue != null) {
//login
String token = sysLoginService.redirectLogin(loginIdValue.getUsername(), loginIdValue.getPassword());
String token = sysLoginService.redirectLogin(loginIdValue.getUsername(), loginIdValue.getPassword(), language);
ajax.put(Constants.TOKEN, token);
} else {
log.info("loginId:{} ", loginId);
@@ -236,7 +237,7 @@ public class SocialLoginServiceImpl implements ISocialLoginService {
}
@Override
public AjaxResult bindLogin(BindLoginBody bindLoginBody) {
public AjaxResult bindLogin(BindLoginBody bindLoginBody, String language) {
BindIdValue bindValue = redisCache.getCacheObject(BIND_REDIS_KEY + bindLoginBody.getBindId());
SocialUser socialUser = findSocialUser(bindValue.getUuid(), bindValue.getSource());
AjaxResult checkAjax = checkSocialUser(socialUser, bindLoginBody.getBindId());
@@ -248,19 +249,19 @@ public class SocialLoginServiceImpl implements ISocialLoginService {
SysUser sysUser = iSysUserService.selectUserByUserName(bindLoginBody.getUsername());
if (sysUser == null) {
// 单独返回code用户不存在,给前端处理
return AjaxResult.error(HttpStatus.USER_NO_EXIST, "用户不存在");
return AjaxResult.error(HttpStatus.USER_NO_EXIST, MessageUtils.message("socialLogin.user.not.exist"));
}
// 自定义一下密码错误的提示
if(!SecurityUtils.matchesPassword(bindLoginBody.getPassword(), sysUser.getPassword())){
throw new ServiceException("密码错误");
throw new ServiceException(MessageUtils.message("password.fail"));
}
List<SocialUser> socialUserList = iSocialUserService.selectBySysUserId(sysUser.getUserId());
if (CollectionUtils.isNotEmpty(socialUserList)) {
throw new ServiceException("该账号已经绑定其他微信,请先解绑后重试!");
throw new ServiceException(MessageUtils.message("socialLogin.account.already.bind.other.wechat.please.unbind"));
}
// 生成令牌
String token = sysLoginService.login(bindLoginBody.getUsername(), bindLoginBody.getPassword(), bindLoginBody.getCode(),
bindLoginBody.getUuid());
bindLoginBody.getUuid(), language);
LoginUser loginUser = tokenService.getLoginUserByToken(token);
//绑定和更新
SocialUser updateSocialUser = new SocialUser();
@@ -274,7 +275,7 @@ public class SocialLoginServiceImpl implements ISocialLoginService {
}
@Override
public AjaxResult bindRegister(BindRegisterBody bindRegisterBody) {
public AjaxResult bindRegister(BindRegisterBody bindRegisterBody, String language) {
if (!("true".equals(iSysConfigService.selectConfigByKey("sys.account.registerUser")))) {
return error("当前系统没有开启注册功能!");
}
@@ -301,7 +302,7 @@ public class SocialLoginServiceImpl implements ISocialLoginService {
iSocialUserService.updateSocialUser(updateSocialUser);
redisCache.deleteObject(BIND_REDIS_KEY + bindRegisterBody.getBindId());
// 生成令牌
String token = sysLoginService.redirectLogin(sysUser.getUserName(), sysUser.getPassword());
String token = sysLoginService.redirectLogin(sysUser.getUserName(), sysUser.getPassword(), language);
ajax.put(Constants.TOKEN, token);
return ajax;
}
@@ -410,7 +411,7 @@ public class SocialLoginServiceImpl implements ISocialLoginService {
public AjaxResult checkSocialUser(SocialUser socialUser, String bindId) {
if (socialUser == null) {
log.info("bindId不存在, bindId: {}", bindId);
return error("绑定账户不存在");
return error(MessageUtils.message("bind.account.not.exist"));
} else {
return null;
}

View File

@@ -10,6 +10,7 @@ import com.fastbee.common.core.redis.RedisKeyBuilder;
import com.fastbee.common.enums.ThingsModelType;
import com.fastbee.common.exception.ServiceException;
import com.fastbee.common.utils.DateUtils;
import com.fastbee.common.utils.MessageUtils;
import com.fastbee.common.utils.StringUtils;
import com.fastbee.iot.domain.Product;
import com.fastbee.iot.domain.ThingsModel;
@@ -427,7 +428,7 @@ public class ThingsModelServiceImpl implements IThingsModelService {
*/
public String importData(List<ThingsModel> lists, Integer tempSlaveId) {
if (null == tempSlaveId || CollectionUtils.isEmpty(lists)) {
throw new ServiceException("导入数据异常");
throw new ServiceException(MessageUtils.message("things.model.import.data.exception"));
}
int success = 0;
int failure = 0;

View File

@@ -10,6 +10,7 @@ import com.fastbee.common.core.domain.entity.SysUser;
import com.fastbee.common.core.domain.model.LoginUser;
import com.fastbee.common.exception.ServiceException;
import com.fastbee.common.utils.DateUtils;
import com.fastbee.common.utils.MessageUtils;
import com.fastbee.common.utils.StringUtils;
import com.fastbee.iot.domain.ThingsModel;
import com.fastbee.iot.model.varTemp.EnumClass;
@@ -107,7 +108,7 @@ public class ThingsModelTemplateServiceImpl implements IThingsModelTemplateServi
return thingsModelTemplateMapper.insertThingsModelTemplate(template);
}catch (Exception e){
if (e.getMessage().contains("iot_things_modes_slaveId_reg")){
throw new ServiceException("同一个采集点模板下,寄存器地址重复,请检查导入变量寄存器地址");
throw new ServiceException(MessageUtils.message("things.model.register.address.repeat"));
}else {
throw new ServiceException(e.getMessage());
}
@@ -196,7 +197,7 @@ public class ThingsModelTemplateServiceImpl implements IThingsModelTemplateServi
*/
public String importData(List<ThingsModelTemplate> lists, String tempSlaveId) {
if (null == tempSlaveId || CollectionUtils.isEmpty(lists)) {
throw new ServiceException("导入数据异常");
throw new ServiceException(MessageUtils.message("things.model.import.data.exception"));
}
int success = 0;
int failure = 0;

View File

@@ -4,6 +4,7 @@ import com.fastbee.common.constant.HttpStatus;
import com.fastbee.common.core.domain.AjaxResult;
import com.fastbee.common.core.redis.RedisCache;
import com.fastbee.common.enums.SocialPlatformType;
import com.fastbee.common.utils.MessageUtils;
import com.fastbee.iot.domain.SocialUser;
import com.fastbee.iot.domain.UserSocialProfile;
import com.fastbee.iot.model.login.BindIdValue;
@@ -65,7 +66,7 @@ public class UserSocialProfileServiceImpl implements IUserSocialProfileService {
updateSocialUser.setSysUserId(sysUserId);
iSocialUserService.updateSocialUser(updateSocialUser);
redisCache.deleteObject(BIND_REDIS_KEY + bindId);
return AjaxResult.success("绑定成功!");
return AjaxResult.success(MessageUtils.message("bind.success"));
}
@Override
@@ -73,7 +74,7 @@ public class UserSocialProfileServiceImpl implements IUserSocialProfileService {
try {
SocialPlatformType.valueOf(platform);
} catch (Exception e) {
return AjaxResult.error("错误平台类型");
return AjaxResult.error(MessageUtils.message("socialLogin.platform.type.fail"));
}
return AjaxResult.success();
}
@@ -82,15 +83,15 @@ public class UserSocialProfileServiceImpl implements IUserSocialProfileService {
public AjaxResult unbindSocialAccount(Long socialUserId, Long sysUserId) {
SocialUser socialUser = iSocialUserService.selectSocialUserBySocialUserId(socialUserId);
if (socialUser == null) {
return AjaxResult.error("绑定账户不存在!");
return AjaxResult.error(MessageUtils.message("bind.account.not.exist"));
} else if (!socialUser.getSysUserId().equals(socialUserId)) {
return AjaxResult.error("用户账户和绑定账户不匹配!");
return AjaxResult.error(MessageUtils.message("user.account.and.bind.account.not.match"));
} else {
SocialUser updateSocialUser = new SocialUser();
updateSocialUser.setSocialUserId(socialUserId);
updateSocialUser.setSysUserId(-1L);
iSocialUserService.updateSocialUser(updateSocialUser);
return AjaxResult.success("解除绑定成功!");
return AjaxResult.success(MessageUtils.message("unbind.success"));
}
}

View File

@@ -21,14 +21,14 @@ public interface WeChatService {
* @param weChatLoginBody 微信登录参数
* @return String
*/
WeChatLoginResult mobileLogin(WeChatLoginBody weChatLoginBody);
WeChatLoginResult mobileLogin(WeChatLoginBody weChatLoginBody, String language);
/**
* 小程序微信登录
* @param weChatLoginBody 微信登录参数
* @return 登录结果
*/
WeChatLoginResult miniLogin(WeChatLoginBody weChatLoginBody);
WeChatLoginResult miniLogin(WeChatLoginBody weChatLoginBody, String language);
/**
* 取消所有相关微信绑定

View File

@@ -10,6 +10,7 @@ import com.fastbee.common.enums.SocialPlatformType;
import com.fastbee.common.enums.VerifyTypeEnum;
import com.fastbee.common.exception.ServiceException;
import com.fastbee.common.utils.DateUtils;
import com.fastbee.common.utils.MessageUtils;
import com.fastbee.common.utils.StringUtils;
import com.fastbee.common.utils.bean.BeanUtils;
import com.fastbee.common.utils.http.HttpUtils;
@@ -107,19 +108,19 @@ public class WeChatServiceImpl implements WeChatService {
* @param weChatLoginBody 微信登录参数
* @return String
*/
public WeChatLoginResult mobileLogin(WeChatLoginBody weChatLoginBody) {
public WeChatLoginResult mobileLogin(WeChatLoginBody weChatLoginBody, String language) {
WeChatLoginResult weChatLoginResult = new WeChatLoginResult();
SocialPlatformType socialPlatformType = SocialPlatformType.WECHAT_OPEN_MOBILE;
// 查询微信平台信息
SocialPlatform socialPlatform = socialPlatformService.selectSocialPlatformByPlatform(socialPlatformType.getSourceClient());
if (socialPlatform == null) {
throw new ServiceException("请先配置微信开放平台移动应用信息");
throw new ServiceException(MessageUtils.message("wechat.please.config.open.platform"));
}
// 用户凭证code只能消费一次前端调的uni.login有时会消费然后直接就把获取到的信息传过来不会消费的话就这里通过code拿
if (StringUtils.isEmpty(weChatLoginBody.getAccessToken()) || StringUtils.isEmpty(weChatLoginBody.getOpenId()) || StringUtils.isEmpty(weChatLoginBody.getUnionId())) {
WeChatAppResult weChatResult = this.getAccessTokenOpenId(weChatLoginBody.getCode(), socialPlatform);
if (weChatResult == null || weChatResult.getErrCode() != null) {
throw new ServiceException("用户凭证获取失败,请重新登录!");
throw new ServiceException(MessageUtils.message("wechat.user.certificate.gain.fail"));
}
weChatLoginBody.setAccessToken(weChatResult.getAccessToken()).setRefreshToken(weChatResult.getRefreshToken()).setExpiresIn(weChatResult.getExpiresIn()).setOpenId(weChatResult.getOpenId()).setUnionId(weChatResult.getUnionId()).setScope(weChatResult.getScope());
}
@@ -165,9 +166,9 @@ public class WeChatServiceImpl implements WeChatService {
} else {
SysUser sysUser = sysUserService.selectUserById(bindSysUserId);
if (sysUser == null) {
throw new ServiceException("用户不存在");
throw new ServiceException(MessageUtils.message("user.not.exist"));
}
String token = sysLoginService.redirectLogin(sysUser.getUserName(), sysUser.getPassword());
String token = sysLoginService.redirectLogin(sysUser.getUserName(), sysUser.getPassword(), language);
weChatLoginResult.setToken(token);
}
return weChatLoginResult;
@@ -179,26 +180,26 @@ public class WeChatServiceImpl implements WeChatService {
* @return 登录结果
*/
@Override
public WeChatLoginResult miniLogin(WeChatLoginBody weChatLoginBody) {
public WeChatLoginResult miniLogin(WeChatLoginBody weChatLoginBody, String language) {
// 使用微信手机号去登录不绑定微信,没有用户则用手机号自动注册一个登录,密码是手机号
SocialPlatformType socialPlatformType = SocialPlatformType.WECHAT_OPEN_MINI_PROGRAM;
// 查询微信平台信息
SocialPlatform socialPlatform = socialPlatformService.selectSocialPlatformByPlatform(socialPlatformType.getSourceClient());
if (socialPlatform == null) {
throw new ServiceException("请先配置微信公众平台小程序信息!");
throw new ServiceException(MessageUtils.message("wechat.please.config.open.platform.mini"));
}
if (StringUtils.isEmpty(weChatLoginBody.getPhoneCode())) {
throw new ServiceException("用户手机号凭证获取失败,请重新登录!");
throw new ServiceException(MessageUtils.message("wechat.user.phone.certificate.gain.fail"));
}
// 先获取token
WeChatAppResult result = getAccessToken(socialPlatform);
if (result == null || StringUtils.isEmpty(result.getAccessToken())) {
throw new ServiceException("获取用户调用凭据失败,请重新登录!");
throw new ServiceException(MessageUtils.message("wechat.user.certificate.gain.fail"));
}
// 根据phoneCode获取用户手机号
WeChatPhoneInfo userPhoneInfo = getWechatUserPhoneInfo(weChatLoginBody.getPhoneCode(), result.getAccessToken());
if (userPhoneInfo == null || !userPhoneInfo.getErrCode().equals("0")) {
throw new ServiceException("获取用户手机号失败,请重新登录!");
throw new ServiceException(MessageUtils.message("wechat.user.phone.certificate.gain.fail"));
}
String phoneNumber = userPhoneInfo.getPhoneInfo().getPhoneNumber();
SysUser sysUser = sysUserService.selectUserByPhoneNumber(phoneNumber);
@@ -214,9 +215,9 @@ public class WeChatServiceImpl implements WeChatService {
if (StringUtils.isNotEmpty(registerUserOutput.getMsg())) {
throw new ServiceException(registerUserOutput.getMsg());
}
token = sysLoginService.redirectLogin(phoneNumber, phoneNumber);
token = sysLoginService.redirectLogin(phoneNumber, phoneNumber, language);
} else {
token = sysLoginService.redirectLogin(sysUser.getUserName(), sysUser.getPassword());
token = sysLoginService.redirectLogin(sysUser.getUserName(), sysUser.getPassword(), language);
}
weChatLoginResult.setToken(token);
return weChatLoginResult;
@@ -252,21 +253,21 @@ public class WeChatServiceImpl implements WeChatService {
public AjaxResult cancelBind(WxCancelBindReqVO wxCancelBindReqVO) {
LoginUser loginUser = getLoginUser();
if (loginUser == null || loginUser.getUserId() == null) {
throw new ServiceException("请先登录后重试");
throw new ServiceException(MessageUtils.message("wechat.please.login"));
}
// 密码验证
if (VerifyTypeEnum.PASSWORD.getVerifyType().equals(wxCancelBindReqVO.getVerifyType())) {
if (StringUtils.isEmpty(wxCancelBindReqVO.getPassword())) {
throw new ServiceException("请传入用户密码");
throw new ServiceException(MessageUtils.message("wechat.please.enter.user.password"));
}
Boolean validateResult = sysUserService.validatePassword(loginUser.getUser().getPassword(), wxCancelBindReqVO.getPassword());
if (Boolean.FALSE.equals(validateResult)) {
throw new ServiceException("密码错误,请重新输入");
throw new ServiceException(MessageUtils.message("wechat.cancelBind.password.fail"));
}
}
// 解绑所有微信应用
int cancelBind = socialUserService.cancelBind(loginUser.getUserId(), SocialPlatformType.listWechatPlatform);
return cancelBind >= 1 ? success("解绑成功") : AjaxResult.error("解绑失败");
return cancelBind >= 1 ? success(MessageUtils.message("unbind.success")) : AjaxResult.error(MessageUtils.message("unbind.fail"));
}
/**
@@ -278,30 +279,30 @@ public class WeChatServiceImpl implements WeChatService {
public AjaxResult bind(WxBindReqVO wxBindReqVO) {
Long sysUserId = getUserId();
if (sysUserId == null) {
throw new ServiceException("请登录后重试");
throw new ServiceException(MessageUtils.message("wechat.please.login"));
}
String openId = "";
String unionId = "";
// 区分小程序绑定还是移动应用绑定
if (SocialPlatformType.WECHAT_OPEN_MOBILE.sourceClient.equals(wxBindReqVO.getSourceClient())) {
if (StringUtils.isEmpty(wxBindReqVO.getOpenId()) || StringUtils.isEmpty(wxBindReqVO.getUnionId())) {
throw new ServiceException("请传入微信用户信息");
throw new ServiceException(MessageUtils.message("wechat.please.enter.wechat.user.info"));
}
openId = wxBindReqVO.getOpenId();
unionId = wxBindReqVO.getUnionId();
} else if (SocialPlatformType.WECHAT_OPEN_MINI_PROGRAM.sourceClient.equals(wxBindReqVO.getSourceClient())) {
if (StringUtils.isEmpty(wxBindReqVO.getCode())) {
throw new ServiceException("请传入用户凭证");
throw new ServiceException(MessageUtils.message("wechat.please.enter.user.certificate"));
}
// 查询微信平台信息
SocialPlatform socialPlatform = socialPlatformService.selectSocialPlatformByPlatform(SocialPlatformType.WECHAT_OPEN_MINI_PROGRAM.sourceClient);
if (socialPlatform == null) {
throw new ServiceException("请先配置微信开放平台小程序信息!");
throw new ServiceException(MessageUtils.message("wechat.please.config.open.platform.mini"));
}
WeChatMiniProgramResult weChatMiniProgramResult = this.codeToSession(wxBindReqVO.getCode(), socialPlatform);
if (weChatMiniProgramResult == null
|| (StringUtils.isEmpty(weChatMiniProgramResult.getOpenId()) && StringUtils.isEmpty(weChatMiniProgramResult.getUnionId()))) {
throw new ServiceException("获取微信信息失败,请重试!");
throw new ServiceException(MessageUtils.message("wechat.gain.wechat.info.fail"));
}
openId = weChatMiniProgramResult.getOpenId();
unionId = weChatMiniProgramResult.getUnionId();
@@ -311,11 +312,11 @@ public class WeChatServiceImpl implements WeChatService {
int bindResult;
List<SocialUser> socialUserList = socialUserService.selectBySysUserId(sysUserId);
if (CollectionUtils.isNotEmpty(socialUserList)) {
return success("绑定成功!");
return success(MessageUtils.message("bind.success"));
}
if (socialUser != null) {
if (socialUser.getSysUserId() != null && !sysUserId.equals(socialUser.getSysUserId())) {
throw new ServiceException("该微信已绑定其他账号,请先使用微信登录解绑后重试!");
throw new ServiceException(MessageUtils.message("wechat.this.wechat.already.bind.other.account"));
}
SocialUser updateSocialUser = new SocialUser();
updateSocialUser.setSocialUserId(socialUser.getSocialUserId());
@@ -334,19 +335,19 @@ public class WeChatServiceImpl implements WeChatService {
bindResult = socialUserService.insertSocialUser(insertSocialUser);
}
// 绑定
return bindResult >= 1 ? success("绑定成功!") : AjaxResult.error("绑定失败");
return bindResult >= 1 ? success(MessageUtils.message("bind.success")) : AjaxResult.error(MessageUtils.message("bind.fail"));
}
@Override
public AjaxResult getWxBindQr(HttpServletRequest httpServletRequest) {
Long sysUserId = getUserId();
if (sysUserId == null) {
throw new ServiceException("请先登录后重试!");
throw new ServiceException(MessageUtils.message("wechat.please.login"));
}
WeChatLoginQrRes weChatLoginQrRes = new WeChatLoginQrRes();
SocialPlatform socialPlatform = socialPlatformService.selectSocialPlatformByPlatform(SocialPlatformType.WECHAT_OPEN_WEB_BIND.sourceClient);
if (socialPlatform == null) {
throw new ServiceException("请先配置微信开放平台网站应用个人中心绑定信息");
throw new ServiceException(MessageUtils.message("wechat.please.config.open.platform.web.application.personal.bind.info"));
}
weChatLoginQrRes.setAppid(socialPlatform.getClientId());
weChatLoginQrRes.setScope("snsapi_login");
@@ -362,36 +363,36 @@ public class WeChatServiceImpl implements WeChatService {
SocialPlatform socialPlatform = socialPlatformService.selectSocialPlatformByPlatform(SocialPlatformType.WECHAT_OPEN_WEB_BIND.sourceClient);
if (socialPlatform == null) {
String serverName = httpServletRequest.getServerName();
String msgId = socialLoginService.genErrorId("请先配置微信开放平台网站应用个人中心绑定信息");
String msgId = socialLoginService.genErrorId(MessageUtils.message("wechat.please.config.open.platform.web.application.personal.bind.info"));
return "https://" + serverName + "/user/profile?wxBindMsgId=" + msgId;
}
String url = socialPlatform.getRedirectLoginUri();
//获取临时票据 code
log.info("code:{}", code);
if (StringUtils.isEmpty(code)) {
String msgId = socialLoginService.genErrorId("您已取消授权或未获取到授权信息");
String msgId = socialLoginService.genErrorId(MessageUtils.message("wechat.you.cancel.or.not.gain.authorization.info"));
return url + msgId;
}
Long sysUserId = redisCache.getCacheObject(WX_BIND_REDIS_KEY + wxBindId);
if (sysUserId == null) {
String msgId = socialLoginService.genErrorId("二维码已失效,请重新点击绑定");
String msgId = socialLoginService.genErrorId(MessageUtils.message("wechat.the.qr.code.has.expired"));
return url + msgId;
}
List<SocialUser> socialUserList = socialUserService.selectBySysUserId(sysUserId);
if (CollectionUtils.isNotEmpty(socialUserList)) {
String msgId = socialLoginService.genErrorId("您的账号已绑定微信,请先解绑");
String msgId = socialLoginService.genErrorId(MessageUtils.message("wechat.your.account.already.bind.wechat"));
return url + msgId;
}
// 组装获取accessToken的url
WeChatAppResult weChatAppResult = this.getAccessTokenOpenId(code, socialPlatform);
if (weChatAppResult == null || StringUtils.isEmpty(weChatAppResult.getAccessToken())
|| StringUtils.isEmpty(weChatAppResult.getOpenId()) || StringUtils.isEmpty(weChatAppResult.getUnionId())) {
String msgId = socialLoginService.genErrorId("获取微信信息失败,请重试");
String msgId = socialLoginService.genErrorId(MessageUtils.message("wechat.gain.wechat.info.fail"));
return url + msgId;
}
Long bindUserId = socialUserService.selectSysUserIdByUnionId(weChatAppResult.getUnionId());
if (bindUserId != null && !bindUserId.equals(sysUserId)) {
String msgId = socialLoginService.genErrorId("您的微信已绑定其他账号,请先使用微信登录解绑后重试!");
String msgId = socialLoginService.genErrorId(MessageUtils.message("wechat.your.wechat.already.bind.other.account"));
return url + msgId;
}
SocialUser socialUser = socialUserService.selectOneByOpenIdAndUnionId(weChatAppResult.getOpenId(), weChatAppResult.getUnionId());