门户定制案例-接管页面标题 Title
运行效果
根据页面参数判断页面显示 title,以移动端门户判断流程审批页面显示固定名称“流程审批”为例
实现方法
- 定制企业门户应用,参考《门户定制》
- 云 ide 切换为“代码”,打开文件 \UI2\mobile\app.js,在 onLaunch 方法内新增接管逻辑
- 使用 JS 内置能力 Object.defineProperty 对文档标题进行自定义接管
接管页面标题
打开文件 \UI2\mobile\app.js 文件,在 onLaunch 方法进行自定义接管逻辑,示例代码如下:
//接管title设置
Object.defineProperty(document, 'title', {
set: function (newValue) {
//获取页面参数
const {sData1,process}=page.params||{};
//根据sdata1及process参数判断当前页面是否为审批页面
const isWfPage= !!sData1&&!!process;
if (newValue != ""||isWfPage) { // 这里写你的逻辑
document.getElementsByTagName("title")[0].innerHTML =isWfPage?"流程审批":newValue;
}
},
get: function () {
return document.getElementsByTagName("title")[0].innerHTML;
}
});
注意:移动端 app.js 文件修改完成之后,需要重启应用才能生效
完整代码
/UI2/mobile/app.js 文件的完整代码如下
import UserImplement from '$UI/wxsys/comps/uixContainer/UserImplement';
import history from "core/appHistory";
import ActionUtil from "$UI/mobile/js/actionUtil";
import ConfigUtil from "$UI/mobile/js/configUtil";
import store from 'store';
App({
async onLaunch({page}) {
try {
//接管title设置
Object.defineProperty(document, 'title', {
set: function (newValue) {
//获取页面参数
const {sData1,process}=page.params||{};
//根据sdata1及process参数判断当前页面是否为审批页面
const isWfPage= !!sData1&&!!process;
if (newValue != ""||isWfPage) { // 这里写你的逻辑
document.getElementsByTagName("title")[0].innerHTML =isWfPage?"流程审批":newValue;
}
},
get: function () {
return document.getElementsByTagName("title")[0].innerHTML;
}
});
} catch (e) {
//title自定义只能一次,多次会提示异常,不做处理
}
//设置上一次主题
ActionUtil.setCurrentThemeByLocal(page);
//iframe页面方法兼容处理
try{
ActionUtil.getMessage(page);
}catch(e){
console.error("iframe页面方法兼容处理异常",e);
}
//跳转url
let sourcePath = "";
//获取page参数进行跳转
const {page:pageUrl}=this.searches||{};
try {
let userInfo = await UserImplement.checkLogin(page);
//设置语言资源
await ActionUtil.setCurrentLanguage(page,userInfo);
//初始化岗位信息,默认主岗
let [orgData, menus,portalPublicConfig,portalConfig] = await Promise.all([
ActionUtil.getOrgData(userInfo),
ActionUtil.getCurrentMenus(page, userInfo),
ConfigUtil.getPortalPublicConfig(page),
ConfigUtil.getPortalConfig(page)
]);
let [defaultMenu,currentTheme] = await Promise.all([
//获取默认菜单
ActionUtil.getDefaultMenu(page,false,portalConfig),
//动态获取主题信息进行设置
ActionUtil.setCurrentTheme(page,userInfo)
]);
//通过菜单获取主要的跳转url
sourcePath=defaultMenu?.url||window.microService.mainPagePath;
//同步皮肤主题色到整个移动页面上
let {configContext}=page;
let primaryColor=configContext[configContext.currentTheme]?.token?.primaryColor;
if(primaryColor){
document.documentElement.style.setProperty('--primary-color', primaryColor);
}
//page参数跳转
if(pageUrl){
page.navigateTo({ url:pageUrl, params: { executor: ""} });
return;
}
history.router.configContext=configContext;
//登录成功后初始页面为空的情况跳转到指定默认页面
let {initUrl}=window.microService;
const getOriginUrl=(pageObj,url=pageObj.pagePath)=>{
const prefixPath=`/${pageObj.serviceName}/${pageObj.contextPath}`;
if(url.startsWith(prefixPath)){
return url.substr(prefixPath.length);
}
return url;
}
if(!initUrl&&(sourcePath!=page.pagePath)&&(sourcePath!=getOriginUrl(page))){
store.set("defaultUrl",sourcePath);
history.replace(sourcePath);
}
} catch (error) {
//设置多语言
await ActionUtil.setCurrentLanguage(page);
sourcePath = pageUrl||history.getLocation().pathname;
let loginUrl = "/entry/mobileapp/mobile/user/login";
if (sourcePath != loginUrl) {
history.replace(loginUrl+"?redirect="+sourcePath);
}
}
}
})