#!/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.mci = ''
        self.user_id = ''
        self.uid = ''
        self.serial_number = ''
        self.old_version = ''
        self.new_version = ''
        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')
            self.Url = res.get('Url', 'null')
            self.mci = res.get('mci', 'null')
            self.user_id = res.get('user_id', 'null')
            self.uid = res.get('uid', 'null')
            self.serial_number = res.get('serial_number', 'null')
            self.old_version = res.get('old_version', 'null')
            self.new_version = res.get('new_version', 'null')


        except jwt.ExpiredSignatureError as e:
            print('UrlTokenObject_except_过期')
            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')
        self.token = token
        return token
