Taro API封装

toast提示

const showToast = title => {
  Taro.showToast({
    title,
    mask: true,
    icon: 'none',
    duration: 2000,
  }).then(r => r)
};

加载提示

const showLoading = (title= '加载中', mask = true) => {
  Taro.showLoading({
    title,
    mask
  }).then(r => r)
};

跳转到其他小程序

const goToMiniProgram = (url = '', envVersion = 'release', extraData) => {
  Taro.navigateToMiniProgram({
    appId: cfg.shopAppId,
    path:url,
    extraData,
    envVersion,
    success(res) {
      // 打开成功
    }
  })
}

设置标题栏

const setNavigationBarTitle = (title) => {
  Taro.setNavigationBarTitle({title});
}

选择图片

const chooseImage = (
  num, // 最大选择数
  length = 0, // 已选数量
  sizeType = ['compressed'],
  sourceType = ['album', 'camera']
) =>{
  return new Promise((resolve, reject) => {
    Taro.chooseImage({
      count: num - length,
      sizeType,
      sourceType
    })
      .then(response => {
        resolve(response)
      }, err => {
        reject(err)
      });
  })
}

选择视频

const chooseVideo = (
  sourceType = ['camera', 'album'],
  maxDuration = 60,
  compressed = true
) => {
  return new Promise((resolve, reject) => {
    Taro.chooseVideo({
      sourceType,
      maxDuration,
      compressed,
      success: (res) => {
        if (res.duration > 60) return showToast( '视频时长超过60s,请重新选择')
        resolve(res)
      }
    })
  })
}

上传图片到服务器

const uploadFile = (
  data,
  type,
  url,
  fileType = 'IMG'
) => {
  return new Promise((resolve, reject)=>{
    Taro.uploadFile({
      url,
      filePath: data,
      name: 'file',
      header: {
        Authorization: getGlobalData('Authorization')
      },
      formData:{
        'fileType': fileType,
        'type': type
      },
      success: (res) => {
        const jsonStr = JSON.parse(res.data)
        const { code, msg } = jsonStr || {};
        resolve(jsonStr)
      },
      fail: (res) => {
        reject(res)
      }
    });
  })
}

订阅消息

const requestSubscribeMessage = (tmplIds) => {
  return new Promise((resolve, reject)=>{
    Taro.requestSubscribeMessage({
      tmplIds,
      success: (res) => {
        resolve(res)
      },
      fail: (error) => {
        reject(error)
      }
    })
  })
}

图片点击放大

const previewImage = (url, urlList) => {
  Taro.previewImage({
    current: url, // 当前显示图片的http链接
    urls: urlList
  })
}

拨打电话

const makePhoneCall = (number) =>{
  Taro.makePhoneCall({
    phoneNumber:number
  })
}

支付

const requestPayment = (payDataMsg) => {
  const { timeStamp, nonceStr, sign } = payDataMsg;
  return new Promise((resolve, reject)=>{
    Taro.requestPayment({
      timeStamp,
      nonceStr,
      package: payDataMsg.package,
      signType: 'MD5',
      paySign: sign,
      success: (res) => {
        resolve(res)
      },
      fail: (error) => {
        reject(error)
      }
    })
  })
}

页面滚动至相应位置

const pageScrollTo = (scrollTop, duration) => {
  Taro.pageScrollTo({
    scrollTop,
    duration
  })
}

用户授权状态

const getSetting = (type) => {
  return new Promise((resolve, reject)=>{
    Taro.getSetting({
      success: (res) => {
        let recordAuth = res.authSetting[type];
        resolve(recordAuth)
      },
      fail: function () {
        showToast('error', '鉴权失败,请重试')
      }
    })
  })
}

模态框

const showModal = (title, content, showCancel, confirmText = '确定', cancelText = '取消') => {
  return new Promise((resolve, reject)=>{
    Taro.showModal({
      title,
      content,
      showCancel,
      cancelText,
      confirmText,
      success:(success)=> {
        resolve(success)
      },
      fail:(error)=>{
        reject(error)
      }
    })
  })
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值