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.
421 lines
10 KiB
421 lines
10 KiB
import store from '@/store/index'
|
|
import { uploadUrl, XAPPID, enterpriseType } from '@/enums/index.js'
|
|
import { pushCustomerOff } from '@/apis/commonApi'
|
|
import { debounce, parseUrl } from '@/utils/index'
|
|
// 框架方法封装
|
|
const tabList = ['index', 'order', 'mine', 'category']
|
|
|
|
/**
|
|
* @param {string} 返回上一级
|
|
* @return {null}
|
|
*/
|
|
export function back() {
|
|
if (getCurrentPages().length > 1) {
|
|
uni.navigateBack({
|
|
delta: 1
|
|
})
|
|
} else {
|
|
go2('index')
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取当前页面
|
|
*/
|
|
export function getActivePage() {
|
|
const curPages = getCurrentPages()
|
|
if (curPages.length) {
|
|
return curPages[curPages.length - 1]
|
|
}
|
|
return null
|
|
}
|
|
|
|
/**
|
|
* 跳转到指定页面,包括tabBar页面
|
|
* @param {string} url 页面名称
|
|
* @param {object} data 页面参数
|
|
* @param {boolean} isRedirect 是否重定向 默认false
|
|
* @param {boolean} isReLaunch 是否重新加载 默认false
|
|
* @return {null}
|
|
*/
|
|
export function go2(url, data = {}, isRedirect = false, isReLaunch = false) {
|
|
if (!url) {
|
|
console.error('请选择页面')
|
|
return
|
|
}
|
|
let param = ''
|
|
Object.keys(data).forEach((key) => {
|
|
let value = data[key]
|
|
let type = typeof value
|
|
if (type === 'object') {
|
|
value = JSON.stringify(value)
|
|
}
|
|
if (param === '') {
|
|
param = `${key}=${value}`
|
|
} else {
|
|
param += `&${key}=${value}`
|
|
}
|
|
})
|
|
if (isReLaunch) {
|
|
uni.reLaunch({
|
|
url: `/pages/${url}/index${param ? '?' + param : ''}`
|
|
})
|
|
return
|
|
}
|
|
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')
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 重定向到登陆页面
|
|
*/
|
|
export const redirectLogin = debounce(() => {
|
|
let pages = getCurrentPages()
|
|
let curPage = pages[pages.length - 1]
|
|
let fullPath = curPage.$page.fullPath
|
|
const { host, query } = parseUrl(fullPath)
|
|
if (host) {
|
|
let reg = /\/pages\/(.*)\/index/
|
|
let path = reg.exec(host)[1]
|
|
if (path !== 'login') {
|
|
go2('login', { target: path, data: JSON.stringify(query) }, false, true)
|
|
}
|
|
}
|
|
}, 300)
|
|
|
|
/**
|
|
* 退出登录并跳转到登录页面
|
|
* @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')
|
|
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 })
|
|
}
|
|
|
|
/**
|
|
* 根据后端统一的skuList转化为前端需要的skuList
|
|
* @param {array} list
|
|
* @returns {array} skuList
|
|
*/
|
|
export function transformSku(list) {
|
|
let skuList = []
|
|
list.forEach((item) => {
|
|
let sku = {
|
|
id: item.id,
|
|
stock: item.stock,
|
|
scribingPrice: item.scribingPrice,
|
|
listPrice: item.listPrice
|
|
}
|
|
let attrs = {}
|
|
item.attrList.forEach((attr) => {
|
|
attrs[attr.skuAttrId] = {
|
|
value: attr.skuAttrValueId,
|
|
extraInfo: attr
|
|
}
|
|
})
|
|
skuList.push({ ...sku, attrs })
|
|
})
|
|
return skuList
|
|
}
|
|
|
|
/**
|
|
* 跳转到微信客服
|
|
*/
|
|
export function jumpService(info) {
|
|
// #ifdef MP-WEIXIN
|
|
wx.openCustomerServiceChat(info || { extInfo: { url: 'https://work.weixin.qq.com/kfid/kfca1fe7388697db25c' }, corpId: 'wwe1732a63029fe997' })
|
|
// #endif
|
|
|
|
// #ifndef MP-WEIXIN
|
|
uni.showToast({
|
|
title: '请在微信中打开'
|
|
})
|
|
// #endif
|
|
}
|