/**
 * 加密工具类
 * Created by wangxiaoying 2019/04/17
 * **/
import JsEncrypt from 'jsencrypt'
import store from '../store'

/**
 * 加密
 * @param value 待加密数据
 * @returns 加密结果
 */
export function RSAencrypt(value) {
  var encrypt = new JsEncrypt()
  // 如果公钥没有了，重新申请公钥
  console.log("pk:"+store.getters.publicKey)
  if (store.getters.publicKey && store.getters.publicKey === '') {
    getConfig().then(() => {
      console.log('getConfig')
      encrypt.setPublicKey(store.getters.publicKey)// 从store中读取公钥
      var enRes = encrypt.encrypt(value)
      return enRes
    }).catch((e) => {
      this.loading = false
    })
  } else {
    encrypt.setPublicKey(store.getters.publicKey)// 从store中读取公钥
    var enRes = encrypt.encrypt(value)
    return enRes
  }
}

