重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章主要介绍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)){ // 查询用户信息 ListmemberList = 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 awardList = appLotteryService.findAwardList(cxhVoteAward); // 返回Map Map rtnMap = new HashMap (); 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"); // 中奖 // 查询中奖用户名单(分页) PagepageList = appLotteryService.findPage(new Page (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 MaprtnMap = new HashMap (); 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 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; } // 查询用户信息 ListmemberList = 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服务端接口的示例分析”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!