utils_wx.js 2.19 KB
/**
 * 微信校验通知类工具封装
 */

const tipsObj = {
  phone: {
    icon: 'error',
    title: '请输入手机号'
  },
  username: {
    icon: 'error',
    title: '请输入用户名'
  },
  password: {
    icon: 'error',
    title: '请输入密码'
  }
}

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

/**设置粘贴板内容 */
const setCopyData = (data, callBack) => {
  wx.setClipboardData({
    data: data,
    success(res) {
      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'
  } 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 getSystemInfo = callBack => {
  wx.getSystemInfo({
    success(res) {
      callBack(res)
    },
    fail(err) {
      callBack(err)
    }
  })
}

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

/**查看大图 */
const previewImage = params => {
  wx.previewImage(params)
}
const wxApi = {
  previewImage,
  getSystemInfo,
  showLoading,
  hideLoading,
  setCopyData,
  showToast,
  phoneCall,
  noScroll,
  wxShowToast
}
export default wxApi