utils_wx.js 6.15 KB
import wxqrcode from '~/utils/lib/wxqrcode.js'
/**
 * 微信校验通知类工具封装
 */
const tipsObj = {
  phone: {
    icon: 'error',
    title: '请输入手机号'
  },
  username: {
    icon: 'error',
    title: '请输入用户名'
  },
  password: {
    icon: 'error',
    title: '请输入密码'
  }
}

/**
 * 禁止滑动
 */
const noScroll = () => {}

/**设置粘贴板内容 */
const setCopyData = (data, callBack) => {
  console.log(data, 'setCopyData')
  wx.setClipboardData({
    data: data,
    success(res) {
      console.log(res, 'setCopyData')
      if (callBack && typeof callBack === 'function') callBack(res)
    }
  })
}

/**
 * 防止重复点击
 */
const showLoading = (title = '加载中') => {
  if (wx.showLoading) {
    // 基础库 1.1.0 微信6.5.6版本开始支持,低版本需做兼容处理
    wx.showLoading({
      title,
      mask: true
    })
  } else {
    // 低版本采用Toast兼容处理并将时间设为20秒以免自动消失
    wx.showToast({
      title,
      icon: 'loading',
      mask: true,
      duration: 20000
    })
  }
}

const hideLoading = () => {
  if (wx.hideLoading) {
    // 基础库 1.1.0 微信6.5.6版本开始支持,低版本需做兼容处理
    wx.hideLoading()
  } else {
    wx.hideToast()
  }
}
const showToast = val => {
  let obj = {}
  if (val && typeof val === 'string') {
    obj.title = val
    ;(obj.icon = 'none'), (obj.duration = 3000)
  } else {
    obj = val
  }
  return wx.showToast(obj)
}

/**
 *
 * @param {* string | object} name 信息名称
 * @param {* object | null} [params] 信息名称参数
 */
const wxShowToast = (name, params) => {
  if (arguments.length == 2) {
    wx.showToast({
      title: tipsObj[name]['title'],
      icon: tipsObj[name]['icon'],
      ...params
    })
  } else {
    wx.showToast({
      ...params
    })
  }
}

/**拨打电话 */
const phoneCall = phoneNumber => {
  wx.makePhoneCall({
    phoneNumber: `${phoneNumber}`
  })
}

/**查看大图 */
const previewImage = params => {
  wx.previewImage(params)
}

export function handleCopy(url) {
  // var { idx } = e.currentTarget.dataset
  // var currentItem = this.data.orderObj.activityList[idx]
  // var qr = wxqrcode.createQrCodeImg(url)
  const base64src = function (base64data) {
    return new Promise((resolve, reject) => {
      const [a, format, bodyData] =
        /data:image\/(\w+);base64,(.*)/.exec(base64data) || []
      if (!format) reject(new Error('ERROR_BASE64SRC_PARSE'))
      const filePath = `${wx.env.USER_DATA_PATH}/tmp_base64src.${format}`
      const buffer = wx.base64ToArrayBuffer(bodyData)
      wx.getFileSystemManager().writeFile({
        filePath,
        data: buffer,
        encoding: 'binary',
        success() {
          resolve(filePath)
        },
        fail() {
          reject(new Error('ERROR_BASE64SRC_WRITE'))
        }
      })
    })
  }
  return new Promise((resolve, reject) => {
    let qr = wxqrcode.createQrCodeImg(url)
    resolve(qr)
    // base64src(qr)
    //   .then(res => {
    //     console.log(res)
    //     // return res
    //     resolve(res)
    //     // wx.getImageInfo({
    //     //   src: res,
    //     //   success(obj) {
    //     //     return obj.path
    //     //     // wx.showShareImageMenu({
    //     //     //   path: obj.path
    //     //     // })
    //     //   },
    //     //   fail() {}
    //     // })
    //   })
    //   .catch(err => {
    //     reject(err)
    //   })
  })
}

/**获取节点信息
 * name(id获取class)
 * callBack 回调函数
 * isComponents是否在组件
 */
const getViewInfo = (name, callBack, isComponents) => {
  const query = !isComponents
    ? wx.createSelectorQuery()
    : wx.createSelectorQuery().in(isComponents)
  query
    .select(name)
    .boundingClientRect(res => {
      // console.log('res: ', res)
      callBack(res)
    })
    .exec()
}
// 获取设备信息
const getDeviceSize = () => {
  return new Promise((resolve, reject) => {
    wx.getSystemInfo({
      success: function (res) {
        // console.log(res)
        const { screenHeight, safeArea } = res
        const { bottom } = safeArea
        const bottomLift = screenHeight - bottom
        resolve({
          bottomLift
        })
      }
    })
  })
}

//是否是全面屏
const isFullSucreen = isComponents => {
  const thisPage = wx.$router.getPageInstance()
  const routerList = wx.$router.getRouterList()
  const router = routerList.find(item => item.url === `/${thisPage.route}`)
  let val = false
  wx.getSystemInfo({
    success(res) {
      let { statusBarHeight, screenHeight, windowHeight, safeArea } = res
      // console.log(statusBarHeight, screenHeight, windowHeight, safeArea)
      // const { screenHeight, safeArea } = res
      const { bottom } = safeArea
      // console.log(bottom)
      // console.log(router.isTab, 'router.isTab')
      // windowHeight 属性会在自定义导航栏时跟screenHeight 一样导致无法区分是否是iphoneX的机型
      // 用safeArea可以处理
      val = router.isTab
        ? screenHeight - windowHeight - statusBarHeight - 32 > 72
        : screenHeight - bottom > 0
      // val = router.isTab ? screenHeight - windowHeight - statusBarHeight - 32 > 72 : screenHeight - windowHeight > 72
      // if (isComponents) val = screenHeight - windowHeight > 72
      if (isComponents) val = screenHeight - bottom > 0

      // console.log(router, 'route')
      // if (router.url.includes('tabIndex/pages/index/index')) {
      //   // console.log('当前页面为 TabBar 页面');
      //   val = true
      // }
    },

    fail(err) {
      return false
    }
  })
  return val
}
// /**获取手机信息 */
// const getPhoneInfo = callBack => {
//   wx.getSystemInfo({
//     success(res) {
//       callBack(res)
//     },
//     fail(err) {
//       callBack(err)
//     }
//   })
// }
/**获取设备信息 */
const getSystemInfo = callBack => {
  wx.getSystemInfo({
    success(res) {
      callBack(res)
    },
    fail(err) {
      callBack(err)
    }
  })
}
const wxApi = {
  // getPhoneInfo,
  previewImage,
  getSystemInfo,
  showLoading,
  hideLoading,
  setCopyData,
  showToast,
  phoneCall,
  noScroll,
  getViewInfo,
  wxShowToast,
  getDeviceSize,
  isFullSucreen,
  handleCopy
}

export default wxApi