
const crypto = require("crypto");
const getters = {
  token: state => state.user.tokenObj.token,
  userInfo: state => state.user.userInfo,
  isVip: state => {
  
    //#ifdef MP-WEIXIN
    if (state.user.userInfo && state.user.userInfo.expireTime) {
      let expireTime = new Date(state.user.userInfo.expireTime).getTime()
      let nowTime = new Date().getTime()
      if (expireTime >= nowTime) {
        return true
      }
      else {
        return false
      }

    } else {
      return false
    }
    //#endif

    //#ifdef MP-TOUTIAO
    // console.log(tt.getStorageSync("unionid"),state.user.userInfo)
    if(uni.getStorageSync("unionid")!==state.user.userInfo.unionId){
      return false
    }
    if(Number(state.user.userInfo.isVip)==0){
      return false

    }
    let publicKey = "-----BEGIN PUBLIC KEY-----MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDCzR04LMmDlwyVRfK+Kgsm/vFMOFyqvGk4QIVC" +
      "Gj2CKKfhycRExm15N3tKOOouIjl/EU79It45oS+ltJkscvYrJn1zz+1bSoBLelZzJrn7K0NpHF8k" +
      "bCprsFHd60x+YJ4l8C3SDxfFebo4kqveJzx6JNZKwzFm7LPEO5XB9pEBRwIDAQAB-----END PUBLIC KEY-----"
    
    let verifyString = state.user.userInfo.unionId + state.user.userInfo.isVip
    let sign = state.user.userInfo.sign
    function verifySign(data, sign, publicKey) {
      console.log(data, sign, publicKey)
      const verify = crypto.createVerify('RSA-SHA256');
      verify.update(data);
      verify.end();
      return verify.verify(publicKey, Buffer.from(sign, 'base64'));
    }
    return verifySign(verifyString, sign, publicKey)
    // console.log(verifySign(verifyString,sign,publicKey),'验证结果1')

    // function verify(publicKey,verifyString,sign) {
    //   console.log(publicKey,verifyString,sign)
    //   // signData: 加签的数据
    //   // data: 加签之后得到的签文
    //   try {
    //     // 公钥

    //     let signatureVf = new KJUR.crypto.Signature({alg:"SHA256withRSA"});
    //     signatureVf.init(publicKey)
    //     signatureVf.updateString(verifyString)

    //     // !接受的参数是16进制字符串!
    //     let b = signatureVf.verify(b64tohex(sign));
    //     return b


    //   } catch (e) {
    //     console.error(e);
    //   }
    // }
    // console.log(verify(publicKey,verifyString,sign),"验证结果2")
    //#endif


  },
  avatar: state => state.user.avatar,
  name: state => state.user.name,
  introduction: state => state.user.introduction,
  roles: state => state.user.roles,
  permissions: state => state.user.permissions
}
export default getters
