APP轉(zhuǎn)盤抽獎Java服務(wù)端接口詳解
應(yīng)公司需求開發(fā)一個微信公眾號中抽獎活動
功能:獎品及中獎概率可在后臺配置,滾動刷新中獎名單,控制用戶每日抽獎次數(shù)等。
規(guī)則:在活動期間,每日可抽獎一次,中獎后填寫個人信息以便獎品的配送。
1.獲取抽獎頁面數(shù)據(jù)
/**
* 獲取抽獎頁面數(shù)據(jù)
* @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();
// 用戶同意授權(quán)后,能獲取到code
String code = request.getParameter("code");
// 用戶同意授權(quán)
if (!"authdeny".equals(code)) {
// 獲取網(wǎng)頁授權(quán)access_token
WeixinOauth2Token weixinOauth2Token = CommonUtil
.getOauth2AccessToken(ConfigUtil.APPID, ConfigUtil.APP_SECRECT, code);
// 用戶標(biāo)識
String openId = weixinOauth2Token.getOpenId();
if(!StringUtil.isEmpty(openId)){
// 查詢用戶信息
List<CxhWechatMember> memberList = appLotteryService.getMemberList(openId);
// 操作次數(shù)
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("參數(shù)錯誤");
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("授權(quán)失敗");
}
}else{
res.setResult("-1");
res.setMsg("授權(quán)失敗");
}
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("系統(tǒng)異常");
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("參數(shù)錯誤");
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);
// 修改獎品剩余數(shù)量
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("系統(tǒng)異常");
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 {
// 用戶同意授權(quán)后,能獲取到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("參數(shù)錯誤");
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("系統(tǒng)異常");
logger.info(e.toString(), e);
}
return appResult;
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解Spring Boot 定制HTTP消息轉(zhuǎn)換器
本篇文章主要介紹了詳解Spring Boot 定制HTTP消息轉(zhuǎn)換器,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11
Java設(shè)計模塊系列之書店管理系統(tǒng)單機版(二)
這篇文章主要為大家詳細(xì)介紹了Java單機版的書店管理系統(tǒng)設(shè)計模塊和思想第二章,感興趣的小伙伴們可以參考一下2016-08-08
Java Hibernate中使用HQL語句進行數(shù)據(jù)庫查詢的要點解析
HQL是Hibernate框架中提供的關(guān)系型數(shù)據(jù)庫操作腳本,當(dāng)然我們也可以使用原生的SQL語句,這里我們來看一下在Java Hibernate中使用HQL語句進行數(shù)據(jù)庫查詢的要點解析:2016-06-06
詳解@ConfigurationProperties如何裝載到Spring容器中
這篇文章主要為大家詳細(xì)介紹了@ConfigurationProperties該如何裝載到Spring容器中,文中的示例代碼講解詳細(xì),需要的小伙伴可以參考一下2023-07-07

