yfs/common/platform/BasePlatform.js
2025-05-20 00:29:56 +08:00

209 lines
3.9 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 { navigateTo } from '@/common/router'
/**
* 多端平台抽象基类(父类)
* 定义所有端必须实现的方法
*/
class BasePlatform {
constructor() {
if (new.target === BasePlatform) {
throw new Error('BasePlatform 是抽象类,不能直接实例化');
}
this.code = ''; // 平台代码WEB/MP/APP
this.env = ''; // 运行环境标识
}
/**
* 获取配置
* @returns
*/
async getConfig() {
return {};
}
getPayData(url, data) {
throw new Error('子类必须实现 getPayData 方法');
}
/**
*
*/
chooseAddress() {
return new Promise((resolve, reject) => {
throw new Error('子类必须实现 chooseAddress 方法');
});
}
/**
* 支付方法(子类必须实现)
* @param {number} amount - 支付金额(分)
* @param {string} orderId - 订单号
*/
pay({
data
}, event) {
throw new Error('子类必须实现 pay 方法');
}
/**
* 分享方法(子类必须实现)
* @param {object} params - 分享参数 { title, desc, image, url }
*/
share({
title,
desc,
link,
image
}) {
throw new Error('子类必须实现 share 方法');
}
/**
* 通用方法(所有端共用)
*/
getPlatformInfo() {
return {
code: this.code,
env: this.env,
ua: navigator?.userAgent || ''
};
}
downloadFile(url) {
return new Promise((resolve, reject) => {
throw new Error('子类必须实现 downloadFile 方法');
});
}
AppLaunch(options) {
throw new Error('子类必须实现 AppLaunch 方法');
}
/**
* 获取订单号
*/
getOrderNo(event) {
throw new Error('子类必须实现 getOrderNo 方法');
}
delOrderNo() {
}
/**
* 获取用户中心菜单列表
* @returns {Array} 菜单项数组每项包含id, show, title, icon, path和handler
*/
getUserMenuList() {
let m = [
{
id: 1,
show: true,
title: '消费记录',
icon: 'my/s1.png',
path: '/pages/other/order_list',
handler: this.navigateToPath.bind(this)
},
{
id: 3,
show: true,
title: '我的收藏',
icon: 'my/s3.png',
path: '/package/mine/collect',
handler: this.navigateToPath.bind(this)
},
{
id: 4,
show: true,
title: '优惠券',
icon: 'my/s4.png',
path: '/pages/user/coupon',
handler: this.navigateToPath.bind(this)
},
{
id: 5,
show: true,
title: '邀请好友',
icon: 'my/s5.png',
path: '/pages/user/tui-guang',
handler: this.navigateToPath.bind(this)
},
{
id: 6,
show: true,
title: '加入福利群',
icon: 'my/s6.png',
path: '',
handler: this.handleJoinGroup.bind(this)
},
{
id: 7,
show: true,
title: '用户协议',
icon: 'my/s7.png',
path: '/pages/guize/guize?type=4',
handler: this.navigateToPath.bind(this)
}
];
if (uni.getStorageSync('token') != null && uni.getStorageSync('token') != "") {
m.push({
id: 8,
show: true,
title: '退出登录',
icon: 'my/exit.png',
path: '',
handler: this.handleLogout.bind(this)
})
}
return m;
}
/**
* 导航到指定页面
* @param {Object} item 菜单项
*/
navigateToPath(item) {
navigateTo(item.path);
}
/**
* 处理加入福利群
* @param {Object} item 菜单项
* @param {Object} context 页面上下文
*/
handleJoinGroup(item, context) {
if (context && context.$refs && context.$refs.qunliao_show) {
context.$refs.qunliao_show.open();
}
}
/**
* 处理退出登录
*/
handleLogout() {
uni.showModal({
title: '提示',
content: '确定要退出登录吗?',
success: (res) => {
if (res.confirm) {
uni.removeStorageSync('token');
uni.removeStorageSync('userinfo');
// uni.switchTab({
// url: '/pages/user/index'
// });
window.location.href = window.location.href;
}
}
});
}
/**
* 开启调试
*/
startDeb() {
}
/**
* 关闭调试
*/
closeDeb() {
}
getVersion() {
return '1.0.0';
}
}
export default BasePlatform;