欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

APP转盘抽奖Java服务端接口详解

程序员文章站 2024-02-22 21:56:30
应公司需求开发一个微信公众号中抽奖活动 功能:奖品及中奖概率可在后台配置,滚动刷新中奖名单,控制用户每日抽奖次数等。 规则:在活动期间,每日可抽奖一次,中奖后填写个人信...

应公司需求开发一个微信公众号中抽奖活动

功能:奖品及中奖概率可在后台配置,滚动刷新中奖名单,控制用户每日抽奖次数等。

规则:在活动期间,每日可抽奖一次,中奖后填写个人信息以便奖品的配送。

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;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。