You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
4.8 KiB
JavaScript

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import api from "@/api/index.js";
const tabBarList = [
"/pages/index/index",
"/pages/cat/cat",
"/pages/shoppingCart/index",
"/pages/my/my",
];
function isInWeChatBrowser() {
let isWeChat = false;
/* #ifdef H5 */
let ua = window.navigator.userAgent.toLowerCase();
if (ua.includes("micromessenger")) {
return true
} else {
// api.common.wechatH5().then((res) => {
// const appid = res.data.appid
// let redirect_uri = window.location.href;
// redirect_uri = encodeURIComponent(redirect_uri);
// // 构建微信授权链接包含应用的appid、重定向地址等参数
// const wxAuthUrl = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`;
// // 通过重定向将用户导向微信授权页面
// window.location.href = wxAuthUrl;
// })
}
/* #endif */
return isWeChat;
}
function isInAliBrowser() {
let isAli = false;
/* #ifdef H5 */
let ua = window.navigator.userAgent.toLowerCase();
if (ua.indexOf('alipayclient') > -1 || ua.indexOf('aliapp') > -1) {
return true
} else {
// api.common.wechatH5().then((res) => {
// const appid = res.data.appid
// let redirect_uri = window.location.href;
// redirect_uri = encodeURIComponent(redirect_uri);
// // 构建微信授权链接包含应用的appid、重定向地址等参数
// const wxAuthUrl = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`;
// // 通过重定向将用户导向微信授权页面
// window.location.href = wxAuthUrl;
// })
}
/* #endif */
return isAli;
}
export default {
isInWeChatBrowser,
isInAliBrowser,
// 跳转页面
toUrl(url, type) {
if (tabBarList.some(item => {
return url.includes(item)
})) {
uni.switchTab({
url: url,
});
return;
}
if (type == "redirectTo") {
uni.redirectTo({
url,
});
} else if (type == "reLaunch") {
uni.reLaunch({
url,
});
} else {
uni.navigateTo({
url,
});
}
},
// 后退页面
backTo(num = 1) {
const current = getCurrentPages();
if (current.length <= 1) {
this.toUrl("/pages/home/home");
} else {
uni.navigateBack({
delta: num,
});
}
},
/**
* @des 复制
*/
copy(data, toast = "内容已复制") {
uni.setClipboardData({
data,
success: () => {
uni.$u.toast(toast);
},
});
},
getRouterInfo() {
let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
let curRoute = routes[routes.length - 1].route // 获取当前页面路由,也就是最后一个打开的页面路由
let curParam = routes[routes.length - 1].options; //获取路由参数
const params = Object.entries(curParam).reduce((param, [key, val], index) => {
if (index === 0) {
return param + key + '=' + val
}
return param += '&' + key + '=' + val
}, "?")
return [curRoute, params]
},
loginOut() {
// 获取当前路由信息
const [curRoute, curRouteParam] = this.getRouterInfo();
// 如果当前路由不包含 'login/login'
if (!("/" + curRoute + curRouteParam).includes('login/login')) {
// 如果当前路由参数不为 "?"
if (curRouteParam != "?") {
// 将当前页面信息存储到本地缓存中
uni.setStorageSync("backPage", "/" + curRoute + curRouteParam);
} else {
// 将当前页面信息存储到本地缓存中
uni.setStorageSync("backPage", "/" + curRoute);
}
}
// 移除本地缓存中的 token 和 user_info
uni.removeStorageSync("token");
uni.removeStorageSync("user_info");
// 如果当前路由不包含 'login/login'
if (!curRoute.includes('login/login')) {
// 跳转到登录页面
this.toUrl("/subPackages/login/login/index", "redirectTo");
}
},
kmUnit(m) {
var v;
if (typeof m === 'number' && !isNaN(m)) {
if (m >= 1000) {
v = (m / 1000).toFixed(2) + ' km'
} else {
v = m.toFixed(2) + ' m'
}
} else {
v = '0 m'
}
return v;
},
objectToQueryString(obj) {
var params = '';
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
if (params !== '') {
params += '&';
}
params += encodeURIComponent(key) + '=' + encodeURIComponent(obj[key]);
}
}
return '?' + params;
},
oneValues(url) {
var name, value;
var str = url; //取得整个地址栏
var num = str.indexOf("?");
str = str.substr(num + 1); //取得所有参数 stringvar.substr(start [, length ]
let obj = {};
var arr = str.split("&"); //各个参数放到数组里
for (var i = 0; i < arr.length; i++) {
num = arr[i].indexOf("=");
if (num > 0) {
name = arr[i].substring(0, num);
value = arr[i].substr(num + 1);
obj[name] = value;
}
}
return obj;
},
httpToHttps(url) {
return url.replace(/^http:\/\//i, 'https://');
},
};