312 lines
8.5 KiB
JavaScript
312 lines
8.5 KiB
JavaScript
import { projectInfo,OFFICIAL_URL} from "@/config/index.js";
|
||
import i18n from '../locale/index';
|
||
import { useLoginApi } from "@/Apis/login.js";
|
||
import { useMainStore } from "@/store/index.js";
|
||
const getApi = useLoginApi();
|
||
const { t } = i18n.global;
|
||
|
||
export const onTabItemTap = (event)=>{
|
||
if(event.index === 2) {
|
||
makePhoneCall()
|
||
return
|
||
}
|
||
uni.switchTab({
|
||
url: `/${event.pagePath}`
|
||
});
|
||
}
|
||
export const makePhoneCall = (phoneNumber = projectInfo.callPhone)=>{
|
||
if(!(typeof phoneNumber == 'string' || typeof phoneNumber == 'number')){
|
||
phoneNumber = '4008181813'
|
||
}
|
||
uni.makePhoneCall({
|
||
phoneNumber,
|
||
});
|
||
}
|
||
// tab列
|
||
const pagePathList = [
|
||
'/pages/index/index',
|
||
'/pages/unlock/index',
|
||
'/pages/personal/index',
|
||
]
|
||
export const navigateBack = (url)=>{
|
||
const app = getApp();
|
||
|
||
const pages = getCurrentPages()
|
||
// 有可返回的页面则直接返回,uni.navigateBack 默认返回失败之后会自动刷新页面 ,无法继续返回
|
||
if (pages.length > 1) {
|
||
uni.navigateBack(1)
|
||
return;
|
||
}
|
||
if(url){
|
||
if(pagePathList.includes(url))
|
||
{
|
||
uni.switchTab({
|
||
url,
|
||
})
|
||
}else{
|
||
uni.navigateTo({
|
||
url,
|
||
})
|
||
}
|
||
|
||
return
|
||
}
|
||
uni.reLaunch({
|
||
url: "/pages/index/index"
|
||
})
|
||
return;
|
||
}
|
||
// onshow 判断有没token 没有就不去请求 有的话就去请求
|
||
export const onShowRerequestByIsLogin = (callBack,errCallBack)=>{
|
||
// 检查是否存在token 没有直接跳转
|
||
const token = uni.getStorageSync('token')
|
||
if(token){
|
||
callBack()
|
||
}else{
|
||
errCallBack&&errCallBack()
|
||
}
|
||
|
||
}
|
||
// 登陆完 是否可以返回到登录前的页面
|
||
export const isToHome = () => {
|
||
let routes = getCurrentPages();
|
||
let isNeeToHome = false;
|
||
// 登录后返回原来页面的路由
|
||
let refreshPage = ["pages/unlock/index", "pages/setOrder/index", "pages/site/index", "pages/orderdetail/index", "pages/orderdetail/door", "pages/orderdetail/lock","pagesb/reserve/index"];
|
||
let preRoute = routes[routes.length - 2]?.route;
|
||
if (preRoute && refreshPage.includes(preRoute)) {
|
||
isNeeToHome = false
|
||
}else{
|
||
isNeeToHome = true
|
||
}
|
||
if (isNeeToHome) {
|
||
uni.reLaunch({
|
||
url:'/pages/index/index'
|
||
});
|
||
} else {
|
||
navigateBack();
|
||
}
|
||
}
|
||
|
||
// 将角度转换为弧度常量
|
||
const RADIANS_PER_DEGREE = Math.PI / 180.0;
|
||
// 通过经纬度计算两点之间距离
|
||
export const getDistance = (lat1, lng1, lat2, lng2) => {
|
||
const radLat1 = lat1 * RADIANS_PER_DEGREE;
|
||
const radLat2 = lat2 * RADIANS_PER_DEGREE;
|
||
const deltaLat = radLat1 - radLat2;
|
||
const deltaLng = lng1 * RADIANS_PER_DEGREE - lng2 * RADIANS_PER_DEGREE;
|
||
let distance = 2 * Math.asin(Math.sqrt(Math.sin(deltaLat / 2) ** 2 + Math.cos(radLat1) * Math.cos(radLat2) * Math.sin(deltaLng / 2) ** 2));
|
||
distance = distance * 6378.137; // 地球半径
|
||
distance = Math.round(distance * 10) / 10;
|
||
if (distance > 99) {
|
||
return { distance: ">99", number: distance };
|
||
}
|
||
return { distance, number: distance };
|
||
}
|
||
// 导出导航栏高度
|
||
export const navbarHeightAndStatusBarHeight = () => {
|
||
let systemInfo = {};
|
||
// #ifdef MP-WEIXIN
|
||
systemInfo = uni.getDeviceInfo();
|
||
// #endif
|
||
let navbarHeight = 0;
|
||
let statusBarHeight = 0;
|
||
let iStatusBarHeight = 0;
|
||
|
||
|
||
navbarHeight = getApp().globalData.navbarHeight;
|
||
statusBarHeight = getApp().globalData.statusBarHeight;
|
||
iStatusBarHeight = uni.getSystemInfoSync().statusBarHeight
|
||
// 不知道什么原因 navbarHeight 会出现0的情况 如果为0 再重新获取一下
|
||
if (navbarHeight === 0 || statusBarHeight === 0) {
|
||
// #ifdef MP-WEIXIN || MP-XHS
|
||
// 状态栏高度
|
||
const wxStatusBarHeight = uni.getSystemInfoSync().statusBarHeight;
|
||
const wxMenuBtn = uni.getMenuButtonBoundingClientRect();
|
||
// 导航栏高度(标题栏高度) = 胶囊高度 + (顶部距离 - 状态栏高度) * 2
|
||
const barHeight = wxMenuBtn.height + (wxMenuBtn.top - wxStatusBarHeight) * 2;
|
||
// 总体高度 = 状态栏高度 + 导航栏高度
|
||
navbarHeight = (barHeight || 40) + wxStatusBarHeight+iStatusBarHeight;
|
||
statusBarHeight = wxStatusBarHeight || 20;
|
||
// #endif
|
||
}else{
|
||
// #ifdef APP-PLUS
|
||
navbarHeight = navbarHeight+iStatusBarHeight
|
||
// #endif
|
||
}
|
||
|
||
// 判断是否为PC(Windows、Mac)
|
||
if (systemInfo.platform === 'windows' || systemInfo.platform === 'mac') {
|
||
// 在这里处理PC端的逻辑
|
||
let tempHeight = {
|
||
navbarHeight,
|
||
statusBarHeight
|
||
}
|
||
return {
|
||
navbarHeight: 0,
|
||
statusBarHeight: 0,
|
||
tempHeight
|
||
}
|
||
}
|
||
|
||
return {
|
||
navbarHeight,
|
||
statusBarHeight
|
||
}
|
||
}
|
||
// h5 跳转小程序
|
||
export const h5GoWx =(successCallback, failCallback,completeCallback)=>{
|
||
uni.showModal({
|
||
cancelText:t("request.cancel"),
|
||
confirmText:t("request.confirm"),
|
||
title:t("request.tip"),
|
||
content:"前往去登录吗?",
|
||
success:(e)=>{
|
||
if(e.confirm){
|
||
shunfenLogin()
|
||
// window.location.href = OFFICIAL_URL
|
||
}
|
||
successCallback && successCallback(e)
|
||
} ,
|
||
fail: failCallback,
|
||
complete: completeCallback
|
||
})
|
||
}
|
||
// 去掉小数点后两位
|
||
export const removeTrailingZeros = (num) => {
|
||
// Check if input is a valid number
|
||
if (isNaN(num) || num === null || num === undefined) {
|
||
return num;
|
||
}
|
||
// Convert to float and then to string to remove trailing zeros
|
||
return parseFloat(num).toString();
|
||
}
|
||
|
||
// 分享页的参数
|
||
export const shareParam = {
|
||
title: `${projectInfo.miniName}`,
|
||
path: '/pages/index/index'
|
||
}
|
||
|
||
// 根据门店id 跳转到时昌的小程序
|
||
export const jumpToSc = [
|
||
{ id: '1906958815820124160', targetId: '1881917629627764736' },
|
||
]
|
||
|
||
// 五羊门店的id
|
||
export const wuyangSiteIds = [
|
||
'1854360658481647616',
|
||
'1854360658913660928',
|
||
'1854360659777687552',
|
||
'1854360654157320192',
|
||
]
|
||
// ['10078', '10079', '10082', '10084']
|
||
// [
|
||
// '1854360658481647616',
|
||
// '1854360658913660928',
|
||
// '1854360659777687552',
|
||
// '1854360654157320192',
|
||
// ];
|
||
|
||
|
||
/**
|
||
* 合并五羊门店,把同组 id push 到 siteList
|
||
* @param {Array} stores 门店列表
|
||
* @returns {Array}
|
||
*/
|
||
export const mergeFiveGoatStores = (stores) => {
|
||
// 找到所有存在的五羊门店(按原顺序)
|
||
const existing = wuyangSiteIds
|
||
.map(id => stores.find(s => s.id === id))
|
||
.filter(Boolean);
|
||
|
||
if (existing.length === 0) return stores; // 没有任何五羊门店,不处理
|
||
|
||
// 选出主门店(按 ids 顺序第一个存在的)
|
||
const mainStore = existing[0];
|
||
|
||
// 其余门店作为子站点
|
||
const siteList = existing.slice(1);
|
||
|
||
return stores
|
||
.filter(store => !wuyangSiteIds.includes(store.id) || store.id === mainStore.id) // 非主门店不再出现在列表
|
||
.map(store => {
|
||
if (store.id === mainStore.id) {
|
||
return {
|
||
...store,
|
||
siteList,
|
||
isFiveGoatStores: siteList.length > 0,
|
||
};
|
||
}
|
||
return store;
|
||
});
|
||
};
|
||
|
||
// 把 url 的参数转换成对象
|
||
export const parseUrlParams = (url) => {
|
||
const params = {};
|
||
const queryString = url.split('?')[1];
|
||
if (!queryString) return params;
|
||
|
||
const pairs = queryString.split('&');
|
||
for (const pair of pairs) {
|
||
const [key, value] = pair.split('=');
|
||
if (key) {
|
||
params[key] = value || '';
|
||
}
|
||
}
|
||
return params;
|
||
}
|
||
// 顺丰参数
|
||
export const shunfenLogin = async (code) => {
|
||
// 授权参数(请根据实际情况修改)
|
||
const authOptions = {
|
||
appId: window?.config?.SF_APP_ID ??"202603041358374970", // 第三方接入ID
|
||
scope: ["sf_userinfo", "sf_mobile", "sf_base", "sf_emp", "sf_channelId"],
|
||
successUrl: window?.config?.BACK_URL ?? "https://uat.kingkongcang.com/client/", // 当前页面地址
|
||
};
|
||
window.sf.getAuthCode({
|
||
...authOptions,
|
||
})
|
||
.then(async(res) => {
|
||
if (window.sf.isSfApp()) {
|
||
const authCode = res.authCode;
|
||
if(authCode){
|
||
let source = window.sf.isSfApp() ? 2 : 1;
|
||
uni.showLoading({
|
||
mask:true,
|
||
});
|
||
getApi.ShunFengLogin({
|
||
code:authCode,
|
||
source,
|
||
})
|
||
.then(async(res) => {
|
||
uni.hideLoading();
|
||
if (res.code == 200) {
|
||
const store = useMainStore(); // ✅ 在这里调用
|
||
store.storeState.token = res.data.token;
|
||
store.storeState.hasTrytoLogin = true;
|
||
await store.getUserInfo();
|
||
console.log("token", res.data.token);
|
||
uni.setStorageSync("token", res.data.token);
|
||
uni.setStorageSync("openId", res.data.openId);
|
||
console.log("登陆成功", res.data.openId);
|
||
|
||
}
|
||
}).catch((err) => {
|
||
})
|
||
}else{
|
||
shunfenLogin()
|
||
}
|
||
}
|
||
console.log('getAuthCode success',res);
|
||
})
|
||
.catch((err) => console.log('getAuthCode err',err));
|
||
|
||
}
|
||
// 获取url 参数
|
||
export const getQueryParam = (name, url = window.location.href) => {
|
||
const params = new URL(url).searchParams;
|
||
return params.get(name);
|
||
} |