UNPKG

@sinosun/lib

Version:

sinosun jsbridge and net toolkit

482 lines (390 loc) 14.1 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault"); var _typeof2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/typeof")); require("core-js/modules/es.regexp.exec.js"); require("core-js/modules/es.string.split.js"); require("core-js/modules/es.function.name.js"); var _defineProperty = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/define-property")); var _assign = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/assign")); var __importDefault = void 0 && (void 0).__importDefault || function (mod) { return mod && mod.__esModule ? mod : { "default": mod }; }; (0, _defineProperty["default"])(exports, "__esModule", { value: true }); /* * @Description: 入口页-baseApp */ /* 所有page页面基础类,封装处理注册app的back、refresh事件 设置页面的前进、回退动画 // 记录当前设置的titleBarConfig window.TITLEBARCONFIG = titleBarConfig; // 存储当前设置的titleBarConfig 后续二级弹框关闭需要恢复 window.STORECURRENTTITLEBARCONFIG = titleBarConfig; */ var baseUtils_1 = require("./baseUtils"); var EventType_1 = __importDefault(require("./EventType")); // 页面默认主题 表示背景白色字体黑色 // 页面暗黑模式主题 表示背景黑色字体白色 var basePageConfig = { // Title栏背景色颜色 backgroundColor: "#ffffff", // Title栏字体颜色 并非主题 themeMode: "dark", // Title栏透明度 opacity: 1, // 图标文字透明度 contentOpacity: 1, // 是否显示title栏 showTitleBar: true, // 状态栏是否占位 showStatusBar: true, // 标题栏是否悬浮内容区域之上 suspend: false, // 右侧菜单 showMenuAction: true }; var BaseApp = { provide: function provide() { return { getThemeConfig: this.getThemeConfig, restoreDefaultTitleBar: this.restoreDefaultTitleBar, restoreCurrentTitleBar: this.restoreCurrentTitleBar }; }, data: function data() { return { // 当前页面主题 basePageConfig: null, // 当前的url参数 currentUrlParams: {}, // 页面路由动画 transitionName: 'slide-left' }; }, beforeCreate: function beforeCreate() { // 为了快速设置titleBar 状态 页面创建之前设置 this.$options.methods.setDefaultTitleBar(this); }, created: function created() { // 设置当前主题 this.setThemeConfig(); // 获取主题 this.getThemeConfig(); // 获取url参数 this.dealUrlParams(); // 注册回退事件 this.registerBack(); // 注册多页回退到当页事件 this.registerRefresh(); // 注册APP切换到前台事件 this.registerAppPause(); // 注册APP切换到后台事件 this.registerAppResume(); // 切换企业 this.registerSwitchCompany(); // 非单页 不处理 自己业务代码自己处理 if (this.isPageNoRouter()) { this.registerBrowserMenuList(); } }, methods: { // 根据主题模式设置默认主题 setThemeConfig: function setThemeConfig(titleConfig) { if (titleConfig === void 0) { titleConfig = {}; } // 页面暗黑模式主题 表示背景黑色字体白色 var theme = window.SINO_THEME !== 'dark' ? 'dark' : 'light'; var themeConfig = theme === "dark" ? { // Tile栏背景色颜色 backgroundColor: "#ffffff", // 主题色 themeMode: "dark" } : { // Tile栏背景色颜色 backgroundColor: "#1C1D1F", // 主题色 themeMode: "light" }; this.basePageConfig = (0, _assign["default"])({}, this.basePageConfig || basePageConfig, titleConfig, themeConfig); }, // 获取tilte栏配置 getThemeConfig: function getThemeConfig() { return this.basePageConfig; }, // 获取路由参数 dealUrlParams: function dealUrlParams() { this.currentUrlParams = baseUtils_1.getUrlParams() || {}; }, // 设置title定制 setDefaultTitleBar: function setDefaultTitleBar(that) { // 非单页 不处理 自己业务代码自己处理 if (this.isPageNoRouter(that)) { this.setPageTitleBar(that); return; } // 获取单页的当前路由的titleBar 状态 var metaConfig = this.getRoutesMeta(that || this); this.setTitleBar(metaConfig); }, // 设置titlebar setTitleBar: function setTitleBar(metaConfig) { var title = metaConfig.title || ''; if (title) { this.setTitle(title); } var titleBarConfig = metaConfig.titleBarConfig || {}; if (titleBarConfig && typeof titleBarConfig.backgroundColor === 'function') { titleBarConfig.backgroundColor = titleBarConfig.backgroundColor(); } titleBarConfig = (0, _assign["default"])({}, this.basePageConfig, titleBarConfig); // 记录当前设置的titleBarConfig window.TITLEBARCONFIG = titleBarConfig; // 存储当前设置的titleBarConfig 后续二级弹框关闭需要恢复 window.STORECURRENTTITLEBARCONFIG = titleBarConfig; // 设置titleBar BrowserApi.setTitleBarTheme(titleBarConfig); }, // 是否是仅是入口页 isPageNoRouter: function isPageNoRouter(that) { var _a, _b, _c, _d; that = that || this; var flag = true; // 单页 if (((_b = (_a = that.$router) === null || _a === void 0 ? void 0 : _a.options) === null || _b === void 0 ? void 0 : _b.routes) && 0 < ((_d = (_c = that.$router) === null || _c === void 0 ? void 0 : _c.options) === null || _d === void 0 ? void 0 : _d.routes.length)) { flag = false; } return flag; }, // 多页面设置titleBar入口 setPageTitleBar: function setPageTitleBar(that) { console.log("baseApp_setPageTitleBar ------>", that); var title = that.$options.data.title || ''; if (title) { this.setTitle(title); } var titleBarConfig = that.$options.data.titleBarConfig || {}; if (titleBarConfig && typeof titleBarConfig.backgroundColor === 'function') { titleBarConfig.backgroundColor = titleBarConfig.backgroundColor(); } this.setTitleBar(titleBarConfig); }, // 根据hash获取当前路由route的mate配置 getRoutesMeta: function getRoutesMeta(that) { var routerPath = window.location.hash.split('#')[1].split('?')[0]; var matchPath = "currentRouter_" + routerPath; var routesList = that.$router.options.routes || []; var routerConfig = this.getRouterTitleConfig(routesList, matchPath); return routerConfig || {}; }, // 从页面router配置中获取当前页面的定制title信息 getRouterTitleConfig: function getRouterTitleConfig(routesList, matchPath) { var routerConfig = { hasExit: false }; for (var i = 0; i < routesList.length; i++) { var item = routesList[i]; item.path = item.path[0] === "/" ? item.path : "/" + item.path; if ("currentRouter_" + item.path === matchPath) { routerConfig.title = (item.meta || {}).title; routerConfig.titleBarConfig = (item.meta || {}).titleBarConfig; routerConfig.hasExit = true; break; } else { if (item.children) { routerConfig = this.getRouterTitleConfig(item.children, matchPath); if (routerConfig.hasExit) { break; } } } } return routerConfig; }, // 二级弹框恢复默认 restoreDefaultTitleBar: function restoreDefaultTitleBar(titleBarConfig) { if (titleBarConfig === void 0) { titleBarConfig = {}; } titleBarConfig = (0, _assign["default"])({}, this.basePageConfig, titleBarConfig); BrowserApi.setTitleBarTheme(titleBarConfig); }, // 关闭二级弹框恢复当前 首页为全屏时并且可滚动 restoreCurrentTitleBar: function restoreCurrentTitleBar(titleBarConfig, isScroll) { if (titleBarConfig === void 0) { titleBarConfig = {}; } if (isScroll === void 0) { isScroll = true; } if (isScroll) { titleBarConfig = (0, _assign["default"])({}, window.STORECURRENTTITLEBARCONFIG, titleBarConfig); } else { titleBarConfig = (0, _assign["default"])({}, window.TITLEBARCONFIG, titleBarConfig); } BrowserApi.setTitleBarTheme(titleBarConfig); }, /** * 获取浏览器事件名称 */ getEventName: function getEventName(type) { var path = this.$route.path ? this.$route.path : '/'; return this.$route.name + "_" + path + "_" + type; }, /** * 注册APP回退事件 */ registerBack: function registerBack() { var _this = this; BrowserApi.registeAppBackEvent(function (data) { //页面有弹框,先关闭弹框 if (baseUtils_1.hasModalConfirm()) { // 关闭弹框 _this.$modal.close(); return; } // 非单页 不处理 自己业务代码自己处理 if (_this.isPageNoRouter()) { _this.onBack(); return; } // 单页事件广播 _this.$EventBus.$emit(_this.getEventName(EventType_1["default"].NOTIFYAPPBACK), data); }); }, /** * 注册APP刷新事件 */ registerRefresh: function registerRefresh() { var _this = this; //注册APP刷新事件 BrowserApi.registPageRefreshEvent(function (data) { // 非单页 不处理 自己业务代码自己处理 if (_this.isPageNoRouter()) { _this.onRefreshPrev(); return; } // 单页事件广播 _this.$EventBus.$emit(_this.getEventName(EventType_1["default"].REFRESHPAGE), data); }); }, /** * 注册app切到后台 */ registerAppPause: function registerAppPause() { var _this = this; BrowserApi.registerHandler("onAppPause", function (data) { // 非单页 不处理 自己业务代码自己处理 if (_this.isPageNoRouter()) { _this.onAppPause(); return; } // 单页事件广播 _this.$EventBus.$emit(_this.getEventName(EventType_1["default"].APPPAUSE), data); }); }, /** * 注册app切到前台 */ registerAppResume: function registerAppResume() { var _this = this; BrowserApi.registerHandler("onAppResume", function (data) { // 非单页 不处理 自己业务代码自己处理 if (_this.isPageNoRouter()) { _this.onAppResume(); return; } // 单页事件广播 _this.$EventBus.$emit(_this.getEventName(EventType_1["default"].APPRESUME), data); }); }, /** * @description: * @param {*} */ registerSwitchCompany: function registerSwitchCompany() { var _this = this; BrowserApi.registerHandler("onSwitchCompany", function (data) { // 清除业务缓存数据 NativeSupportApi.clearStoreData(); // 非单页 不处理 自己业务代码自己处理 if (_this.isPageNoRouter()) { _this.onSwitchCompany(data); return; } // 单页事件广播 _this.$EventBus.$emit(_this.getEventName(EventType_1["default"].SWITCHCOMPANY), data); }); }, /** * 注册app菜单监听事件,子类没有重写 getMenuList() 方法的不额外加 menu 菜单项 */ registerBrowserMenuList: function registerBrowserMenuList() { BrowserApi.registerMenu(this.getMenuList()); }, /** * 页面回退处理 * @abstract */ onBack: function onBack() { this.goBack(); }, /** * @param * url:目标页面的url, 为''时关闭当前页 * backSteps:当前页面回退步数 * loadData:传递给目标页面的数据 */ goBack: function goBack(url, backSteps, loadData) { if (url === void 0) { url = ''; } BrowserApi.goBack(url, backSteps, loadData); }, // APP切换后台 onAppPause: function onAppPause() {}, // APP切换前台 onAppResume: function onAppResume() {}, // 切换企业 onSwitchCompany: function onSwitchCompany() {}, /** * @description: 需要重薪设置titlebar数据 */ onRefreshPrev: function onRefreshPrev() { try { var titleInfo = BrowserApi.getCurrentTitleInfo(); this.setTitle(titleInfo); var menuList = BrowserApi.getCurrentMenuList(); BrowserApi.registerMenu(menuList); var titleBarTheme = BrowserApi.getCurrentTitleBarTheme(); BrowserApi.setTitleBarTheme(titleBarTheme); } catch (err) { console.log("onRefreshPrev @@Error", err); } this.onRefresh(); }, /** * 子页面刷新逻辑处理,子类可以实现该方法 默认页面回退设置title和menu * @abstract */ onRefresh: function onRefresh() {}, /** * 设置页面title */ setTitle: function setTitle(data) { var titleInfo = {}; if ((0, _typeof2["default"])(data) !== "object") { titleInfo.title = data; } else { titleInfo = data; } BrowserApi.setTitle(titleInfo); }, // 设置注册按钮 getMenuList: function getMenuList() { return []; } }, watch: { /** * 设置进入、回退页面动画 */ $route: function $route(val) { var _a; var isBack = this.$router.isBack; if (isBack) { this.transitionName = 'slide-right'; } else { if (((_a = val === null || val === void 0 ? void 0 : val.meta) === null || _a === void 0 ? void 0 : _a.entry) && !this.$router.hasAnimate) { this.transitionName = ""; this.$router.hasAnimate = false; } else { this.transitionName = 'slide-left'; } } this.$router.isBack = false; } } }; exports["default"] = BaseApp;