import store from '@/store/index' import { uploadUrl, XAPPID } from '@/enums/index.js' // 框架方法封装 const tabList = ['store', 'cart', 'mine'] /** * @param {string} 跳转的tabBar页面 * @return {null} */ export function tab2(tabPage) { if (tabList.includes(tabPage)) { uni.switchTab({ url: `/pages/${tabPage}/index` }) } } /** * @param {string} 返回上一级 * @return {null} */ export function back() { if (getCurrentPages().length > 1) { uni.navigateBack({ delta: 1 }) } else { go2('store') } } /** * 跳转到指定页面,包括tabBar页面 * @param {string} url 页面名称 * @param {object} data 页面参数 * @param {string} isRedirect 是否重定向 默认false * @return {null} */ export function go2(url, data = {}, isRedirect = false) { if (!url) { console.error('请选择页面') return } let param = '' Object.keys(data).forEach((key) => { if (param === '') { param = `${key}=${data[key]}` } else { param += `&${key}=${data[key]}` } }) if (tabList.includes(url)) { uni.switchTab({ url: `/pages/${url}/index${param ? '?' + param : ''}` }) } else { if (isRedirect) { uni.redirectTo({ url: `/pages/${url}/index${param ? '?' + param : ''}` }) } else { uni.navigateTo({ url: `/pages/${url}/index${param ? '?' + param : ''}` }) } } } /** * 跳转到指定页面,包括tabBar页面,校验是否已登录,未登录则跳转到登录页面 * @param {string} url 页面名称 * @param {object} data 页面参数 * @param {string} isRedirect 是否重定向 默认false * @return {null} */ export function loginGo2(url, data = {}, isRedirect) { const token = store.state.qnToken if (token) { go2(url, data, isRedirect) } else { store.commit('setNextPage', { name: url, data }) go2('login') } } /** * 退出登录并跳转到登录页面 * @return {null} */ export function exit() { store.dispatch('logout') go2('login') } /** * 文件上传 * @param {array} sourceType 上传的方式 album:相册 camera:相机 * @param {number} count 上传的数量 * @return {Promise} 以数组的形式返回对应的文件地址 */ export function uploadImage(sourceType = ['album', 'camera'], count = 1) { return new Promise((resolve, reject) => { uni.chooseImage({ count: count, sizeType: ['original', 'compressed'], sourceType: sourceType, success: (res) => { const tempFilePaths = res.tempFilePaths let cache = tempFilePaths.map((path) => { return uploadFile(path, 'image') }) Promise.all(cache) .then((res) => { resolve(res) }) .catch((err) => { reject(err) }) }, fail: (err) => { console.error('chooseImage error:', err) reject(err) } }) }) } /** * 单文件上传 * @param {string} path 文件路径 * @param {string} type 文件类型 image | file,默认为image * @returns {Promise} 返回单文件上传地址 */ export function uploadFile(path, type = 'image') { return new Promise((resolve, reject) => { uni.uploadFile({ url: uploadUrl[type], filePath: path, name: type, header: { Authorization: store.state.qnToken, 'X-APP-ID': XAPPID }, // fileType: type, success: (res) => { let result = JSON.parse(res.data) if (result.data) { resolve(result.data) } else { reject(result.message) } }, fail: (err) => { console.error('uploadFile error:', err) reject(err) } }) }) } /** * 根据类型获取缓存 * @param {string} type 缓存类型 * @returns object 缓存数据 */ export function getCache(type) { let data = null if (store.state.cache.type === type) { data = store.state.cache.data // 防止同一事件循环的数据竞争 setTimeout(() => { store.commit('resetCache') }) } return data } /** * 设置缓存 * @param {string} type 缓存类型 * @param {object} data 缓存数据 * @returns null */ export function setCache(type, data) { store.commit('setCache', { type, data }) }