操作员扫码的时候是有user_id的,并且操作的时候是有当前的时间的。怎么在那第一次来的时候,把user_id和当前的时间存入到session里面 key是user_id 值是当前时间。下次来的时候如果是相同的user_id 则取出session里对应user_id的值也就是上次的时间值,如果间隔小于1秒那么就是重复访问,就直接return。怎么在代码里实现,帮忙修改一下controller层的代码谢谢
@RequestMapping(value = "/adduserproductmap", method = RequestMethod.GET)
private String addUserProductMap(HttpServletRequest request, HttpServletResponse response) throws IOException {
// 获取微信授权信息
WechatIden iden = getOperatorInfo(request);
if (iden != null) {
PersonInfo operator = iden.getPersonInfo();
request.getSession().setAttribute("user", operator);
// 获取二维码里state携带的content信息并解码
String qrCodeinfo = new String(
URLDecoder.decode(HttpServletRequestUtil.getString(request, "state"), "UTF-8"));
ObjectMapper mapper = new ObjectMapper();
WechatInfo wechatInfo = null;
try {
// 将解码后的内容用aaa去替换掉之前生成二维码的时候加入的aaa前缀,转换成WechatInfo实体类
wechatInfo = mapper.readValue(qrCodeinfo.replace("aaa", "\""), WechatInfo.class);
} catch (Exception e) {
return "shop/operationfail";
}
// 校验二维码是否已经过期
if (!checkQRCodeInfo(wechatInfo)) {
return "shop/operationfail";
}
// 获取添加消费记录所需要的参数并组建成userproductmap实例
Long productId = wechatInfo.getProductId();
Long customerId = wechatInfo.getCustomerId();
UserProductMap userProductMap = compactUserProductMap4Add(customerId, productId, iden.getPersonInfo());
if (userProductMap != null && customerId != -1) {
try {
if (!checkShopIden(operator.getUserId(), userProductMap)) {
return "shop/operationfail";
}
UserProductMapExecution se = service_UserProductMap.addUserProductMap(userProductMap);
if (se.getState() == UserProductMapStateEnum.SUCCESS.getState()) {
return "shop/operationsuccess";
}
} catch (RuntimeException e) {
return "shop/operationfail";
}
}
}
return "shop/operationfail";
}
//根据code获取UserAccessToken,进而通过token里的openId获取微信用户信息
private WechatIden getOperatorInfo(HttpServletRequest request) {
String code = request.getParameter("code");
WechatIden iden = null;
if (null != code) {
UserAccessToken token;
try {
token = WechatUtil.getUserAccessToken(code);
String openId = token.getOpenId();
request.getSession().setAttribute("openId", openId);
iden = service_WeChatIden.getWechatIdenByOpenId(openId);
} catch (IOException e) {
e.printStackTrace();
}
}
return iden;
}
//超过5分钟则认为过期
private boolean checkQRCodeInfo(WechatInfo wechatInfo) {
if (wechatInfo != null && wechatInfo.getProductId() != null && wechatInfo.getCustomerId() != null
&& wechatInfo.getCreateTime() != null) {
long nowTime = System.currentTimeMillis();
if ((nowTime - wechatInfo.getCreateTime()) <= 300000) {
return true;
} else {
return false;
}
} else {
return false;
}
}
//根据传入的customerId, productId以及操作员信息组建用户消费记录
private UserProductMap compactUserProductMap4Add(Long customerId, Long productId, PersonInfo operator) {
UserProductMap userProductMap = null;
if (customerId != null && productId != null) {
userProductMap = new UserProductMap();
PersonInfo customer = new PersonInfo();
customer.setUserId(customerId);
// 主要为了获取商品积分
Product product = service_Product.getProductById(productId);
userProductMap.setProduct(product);
userProductMap.setShop(product.getShop());
userProductMap.setUser(customer);
userProductMap.setJifen(product.getJifen());
userProductMap.setCreateTime(new Date());
userProductMap.setOperator(operator);
}
return userProductMap;
}
//检查是否有操作权限
private boolean checkShopIden(long userId, UserProductMap userProductMap) {
// 获取该店铺的所有授权信息
ShopIdenMapExecution shopIdenMapExecution = service_ShopIdenMap
.shopIdenMapListByShopId(userProductMap.getShop().getShopId(), 1, 1000);
for (ShopIdenMap shopIdenMap : shopIdenMapExecution.getShopIdenMapList()) {
// 看看是否给过该人员进行授权
if (shopIdenMap.getWork().getUserId() == userId) {
return true;
}
}
}
PzZZ