微信公衆號關注後,關鍵詞回覆

@csrf_exempt
def weixin_main(request):
    # get 請求是驗證
    if request.method == "GET":
        # 接收微信服務器get請求發過來的參數
        signature = request.GET.get('signature', None)
        timestamp = request.GET.get('timestamp', None)
        nonce = request.GET.get('nonce', None)
        echostr = request.GET.get('echostr', None)
        # 服務器配置中的token
        token = 'gongchang'
        # 把參數放到list中排序後合成一個字符串,再用sha1加密得到新的字符串與微信發來的signature對比,如果相同就返回echostr給服務器,校驗通過
        hashlist = [token, timestamp, nonce]
        hashlist.sort()
        hashstr = ''.join([s for s in hashlist])
        hashstr = hashlib.sha1(hashstr).hexdigest()
        if hashstr == signature:
            return HttpResponse(echostr)
        else:
            return HttpResponse("field")

    elif request.method == "POST":
        webData = request.body
        xmlData = ET.fromstring(webData)
        msg_type = xmlData.find('MsgType').text
        to_user_name = xmlData.find('ToUserName').text
        from_user_name = xmlData.find('FromUserName').text
        creat_time = xmlData.find('CreateTime').text
        info = Config.objects.get(config_name='shovel_free')
        data = info.config_value
        data = json.loads(data)
        wx_num = data.get('wx_num')
        now = time.strftime('%Y%m%d')
        try:
            if msg_type == 'text':
                content = xmlData.find("Content").text
                try:
                    if content == '免費領鏟子':
                        user_wx_info = UserWxInfo.objects.get(openid=from_user_name)
                        sign_time = user_wx_info.sign_time
                        if user_wx_info.focus_state == 1:
                            if sign_time:
                                if int(now) == int(sign_time):
                                    reply_info = "今日鏟子已領,請明日再來哦 <a href='https://xingkuang-help.westarcloud.net/faq/d_5d82edd74db67f2b3856840e.html'>更多免費領鏟子攻略</a> ~"
                                    replyMsg = TextMsg(from_user_name, to_user_name, reply_info)
                                    return HttpResponse(replyMsg.send())
                                else:
                                    user_wx_info.sign_time = now
                                    user_wx_info.sign_state = 1
                                    user_wx_info.save()
                                    # 用戶賬戶變動保存
                                    try:
                                        _ = FinanceAccount.objects.get(uid=user_wx_info.uid)

                                    except Exception as e:
                                        pass
                                    else:
                                        _.uptime = int(time.time())
                                        _.shovel = decimal.Decimal(wx_num) + _.shovel  # 鏟子數量保存
                                        _.save()
                                        # 簽到領鏟子保存
                                        data = FinanceShovelLog(uid=user_wx_info.uid, incr_num=decimal.Decimal(wx_num), reason='獎勵', type=2,
                                                                reason_meta='公衆號領取', shovel=decimal.Decimal(wx_num) + _.shovel)
                                        data.save()
                                    reply_info = "領取成功,鏟子+%s <a href='https://xingkuang-help.westarcloud.net/faq/d_5d82edd74db67f2b3856840e.html'>更多免費領鏟子攻略</a> ~" % wx_num
                                    replyMsg = TextMsg(from_user_name, to_user_name, reply_info)
                                    return HttpResponse(replyMsg.send())
                            else:
                                user_wx_info.sign_time = now
                                user_wx_info.sign_state = 1
                                user_wx_info.save()
                                # 用戶賬戶變動保存
                                try:
                                    _ = FinanceAccount.objects.get(uid=user_wx_info.uid)

                                except Exception as e:
                                    pass
                                else:
                                    _.uptime = int(time.time())
                                    _.shovel = int(wx_num) + _.shovel  # 鏟子數量保存
                                    _.save()
                                    # 簽到領鏟子保存
                                    data = FinanceShovelLog(uid=user_wx_info.uid, incr_num=wx_num, reason='獎勵', type=2,
                                                            reason_meta='公衆號領取', shovel=int(wx_num) + _.shovel)
                                    data.save()
                                reply_info = "領取成功,鏟子+%s <a href='https://xingkuang-help.westarcloud.net/faq/d_5d82edd74db67f2b3856840e.html'>更多免費領鏟子攻略</a> ~" % wx_num
                                replyMsg = TextMsg(from_user_name, to_user_name, reply_info)
                                return HttpResponse(replyMsg.send())

                        if user_wx_info.focus_state == 0:
                            reply_info = "領取失敗,你的微信未綁定星礦賬號,請綁定成功後再進行領取吧~"
                            replyMsg = TextMsg(from_user_name, to_user_name, reply_info)
                            return HttpResponse(replyMsg.send())
                except:
                    reply_info = "領取失敗,你的微信未綁定星礦賬號,請綁定成功後再進行領取吧~"
                    replyMsg = TextMsg(from_user_name, to_user_name, reply_info)
                    return HttpResponse(replyMsg.send())

                if content == '獲得鏟子':
                    reply_info = "免費獲取更多鏟子,點此查看 <a href='https://xingkuang-help.westarcloud.net/faq/d_5d82edd74db67f2b3856840e.html'>鏟子獲取攻略</a> ~"
                    replyMsg = TextMsg(from_user_name, to_user_name, reply_info)
                    return HttpResponse(replyMsg.send())
                if content == '邀請有獎':
                    scheme = config["starkeyword_antd"]["scheme"]
                    host = config["starkeyword_antd"]["host"]
                    long_url ="%s://%s/subject/20191111/home"% (scheme, host)
                    shorts_url = get_short_url(long_url)
                    reply_info = '【招募礦主天天挖寶】邀2個好友搶小米AI音箱,點擊挖寶:%s' % shorts_url
                    replyMsg = TextMsg(from_user_name, to_user_name, reply_info)
                    return HttpResponse(replyMsg.send())
                else:
                    reply_info = "請您輸入準確的關鍵詞,如:免費領鏟子,邀請有獎,獲得鏟子"
                    replyMsg = TextMsg(from_user_name, to_user_name, reply_info)
                    return HttpResponse(replyMsg.send())

        except Exception as e:
            replyMsg = TextMsg(from_user_name, to_user_name, '你的微信未綁定星礦賬號,請綁定成功後再進行領取吧~')
            return HttpResponse(replyMsg.send())
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章