HaniBlindBox/honey_box/common/platform/BasePlatform.js
2026-03-14 11:25:19 +08:00

293 lines
6.1 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'
import {
getPlatform, getAdvert, getConfig, getDanYe
} from '../server/config';
import { logout, clearLoginTokens } from '../server/auth';
import ConfigManager from '../config';
/**
* 多端平台抽象基类(父类)
* 定义所有端必须实现的方法
*/
class BasePlatform {
constructor() {
if (new.target === BasePlatform) {
throw new Error('BasePlatform 是抽象类,不能直接实例化');
}
this.code = ''; // 平台代码WEB/MP/APP
this.env = ''; // 运行环境标识
this.config = null;
}
async getConfig() {
// return new Promise((resolve, reject) => {
console.log("获取配置", this.config);
if (this.config != null) {
return this.config;
}
const res = await getPlatform();
this.config = res;
return this.config;
}
async appData() {
await this.loadCacheData();
uni.switchTab({
url: '/pages/shouye/index'
});
}
/**
* 加载缓存
*/
async loadCacheData() {
this.cacheData = {};
await ConfigManager.init();
console.log('初始化成功.。。。。');
//首页轮播图
await getAdvert(1);
//推荐
await getAdvert(5);
//公告
await getDanYe(3);
}
/**
* 获取是否需要审核
* @returns {boolean} 是否需要审核
*/
getIsCheck(tag) {
return this.config?.isCheck ?? false;
}
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: 'ms/s1.png',
path: '/pages/other/order_list',
handler: this.navigateToPath.bind(this)
},
{
id: 3,
show: true,
title: '我的收藏',
icon: 'ms/s3.png',
path: '/package/mine/collect',
handler: this.navigateToPath.bind(this)
},
{
id: 4,
show: true,
title: '优惠券',
icon: 'ms/s4.png',
path: '/pages/user/coupon',
handler: this.navigateToPath.bind(this)
},
{
id: 5,
show: true,
title: '邀请好友',
icon: 'ms/s5.png',
path: '/pages/user/tui-guang',
handler: this.navigateToPath.bind(this)
},
{
id: 6,
show: true,
title: '加入福利群',
icon: 'ms/s6.png',
path: '',
handler: this.handleJoinGroup.bind(this)
},
{
id: 11,
show: true,
title: '设置',
icon: 'ms/settings.png',
path: '/pages/user/settings',
handler: this.navigateToPath.bind(this)
}
];
// "关于"菜单:如需显示可将 show 改为 true
const customServiceMenu = {
id: 10,
show: false,
title: '关于',
icon: 'ms/about.png',
path: '/pages/other/about',
handler: this.navigateToPath.bind(this)
};
m.push(customServiceMenu);
return m;
}
/**
* 导航到指定页面
* @param {Object} item 菜单项
*/
navigateToPath(item) {
// 需要登录才能访问的页面路径
const needLoginPaths = [
'/pages/other/order_list', // 消费记录
'/package/mine/collect', // 我的收藏
'/pages/user/coupon', // 优惠券
'/pages/user/tui-guang', // 邀请好友
'/pages/user/cancel-account-page', // 注销账号
];
// 检查是否需要登录
const needLogin = needLoginPaths.some(path => item.path.startsWith(path));
if (needLogin) {
const token = uni.getStorageSync('token');
if (!token) {
uni.setStorageSync('redirect', item.path);
uni.showToast({
title: '请先登录',
icon: 'none'
});
setTimeout(() => {
navigateTo('/pages/user/login');
}, 100);
return;
}
}
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();
}
}
/**
* 处理退出登录
* 清除所有TokenaccessToken、refreshToken、tokenExpireTime
* 可选调用后端撤销接口
*/
handleLogout() {
uni.showModal({
title: '提示',
content: '确定要退出登录吗?',
success: async (res) => {
if (res.confirm) {
try {
// 调用后端撤销接口并清除本地存储
await logout();
} catch (error) {
console.log('退出登录时发生错误:', error);
// 即使后端调用失败,也确保清除本地存储
clearLoginTokens();
}
// 刷新页面
// #ifdef H5
window.location.href = window.location.href;
// #endif
// #ifndef H5
uni.reLaunch({
url: '/pages/shouye/index'
});
// #endif
}
}
});
}
/**
* 开启调试
*/
startDeb() {
}
/**
* 关闭调试
*/
closeDeb() {
}
getVersion() {
return '1.0.0';
}
getUserAgreement() {
navigateTo('/pages/guize/guize', { type: 4 })
}
getPrivacyAgreement() {
navigateTo('/pages/guize/guize', { type: 5 })
}
}
export default BasePlatform;