# -*- coding: utf-8 -*-


def DJANGO_WE_CFG_FUNC(request, state=None):
    """ WeChat CFG Callback Func """


def DJANGO_WE_QUOTE_STATE_FUNC(request, state):
    """ WeChat Quote Callback Func """
    from utils.redis.connect import r
    return r.quote(state, short_uuid=True)


def DJANGO_WE_UNQUOTE_STATE_FUNC(request, state):
    """ WeChat UnQuote Callback Func """
    from utils.redis.connect import r
    return r.unquote(state, buf=True)


def DJANGO_WE_BASE_FUNC(code, state, access_info=None):
    """ WeChat Base Redirect Callback Func """


def DJANGO_WE_USERINFO_FUNC(code, state, access_info=None, userinfo=None):
    """ WeChat Userinfo Redirect Callback Func """


def DJANGO_WE_SHARE_FUNC(request, state=None):
    """ WeChat Share Callback Func """


def DJANGO_WE_MESSAGE_CALLBACK_FUNC(request, data):
    """ WeChat Message Callback Func """
    from account.models import UserInfo

    event = dict(data).get('Event', '')
    fromusername = dict(data).get('FromUserName', '')
    unionid = dict(data).get('UnionId', '')
    membercardid = dict(data).get('CardId', '')

    if event == 'user_get_card':
        UserInfo.objects.filter(unionid=unionid).update(has_membercard=True, membercardid=membercardid)
    elif event == 'user_del_card':
        UserInfo.objects.filter(membercardid=membercardid).update(has_membercard=False)
