时间:2021-05-21
应公司需求开发一个微信公众号中抽奖活动
功能:奖品及中奖概率可在后台配置,滚动刷新中奖名单,控制用户每日抽奖次数等。
规则:在活动期间,每日可抽奖一次,中奖后填写个人信息以便奖品的配送。
1.获取抽奖页面数据
/** * 获取抽奖页面数据 * @param request * @param response * @return * @throws ServletException * @throws IOException */@RequestMapping(value="/queryLotteryActivity")@ResponseBodypublic 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(); } 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;}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了PHP转盘抽奖接口的实现方法。分享给大家供大家参考。具体如下:这里的转盘抽奖随机返回一个转盘角度,概率可自己定义lottery_get.php接口
近期使用node做服务端渲染,作为中间层需要请求后端接口,需要封装服务端的请求,接下来来了解下如何使用request。基本使用constrequest=requ
时间戳作用客户端在向服务端接口进行请求,如果请求信息进行了加密处理,被第三方截取到请求包,可以使用该请求包进行重复请求操作。如果服务端不进行防重放攻击,就会服务
最近在研究vue+webAPI进行前后端分离,在一些如前端定时循环请求后台接口判断状态等应用场景用使用mqtt进行主动的消息推送能够很大程度的减小服务端接口的压
微信公众号的服务号和订阅号都可以制作,微信抽奖有多种,有大转盘、砸金蛋、摇一摇抽奖等,商家可以利用这些抽奖活动吸引粉丝互动,那么微信公众平台大转盘、砸金蛋、摇一