#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved.
@AUTHOR: ASJRD019
@NAME: AnsjerFormal
@software: PyCharm
@DATE: 2019/4/17 11:46
@Version: python3.6
@MODIFY DECORD:ansjer dev
@file: UidTokenObject.py
@Contact:
"""
from Ansjer.config import UID_TOKEN_KEY
import jwt, time


class UrlTokenObject:

    def __init__(self, token=None):
        self.token = token
        self.Url = ''
        self.flag = self.valid()

    def valid(self):
        try:
            token = self.token
            if self.token is None:
                return False
            res = jwt.decode(token, '12345', algorithms='HS256')
            # print(res)
            Url = res.get('Url', None)
            if Url is None:
                return False
            self.Url = Url
        except jwt.ExpiredSignatureError as e:
            # print('过期')
            return False

    def generate(self, data={}):
        now_stamp = int(time.time())
        # print (now_stamp)
        # 过期时间为两天
        data['exp'] = 172800 + now_stamp
        token = jwt.encode(data, '12345', algorithm='HS256').decode('utf-8')
        self.token=token
        return token
