fixed 订单空指针异常及用户信息获取
This commit is contained in:
parent
dc5119b234
commit
56c69c3ff8
@ -13,6 +13,7 @@ import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.Resource;
|
||||
import java.util.HashMap;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
@ -33,7 +34,8 @@ public class ShopOrderInfoController {
|
||||
private StringRedisTemplate stringRedisTemplate;
|
||||
|
||||
// 是否已经没有库存
|
||||
private ConcurrentHashMap<Long, Boolean> stockOutMap = new ConcurrentHashMap<>();
|
||||
// TODO 优化并发加锁
|
||||
private HashMap<Long, Boolean> stockOutMap = new HashMap<>();
|
||||
private static final String CACHE_STOCK_KEY = "goods:stock:";
|
||||
|
||||
private String cacheKey(long gid) {
|
||||
@ -61,21 +63,23 @@ public class ShopOrderInfoController {
|
||||
}
|
||||
int uid = StpUtil.getLoginIdAsInt();
|
||||
// 如果限制用户只能购买的数量,可以添加一个map记录用户的请求数
|
||||
int size = stockOutMap.size();
|
||||
Long buyId = (long)data.getGoodsId();
|
||||
//1.内存判断
|
||||
if (stockOutMap.size() > 0 && stockOutMap.get(data.getGoodsId())) {
|
||||
if (size > 0 && stockOutMap.containsKey(buyId) && stockOutMap.get(buyId)) {
|
||||
throw BizException.create("库存不足");
|
||||
}
|
||||
//2.缓存(redis)判断
|
||||
long count = stringRedisTemplate.opsForValue().decrement(cacheKey(data.getGoodsId()), data.getBuyCount());
|
||||
long count = stringRedisTemplate.opsForValue().decrement(cacheKey(buyId), data.getBuyCount());
|
||||
if (count < 0) {
|
||||
log.info("stock count ===>" + count);
|
||||
// 对缓存进行库存 还原
|
||||
stringRedisTemplate.opsForValue().increment(cacheKey(data.getGoodsId()),data.getBuyCount()); //
|
||||
stringRedisTemplate.opsForValue().increment(cacheKey(buyId),data.getBuyCount()); //
|
||||
throw BizException.create("库存不足");
|
||||
}
|
||||
if(count == 0){
|
||||
// 此时库存没有了 , 保存到已买完的对象
|
||||
stockOutMap.put((long)data.getGoodsId(),true);
|
||||
stockOutMap.put(buyId,true);
|
||||
}
|
||||
//3.数据库
|
||||
try{
|
||||
|
@ -3,10 +3,14 @@ package me.xiaoyan.point.api.controller;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.SneakyThrows;
|
||||
import me.xiaoyan.point.api.error.BizException;
|
||||
import me.xiaoyan.point.api.pojo.UserInfo;
|
||||
import me.xiaoyan.point.api.pojo.dto.UserInfoWithToken;
|
||||
import me.xiaoyan.point.api.pojo.vo.SignResult;
|
||||
import me.xiaoyan.point.api.pojo.vo.UserLoginData;
|
||||
import me.xiaoyan.point.api.service.UserInfoService;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
@ -38,8 +42,22 @@ public class UserinfoController {
|
||||
|
||||
@ApiOperation("获取登录用户的基本信息")
|
||||
@RequestMapping("info")
|
||||
public UserInfo getInfo() {
|
||||
int uid = StpUtil.getLoginIdAsInt();
|
||||
return userInfoService.getInfoById(uid);
|
||||
public UserInfoWithToken getInfo(String code) {
|
||||
UserInfoWithToken data = new UserInfoWithToken();
|
||||
UserInfo info;
|
||||
if (StringUtils.hasLength(code)) {
|
||||
info = userInfoService.getInfoByCode(code);
|
||||
if (info == null) {
|
||||
throw BizException.create("用户不存在");
|
||||
}
|
||||
StpUtil.login(info.getId());
|
||||
String token = StpUtil.getTokenInfo().getTokenValue();
|
||||
data.setToken(token);
|
||||
}else{
|
||||
int uid = StpUtil.getLoginIdAsInt();
|
||||
info = userInfoService.getInfoById(uid);
|
||||
}
|
||||
BeanUtils.copyProperties(info,data);
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,15 @@
|
||||
package me.xiaoyan.point.api.pojo.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import me.xiaoyan.point.api.pojo.UserInfo;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class UserInfoWithToken extends UserInfo implements Serializable {
|
||||
private String token;
|
||||
}
|
@ -14,4 +14,6 @@ public interface UserInfoService extends IService<UserInfo> {
|
||||
public UserInfo login(UserLoginData data);
|
||||
|
||||
UserInfo getInfoById(Integer uid);
|
||||
|
||||
UserInfo getInfoByCode(String code);
|
||||
}
|
||||
|
@ -86,6 +86,7 @@ public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@Transactional
|
||||
@Override
|
||||
public UserInfo login(UserLoginData data) {
|
||||
@ -149,4 +150,11 @@ public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo>
|
||||
}
|
||||
return userinfo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserInfo getInfoByCode(String code) {
|
||||
CodeSessionData sessionData = getOpenIdByCode(code);
|
||||
// 使用openid查询用户信息
|
||||
return this.getBaseMapper().selectOneByOpenId(sessionData.getOpenid());
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user