Files

131 lines
3.2 KiB
JavaScript
Raw Normal View History

2023-10-11 11:27:47 +08:00
import stringify from '@/utils/querystring'
2023-11-14 17:21:03 +08:00
import { timeFormat } from '@/uni_modules/uv-ui-tools/libs/function/index.js';
2023-10-11 11:27:47 +08:00
import router from './router'
import cookie from './cookie'
2023-11-14 17:21:03 +08:00
/**
* 未登录无权限
*/
2023-10-11 11:27:47 +08:00
export const handleLoginFailure = () => {
2023-11-14 17:21:03 +08:00
// todo 添加记录参数逻辑
// router.replace({
// path: '/pages/login/login',
// })
const routes = getCurrentPages();
const curRoute = routes[routes.length - 1];
if (["pages/login/guid", "pages/login/index"].includes(curRoute.route)) {
return;
}
const data = curRoute?.$page?.fullPath;
uni.setStorage({
key: 'lastFullPath',
data
})
uni.redirectTo({
url: '/pages/login/guid',
})
}
export const afterLogin = () => {
const tabsList = [
'/pages/goodsCategory/goodsCategory',
'/pages/index/index',
'/pages/shoppingCart/shoppingCart',
'/pages/user/user'
]
const lastPath = uni.getStorageSync('lastFullPath');
if (lastPath) {
const str = lastPath.split("?")
if (tabsList.includes(str[0])) {
uni.switchTab({url: lastPath})
} else {
uni.redirectTo({url: lastPath})
}
uni.removeStorageSync('lastFullPath')
} else {
uni.switchTab({url: '/pages/index/index'})
}
2023-10-11 11:27:47 +08:00
}
export function parseUrl(location) {
2023-11-14 17:21:03 +08:00
if (typeof location === 'string') return location
const {url, query} = location
const queryStr = stringify(query)
if (!queryStr) {
return url
}
2023-10-11 11:27:47 +08:00
2023-11-14 17:21:03 +08:00
return `${ url }?${ queryStr }`
}
export function objectURLToBlob(url) {
return new Promise(resolve => {
const http = new XMLHttpRequest()
http.open('GET', url, true)
http.responseType = 'blob'
http.onload = function (e) {
if (this.status == 200 || this.status === 0) {
resolve(this.response)
}
}
http.send()
})
}
2023-10-11 11:27:47 +08:00
2023-11-14 17:21:03 +08:00
/**
* 判断h5环境是否在微信浏览器内
* @returns {boolean}
*/
export function h5InWeChat() {
if (!navigator || (navigator && !navigator.userAgent)) return false
let ua = navigator.userAgent.toLowerCase();
return ua.match(/MicroMessenger/i) == "micromessenger"
}
2023-10-11 11:27:47 +08:00
2023-11-14 17:21:03 +08:00
export function createMessage(msg, data) {
return {
msg,
data
}
2023-10-11 11:27:47 +08:00
}
2023-11-14 17:21:03 +08:00
2023-10-11 11:27:47 +08:00
const toAuth = () => {
2023-11-14 17:21:03 +08:00
uni.showToast({
title: '暂未开放',
icon: 'none',
duration: 2000,
})
2023-10-11 11:27:47 +08:00
}
export default {
2023-11-14 17:21:03 +08:00
install: (app, options) => {
// 在这里编写插件代码
// 注入一个全局可用的 $translate() 方法
app.config.globalProperties.$yrouter = router
app.config.globalProperties.$cookie = cookie
app.config.globalProperties.$toAuth = toAuth
app.config.globalProperties.$timeFormat = timeFormat
app.config.globalProperties.$onClickLeft = () => {
uni.navigateBack()
// router.back()
}
2023-10-11 11:27:47 +08:00
2023-11-14 17:21:03 +08:00
// #ifdef H5
app.config.globalProperties.$platform = 'h5'
// #endif
2023-10-11 11:27:47 +08:00
2023-11-14 17:21:03 +08:00
// #ifdef APP-PLUS
// app端
app.config.globalProperties.$platform = 'app'
// #endif
2023-10-11 11:27:47 +08:00
2023-11-14 17:21:03 +08:00
// #ifdef MP-WEIXIN
app.config.globalProperties.$platform = 'routine'
// #endif
},
2023-10-11 11:27:47 +08:00
}