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)
}
})
})
}