
import Taro from '@tarojs/taro'
import { authorize } from './wxAuth'
// eslint-disable-next-line no-undef
const host = HOST;

export function getImgURL(img) {
  if (!img) return undefined;

  if (img.indexOf('http') === 0) return img;

  return `${host}${img}`;
}

/**
 * 保存到相册
 * @param {*} tempPath 
 * @returns 
 */
export function save2Album(tempPath) {
  return new Promise((resolve, reject) => {
    authorize('scope.writePhotosAlbum').then(() => {
      // 再保存到相册
      Taro.saveImageToPhotosAlbum({
        filePath: tempPath,
        success: () => {
          resolve()
        },
        fail: (err) => {
          console.error(err)
          let message = err.errMsg || err.message || err
          if (message.indexOf('cancel') > -1) {
            message = '您已取消'
          }
          Taro.showToast({
            title: message,
            icon: 'none',
          })
          reject(err)
        }
      })
    }).catch((err) => {
      console.error(err)
      let message = err.errMsg || err.message || err
      if (message.indexOf('cancel') > -1) {
        message = '您已取消'
      }

      Taro.showToast({
        title: message,
        icon: 'none',
      })
      reject(err)
    })
  })
}
