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

import time

import shortuuid
from django.conf import settings
from django.http import HttpResponse
from wechatpy import WeChatClient


r = settings.REDIS_CACHE
WECHAT = settings.WECHAT
JSAPI = WECHAT.get('JSAPI', {})


def wx_jsapi_signature_api(request):
    url = request.GET.get('url', '')
    callback = request.GET.get('callback', '')

    nonceStr, timestamp = shortuuid.uuid(), int(time.time())

    client = WeChatClient(JSAPI['appID'], JSAPI['appsecret'])
    ticket = client.jsapi.get_jsapi_ticket()
    signature = client.jsapi.get_jsapi_signature(nonceStr, ticket, timestamp, url)

    return HttpResponse('{}({})'.format(callback, {
        'appId': JSAPI['appID'],
        'nonceStr': nonceStr,
        'timestamp': timestamp,
        'signature': signature,
    }))
