67 lines
2.1 KiB
JavaScript
67 lines
2.1 KiB
JavaScript
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';
|
|
}
|
|
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();
|
|
|
|
// 添加客服菜单项(仅微信小程序)
|
|
|
|
|
|
// 将客服菜单插入到第二个位置
|
|
return [...baseMenuList.slice(0, baseMenuList.length - 1), ...baseMenuList.slice(baseMenuList.length - 1)];
|
|
}
|
|
getVersion() {
|
|
return uni.getStorageSync('version') == '' ? '1.0.0' : uni.getStorageSync('version');
|
|
}
|
|
}
|
|
export default WebAppPlatform; |