【前端】印包客app
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

375 lines
8.9 KiB

import store from '@/store/index'
import { uploadUrl, XAPPID, enterpriseType } from '@/enums/index.js'
import { pushCustomerOff } from '@/apis/commonApi'
// 框架方法封装
const tabList = ['digital-workshops', 'factory', 'mall', 'mine']
/**
* @param {string} 跳转的tabBar页面
* @return {null}
*/
export function tab2(tabPage) {
if (tabList.includes(tabPage)) {
uni.switchTab({
url: `/pages/${tabPage}/index`
})
}
}
/**
* 根据用户角色动态改变tabbar
* @param {number} type 企业类型
* @return {null}
*/
export function changeTabbar(type) {
if (type == enterpriseType.PERSONAL) {
;[0, 1].forEach((index) => {
uni.setTabBarItem({
index: index,
visible: false
})
})
} else {
;[0, 1].forEach((index) => {
uni.setTabBarItem({
index: index,
visible: true
})
})
}
}
/**
* @param {string} 返回上一级
* @return {null}
*/
export function back() {
if (getCurrentPages().length > 1) {
uni.navigateBack({
delta: 1
})
} else {
go2('mine')
}
}
/**
* 跳转到指定页面,包括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 = false) {
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) {
// #ifdef APP-PLUS
let cid = plus.push.getClientInfo().clientid
let platform = uni.getSystemInfoSync().platform
pushCustomerOff(cid, platform)
// #endif
store.dispatch('logout')
uni.reLaunch({url: '/pages/login/index'})
// go2('login', {}, redirect)
}
/**
* 图片文件上传
* @param {array} sourceType 上传的方式 album:相册 camera:相机
* @param {number} count 上传的数量
* @return {Promise<string[]>} 以数组的形式返回对应的文件地址
*/
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((result) => {
resolve(result)
})
.catch((err) => {
reject(err)
})
},
fail: (err) => {
console.error('chooseImage error:', err)
resolve(null)
}
})
})
}
/**
* 视频文件上传
* @param {array} sourceType 上传的方式 album:相册 camera:相机
* @param {boolean} compressed 是否压缩
* @return {Promise<object>} 返回对应的文件地址
* @value {string} url 视频地址
* @value {string} thumb 视频缩略图地址
*/
export function uploadVideo(sourceType = ['album', 'camera'], compressed = true) {
return new Promise((resolve, reject) => {
uni.chooseVideo({
compressed,
sourceType: sourceType,
maxDuration: 30,
success: (res) => {
const tempFilePath = res.tempFilePath
console.log('video size:', res, res.size)
if (res.size && res.size > 1024 * 1024 * 10) {
reject('视频大小不能超过10M')
} else {
uploadFile(tempFilePath, 'video')
.then((result) => {
resolve(result)
})
.catch((err) => {
reject(err)
})
}
},
fail: (err) => {
console.error('chooseImage error:', err)
resolve(null)
}
})
})
}
/**
* 单文件上传
* @param {string} path 文件路径
* @param {string} type 文件类型 image | file,默认为image
* @returns {Promise<string>} 返回单文件上传地址
*/
export function uploadFile(path, type = 'image') {
return new Promise((resolve, reject) => {
uni.uploadFile({
url: uploadUrl[type],
filePath: path,
name: type == 'image' ? 'image' : 'file',
header: {
Authorization: store.state.qnToken,
'X-APP-ID': XAPPID
},
// fileType: type,
success: (res) => {
let result = JSON.parse(res.data)
if (result.data) {
if (type == 'video') {
resolve({
url: result.data.videoFileUrl,
thumb: result.data.videoImgUrl
})
} else {
resolve(result.data)
}
} else {
reject(result.message)
}
},
fail: (err) => {
console.error('uploadFile error:', err)
reject(err)
}
})
})
}
/**
* 屏幕截图并保存到相册(不包含状态栏和tabBar),原理就是用webview的draw方法
*/
export function screenShot() {
let pages = getCurrentPages()
let page = pages[pages.length - 1]
let bitmap = null
// 获取当前页面 webview 的对象实例
let 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) {
uni.showToast({
title: '保存图片失败,请手动截图',
mask: false,
duration: 1500
})
}
)
},
function (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()
}
})
})
}
/**
* 根据类型获取缓存
* @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 })
}