APP转盘抽奖Java服务端接口详解
时间:2020-10-08 13:28:31|栏目: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; }
上一篇:Java 在Excel单元格中应用一种/多种字体样式(实例代码)
栏 目:JAVA代码
本文标题:APP转盘抽奖Java服务端接口详解
本文地址:http://www.codeinn.net/misctech/8614.html
阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机