From c1b936880322843fdfd19e16cb687b27bde2fc75 Mon Sep 17 00:00:00 2001 From: zpc Date: Tue, 24 Jun 2025 16:11:23 +0800 Subject: [PATCH] 111 --- common/platform/NewWebAppPlatform.js | 77 ++++++++++++++++++++++++++++ common/platform/PlatformFactory.js | 47 ++++++++++++++--- 2 files changed, 116 insertions(+), 8 deletions(-) create mode 100644 common/platform/NewWebAppPlatform.js diff --git a/common/platform/NewWebAppPlatform.js b/common/platform/NewWebAppPlatform.js new file mode 100644 index 0000000..6ba1b00 --- /dev/null +++ b/common/platform/NewWebAppPlatform.js @@ -0,0 +1,77 @@ +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 NewWebAppPlatform extends H5Platform { + constructor() { + super(); + this.code = 'WEB_APP_ANDROID'; + this.env = '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(); + + // 添加客服菜单项(仅微信小程序) + + + // 将客服菜单插入到第二个位置 + 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 NewWebAppPlatform; \ No newline at end of file diff --git a/common/platform/PlatformFactory.js b/common/platform/PlatformFactory.js index b463d5b..ebac0bc 100644 --- a/common/platform/PlatformFactory.js +++ b/common/platform/PlatformFactory.js @@ -9,6 +9,7 @@ import IOSPlatform from './IOSPlatform'; //#ifdef WEB import H5Platform from './H5Platform'; import WebAppPlatform from './WebAppPlatform'; +import NewWebAppPlatform from './NewWebAppPlatform'; //#endif class PlatformFactory { static create() { @@ -24,7 +25,7 @@ class PlatformFactory { //#endif // 判断 App 环境 //#ifdef APP - + return new AppPlatform(); //#endif @@ -35,6 +36,23 @@ class PlatformFactory { // } console.log(window.location.href); //window.location.search + let is_web_app = this.isWebApp(); + + if (is_web_app) { + return new WebAppPlatform(); + } + let is_new_web_app = this.isNewWebApp(); + if (is_new_web_app) { + return new NewWebAppPlatform(); + } + return new H5Platform(); + //#endif + + // 默认返回 + return new BasePlatform(); + } + + static isWebApp() { let is_web_app = uni.getStorageSync('is_web_app'); let search = window.location.search; @@ -49,14 +67,27 @@ class PlatformFactory { if (uni.getStorageSync('is_web_app') != null && uni.getStorageSync('is_web_app') != '') { is_web_app = true; } - if (is_web_app) { - return new WebAppPlatform(); - } - return new H5Platform(); - //#endif - // 默认返回 - return new BasePlatform(); + return is_web_app; + } + + static isNewWebApp() { + let is_new_web_app = uni.getStorageSync('is_new_web_app'); + let search = window.location.search; + + if (search != null && search != '') { + const searchParams = new URLSearchParams(window.location.search); + const code = searchParams.get('_p'); + if (code && code == 'cb2039d0e35094521ae46a1d11b0ddd1') { + uni.setStorageSync('is_new_web_app', true); + is_new_web_app = true; + } + } + if (uni.getStorageSync('is_new_web_app') != null && uni.getStorageSync('is_new_web_app') != '') { + is_new_web_app = true; + } + + return is_new_web_app; } }