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

import time

import shortuuid
from django.conf import settings
from json_response import auto_response
from wechatpy import WeChatClient


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


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

    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 {
        'appId': JSAPI['appID'],
        'nonceStr': nonceStr,
        'timestamp': timestamp,
        'signature': signature,
    }
