import { logger } from "mslib"
const CryptoJS = require('crypto-js');


/**
 * 
 * @date 2021-07-14 16:09:43
 * @author 陈招林
 */
export class StcAccessTokenUtil {
    getOpenId(accessToken: string): any {
        let key = CryptoJS.enc.Utf8.parse('stc_pp3_12345678');
        let iv = CryptoJS.enc.Hex.parse('00000000');
        var options = {   
            iv: iv,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        };
        accessToken=accessToken.replace(/=/g, "").replace(/\-/g, "+").replace(/_/g, "/")   ; // 转换为字符串
        console.log("999999:"+accessToken);
        var plainText = CryptoJS.AES.decrypt(accessToken, key, options).toString(CryptoJS.enc.Utf8);  
        logger.info("AES解密出来的明文----%s", plainText);
        var accessTokenArray=plainText.split(":");
        logger.info("请求参数----%s", accessTokenArray);
        return {"appid":accessTokenArray[5] ,'openid':accessTokenArray[4]};
    }

    setAccessToken(accessToken: string): any {
        let key = CryptoJS.enc.Utf8.parse('stc_pp3_12345678');
        let iv = CryptoJS.enc.Hex.parse('00000000');
        var options = {   
            iv: iv,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        };
        accessToken=accessToken.replace(/=/g, "").replace(/\-/g, "+").replace(/_/g, "/")   ; // 转换为字符串
        console.log("999999:"+accessToken);
        var plainText = CryptoJS.AES.decrypt(accessToken, key, options).toString(CryptoJS.enc.Utf8);  
        logger.info("AES解密出来的明文----%s", plainText);
        var accessTokenArray=plainText.split(":");
        logger.info("请求参数----%s", accessTokenArray);
        return {"appid":accessTokenArray[5] ,'openid':accessTokenArray[4]};
    }

}