import Taro from '@tarojs/taro'

export default function navTo(url: string, isLogin = false) {
  if (isLogin) {
    if (!Taro.getStorageSync('access_token')) {
      return false;
    }
  }
  Taro.navigateTo({
    url: url
  })
}


// 转base64
export function fileToBase64(filePath) {
  return new Promise((resolve) => {
    let fileManager = Taro.getFileSystemManager();
    fileManager.readFile({
      filePath,
      encoding: 'base64',
      success: (e: any) => {
        resolve(`data:image/jpg;base64,${e.data}`);
      }
    });
  });
};

/**
 * 转义微信富文本图片样式
 * @param htmlText
 */
export function wxParse(htmlText) {
  // Replace <img> tags with max-width and height styles
  htmlText = htmlText.replace(/\<img/gi, '<img style="max-width:100%;height:auto;"');

  // Replace style attributes that do not contain text-align
  htmlText = htmlText.replace(/style\s*?=\s*?(['"])(?!.*?text-align)[\s\S]*?\1/ig, 'style="max-width:100%;height:auto;"');

  return htmlText;
}


export function copyText(text: string) {
  Taro.setClipboardData({
    data: text,
    success: function () {
      Taro.showToast({
        title: '复制成功',
        icon: 'success',
        duration: 2000
      });
    },
    fail: function () {
      Taro.showToast({
        title: '复制失败',
        icon: 'none',
        duration: 2000
      });
    }
  });
}
