你的位置:首页 > Java教程

[Java教程]微信公众平台开发—成语接龙


一、源码

package org.xs.dntown.wx.idiom.modules.web;import java.util.Date;import java.util.List;import java.util.Random;import javax.servlet.http.HttpServletRequest;import org.apache.commons.lang3.StringUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.xs.dntown.wx.common.utils.MessageUtil;import org.xs.dntown.wx.common.web.BaseController;import org.xs.dntown.wx.core.modules.entity.UserInfo;import org.xs.dntown.wx.core.modules.entity.enums.ModuleEnum;import org.xs.dntown.wx.core.modules.entity.req.BaseMsgReq;import org.xs.dntown.wx.core.modules.service.LogService;import org.xs.dntown.wx.core.modules.service.UserService;import org.xs.dntown.wx.idiom.modules.entity.IdiomListInfo;import org.xs.dntown.wx.idiom.modules.entity.IdiomUserInfo;import org.xs.dntown.wx.idiom.modules.entity.enums.StepEnum;import org.xs.dntown.wx.idiom.modules.service.IdiomListService;import org.xs.dntown.wx.idiom.modules.service.IdiomUserService;/** * 核心Controller */@Controller(value = "idiomCoreController")@RequestMapping("idiom")public class CoreController extends BaseController {		@Autowired	private IdiomUserService idiomUserService;		@Autowired	private IdiomListService idiomListService;		@Autowired	private UserService userService;		@Autowired	private LogService logService;		/**	 * 文本消息	 */	@ResponseBody	@RequestMapping(value = "msg", produces = { "text/plain;charset=utf-8" })	public String textMsg(HttpServletRequest request) throws Exception {		String result = "";		String openId = "";		String userName = "";		String content = "";		try {						//得到接收消息实体			BaseMsgReq msgReq = (BaseMsgReq)request.getAttribute("msgReq");			openId = msgReq.getFromUserName();			content = msgReq.getContent();						//得到用户信息实体			UserInfo userInfo = (UserInfo)request.getAttribute("userInfo");			userName = userInfo.getUserName();						log.info("收到消息:" + msgReq.getContent());						//更新模块			userService.setModule(msgReq.getFromUserName(), ModuleEnum.idiom.getValue());						//得到接龙用户信息实体			IdiomUserInfo iUserInfo = idiomUserService.getByOpenId(userInfo.getOpenId());			if(iUserInfo == null) {				log.info("新增接龙用户");								//如果没有就新增				iUserInfo = new IdiomUserInfo();				iUserInfo.setOpenId(userInfo.getOpenId());				iUserInfo.setModuleStep(StepEnum.normal.getValue());				iUserInfo.setStepTime(new Date());				iUserInfo.setIsAnswered(false);				idiomUserService.add(iUserInfo);			}						//刷新模块(1小时)			long timeDiff = new Date().getTime() - userInfo.getModuleTime().getTime();			long hoursDiff = timeDiff / (1000 * 60 * 60);			if(hoursDiff >= 1) {				idiomUserService.setStep(userInfo.getOpenId(), StepEnum.normal.getValue());				iUserInfo.setModuleStep(StepEnum.normal.getValue());			}						log.info("开始转入步骤");			//出题			if(msgReq.getContent().equals("开始")) {								//完成上一个成语				if(!StringUtils.isEmpty(iUserInfo.getIdiomId()) && iUserInfo.getModuleStep().equals(StepEnum.answer.getValue())) {					//处理答题结果					disposeAnswer(iUserInfo, msgReq);										result += finishAnswer(iUserInfo, msgReq, false) + "\n";				}								log.info("开始下一题");								//下一题				result = nextIdiom(iUserInfo, result);								logService.addInfoLog(openId, userName, content, result, ModuleEnum.idiom.getValue(), StepEnum.answer.getValue());				result += MessageUtil.msgReqTo

源码地址:https://github.com/ctxsdhy/dntown 

二、演示