签到思路
签到会都有个周期,一般以一周为单位更新一次签到奖励,清空周累计签到次数。
签到功能的实现,关键在于: 第一,要记录最后一次签到的时间戳,(存储在数据库的字段) 第二,要判今天是否已经签到,若以签到则不能在签到(应用时间戳判断数据库上次时间戳与当前时间戳是否超过一天的时间戳单位)
第三,新的一周开始后,将签到次数归0,用户可重新签到
实现过程
1.获取当前时间戳的日期 与 上次签到时间戳的日期 若是同一天,获取时间当前戳判断与数据库差值是否超过一天(说明是不同月份日期相同),即可签到。不是说明是同年同月同日判断是否已签到即可。
2.刷新签到,当用户上周三签到后中间都没签到,这周二登入获取签到信息时就是重新刷新过的,那么如何判断呢,小编这里用法是:时间戳可以获取到周几,单上次签到是周三时,本次签到时是周二,明显是周二后才周三的,明显是同一周刷新数据库签到,
若是上周是周二签到,本周是周三签到,这就要考虑是否是同一周时间了,若是同一周就不应该刷新数据。不是同一周的时候时间搓肯定相差超过7天。
分析完来看下代码实现
-- user : 数据库中用户信息包括是否已签到字段和签到次数 -- user.signtime 上次签到的时间戳 -- user.issign 是否已签到 function get_user(s, req) local stype = req[1] local ctype = req[2] local utag = req[3] local body = req[4] local userid = body.userid; print("stype:"..stype.." ctype:"..ctype.." utag:"..utag) --"body:"..body mysql_wechat_threecountry.get_user(userid,function(err, user) if err then -- 告诉客户端系统错误信息; print("get user fail:"..err); return end local cDateCurrectTime = os.date("*t") --周日 = 1 -- 周六 = 7 每周日刷新 local tmpWDay = cDateCurrectTime.wday -- 当前签到日期 周几 local lastWDay = os.date("*t",user.signtime).wday -- 上一次签到日期 周几 local stime = tonumber(os.time()) - tonumber(user.signtime) if (tmpWDay <= lastWDay and stime > 86400) then --更新 print("zhou tian shao") end if tmpWDay > lastWDay and stime > (86400 * 7) then --(3600 * 24) = 86400 一天24小时 超过一天就说明是 --更新 print("zhou tian duo") end --签到次数大于1时 且 签到次数大于1时 if ((tmpWDay <= lastWDay) and (stime > 86400)) or ((tmpWDay > lastWDay) and (stime > (86400 * 7))) then --签到次数大于1时 --if tonumber(user.signcount) > 1 then mysql_wechat_threecountry.update_user(userid,Constant.SignInType.NoSignIn,user.signtime,0,function(err,ret) if err then -- 告诉客户端系统错误信息; print("get update fail:"..err); return end user.issign = Constant.SignInType.NoSignIn user.signcount = 0 local data = { status = Respones.OK, userinfo = user, } local msg = {Stype.System, Cmd.eOnGetUserRes,utag, data} Session.send_msg(s,msg) end) --end else local lastDay = tonumber(os.date("%d",user.signtime)) local tmpDay = tonumber(os.date("%d",os.time())) local intvel = tmpDay - lastDay -- 上次签到至今相差几天 --日期不同号 if intvel >= 1 then user.issign = Constant.SignInType.NoSignIn end --排除不同月分的同一号 local timestatus = os.time() - user.signtime if timestatus > 86400 then --(3600 * 24) = 86400 user.issign = Constant.SignInType.NoSignIn end local data = { status = Respones.OK, userinfo = user, } local msg = {Stype.System, Cmd.eOnGetUserRes,utag, data} Session.send_msg(s,msg) end end) end