#!/usr/bin/env python3  
# -*- coding: utf-8 -*-  
"""
@Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved.
@AUTHOR: ASJRD018
@NAME: AnsjerFormal
@software: PyCharm
@DATE: 2018/12/5 11:52
@Version: python3.6
@MODIFY DECORD:ansjer dev
@file: UidTokenObject.py
@Contact: chanjunkai@163.com
"""
from Ansjer.config import UID_TOKEN_KEY
import jwt
# UID_TOKEN_KEY = 'c+565*j@%^'


class UidTokenObject:

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

    def valid(self):
        token = self.token
        if self.token is None:
            return False
        try:
            res = jwt.decode(token, UID_TOKEN_KEY, algorithms='HS256')
            print(res)
            UID = res.get('uid', None)
            channel = res.get('channel', None)
            if UID is None:
                return False
            self.UID = UID
            self.channel = channel
        except jwt.ExpiredSignatureError as e:
            print('过期')
            print(repr(e))
        except Exception as e:
            print(repr(e))

    def generate(self, data={}):
        token = jwt.encode(data, UID_TOKEN_KEY, algorithm='HS256').decode('utf-8')
        self.token= token
        return token
# uidToken = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1aWQiOiJNVUo4ODdOTFI4SzhHQk05MTExQSJ9.NHYNwmcRLCRBv2FUMA-FlM1Gtx4ir8rrwfoz7QQ67bM'
# utko = UidTokenObject(uidToken)
#
# utko = UidTokenObject()
# rr = utko.generate(data={'uid':'JW3684H8BSHG9TTM111A','channel':1})
# print(rr)

# print(utko)

