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 WeixinOauth2Token weixinOauth2Token = CommonUtil .getOauth2AccessToken(ConfigUtil.APPID, ConfigUtil.APP_SECRECT, code); // 用户标识 String openId = weixinOauth2Token.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; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)