/**
 * 加密工具类
 * Created by wangxiaoying 2022/11/17
 * **/
import JsEncrypt from 'jsencrypt'
import useUserStore from '@/store/modules/user'
const userStore = useUserStore()

/**
  * RSA加密
  * @param value 待加密数据
  * @returns 加密结果
  */
export async function RSAencrypt(value: string): Promise<string> {
  const encrypt = new JsEncrypt()
  let enRes = ''
  if (userStore.getPublicKey && userStore.getPublicKey === '') {
    const res = await userStore.getBaseConfig()
    if (res && res.publicKey) {
      const publicKey = res.publicKey
      encrypt.setPublicKey(publicKey)// 从store中读取公钥
      enRes = encrypt.encrypt(value) || ''
    }
  }
  else {
    encrypt.setPublicKey(userStore.getPublicKey)// 从store中读取公钥
    enRes = encrypt.encrypt(value) || ''
  }
  return enRes
}

