import Base64 from "@/utils/base64.js"
import cryptoJS from "@/node_modules/crypto-js"

const key_code = 'gxzhptxiaochengxu_1';

// 加密
const encode = (key, data='')=>{
  try{
    let str;
    typeof data == 'object'? str = JSON.stringify(data) : str = data;
    str = cryptoJS.AES.encrypt(str, key_code).toString();
    uni.setStorageSync(key, str)
    return true;
  }catch(e){
    console.log(e);
    return false;
  }
}

// 解密
const decode = (key)=>{
  try{
    let str = uni.getStorageSync(key);
    str = cryptoJS.AES.decrypt(str, key_code).toString(cryptoJS.enc.Utf8);
    isJSON(str) ? str = JSON.parse(str) : null;
    return str;
  }catch(e){
    console.log(e);
    return undefined;
  }
}

const isJSON = (str)=>{
  try{
    JSON.parse(str)
    return true;
  }catch(e){
    return false;
  }
}


const LOGIN = ()=>{
  return new Promise((reslove, reject) => {
    uni.request({
      url: Url + '/api' + url,
      method: 'POST',
      data: data,
      success: (res) => {
        
      },
      fail: (message) => {
        uni.showToast({
          title: '网络错误',
          icon: 'none'
        })
        reject('请求失败');
      }
    })
  });
}

export default {
  encode,
  decode
}