def subEvent(msg):
    data = tools.client.user.get(msg.source)
    try:
        username = data["nickname"]
        users = User.objects.filter(openId=msg.source)
        if len(users) > 0:
            user = users[0]
        else:
            user = User(name=data["nickname"],
                        sex=int(data["sex"]),
                        openId=msg.source,
                        comment=u"现在还是空的><")
            user.save()
            team = Team(name=basic_tools.teamName(None, username), type=0)
            team.save()
            team.members.add(user)
        user.name = username
        user.uid = int(random.random() * 100)
        if "headimgurl" in data:
            user.image = data["headimgurl"]
        user.save()
        date = basic_tools.getDate()
        #如果DayData不存在,则创建
        if DayData.objects.filter(user=user, date=date).count() == 0:
            dayData = DayData(user=user, date=date)
            basic_tools.updateDayData(dayData, user)
            dayData.save()
    except:
        username = ""
    return HttpResponse(
        create_reply(u"%s!欢迎使用新中韩无敌了的手环公众号!\n%s" % (username, tools.help_text),
                     message=msg))
def subEvent(msg):
    data = tools.client.user.get(msg.source)
    try:
        username = data["nickname"]
        users = User.objects.filter(openId=msg.source)
        if len(users) > 0:
            user = users[0]
        else:
            user = User(name=data["nickname"],sex=int(data["sex"]),openId=msg.source,comment=u"现在还是空的><")
            user.save()
            team = Team(name=basic_tools.teamName(None, username),type=0)
            team.save()
            team.members.add(user)
        user.name = username
        user.uid = int(random.random() * 100)
        if "headimgurl" in data:
            user.image = data["headimgurl"]
        user.save()
        date = basic_tools.getDate()
        #如果DayData不存在,则创建
        if DayData.objects.filter(user=user,date=date).count() == 0:
            dayData = DayData(user=user,date=date)
            basic_tools.updateDayData(dayData, user)
            dayData.save()
    except:
        username = ""
    return HttpResponse(create_reply(u"%s!欢迎使用新中韩无敌了的手环公众号!\n%s" % (username, tools.help_text), message=msg))
Beispiel #3
0
 def handle(self, *args, **options):
     date = tools.getDate()
     users = User.objects.all()
     for user in users:
         if DayData.objects.filter(user=user, date=date).count() == 0:
             dayData = DayData(user=user, date=date)
             tools.updateDayData(dayData, user)
             dayData.save()
         Data.objects.filter(user=user, date=preDate(date)).delete()
 def handle(self, *args, **options):
     date = tools.getDate()
     users = User.objects.all()
     for user in users:
         if DayData.objects.filter(user=user,date=date).count() == 0:
             dayData = DayData(user=user,date=date)
             tools.updateDayData(dayData, user)
             dayData.save()
         Data.objects.filter(user=user,date=preDate(date)).delete()
def dataUpdate(data):
    try:
        dayData = DayData.objects.get(user=data.user, date=data.date)
    except:
        dayData = DayData(user=data.user, date=data.date)
        tools.updateDayData(dayData, data.user)
    dayData.calories = dayData.calories + data.calories
    tools.modifiedDayData(dayData, data.user)
    dayData.sleep = dayData.sleep + data.lsNum + data.dsNum
    dayData.save()
    PlansCheck(data.user, data)
    MatchsCheck(data.user, data)
    LevelCheck(data.user, data)
    ArchiveCheck(data.user, data)
def dataUpdate(data):
    try:
        dayData = DayData.objects.get(user=data.user, date=data.date)
    except:
        dayData = DayData(user=data.user, date=data.date)
        tools.updateDayData(dayData, data.user)
    dayData.calories = dayData.calories + data.calories
    tools.modifiedDayData(dayData, data.user)
    dayData.sleep = dayData.sleep + data.lsNum + data.dsNum
    dayData.save()
    PlansCheck(data.user, data)
    MatchsCheck(data.user, data)
    LevelCheck(data.user, data)
    ArchiveCheck(data.user, data)
def clickEvent(msg):
    reply = TextReply(content=u"I am 菜单拉取事件", message=msg)
    user = User.objects.get(openId=msg.source)
    id = user.id
    data = tools.client.user.get(msg.source)
    #更新用户姓名、头像
    user.name = data["nickname"]
    if "headimgurl" in data:
        user.image = data["headimgurl"]
    else:
        user.image = ""
    user.save()
    date = basic_tools.getDate()
    datetime = basic_tools.getDateTime()
    now = basic_tools.getNow()
    if msg.key == "V1001_DATA_TODAY":  #今日战况
        data = DayData.objects.filter(user=user, date=date)
        if len(data) == 0:
            data = DayData(user=user, date=date)
            basic_tools.updateDayData(data, user)
            data.save()
        else:
            data = data[0]
        steps = data.steps
        dayPlan = user.dayPlan
        if steps < dayPlan:
            per = steps * 100 / dayPlan
        else:
            per = 100
        if per <= 25:
            remark = u"前路漫漫,要加油噢!"
        elif per >= 40 and per <= 60:
            remark = u"成功的路已经走了一半,继续努力!"
        elif per == 100:
            remark = u"已经完成预定计划了!你真棒!"
        elif per >= 90:
            remark = u"就快要成功了!加油加油!"
        else:
            remark = ""
        url = "%s/basic/redirect/profile?page=0" % tools.domain
        data = {
            "steps": {
                "value": str(steps),
                "color": "#ff0000",
            },
            "per": {
                "value": str(per),
                "color": "#007fff",
            },
            "remark": {
                "value": remark,
                "color": "#666666"
            }
        }
        tools.customSendTemplate(msg.source, tools.template_id["data"],
                                 "#000000", data, url)
        reply = ""
    elif msg.key == "V1001_DATA_BIND":  #绑定手环
        url = "%s/basic/bind?openId=%s" % (tools.domain, msg.source)
        data = {"content": {"value": u"请点击进入绑定", "color": "#ff5656"}}
        tools.customSendTemplate(msg.source, tools.template_id["msg"],
                                 "#000000", data, url)
        reply = ""
    elif msg.key == "V1001_DATA_HELP":  #帮助
        reply = TextReply(content=tools.help_text, message=msg)
    elif msg.key == "V1001_DATA_PROFILE":  #个人主页
        dayPlan = user.dayPlan
        sleepPlan = user.sleepPlan
        if dayPlan == 0 or sleepPlan == 0:
            remark = u"您的计划设置不完整,请点击进入设置"
        else:
            remark = user.comment
            if remark == "":
                remark = u"点击查看个人信息"
        url = "%s/basic/redirect/profile?page=3" % tools.domain
        data = {
            "dayPlan": {
                "value": str(dayPlan),
                "color": "#ff0000"
            },
            "sleepPlan": {
                "value": str(sleepPlan),
                "color": "#007fff"
            },
            "remark": {
                "value": remark,
                "color": "#666666"
            }
        }
        tools.customSendTemplate(msg.source, tools.template_id["profile"],
                                 "#000000", data, url)
        reply = ""
    elif msg.key == "V1001_PLAN_MAKE":  #制定计划
        articles = []
        articles.append({
            "title": u"制定计划",
            "description": u"快来制定你自己的运动计划吧!",
            "image": "%s/static/img/plan_make.jpg" % tools.domain,
            "url": "%s/plan/redirect/profile?page=0" % tools.domain
        })
        reply = ArticlesReply(articles=articles, message=msg)
    elif msg.key == "V1001_PLAN_OWN":  #我的计划
        articles = []
        articles.append({
            "title": u"查看我的",
            "description": u"来看看你都有什么运动计划吧",
            "image": "%s/static/img/plan_own.jpg" % tools.domain,
            "url": "%s/plan/redirect/profile?page=3" % tools.domain
        })
        reply = ArticlesReply(articles=articles, message=msg)
    elif msg.key == "V1001_PLAN_SQUARE":  #计划广场
        articles = []
        articles.append({
            "title": u"计划广场",
            "description": u"新鲜出炉的运动计划",
            "image": "%s/static/img/plan_square.jpg" % tools.domain,
            "url": "%s/plan/redirect/profile?page=2" % tools.domain
        })
        reply = ArticlesReply(articles=articles, message=msg)
    elif msg.key == "V1001_PLAN_RANK":  #计划排行榜
        articles = []
        articles.append({
            "title": u"计划排行榜",
            "description": u"想知道什么计划更受欢迎么",
            "image": "%s/static/img/plan_rank.jpg" % tools.domain,
            "url": "%s/plan/redirect/profile?page=1" % tools.domain
        })
        reply = ArticlesReply(articles=articles, message=msg)
    elif msg.key == "V1001_MATCH_MAKE":  #创建比赛
        articles = []
        articles.append({
            "title":
            u"创建比赛",
            "description":
            u"开始一场新的比赛吧!",
            "image":
            "%s/static/img/match_make.jpg" % tools.domain,
            "url":
            "%s/match/redirect/profile?page=0" % tools.domain
        })
        reply = ArticlesReply(articles=articles, message=msg)
    elif msg.key == "V1001_MATCH_CHECK":  #我的比赛进度查看
        closest_match = match_tools.closest_match(user, now)
        if not closest_match:  #没有未结束的比赛
            data = {
                "title": {
                    "value": u"比赛进度提醒",
                    "color": "#000000"
                },
                "content": {
                    "value": u"没有未结束的比赛",
                    "color": "#ff0000"
                },
                "remark": {
                    "value": u"来亲自发起一场比赛吧!",
                    "color": "#666666"
                }
            }
            url = "%s/match/redirect/profile?page=0" % tools.domain
            tools.customSendTemplate(msg.source, tools.template_id["msg"],
                                     "#000000", data, url)
        else:  #返回最近的比赛的进度
            left_time = basic_tools.left_time(now, closest_match.endTime)
            steps = match_tools.getProgress(closest_match, user)
            data = {
                "object": {
                    "value": u"%s 比赛" % closest_match.title,
                    "color": "#007fff"
                },
                "lastTime": {
                    "value": left_time,
                    "color": "#ff0000"
                },
                "steps": {
                    "value": str(steps),
                    "color": "#007fff"
                },
                "remark": {
                    "value": u"请继续努力!",
                    "color": "#666666"
                }
            }
            url = "%s/match/redirect/profile?page=1" % tools.domain
            tools.customSendTemplate(msg.source, tools.template_id["progress"],
                                     "#000000", data, url)
        reply = ""
    elif msg.key == "V1001_MATCH_SQUARE":  #比赛广场
        articles = []
        articles.append({
            "title":
            u"比赛广场",
            "description":
            u"新鲜出炉的比赛",
            "image":
            "%s/static/img/match_square.jpg" % tools.domain,
            "url":
            "%s/match/redirect/profile?page=2" % tools.domain
        })
        reply = ArticlesReply(articles=articles, message=msg)
    return HttpResponse(reply)
def clickEvent(msg):
    reply = TextReply(content=u"I am 菜单拉取事件", message=msg)
    user = User.objects.get(openId=msg.source)
    id = user.id
    data = tools.client.user.get(msg.source)
    #更新用户姓名、头像
    user.name = data["nickname"]
    if "headimgurl" in data:
        user.image = data["headimgurl"]
    else:
        user.image = ""
    user.save()
    date = basic_tools.getDate()
    datetime = basic_tools.getDateTime()
    now = basic_tools.getNow()
    if msg.key == "V1001_DATA_TODAY": #今日战况
        data = DayData.objects.filter(user=user,date=date)
        if len(data) == 0:
            data = DayData(user=user,date=date)
            basic_tools.updateDayData(data, user)
            data.save()
        else:
            data = data[0]
        steps = data.steps
        dayPlan = user.dayPlan
        if steps < dayPlan:
           per = steps * 100 / dayPlan
        else:
           per = 100
        if per <= 25:
           remark = u"前路漫漫,要加油噢!"
        elif per >= 40 and per <= 60:
           remark = u"成功的路已经走了一半,继续努力!"
        elif per == 100:
           remark = u"已经完成预定计划了!你真棒!"
        elif per >= 90:
           remark = u"就快要成功了!加油加油!"
        else:
           remark = ""
        url = "%s/basic/redirect/profile?page=0" % tools.domain
        data = {
          "steps":{
            "value": str(steps),
            "color": "#ff0000",
          },
          "per":{
            "value": str(per),
            "color": "#007fff",
          },
          "remark":{
            "value": remark,
            "color": "#666666"
          }
        }
        tools.customSendTemplate(msg.source, tools.template_id["data"], "#000000", data, url)
        reply = ""
    elif msg.key == "V1001_DATA_BIND": #绑定手环
        url = "%s/basic/bind?openId=%s" % (tools.domain, msg.source)
        data = {
          "content":{
            "value": u"请点击进入绑定",
            "color": "#ff5656"
          }
        }
        tools.customSendTemplate(msg.source, tools.template_id["msg"], "#000000", data, url) 
        reply = ""
    elif msg.key == "V1001_DATA_HELP": #帮助
        reply = TextReply(content=tools.help_text, message=msg)
    elif msg.key == "V1001_DATA_PROFILE": #个人主页
        dayPlan = user.dayPlan
        sleepPlan = user.sleepPlan
        if dayPlan == 0 or sleepPlan == 0:
            remark = u"您的计划设置不完整,请点击进入设置"
        else:
            remark = user.comment
            if remark == "":
                remark = u"点击查看个人信息"
        url = "%s/basic/redirect/profile?page=3" % tools.domain
        data = {
            "dayPlan":{
              "value": str(dayPlan),
              "color": "#ff0000"
            },
            "sleepPlan":{
              "value": str(sleepPlan),
              "color": "#007fff"
            },
            "remark":{
              "value": remark,
              "color": "#666666"
            }
        }
        tools.customSendTemplate(msg.source, tools.template_id["profile"], "#000000", data, url)
        reply = ""
    elif msg.key == "V1001_PLAN_MAKE":  #制定计划
        articles = []
        articles.append({"title":u"制定计划","description":u"快来制定你自己的运动计划吧!","image":"%s/static/img/plan_make.jpg" % tools.domain,"url":"%s/plan/redirect/profile?page=0" % tools.domain})
        reply = ArticlesReply(articles=articles, message=msg)
    elif msg.key == "V1001_PLAN_OWN":   #我的计划
        articles = []
        articles.append({"title":u"查看我的","description":u"来看看你都有什么运动计划吧","image":"%s/static/img/plan_own.jpg" % tools.domain,"url":"%s/plan/redirect/profile?page=3" % tools.domain})
        reply = ArticlesReply(articles=articles, message=msg)
    elif msg.key == "V1001_PLAN_SQUARE":    #计划广场
        articles = []
        articles.append({"title":u"计划广场","description":u"新鲜出炉的运动计划","image":"%s/static/img/plan_square.jpg" % tools.domain,"url":"%s/plan/redirect/profile?page=2" % tools.domain})
        reply = ArticlesReply(articles=articles, message=msg)
    elif msg.key == "V1001_PLAN_RANK":  #计划排行榜
        articles = []
        articles.append({"title":u"计划排行榜","description":u"想知道什么计划更受欢迎么","image":"%s/static/img/plan_rank.jpg" % tools.domain,"url":"%s/plan/redirect/profile?page=1" % tools.domain})
        reply = ArticlesReply(articles=articles, message=msg)
    elif msg.key == "V1001_MATCH_MAKE": #创建比赛
        articles = []
        articles.append({"title":u"创建比赛","description":u"开始一场新的比赛吧!","image":"%s/static/img/match_make.jpg" % tools.domain,"url":"%s/match/redirect/profile?page=0" % tools.domain})
        reply = ArticlesReply(articles=articles, message=msg)
    elif msg.key == "V1001_MATCH_CHECK":    #我的比赛进度查看
        closest_match = match_tools.closest_match(user, now)
        if not closest_match:   #没有未结束的比赛
            data = {
                "title":{
                    "value": u"比赛进度提醒",
                    "color": "#000000"
                },
                "content":{
                    "value": u"没有未结束的比赛",
                    "color": "#ff0000"
                },
                "remark":{
                    "value": u"来亲自发起一场比赛吧!",
                    "color": "#666666"
                }
            }
            url = "%s/match/redirect/profile?page=0" % tools.domain
            tools.customSendTemplate(msg.source, tools.template_id["msg"], "#000000", data, url)
        else:   #返回最近的比赛的进度
            left_time = basic_tools.left_time(now, closest_match.endTime)
            steps = match_tools.getProgress(closest_match, user)
            data = {
                "object":{
                    "value": u"%s 比赛" % closest_match.title,
                    "color": "#007fff"
                },
                "lastTime":{
                    "value": left_time,
                    "color": "#ff0000"
                },
                "steps":{
                    "value": str(steps),
                    "color": "#007fff"
                },
                "remark":{
                    "value": u"请继续努力!",
                    "color": "#666666"
                }
            }
            url = "%s/match/redirect/profile?page=1" % tools.domain
            tools.customSendTemplate(msg.source, tools.template_id["progress"], "#000000", data, url)
        reply = ""
    elif msg.key == "V1001_MATCH_SQUARE": #比赛广场
        articles = []
        articles.append({"title":u"比赛广场","description":u"新鲜出炉的比赛","image":"%s/static/img/match_square.jpg" % tools.domain,"url":"%s/match/redirect/profile?page=2" % tools.domain})
        reply = ArticlesReply(articles=articles, message=msg)
    return HttpResponse(reply)