yfs/common/platform/WebAppPlatform.js
2025-06-08 20:23:01 +08:00

84 lines
2.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import BasePlatform from './BasePlatform';
import H5Platform from './H5Platform';
function parseQueryString(queryString) {
// 如果以 ? 开头,先去掉 ?
if (queryString.startsWith('?')) {
queryString = queryString.substring(1);
}
const params = {};
const pairs = queryString.split('&');
for (const pair of pairs) {
const [key, value] = pair.split('=');
// 解码 URI 组件
params[key] = decodeURIComponent(value);
}
return params;
}
class WebAppPlatform extends H5Platform {
constructor() {
super();
this.code = 'WEB_APP';
this.env = 'WEB_APP';
}
share({ title, desc, image, url }) {
console.log(`H5分享${title} - ${desc}`);
// 调用浏览器原生分享(如果可用)
if (navigator.share) {
return navigator.share({ title, text: desc, url });
}
// 降级方案
// alert(`请手动分享:${url}`);
}
downloadFile(url) {
return new Promise((resolve, reject) => {
try {
// 创建一个隐藏的a标签
const a = document.createElement('a');
a.href = url;
a.download = url.split('/').pop() || 'download'; // 使用URL中的文件名或默认名称
a.style.display = 'none';
document.body.appendChild(a);
a.click(); // 触发下载
document.body.removeChild(a); // 清理DOM
resolve({ success: true });
} catch (error) {
console.error('下载失败:', error);
// 降级方案,直接打开
window.location.href = url;
resolve({ success: false, error: error.message });
}
});
}
/**
* 重写获取用户中心菜单列表,添加微信小程序特有菜单
* @returns {Array} 菜单项数组
*/
getUserMenuList() {
// 获取基础菜单列表
const baseMenuList = super.getUserMenuList();
// 添加客服菜单项(仅微信小程序)
const customServiceMenu = {
id: 10,
show: true,
title: '关于',
icon: 'my/about.png',
path: '/pages/other/about',
handler: this.navigateToPath.bind(this)
};
// 将客服菜单插入到第二个位置
return [...baseMenuList.slice(0, baseMenuList.length - 1), customServiceMenu, ...baseMenuList.slice(baseMenuList.length - 1)];
}
getVersion() {
return uni.getStorageSync('version') == '' ? '1.0.0' : uni.getStorageSync('version');
}
}
export default WebAppPlatform;