Commit 6ffd8c55 authored by 技术部-任文超's avatar 技术部-任文超

从Form重复提交幂等性考虑,解决Jira128(跳过异常返回成功)

parent 0972d902
...@@ -502,17 +502,13 @@ public class InnerController implements IBaseController { ...@@ -502,17 +502,13 @@ public class InnerController implements IBaseController {
addressObj.setUpdateAt(now); addressObj.setUpdateAt(now);
} }
addressObj.setUserId(userId); addressObj.setUserId(userId);
// 替换所有,UTF-8编码时4字节的Emoji表情字符 addressObj.setProvince(province);
addressObj.setProvince(EmojiUtil.filterUnicode4(province));
addressObj.setProvinceCode(provinceCode); addressObj.setProvinceCode(provinceCode);
// 替换所有,UTF-8编码时4字节的Emoji表情字符 addressObj.setCity(city);
addressObj.setCity(EmojiUtil.filterUnicode4(city));
addressObj.setCityCode(cityCode); addressObj.setCityCode(cityCode);
// 替换所有,UTF-8编码时4字节的Emoji表情字符 addressObj.setDistrict(district);
addressObj.setDistrict(EmojiUtil.filterUnicode4(district));
addressObj.setDistrictCode(districtCode); addressObj.setDistrictCode(districtCode);
// 替换所有,UTF-8编码时4字节的Emoji表情字符 addressObj.setAddress(address);
addressObj.setAddress(EmojiUtil.filterUnicode4(address));
addressObj = addressService.save(addressObj); addressObj = addressService.save(addressObj);
log.info("保存后地址详情:{}", addressObj); log.info("保存后地址详情:{}", addressObj);
return JsonResult.buildSuccessResult(null, AddressRet.address2AddressRet(addressObj)); return JsonResult.buildSuccessResult(null, AddressRet.address2AddressRet(addressObj));
......
...@@ -9,7 +9,6 @@ import cn.quantgroup.xyqb.aspect.limit.PasswordFreeAccessValidator; ...@@ -9,7 +9,6 @@ import cn.quantgroup.xyqb.aspect.limit.PasswordFreeAccessValidator;
import cn.quantgroup.xyqb.entity.*; import cn.quantgroup.xyqb.entity.*;
import cn.quantgroup.xyqb.model.JsonResult; import cn.quantgroup.xyqb.model.JsonResult;
import cn.quantgroup.xyqb.service.user.*; import cn.quantgroup.xyqb.service.user.*;
import cn.quantgroup.xyqb.util.EmojiUtil;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference; import com.alibaba.fastjson.TypeReference;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
...@@ -121,10 +120,6 @@ public class UserCenterController { ...@@ -121,10 +120,6 @@ public class UserCenterController {
if (null == userId || userId == 0L) { if (null == userId || userId == 0L) {
return JsonResult.buildErrorStateResult("该用户不存在", null); return JsonResult.buildErrorStateResult("该用户不存在", null);
} }
if (StringUtils.isNotBlank(nick)) {
// 替换所有,UTF-8编码时4字节的Emoji表情字符
nick = EmojiUtil.filterUnicode4(nick);
}
UserAttached userAttached = userCenterService.saveUserNick(userId, nick); UserAttached userAttached = userCenterService.saveUserNick(userId, nick);
return JsonResult.buildSuccessResult(null, userAttached.getNick()); return JsonResult.buildSuccessResult(null, userAttached.getNick());
} }
...@@ -289,11 +284,10 @@ public class UserCenterController { ...@@ -289,11 +284,10 @@ public class UserCenterController {
} }
//查询用户是否有保存地址信息 //查询用户是否有保存地址信息
Address addressInfo = addressService.findByUserId(userId); Address addressInfo = addressService.findByUserId(userId);
if (null == addressInfo) {
Timestamp now = new Timestamp(System.currentTimeMillis()); Timestamp now = new Timestamp(System.currentTimeMillis());
if (null == addressInfo) {
addressInfo = new Address(); addressInfo = new Address();
addressInfo.setCreatedAt(now); addressInfo.setCreatedAt(now);
addressInfo.setUpdateAt(now);
} }
addressInfo.setUserId(userId); addressInfo.setUserId(userId);
addressInfo.setProvince(province); addressInfo.setProvince(province);
...@@ -303,12 +297,7 @@ public class UserCenterController { ...@@ -303,12 +297,7 @@ public class UserCenterController {
addressInfo.setDistrictCode(districtCode); addressInfo.setDistrictCode(districtCode);
addressInfo.setDistrict(district); addressInfo.setDistrict(district);
addressInfo.setAddress(address); addressInfo.setAddress(address);
if (StringUtils.isNotBlank(address)) { addressInfo.setUpdateAt(now);
// 替换所有,UTF-8编码时4字节的Emoji表情字符
addressInfo.setAddress(EmojiUtil.filterUnicode4(address));
}
//更新时间
addressInfo.setUpdateAt(new Timestamp(System.currentTimeMillis()));
addressService.save(addressInfo); addressService.save(addressInfo);
return JsonResult.buildSuccessResult(null, addressInfo); return JsonResult.buildSuccessResult(null, addressInfo);
} }
...@@ -393,25 +382,6 @@ public class UserCenterController { ...@@ -393,25 +382,6 @@ public class UserCenterController {
return JsonResult.buildSuccessResult(null, userExtInfo); return JsonResult.buildSuccessResult(null, userExtInfo);
} }
/**
* 创建用户附加信息实体
*
* @param userId
* @param avatar
* @param nick
* @return
*/
private UserAttached createUserAttached(Long userId, String avatar, String nick) {
UserAttached userAttached = new UserAttached();
Timestamp now = new Timestamp(System.currentTimeMillis());
userAttached.setUserId(userId);
userAttached.setAvatar(avatar);
userAttached.setNick(nick);
userAttached.setCreatedAt(now);
userAttached.setUpdatedAt(now);
return userAttached;
}
/** /**
* 根据手机号查询用户id, nodejs调用接口无法获取到userId. * 根据手机号查询用户id, nodejs调用接口无法获取到userId.
* 所以增加该接口来查询用户id * 所以增加该接口来查询用户id
......
...@@ -13,7 +13,5 @@ public interface IAddressService { ...@@ -13,7 +13,5 @@ public interface IAddressService {
Address save(Address addressObj); Address save(Address addressObj);
Address saveAddress(Address address);
List<Address> findByUserIds(List<Long> userIds); List<Address> findByUserIds(List<Long> userIds);
} }
...@@ -3,6 +3,7 @@ package cn.quantgroup.xyqb.service.user.impl; ...@@ -3,6 +3,7 @@ package cn.quantgroup.xyqb.service.user.impl;
import cn.quantgroup.xyqb.entity.Address; import cn.quantgroup.xyqb.entity.Address;
import cn.quantgroup.xyqb.repository.IAddressRepository; import cn.quantgroup.xyqb.repository.IAddressRepository;
import cn.quantgroup.xyqb.service.user.IAddressService; import cn.quantgroup.xyqb.service.user.IAddressService;
import cn.quantgroup.xyqb.util.EmojiUtil;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.Cacheable;
...@@ -28,15 +29,14 @@ public class AddressServiceImpl implements IAddressService { ...@@ -28,15 +29,14 @@ public class AddressServiceImpl implements IAddressService {
@Override @Override
@CacheEvict(value = "addresscache", key = "'address' + #addressObj.userId", cacheManager = "cacheManager") @CacheEvict(value = "addresscache", key = "'address' + #addressObj.userId", cacheManager = "cacheManager")
public Address save(Address addressObj) { public Address save(Address addressObj) {
/* 替换所有,UTF-8编码时4字节的Emoji表情字符 */
addressObj.setProvince(EmojiUtil.filterUnicode4(addressObj.getProvince()));
addressObj.setCity(EmojiUtil.filterUnicode4(addressObj.getCity()));
addressObj.setDistrict(EmojiUtil.filterUnicode4(addressObj.getDistrict()));
addressObj.setAddress(EmojiUtil.filterUnicode4(addressObj.getAddress()));
return addressRepository.save(addressObj); return addressRepository.save(addressObj);
} }
@Override
public Address saveAddress(Address address) {
return addressRepository.save(address);
}
@Override @Override
public List<Address> findByUserIds(List<Long> userIds) { public List<Address> findByUserIds(List<Long> userIds) {
return addressRepository.findAll((root, query, cb) -> { return addressRepository.findAll((root, query, cb) -> {
......
...@@ -3,7 +3,10 @@ package cn.quantgroup.xyqb.service.user.impl; ...@@ -3,7 +3,10 @@ package cn.quantgroup.xyqb.service.user.impl;
import cn.quantgroup.xyqb.entity.UserAttached; import cn.quantgroup.xyqb.entity.UserAttached;
import cn.quantgroup.xyqb.repository.IUserAttachedRepository; import cn.quantgroup.xyqb.repository.IUserAttachedRepository;
import cn.quantgroup.xyqb.service.user.UserCenterService; import cn.quantgroup.xyqb.service.user.UserCenterService;
import cn.quantgroup.xyqb.util.EmojiUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.hibernate.exception.ConstraintViolationException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.Cacheable;
...@@ -16,6 +19,7 @@ import java.util.Optional; ...@@ -16,6 +19,7 @@ import java.util.Optional;
/** /**
* Created by 11 on 2017/3/22. * Created by 11 on 2017/3/22.
*/ */
@Slf4j
@Service @Service
public class UserCenterServiceImpl implements UserCenterService { public class UserCenterServiceImpl implements UserCenterService {
...@@ -51,9 +55,15 @@ public class UserCenterServiceImpl implements UserCenterService { ...@@ -51,9 +55,15 @@ public class UserCenterServiceImpl implements UserCenterService {
userAttached = createUserAttached(userId, null, nick); userAttached = createUserAttached(userId, null, nick);
} }
if(!Objects.equals(nick, userAttached.getNick())){ if(!Objects.equals(nick, userAttached.getNick())){
userAttached.setNick(nick); // 替换所有,UTF-8编码时4字节的Emoji表情字符
userAttached.setNick(EmojiUtil.filterUnicode4(nick));
userAttached.setUpdatedAt(new Timestamp(System.currentTimeMillis())); userAttached.setUpdatedAt(new Timestamp(System.currentTimeMillis()));
try {
userAttached = userAttachedRepository.save(userAttached); userAttached = userAttachedRepository.save(userAttached);
}catch (ConstraintViolationException e){
// Sql唯一约束异常(诱因是Form重复提交,因为该操作是幂等的,故此不需额外处理,可返回成功)
log.error("Sql约束异常[uni_idx_user_id]重复提交Form是幂等操作,不影响处理结果", e);
}
} }
return userAttached; return userAttached;
} }
......
...@@ -72,10 +72,8 @@ public class TestUserService { ...@@ -72,10 +72,8 @@ public class TestUserService {
return Arrays.asList(new Object[][]{{addressObj}}); return Arrays.asList(new Object[][]{{addressObj}});
} }
@Test @Test
public void testAddress() { public void testAddress() {
addressService.save(addressObj); addressService.save(addressObj);
addressObj=addressService.findByUserId(addressObj.getUserId()); addressObj=addressService.findByUserId(addressObj.getUserId());
System.out.println("测试地址"+addressObj); System.out.println("测试地址"+addressObj);
......
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