import store from '@/store/index' import { uploadUrl, XAPPID } from '@/enums/index.js' import { pushCustomerOff } from '@/apis/commonApi' // 框架方法封装 const tabList = ['client', 'trade', 'mall', '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('client') } } /** * 跳转到指定页面,包括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(redirect = false) { store.dispatch('logout') // #ifdef APP-PLUS let cid = plus.push.getClientInfo().clientid let platform = uni.getSystemInfoSync().platform pushCustomerOff(cid, platform) // #endif go2('login', {}, redirect) } /** * 文件上传 * @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) resolve(null) }, }) }) } /** * 单文件上传 * @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) return result.data } else { reject(result.message) } }, fail: (err) => { console.error('uploadFile error:', err) reject(err) }, }) }) } /** * 屏幕截图并保存到相册(不包含状态栏和tabBar),原理就是用webview的draw方法 */ export function screenShot() { var pages = getCurrentPages() var page = pages[pages.length - 1] var bitmap = null // 获取当前页面 webview 的对象实例 var currentWebview = page.$getAppWebview() bitmap = new plus.nativeObj.Bitmap('amway_img') // 将webview内容绘制到Bitmap对象中 currentWebview.draw( bitmap, function () { console.log('截屏绘制图片成功') bitmap.save( '_doc/a.jpg', {}, function (i) { uni.saveImageToPhotosAlbum({ filePath: i.target, success: function () { bitmap.clear() //销毁Bitmap图片 uni.showToast({ title: '保存图片成功', mask: false, duration: 1500, }) }, }) }, function (e) { // console.log('保存图片失败:' + JSON.stringify(e)) uni.showToast({ title: '保存图片失败,请手动截图', mask: false, duration: 1500, }) } ) }, function (e) { // console.log('截屏绘制图片失败:' + JSON.stringify(e)) uni.showToast({ title: '保存图片失败,请手动截图', mask: false, duration: 1500, }) } ) } /** * 判断是否授权相册并保存图片base64到相册 * @param {string} base64 图片base64 */ export function saveImage(base64) { saveBase64ToTempFile(base64).then((url) => { saveImageToPhotosAlbum(url) }) } /** * 将base64保存为临时文件,并返回文件路径 * @param {string} base64 * @returns */ export function saveBase64ToTempFile(base64) { return new Promise((resolve, reject) => { const bitmap = new plus.nativeObj.Bitmap('img') bitmap.loadBase64Data(base64, () => { const url = '_doc/' + new Date().getTime() + '.png' bitmap.save( url, { overwrite: true, }, () => { bitmap.clear() resolve(url) }, (e) => { reject(e) } ) }) }) } /** * 保存图片到相册 * @param {string} path 图片路径 */ export function saveImageToPhotosAlbum(path) { return new Promise((resolve, reject) => { uni.saveImageToPhotosAlbum({ filePath: path, success: (res) => { uni.showToast({ title: '保存图片成功', mask: false, duration: 1500, }) resolve() }, fail: (err) => { uni.showToast({ title: '保存图片失败,请手动截图', mask: false, duration: 1500, }) resolve() }, }) }) }