亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

各位大神老師能否幫忙修改一下controller層代碼,實現用戶在用一時間只能進行一次接口方法

各位大神老師能否幫忙修改一下controller層代碼,實現用戶在用一時間只能進行一次接口方法

杰哥大大 2018-03-04 23:54:58
操作員掃碼的時候是有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; } } }
查看完整描述

1 回答

?
PzZZ

TA貢獻9條經驗 獲得超2個贊

long?nowTime?=?System.currentTimeMillis();

if?((nowTime?- Integer.valueOf(request.getSession.getAtrribute("user_id")))?<= 1000)?{

? ? return ;

}

查看完整回答
反對 回復 2018-03-15
  • 1 回答
  • 0 關注
  • 1529 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號