def MatchCheck(user, match, progress):
    now = tools.getNow()
    url = "%s/match/redirect/profile?page=3&id=%d" % (wechat_tools.domain,
                                                      match.id)
    if now > match.endTime:
        match.finished = 1
        match.save()
        data = {
            "title": {
                "value": u"比赛结束",
                "color": "#ff0000"
            },
            "content": {
                "value": u"%s比赛已经截止,快来看看你们的战况吧!" % match.name,
                "color": "#007fff"
            },
            "remark": {
                "value": u"点击查看比赛详情",
                "color": "#666666"
            }
        }
        wechat_tools.customSendTemplate(user.openId,
                                        wechat_tools.template_id["msg"],
                                        "#000000", data, url)
    elif match.endTime - now < 7200:
        data = {
            "object": {
                "value": u"%s比赛" % match.name,
                "color": "#007fff"
            },
            "lastTime": {
                "value": tools.left_time(now, match.endTime),
                "color": "#ff0000"
            },
            "steps": {
                "value": str(progress.value),
                "color": "#007fff"
            },
            "remark": {
                "value": u"点击查看比赛详情",
                "color": "#666666"
            }
        }
        wechat_tools.customSendTemplate(user.openId,
                                        wechat_tools.template_id["progress"],
                                        "#000000", data, url)
def MatchCheck(user, match, progress):
    now = tools.getNow()
    url = "%s/match/redirect/profile?page=3&id=%d" % (wechat_tools.domain, match.id)
    if now > match.endTime:
        match.finished = 1
        match.save()
        data = {
            "title": {"value": u"比赛结束", "color": "#ff0000"},
            "content": {"value": u"%s比赛已经截止,快来看看你们的战况吧!" % match.name, "color": "#007fff"},
            "remark": {"value": u"点击查看比赛详情", "color": "#666666"},
        }
        wechat_tools.customSendTemplate(user.openId, wechat_tools.template_id["msg"], "#000000", data, url)
    elif match.endTime - now < 7200:
        data = {
            "object": {"value": u"%s比赛" % match.name, "color": "#007fff"},
            "lastTime": {"value": tools.left_time(now, match.endTime), "color": "#ff0000"},
            "steps": {"value": str(progress.value), "color": "#007fff"},
            "remark": {"value": u"点击查看比赛详情", "color": "#666666"},
        }
        wechat_tools.customSendTemplate(user.openId, wechat_tools.template_id["progress"], "#000000", data, url)
示例#3
0
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 PlanCheck(user, plan, progress, oldValue):
    now = tools.getNow()
    url = "%s/plan/redirect/profile?page=4&id=%d" % (wechat_tools.domain, plan.id)
    if now > plan.endTime:
        plan.finished = 1
        plan.save()
        if plan.goal > 0 and progress.value >= plan.goal:
            content = u"恭喜您完成了计划"
        elif plan.goal > 0:
            content = u"很遗憾您没能完成计划"
        else:
            content = u"不知道您完成得怎么样呢"
        data = {
            "title": {"value": u"计划截止", "color": "#ff0000"},
            "content": {"value": u"%s计划已经截止,%s" % (plan.name, content), "color": "#007fff"},
            "remark": {"value": u"点击查看计划详情", "color": "#666666"},
        }
        wechat_tools.customSendTemplate(user.openId, wechat_tools.template_id["msg"], "#000000", data, url)
        return
    old_per = per = 0
    if plan.goal > 0:
        per = int(progress.value * 100 / plan.goal)
        old_per = int(oldValue * 100 / plan.goal)
    if per > 100:
        per = 100
    if per == 100:
        plan.finished = 1
        plan.save()
        data = {
            "title": {"value": u"计划完成", "color": "#ff0000"},
            "content": {"value": u"恭喜您提前完成了%s计划已经完成" % plan.name, "color": "#007fff"},
            "remark": {"value": u"点击查看计划详情", "color": "#666666"},
        }
        wechat_tools.customSendTemplate(user.openId, wechat_tools.template_id["msg"], "#000000", data, url)
    elif plan.endTime - now < 7200:
        if per < 90:
            remark = u"请抓紧时间!"
        else:
            remark = u"别错过了ddl噢"
        data = {
            "object": {"value": u"%s计划" % plan.name, "color": "#007fff"},
            "lastTime": {"value": tools.left_time(now, plan.endTime), "color": "#ff0000"},
            "steps": {"value": str(progress.value), "color": "#007fff"},
            "content": {"value": u"您已完成了%d\%" % per, "color": "#ff0000"},
            "remark": {"value": remark, "color": "#666666"},
        }
        wechat_tools.customSendTemplate(user.openId, wechat_tools.template_id["progress"], "#000000", data, url)
    elif (per > 80 and old_per < 80) or (per > 50 and old_per < 50) or (per > 20 and old_per < 20):
        if per > 80:
            remark = u"请继续努力"
        elif per > 50:
            remark = u"长路漫漫,还有一半"
        else:
            remark = u"好的开始是成功的一半"
        data = {
            "object": {"value": u"%s计划" % plan.name, "color": "#007fff"},
            "lastTime": {"value": tools.left_time(now, plan.endTime), "color": "#ff0000"},
            "steps": {"value": str(progress.value), "color": "#007fff"},
            "content": {"value": u"您已完成了%d\%" % per, "color": "#ff0000"},
            "remark": {"value": remark, "color": "#666666"},
        }
        wechat_tools.customSendTemplate(user.openId, wechat_tools.template_id["progress"], "#000000", data, url)
示例#5
0
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 PlanCheck(user, plan, progress, oldValue):
    now = tools.getNow()
    url = "%s/plan/redirect/profile?page=4&id=%d" % (wechat_tools.domain,
                                                     plan.id)
    if now > plan.endTime:
        plan.finished = 1
        plan.save()
        if plan.goal > 0 and progress.value >= plan.goal:
            content = u"恭喜您完成了计划"
        elif plan.goal > 0:
            content = u"很遗憾您没能完成计划"
        else:
            content = u"不知道您完成得怎么样呢"
        data = {
            "title": {
                "value": u"计划截止",
                "color": "#ff0000"
            },
            "content": {
                "value": u"%s计划已经截止,%s" % (plan.name, content),
                "color": "#007fff"
            },
            "remark": {
                "value": u"点击查看计划详情",
                "color": "#666666"
            }
        }
        wechat_tools.customSendTemplate(user.openId,
                                        wechat_tools.template_id["msg"],
                                        "#000000", data, url)
        return
    old_per = per = 0
    if plan.goal > 0:
        per = int(progress.value * 100 / plan.goal)
        old_per = int(oldValue * 100 / plan.goal)
    if per > 100:
        per = 100
    if per == 100:
        plan.finished = 1
        plan.save()
        data = {
            "title": {
                "value": u"计划完成",
                "color": "#ff0000"
            },
            "content": {
                "value": u"恭喜您提前完成了%s计划已经完成" % plan.name,
                "color": "#007fff"
            },
            "remark": {
                "value": u"点击查看计划详情",
                "color": "#666666"
            }
        }
        wechat_tools.customSendTemplate(user.openId,
                                        wechat_tools.template_id["msg"],
                                        "#000000", data, url)
    elif plan.endTime - now < 7200:
        if per < 90:
            remark = u"请抓紧时间!"
        else:
            remark = u"别错过了ddl噢"
        data = {
            "object": {
                "value": u"%s计划" % plan.name,
                "color": "#007fff"
            },
            "lastTime": {
                "value": tools.left_time(now, plan.endTime),
                "color": "#ff0000"
            },
            "steps": {
                "value": str(progress.value),
                "color": "#007fff"
            },
            "content": {
                "value": u"您已完成了%d\%" % per,
                "color": "#ff0000"
            },
            "remark": {
                "value": remark,
                "color": "#666666"
            }
        }
        wechat_tools.customSendTemplate(user.openId,
                                        wechat_tools.template_id["progress"],
                                        "#000000", data, url)
    elif (per > 80 and old_per < 80) or (per > 50 and old_per < 50) or (
            per > 20 and old_per < 20):
        if per > 80:
            remark = u"请继续努力"
        elif per > 50:
            remark = u"长路漫漫,还有一半"
        else:
            remark = u"好的开始是成功的一半"
        data = {
            "object": {
                "value": u"%s计划" % plan.name,
                "color": "#007fff"
            },
            "lastTime": {
                "value": tools.left_time(now, plan.endTime),
                "color": "#ff0000"
            },
            "steps": {
                "value": str(progress.value),
                "color": "#007fff"
            },
            "content": {
                "value": u"您已完成了%d\%" % per,
                "color": "#ff0000"
            },
            "remark": {
                "value": remark,
                "color": "#666666"
            }
        }
        wechat_tools.customSendTemplate(user.openId,
                                        wechat_tools.template_id["progress"],
                                        "#000000", data, url)