【实用】每日签到jQuery+Java实现从数据库读取数据并展示

jQuery+SSM实现每日签到   

前台jQuery 后台框架是SSM从数据库读取数据来显示并实现每日签到功能

JavaScript代码:

	<script type="text/javascript">
		 //签到
		 var  dayNumList;//自定义设置的签到天数List
		 var signContinuousFlag = false;
				 $(function() { 
				//获取设置的签到天数List				
				 getDayNum();
				 
				 
		     $("#box").click(function(){
		         $(".alert-box").show();
		         $("#videoDiv").hide();
		     });
		     $(".close").click(function () {
		         $(".alert-box").hide();
		         $("#videoDiv").show();
		     });
		  		 });
		 function getDayNum(){
				var postData = {   };
				$.post("<%=path %>/mobile/GuessManagerController/getIntegrateList.do", postData, function (date) {
					if (date.flag&&date.info.length>0) {
						for(var i=0;i<date.info.length;i++){
							var daynumhtml = "";
							daynumhtml = daynumhtml+
							"<li>"+
			                "<p class=\"week-list\">第"+date.info[i].dayNum+"天</p>"+
			                "<span class=\"icon-money-icon\"></span>"+
			                "</li>"
							$("#dayNumDiv").append(daynumhtml);
							$("#signNum").html(date.signNum.signNumber);
							if(date.signNum.signNumber==date.info[i].dayNum){
								$(".sign-box-list-item > li").css('background-color','#FFD67C');
							}
						}
						
						dayNumList = date.info;
					}else{
						layer.msg(date.msg);
					}
					
				 })
			 }
		 		
             //今日签到
        function signToday(){
			//查看是否连续签到
			var signNum = Number($("#signNum").text());
		   //var signTime = new Date(jsonTimeStamp($("#signTime").val()));//上一次签到日期
			var integrationNum;
			var dayNums;
			//signTime.setDate(signTime.getDate()+1)
			//var today = new Date();
			//查看后台设置的天数和计算当天积分和额外积分
			$.ajax({
	               type: "POST",
	               url: "<%=path %>/mobile/GuessManagerController/getIntegralData.do",
	               async:false,
	               success: function(date){
	            	   dayNums = Number(date.info.length)+1;
	            	   if (date.flag) {
	   					for(var i=0;i<date.info.length;i++){
	   					//如果当前签到天数等于目前展示对应天数,就计算其所获得的总积分
	   					if(signNum==Number(date.info[i].dayNum)){
	   						integrationNum = date.info[i].totalScore;
	   					} }
	   				}else{
	   					layer.msg(date.msg);
	   				}
	                  }
	            });
			if(signContinuousFlag){
				signNum=1;
			}else{
				signNum += 1;
			if(signNum>dayNums) signNum=1;
			}
		
			$.ajax({
	               type: "POST",
	               url: "<%=path %>/mobile/GuessManagerController/signToday.do",
	               data:{
	   				"integrationNum":integrationNum,
					"signNum":signNum
					},
	               async:false,
	               success: function(date){
	            	   if (date.flag) {
	    					/************签到显示**************************/
	    					if(date.signNum==1){ //第一天签到
	   		                $(".sign-box-list-item > li").each(function () {
	   		                    $(this).css('background-color','');
	   		                    $('.week-list').css('color','#999');
	   		                    $('.icon-money-icon').append("<style>.icon-money-icon::before{color:}</style>");
	   		                })
	   		             var isSigns =  $(".sign-box-list-item").children().eq(date.signNum-1);
		   					isSigns.css('background-color','#FFD67C');
		   					isSigns.children().find("p[class='week-list']").css('color','#000');
		   					isSigns.children().find("span[class='icon-money-icon']").append("<style>.icon-money-icon::before{color:#c43737}</style>");
		   					$("#signNum").html(Number(date.signNum));
	    					}else{
	   					var isSign =  $(".sign-box-list-item").children().eq(signNum-1);
	   					isSign.css('background-color','#FFD67C');
	   					isSign.children().find("p[class='week-list']").css('color','#000');
	   					isSign.children().find("span[class='icon-money-icon']").append("<style>.icon-money-icon::before{color:#c43737}</style>");
	   					$("#signNum").html(Number(signNum));
	   					
	    					/************签到显示**************************/


	   					getIntegration();
	   					/***************置灰按钮******************************/
	   					$(".sign-box-btn-day").attr("disabled","disabled");
	   					$(".sign-box-btn-day").css("background-color","#DCDCDC");
	   					return;
	    					}}else{
	    					layer.msg(date.msg);
	    					$(".sign-box-btn-day").attr("disabled","disabled");
		   					$(".sign-box-btn-day").css("background-color","#DCDCDC");
	    					return;
	    				}
	            	   
	               }
	               });
		        }
        
        function communication(){
        	var liveTerm = $("#liveTerm").val();
        	var liveId = $("#liveId").val();
 window.location.href = '<%=path %>/mobile/CommunicationController/CommunicationController_goMain.do?liveId='+liveId+'&liveTerm='+liveTerm;
        }
    </script>

controller代码:

/**
	 * 获取签到天数、签到积分 list
     * 
	 */
	@RequestMapping(value="/getIntegrateList")
	@ResponseBody
	public Object getIntegrateList(Page page) throws Exception {
		PageData pd = new PageData();
		pd = this.getPageData();
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		WxUser tempUser = (WxUser) session.getAttribute(Constant.WX_LOGIN_SESSION);
		pd.put("WxOpenId", tempUser.getWxOpenId());
		try {
			List<PageData> resultList = guessFacade.getIntegrateList(pd);
			PageData signNumber = guessFacade.signNumber(pd);
			pd.put("signNum", signNumber);
			pd.put("info", resultList);
			pd.put("flag", true);
			pd.put("msg", "签到积分成功!");
		} catch (Exception e) {
			pd.put("flag", false);
			pd.put("msg", "签到积分失败!");
			e.printStackTrace();
		}
		return pd;
	}
	
	
	
	/**
	 * 今天签到
     * 
	 */
	@RequestMapping(value="/signToday")
	@ResponseBody
	public Object updateSignToday(Page page) throws Exception {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		PageData pd = new PageData();
		pd = this.getPageData();
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		WxUser tempUser = (WxUser) session.getAttribute(Constant.WX_LOGIN_SESSION);
		//pd.put("WxOpenId", "oQWlJ1PGwqbWx0IMxGJeZMlnWSjs");
		pd.put("WxOpenId", tempUser.getWxOpenId());
		try {
			//查询当前用户最后签到时间和连续签到多少天
			List<PageData> list = guessFacade.findSignRecord(pd);
			if(list.size()>0){
				Date today = new Date();
				String todays = dateFormat.format(today);
			    String lastTime = list.get(0).get("lastSignTime").toString();
			    Date lastSignTime = dateFormat.parse(lastTime);
			    Date todayTime = dateFormat.parse(todays);
				if(UtilsDate.differentDays(lastSignTime,todayTime)>1){
				pd.put("signNum", 1);
				}else if(UtilsDate.differentDays(lastSignTime,todayTime)==0){
					pd.put("flag", false);
					pd.put("msg", "今天已经签到过!请勿重复签到!");
					return pd;
				}
				
			}
			guessFacade.updateSignToday(pd);
			pd.put("flag", true);
			pd.put("msg", "签到成功!");
		} catch (Exception e) {
			pd.put("flag", false);
			pd.put("msg", "签到失败!");
			e.printStackTrace();
		}
		return pd;
	}
	


	
	/**
	 * 获取签到积分
     * 
	 */
	@RequestMapping(value="/GuessManagerController_getUserAnswer")
	@ResponseBody
	public Object getUserAnswer(Page page) throws Exception {
		PageData pd = new PageData();
		pd = this.getPageData();
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		WxUser tempUser = (WxUser) session.getAttribute(Constant.WX_LOGIN_SESSION);
		//pd.put("WxOpenId", "oQWlJ1PGwqbWx0IMxGJeZMlnWSjs");
		pd.put("WxOpenId", tempUser.getWxOpenId());
		
		try {
			List<PageData> result = guessFacade.getUserAnswer(pd);
			pd.put("info", result);
			pd.put("flag", true);
			pd.put("msg", "操作成功!");
		} catch (Exception e) {
			pd.put("flag", false);
			pd.put("msg", "操作失败!");
			e.printStackTrace();
		}
		return pd;

	}

	/**
	 * 获取需要签到的相关信息
     *
	 */
	@RequestMapping(value="/getIntegralData")
	@ResponseBody
	public Object getIntegralData(Page page) throws Exception {
		PageData pd = new PageData();
		pd = this.getPageData();
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		WxUser tempUser = (WxUser) session.getAttribute(Constant.WX_LOGIN_SESSION);
		pd.put("WxOpenId", tempUser.getWxOpenId());
		try {
			List<PageData> resultList = guessFacade.getIntegrateData(pd);
			pd.put("info", resultList);
			pd.put("flag", true);
			pd.put("msg", "签到积分成功!");
		} catch (Exception e) {
			pd.put("flag", false);
			pd.put("msg", "签到积分失败!");
			e.printStackTrace();
		}
		return pd;
	}

mapper文件代码

<!-- 	获取签到记录 -->
	<select id="findSignRecord" parameterType="page" resultType="pd">
		select  
			wx_open_id as WxOpenId,
			last_sign_time as lastSignTime,
			sign_number as signNumber
		 from 
		l_user_sign
		where  wx_open_id =  #{WxOpenId}
	</select>
	 <!-- 签到天数 -->
	<select id="signNumber" parameterType="page" resultType="pd">
		select  
			sign_number as signNumber
		 from 
		l_user_sign
		where  wx_open_id =  #{WxOpenId}
	</select>
	
		<update id="updateSignStatus" parameterType="pd">
	        update l_integrate_setting
	        <set>
	            status=1,
	           updateTime=now()
	        </set>
	        where id =#{inteId}
	</update>
	<!-- 今天签到  -->
	<update id="updateSignToday" parameterType="pd">
	        update l_user_sign
	        <set>
	            sign_number=#{signNum},
	            last_sign_time=now()
	        </set>
	        where wx_open_id =#{WxOpenId}
	</update>
	
	<!-- 今天日期为1时 先删除,再插入  -->
	<!-- 删除 -->
	<delete id="deleteSignToday" parameterType="pd" >
		delete from l_user_sign
			where
				wx_open_id = #{  WxOpenId  }
	</delete>
	<!-- 添加  -->
	<insert id="insertSignToday" parameterType="pd">
		insert into
			l_user_sign
			(
					wx_open_id,
					last_sign_time,
					sign_number,
					createTime,
					updateTime
			)
			values
			(
					#{  WxOpenId  },
					now(),
					#{  signNum  },
					now(),
					now()
					
			)

	</insert>

<select id="getIntegrateData" parameterType="page" resultType="pd">
		select 
				id,
				day_num as dayNum,
				status,
				integrate_num as integrateNum,
				add_integrate_num as addIntegrateNum,
				integrate_num+add_integrate_num as totalScore
		from l_integrate_setting
		where 1=1
		order by day_num asc

	</select>

DAO实现类代码

/**
	 * 获取签到记录
	 */
	@SuppressWarnings({ "unchecked", "deprecation" })
	@Override
	public List<PageData> findSignRecord(PageData pd) throws Exception {
		List<PageData> result = (List<PageData>) dao.findForList("GuessMapper.findSignRecord", pd);
		return result;
	}


	/**
	 * 今日签到
	 */
	public void updateSignToday(PageData pd) throws Exception {
		if (pd.get("signNum").toString().equals("1")) {
			dao.delete("GuessMapper.deleteSignToday", pd);// 先删除用户之前记录
			dao.save("GuessMapper.insertSignToday", pd);// 插入用户签到
			dao.update("GuessMapper.updateSignStatus", pd);//改变是否签到状态
		} else {
			dao.update("GuessMapper.updateSignToday", pd);// 更新题目
		}


		pd.put("activeId", "");// id:问题id
		pd.put("integrationFlag", "+");// 标记
		pd.put("activeName", "签到奖励");// 名称integrationNum
		dao.save("GuessMapper.saveIntegration", pd);// 写入表明细
		dao.save("GuessMapper.updateSignIntegration", pd);// 更新总

	}

效果图


阅读更多
个人分类: JavaScript
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭