您好,登錄后才能下訂單哦!
這篇文章主要介紹APP轉盤抽獎Java服務端接口的示例分析,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
應公司需求開發一個微信公眾號中抽獎活動
功能:獎品及中獎概率可在后臺配置,滾動刷新中獎名單,控制用戶每日抽獎次數等。
規則:在活動期間,每日可抽獎一次,中獎后填寫個人信息以便獎品的配送。
1.獲取抽獎頁面數據
/** * 獲取抽獎頁面數據 * @param request * @param response * @return * @throws ServletException * @throws IOException */ @RequestMapping(value="/queryLotteryActivity") @ResponseBody public AppIntReturn queryLotteryActivity(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { AppIntReturn res = new AppIntReturn(); // 用戶同意授權后,能獲取到code String code = request.getParameter("code"); // 用戶同意授權 if (!"authdeny".equals(code)) { // 獲取網頁授權access_token WeixinOauth3Token weixinOauth3Token = CommonUtil .getOauth3AccessToken(ConfigUtil.APPID, ConfigUtil.APP_SECRECT, code); // 用戶標識 String openId = weixinOauth3Token.getOpenId(); if(!StringUtil.isEmpty(openId)){ // 查詢用戶信息 List<CxhWechatMember> memberList = appLotteryService.getMemberList(openId); // 操作次數 int operNum = 1; // 可寫成后臺可配置的 if(memberList != null && memberList.size() > 0){ operNum = operNum - memberList.size(); /*// 獲取用戶信息 String accessToken = CommonUtil.getAccessToken(ConfigUtil.APPID, ConfigUtil.APP_SECRECT).getToken(); cxhWechatMember = CommonUtil.getWeChatMemberInfo(accessToken, openId); // 保存用戶信息 appLotteryService.saveMemberInfo(cxhWechatMember);*/ } if (null == request.getParameter("activityId") || "".equals(request.getParameter("activityId"))){ res.setResult("-2"); res.setMsg("參數錯誤"); return res; } // 查詢活動信息 CxhVoteActivity cxhVoteActivity = appLotteryService.getActivityInfo(request.getParameter("activityId")); if (null == cxhVoteActivity){ res.setResult("-3"); res.setMsg("暫無該類活動"); return res; } CxhVoteAward cxhVoteAward = new CxhVoteAward(); cxhVoteAward.setCxhVoteActivity(cxhVoteActivity); // 查詢獎品列表 List<CxhVoteAward> awardList = appLotteryService.findAwardList(cxhVoteAward); // 返回Map Map<String, Object> rtnMap = new HashMap<String, Object>(); rtnMap.put("activity", cxhVoteActivity); rtnMap.put("awardList", awardList); rtnMap.put("operNum", operNum); rtnMap.put("openId", openId); res.setResult("0"); res.setMsg("請求成功"); res.setData(rtnMap); }else{ res.setResult("-1"); res.setMsg("授權失敗"); } }else{ res.setResult("-1"); res.setMsg("授權失敗"); } return res; }
2.中獎名單接口
/** * 中獎名單接口 * @author lee * @return */ @ResponseBody @RequestMapping(value = "/winningMemberList") public Object queryWinningMemberList(HttpServletRequest request, HttpServletResponse response) { AppListReturn appResult = new AppListReturn(); try { CxhWechatMember cxhWechatMember = new CxhWechatMember(); cxhWechatMember.setIswinning("1"); // 中獎 // 查詢中獎用戶名單(分頁) Page<CxhWechatMember> pageList = appLotteryService.findPage(new Page<CxhWechatMember>(request, response), cxhWechatMember); appResult.setData(pageList.getList()); appResult.setPageNumber(pageList.getPageNo()); appResult.setPageSize(pageList.getPageSize()); appResult.setTotal((int) pageList.getCount()); appResult.setTotalPages(pageList.getTotalPage()); appResult.setResult(0); appResult.setMsg("成功"); } catch (Exception e) { appResult.setResult(-9); appResult.setMsg("系統異常"); logger.info(e.toString(), e); } return appResult; }
3.抽獎接口
/** * 抽獎接口 * @author lee * @return */ @ResponseBody @RequestMapping(value = "/doLottery") public Object doLottery(HttpServletRequest request, HttpServletResponse response) { AppListReturn appResult = new AppListReturn(); // 返回Map Map<String, Object> rtnMap = new HashMap<String, Object>(); String activityId = request.getParameter("activityId"); String openId = request.getParameter("openId"); try { if (null == activityId || "".equals(activityId) || null == openId || "".equals(openId)){ appResult.setResult(-2); appResult.setMsg("參數錯誤"); return appResult; } // 查詢活動信息 CxhVoteActivity cxhVoteActivity = appLotteryService.getActivityInfo(request.getParameter("activityId")); if (null == cxhVoteActivity){ appResult.setResult(-3); appResult.setMsg("暫無該類活動"); return appResult; } CxhVoteAward cxhVoteAward = new CxhVoteAward(); cxhVoteAward.setCxhVoteActivity(cxhVoteActivity); // 查詢獎品列表 List<CxhVoteAward> awardList = appLotteryService.findAwardList(cxhVoteAward); Random rd = new Random(); double dd = rd.nextDouble(); double before = 0; double end = 0; cxhVoteAward.setLevel("5"); // 5-未中獎 // 計算中獎概率 for (int i = 0; i < awardList.size(); i++) { if(i > 0){ before += awardList.get(i-1).getRate().doubleValue(); } end += awardList.get(i).getRate().doubleValue(); if(dd >= before && dd < end){ if(awardList.get(i).getLeftnum() > 0){ // 中獎獎品 cxhVoteAward = awardList.get(i); // 修改獎品剩余數量 cxhVoteAward.setLeftnum(cxhVoteAward.getLeftnum() - 1); appLotteryService.updateAwardNumber(cxhVoteAward); } break; } } // 新增用戶操作記錄 String accessToken = CommonUtil.getAccessToken(ConfigUtil.APPID, ConfigUtil.APP_SECRECT).getToken(); CxhWechatMember cxhWechatMember = CommonUtil.getWeChatMemberInfo(accessToken, openId); cxhWechatMember.setId(IdGen.uuid()); cxhWechatMember.setJoindate(new Date()); cxhWechatMember.setDelFlag("0"); // 保存用戶信息 appLotteryService.saveMemberInfo(cxhWechatMember); rtnMap.put("awardLevel", cxhVoteAward.getLevel()); rtnMap.put("awardId", cxhVoteAward.getId()); appResult.setData(rtnMap); appResult.setResult(0); appResult.setMsg("成功"); } catch (Exception e) { appResult.setResult(-9); appResult.setMsg("系統異常"); logger.info(e.toString(), e); } return appResult; }
4.保存中獎用戶信息的接口
/** * 保存中獎用戶信息的接口 * @author lee * @return */ @ResponseBody @RequestMapping(value = "/saveMemberInfo") public Object saveMemberInfo(HttpServletRequest request, HttpServletResponse response) { AppListReturn appResult = new AppListReturn(); try { // 用戶同意授權后,能獲取到code String openId = request.getParameter("openId"); String username = request.getParameter("username"); String phone = request.getParameter("phone"); String address = request.getParameter("address"); String awardLevel = request.getParameter("awardLevel"); String awardId = request.getParameter("awardId"); if (null == username || "".equals(username) || null == phone || "".equals(phone) || null == address || "".equals(address) || null == openId || "".equals(openId) || null == awardLevel || "".equals(awardLevel) || null == awardId || "".equals(awardId)){ appResult.setResult(-2); appResult.setMsg("參數錯誤"); return appResult; } // 查詢用戶信息 List<CxhWechatMember> memberList = appLotteryService.getMemberList(openId); CxhWechatMember cxhWechatMember = memberList.get(0); cxhWechatMember.setUsername(username); cxhWechatMember.setPhone(phone); cxhWechatMember.setAddress(address); cxhWechatMember.setIswinning(awardLevel == "5" ? "0" : "1"); cxhWechatMember.setAwardid(awardId); appLotteryService.update(cxhWechatMember); appResult.setResult(0); appResult.setMsg("成功"); } catch (Exception e) { appResult.setResult(-9); appResult.setMsg("系統異常"); logger.info(e.toString(), e); } return appResult; }
以上是“APP轉盤抽獎Java服務端接口的示例分析”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。