微信公衆號,點擊事件

@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')
        if msg_type == 'event':
            event = xmlData.find('Event').text
            if event == 'CLICK':
                eventkey = xmlData.find('EventKey').text
                if eventkey == 'V1001_GET_SHOVEL':
                    try:
                        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:
                        replyMsg = TextMsg(from_user_name, to_user_name, '你的微信未綁定星礦賬號,請綁定成功後再進行操作')
                        return HttpResponse(replyMsg.send())
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章