app-xiangsonghua/app-saas-src/script/index/index.js

1008 lines
31 KiB
JavaScript
Raw Normal View History

2024-12-26 17:00:06 +08:00
apiready = function () {
var ctrl = {
frames: [],
userId: null,
messageSocket: null, //用来获取产生新消息的消息
newsSocket: null, // 用来获取产生新闻
messageTimer: null, //messageSocket 与服务端发送心跳
newsTimer: null,
init: {},
initWebsocket: {}, //初始化websocket
closeWebsocket: {}, //关闭websocket
bind: {},
initBadge: {},
switchFrame: {},
frameX: 0,
frameY: 0,
frameWidth: 0,
frameHeight: 0,
defaultNavItemCallback: {},
featureFrameCallBack: {},
checkToUpdate: {}, //查询已跟新
checkHasUpdated: {}, //查出是否已经跟新
// address: 'api.dianwutong.com'
address: 'wss://cdyapi.saas.dianwutong.com'
}
//初始化页面
ctrl.init = function () {
// 解决iphonex底部标签栏被虚拟home键遮挡
$api.fixTabBar($api.byId('footer'));
// 适配状态栏
var statusBarHeightDom = document.getElementById('statusBarHeight');
if (api.systemType == "android") {
// var statusBar = api.require('statusBar');
// var statusBarHeight = statusBar.getStatusBarHeight();
// statusBarHeightDom.style.height = statusBarHeight + 'px';
statusBarHeightDom.style.height = api.safeArea.top + 'px';
} else {
statusBarHeightDom.style.height = '1.67rem';
}
ctrl.initPorjectPicker();
ctrl.checkToUpdate();
ctrl.checkHasUpdated();
//获取token; 如果token不存存在跳转登录页
var token = $api.getStorage("token");
if (!token) {
api.openWin({
name: 'login',
url: 'widget://html/user/login.html'
});
api.closeWin({});
return false;
}
ctrl.dialogBox = api.require('dialogBox');
//初始化底部badge
ctrl.initBadge();
// privilegeModule.getMenuList(function() {
// ctrl.initFrame();
// });
ctrl.frames["messageFrame"] = {
"title": "消息",
"url": "widget://html/index/messageFrame.html",
"normalIcon": "../../image/message-normal.svg",
"activeIcon": "../../image/message-active.svg",
"navItem": $api.dom(".nav-item[frame='messageFrame']"),
"callback": ctrl.messageNavItemCallback
};
ctrl.frames["rtAlarmFrame"] = {
"title": "实时报警",
"url": "widget://html/index/rtAlarmFrame.html",
"normalIcon": "../../image/rtAlarm-normal.svg",
"activeIcon": "../../image/rtAlarm-active.svg",
"navItem": $api.dom(".nav-item[frame='rtAlarmFrame']"),
"callback": ctrl.defaultNavItemCallback
}
ctrl.frames["featureFrame"] = {
"title": "功能",
"url": "widget://html/index/featureFrame.html",
"normalIcon": "../../image/feature-normal.svg",
"activeIcon": "../../image/feature-active.svg",
"navItem": $api.dom(".nav-item[frame='featureFrame']"),
"callback": ctrl.featureFrameCallBack
};
ctrl.frames["contactFrame"] = {
"title": "一键直通",
"url": "widget://html/index/contactFrame.html",
"normalIcon": "../../image/contact-normal.svg",
"activeIcon": "../../image/contact-active.svg",
"navItem": $api.dom(".nav-item[frame='contactFrame']"),
"callback": ctrl.defaultNavItemCallback
};
ctrl.frames["inspectionFrame"] = {
"title": "巡检",
"url": "widget://html/index/inspectionFrame.html",
"normalIcon": "../../image/inspection-normal.svg",
"activeIcon": "../../image/inspection-active.svg",
"navItem": $api.dom(".nav-item[frame='inspectionFrame']"),
"callback": ctrl.defaultNavItemCallback
};
ctrl.frames["systemOrderFrame"] = {
"title": "工单",
"url": "widget://html/index/systemOrderFrame.html",
"normalIcon": "../../image/repair-normal.svg",
"activeIcon": "../../image/repair-active.svg",
"navItem": $api.dom(".nav-item[frame='systemOrderFrame']"),
"callback": ctrl.defaultNavItemCallback
};
ctrl.bind();
// //计算frame显示的位置和尺寸
var windowWidth = parseInt($api.cssVal($api.dom("body"), "width"));
var windowHeight = parseInt($api.cssVal($api.dom("body"), "height"));
var headerHeight = parseInt($api.cssVal($api.dom(".index-header"), "height"));
var footerHeight = parseInt($api.cssVal($api.dom("footer"), "height"));
var footerPaddingBottom = parseInt($api.cssVal($api.dom("footer"), "padding-bottom"));
this.frameX = 0;
this.frameY = headerHeight;
this.frameWidth = windowWidth;
this.frameHeight = windowHeight - headerHeight - footerHeight - footerPaddingBottom;
// add start
// 员工账号登录所有底部Tab菜单全部可见客户登陆时隐藏底部巡检、工单Tab
var isEmployee = $api.getStorage("isEmployee")
if (isEmployee) {
// 隐藏底部全部tab
// $api.css($api.domAll('.nav-item'), 'display:none');
// $api.domAll('.nav-item').forEach(function (el, index) {
// $api.css(el, 'display:none');
// })
var tabArr = []
$api.css($api.dom('#inspectionFrame'), 'display:block')
tabArr.push('inspectionFrame')
$api.css($api.dom('#systemOrderFrame'), 'display:block')
tabArr.push('systemOrderFrame')
// 隐藏“消息”和“一键直通”tab
// $api.css($api.dom('#messageFrame'), 'display:none')
// $api.css($api.dom('#contactFrame'), 'display:none')
ctrl.switchFrame('rtAlarmFrame');
return
}
privilegeModule.getMenuList(function () {
ctrl.initFrame();
});
ctrl.switchFrame("messageFrame");
ctrl.initWebsocket();
}
// 初始化frame
ctrl.initFrame = function () {
if (!privilegeModule.hasMenu('实时报警')) {
$api.css($api.dom('#rtAlarmFrame'), 'display:none');
}
if (!privilegeModule.hasMenu('功能')) {
$api.css($api.dom('#featureFrame'), 'display:none');
}
if (!privilegeModule.hasMenu('一键直通')) {
$api.css($api.dom('#contactFrame'), 'display:none');
}
$api.css($api.dom('#inspectionFrame'), 'display:none');
$api.css($api.dom('#systemOrderFrame'), 'display:none');
}
// 判断是否为园区客户(显示客户即项目下拉选项)
ctrl.initPorjectPicker = function () {
var customers = $api.getStorage('customers');
var projectPicker = $api.dom('#project-picker');
if (!customers || customers.length <= 1) {
$api.text(projectPicker, '');
} else {
$api.text(projectPicker, '项目');
};
}
//弹出框设置
ctrl.initAlertContent = function (options) {
var options = options || {};
var content = {
texts: {
title: options.title || '提示',
content: options.content || '您将退出此次登陆,是否确定?',
leftBtnTitle: '确认',
rightBtnTitle: '取消',
},
styles: {
bg: '#fff',
corner: 6,
w: 300,
title: {
marginT: 20,
titleSize: 18,
titleColor: '#000'
},
content: {
marginT: 10,
color: '#000',
size: 14
},
left: {
marginB: 7,
marginL: 20,
w: 130,
h: 35,
corner: 2,
bg: 'white',
size: 18,
},
right: {
marginB: 7,
marginL: 10,
w: 130,
h: 35,
corner: 2,
bg: 'white',
size: 18
}
}
}
return content;
}
//初始化websocket
ctrl.initWebsocket = function () {
if (this.messageSocket) {
return false;
}
if (this.newsSocket) {
return false;
}
// var roleId = $api.getStorage('roleId');
var userId = $api.getStorage('userId');
var url = ctrl.address + '/ems/app/msg_socket/' + userId;
var newsUrl = ctrl.address + '/admin/app/msg_socket/' + userId;
this.messageSocket = new WebSocket(url);
this.newsSocket = new WebSocket(newsUrl);
this.messageSocket.onmessage = function (data) {
messageDao.updateData();
}
this.messageSocket.onclose = function () {
ctrl.closeWebsocket();
// api.toast({
// msg: "消息socket连接失败"
// });
}
this.newsSocket.onclose = function () {
ctrl.closeWebsocket();
}
this.newsSocket.onmessage = function (data) {
messageDao.updateData();
}
this.messageTimer = setInterval(function () {
if (ctrl.messageSocket) {
//发送心跳 避免socket长时间没有消息传递而断开
ctrl.messageSocket.send('beat');
}
}, 30000);
this.newsTimer = setInterval(function () {
if (ctrl.newsSocket) {
//发送心跳 避免socket长时间没有消息传递而断开
ctrl.newsSocket.send('beat');
}
}, 30000);
}
//关闭websocket
ctrl.closeWebsocket = function () {
if (this.messageTimer) {
clearInterval(this.messageTimer);
}
if (this.newsTimer) {
window.clearInterval(this.newsTimer);
}
if (this.messageSocket) {
this.messageSocket.close();
this.messageSocket = null;
}
if (this.newsSocket) {
this.newsSocket.close();
this.newsSocket = null;
}
}
//退出登录
ctrl.logout = function () {
// 在清除所有的storage信息时若选择了记住密码则重新存储账号密码
var remember = $api.getStorage('remember');
var account = $api.getStorage('account');
var password = $api.getStorage('password');
$api.clearStorage();
if (remember) {
$api.setStorage('remember', remember);
$api.setStorage("account", account);
$api.setStorage("password", password);
}
api.sendEvent({
name: 'loginout'
});
api.removeEventListener({
name: 'leftPanelClicklogout'
});
api.removeEventListener({
name: 'changePwdTip'
});
api.removeEventListener({
name: 'leftPanelClickClear'
});
api.closeWin({
animation: {
type: "fade"
}
});
api.openWin({
name: 'login',
url: 'widget://html/user/login.html',
});
}
//事件绑定
ctrl.bind = function () {
//修改密码提示
var timer;
api.addEventListener({
name: 'changePwdTip'
}, function () {
if (timer) {
window.clearTimeout(timer);
}
var timer = window.setTimeout(function () {
ctrl.dialogBox.alert({
texts: {
content: '体验账号没有修改密码的权限',
leftBtnTitle: '确定',
},
styles: {
bg: '#fff',
w: 300,
corner: 6,
content: {
color: '#000',
size: 14
},
left: {
marginB: 7,
marginL: 0,
w: 300,
h: 30,
corner: 2,
bg: '#fff',
size: 18
},
}
}, function (ret) {
if (ret.eventType == 'left') {
ctrl.dialogBox.close({
dialogName: 'alert'
});
}
});
}, 100)
})
//避免重复监听
api.removeEventListener({
name: 'leftPanelClicklogout'
});
//退出登录
var alertContent = ctrl.initAlertContent();
api.addEventListener({
name: 'leftPanelClicklogout'
}, function (ret, err) {
if (timer) {
window.clearTimeout(timer);
}
var timer = window.setTimeout(function () {
ctrl.dialogBox.alert(alertContent, function (ret) {
if (ret.eventType == 'right') {
ctrl.dialogBox.close({
dialogName: 'alert'
});
} else if (ret.eventType == 'left') {
ctrl.dialogBox.close({
dialogName: 'alert'
});
ctrl.logout();
}
});
}, 100)
})
//清除缓存
api.addEventListener({
name: 'leftPanelClickClear'
}, function (ret, err) {
if (timer) {
window.clearTimeout(timer);
}
window.setTimeout(function () {
var options = {};
options.title = "清空缓存";
options.content = "该操作将清空图片缓存,消息等数据,是否清空?"
var alertContent = ctrl.initAlertContent(options);
ctrl.dialogBox.alert(alertContent, function (ret) {
if (ret.eventType == 'right') {
ctrl.dialogBox.close({
dialogName: 'alert'
});
} else if (ret.eventType == 'left') {
api.clearCache(function () {
messageDao.clearAll(function () {
api.execScript({
name: 'index',
script: 'api.toast({msg:"成功清空缓存"});'
});
var dialogBox = api.require('dialogBox');
ctrl.dialogBox.close({
dialogName: 'alert'
});
})
});
}
});
}, 100)
})
// 应用从后台回到前台
api.addEventListener({
name: 'resume'
}, function (ret, err) {
ctrl.initWebsocket();
if (!/has_remind_update/.test(document.cookie)) {
ctrl.checkToUpdate();
}
});
//应用到后台
api.addEventListener({
name: 'pause'
}, function (ret, err) {
ctrl.closeWebsocket();
});
//绑定底部导航
var navItems = $api.domAll(".nav-item");
var length = navItems.length;
for (var i = 0; i < length; i++) {
(function (index) {
$api.addEvt(navItems[index], "touchend", function () {
// 切换底部nav时隐藏掉modal(项目选择modal和配电室选择modal)
api.sendEvent({
name: 'requestCloseModal'
});
var frameName = $api.attr(this, "frame");
var frame = ctrl.frames[frameName];
if (typeof (frame.callback) == "function") {
frame.callback(this);
}
}, false);
})(i);
}
//绑定用户按钮 点击弹出左侧的抽屉页
$api.addEvt($api.dom("#user"), "touchend", function () {
//发送点击的事件提醒隐藏配电室;列表
api.sendEvent({
name: 'userTouched',
});
// $api.css($api.dom("#pr-picker-modal"), "display:none");
api.openDrawerPane({
type: 'left'
});
}, false);
//阻止android的返回事件
api.addEventListener({
name: 'keyback'
}, function (ret, err) {
api.closeWidget({});
return false;
});
//阻止ios的返回事件
api.addEventListener({
name: 'swiperight'
}, function (ret, err) {
return false;
});
//如果二级页面设置了横屏显示 返回时变成横屏
api.addEventListener({
name: 'viewappear'
}, function (ret, err) {
});
//切换配电室
$api.addEvt($api.dom("#pr-name"), "touchend", function () {
api.sendEvent({
name: 'requestChangePr'
});
}, false);
//收到配电室改变的消息
api.addEventListener({
name: 'prChanged'
}, function (ret, err) {
if (!err) {
var prName = ret.value.prName;
var length = prName.length;
if (length > 10) {
prName = prName.slice(0, 10) + "...";
}
$api.text($api.dom("#pr-name"), prName);
}
});
//收到消息改变的事件
api.addEventListener({
name: 'messageUpdated'
}, function (ret, err) {
ctrl.initBadge();
});
//收到报警改变的事件
api.addEventListener({
name: 'rtAlarmListUpdated'
}, function (ret, err) {
ctrl.initBadge();
});
// add by guoyuanyuan 2018/9/25
// 对于园区客户的董事长,项目经理角色,增加项目切换
$api.addEvt($api.dom("#project-picker"), "touchend", function () {
api.sendEvent({
name: 'requestChangeProject'
});
}, false);
}
//初始化导航栏底部badge
//如果是ios 初始化桌面的badge
ctrl.initBadge = function () {
//消息badge
var ajpush = api.require('ajpush');
messageDao.getAllUnReadCount(function (sum) {
if (sum == 0) {
$api.css($api.dom("#message-badge"), "display:none");
ajpush.setBadge({
badge: 0
});
return false;
}
if (sum > 99) {
sum = "99+";
}
$api.css($api.dom("#message-badge"), "display:block");
$api.text($api.dom("#message-badge"), sum);
ajpush.setBadge({
badge: sum
});
})
// //实时报警数量
var rtAlarmList = $api.getStorage('rtAlarmList');
rtAlarmList = (rtAlarmList ? rtAlarmList : []);
var alarmCount = rtAlarmList.length;
if (alarmCount == 0) {
$api.css($api.dom("#rtAlarm-badge"), "display:none");
} else {
$api.css($api.dom("#rtAlarm-badge"), "display:block");
}
}
// add by guoyuanyuan 2018/9/25
// 点击消息nav
ctrl.messageNavItemCallback = function (obj) {
ctrl.defaultNavItemCallback(obj);
// 在消息页显示项目选择提示
ctrl.initPorjectPicker();
}
//点击底部导航的默认回调函数
ctrl.defaultNavItemCallback = function (obj) {
//隐藏掉pr-name
//显示title
$api.css($api.dom("#title"), "display:block");
$api.css($api.dom("#pr-name"), "display:none");
// add by guoyuanyuan 2018/9/25
// 在其他页面不显示项目提示
var projectPicker = $api.dom('#project-picker');
$api.text(projectPicker, '');
//切换frame
var frameName = $api.attr(obj, "frame");
ctrl.switchFrame(frameName);
}
//点击底部导航的功能按钮的回调函数
ctrl.featureFrameCallBack = function (obj) {
// add by guoyuanyuan 2018/9/25
// 在其他页面不显示项目提示
var projectPicker = $api.dom('#project-picker');
$api.text(projectPicker, '');
//显示pr-name
//隐藏title
$api.css($api.dom("#title"), "display:none");
$api.css($api.dom("#pr-name"), "display:flex");
var powerRoom = $api.getStorage("powerRoom");
_log(powerRoom);
//如果没用选中配电室
if (!powerRoom) {
api.sendEvent({
name: 'requestChangePr'
});
} else {
var prName = powerRoom.prName;
var length = prName.length;
if (length > 10) {
prName = prName.slice(0, 10) + "...";
}
$api.text($api.dom("#pr-name"), prName);
}
//切换frame;
var frameName = $api.attr(obj, "frame");
ctrl.switchFrame(frameName);
}
//切换frame
ctrl.switchFrame = function (frameName) {
$api.setStorage('activeNav', frameName)
var _this = ctrl;
var frame = _this.frames[frameName];
//显示对应的标题
$api.text($api.dom("#title"), frame.title);
//重置底部导航的选中状态
var frames = _this.frames;
var length = frames.length;
var navItems = $api.domAll(".nav-item");
var showFrame = "$api.css($api.dom('body'), 'display:block');";
var hideFrame = "$api.css($api.dom('body'), 'display:none');";
for (var name in frames) {
if (frame == frames[name]) {
$api.addCls(frames[name].navItem, "active");
$api.attr($api.dom(frames[name].navItem, "img"), "src", frames[name].activeIcon);
api.execScript({
frameName: name,
script: showFrame
});
} else {
$api.removeCls(frames[name].navItem, "active");
$api.attr($api.dom(frames[name].navItem, "img"), "src", frames[name].normalIcon);
api.execScript({
frameName: name,
script: hideFrame
});
}
}
//切换frame
api.openFrame({
name: frameName,
url: frame.url,
rect: {
x: _this.frameX,
y: _this.frameY,
w: _this.frameWidth,
h: _this.frameHeight
}
});
}
//比较版本号
ctrl.cmpVersion = function (thisVersion, otherVersion) {
var _this = thisVersion.split('.');
var _other = otherVersion.split('.');
for (var i = 0; i < 3; i++) {
var _t = parseInt(_this[i]);
var _o = parseInt(_other[i]);
if (_t > _o) {
return 1;
} else if (_t < _o) {
return -1;
}
}
return 0;
}
//版本更新
ctrl.checkToUpdate = function () {
var thisVersion = api.readFile({
sync: true,
path: 'widget://resource/version.txt'
});
$api.getVersion(function (ret) {
if (ret) {
//如果当前版本低于服务器的最低版本 强制更新
if (ctrl.cmpVersion(thisVersion, ret.minSupport) < 0) {
api.alert({
title: '版本更新',
msg: '版本太低,强烈建议更新',
buttons: ['确定']
}, function (ret, err) {
update(function () {
//关闭当前页 退出登录 跳转到登录页
api.openWin({
name: 'login',
url: 'widget://html/user/login.html',
reload: true,
animation: {
type: "fade"
}
});
api.sendEvent({
name: 'loginout'
});
api.closeWin({
animation: {
type: "fade"
}
});
});
});
} else if (ctrl.cmpVersion(thisVersion, ret.latestVersion) < 0) {
//如果当前版本低于服务器的最新版本 建议更新
if (dialogBox) {
return;
}
var dialogBox = api.require('dialogBox');
dialogBox.alert({
texts: {
title: '版本更新',
content: '发现新的版本,请更新',
leftBtnTitle: '确定',
rightBtnTitle: '取消',
},
styles: {
bg: '#fff',
w: 300,
corner: 6,
title: {
marginT: 20,
titleSize: 18,
titleColor: '#000'
},
content: {
marginT: 10,
color: '#000',
size: 14
},
left: {
marginB: 7,
marginL: 20,
w: 130,
h: 35,
corner: 2,
bg: '#fff',
size: 18
},
right: {
marginB: 7,
marginL: 10,
w: 130,
h: 35,
corner: 2,
bg: 'white',
size: 18
}
}
}, function (ret) {
if (ret.eventType == 'right') {
var dialogBox = api.require('dialogBox');
dialogBox.close({
dialogName: 'alert'
});
} else if (ret.eventType == 'left') {
update();
var dialogBox = api.require('dialogBox');
dialogBox.close({
dialogName: 'alert'
});
}
});
}
//如果版本号不一样 标记已经提示过
//一天之内不在提示
if (ctrl.cmpVersion(thisVersion, ret.latestVersion) != 0) {
var exdate = new Date()
exdate.setHours(exdate.getHours() + 24)
document.cookie = "has_remind_update=1;expires=" + exdate.toGMTString();
}
}
}, 'json');
function update(callback) {
var platform = api.systemType;
if (platform == "ios") {
api.openApp({
iosUrl: 'https://itunes.apple.com/us/app/%E7%94%B5%E5%8A%A1%E9%80%9Asaas/id1398709195?l=zh&ls=1&mt=8'
}, function (ret, err) {
//coding...
});
callback && callback();
} else if (platform == "android") {
api.showProgress({
title: '下载中',
text: '请稍等...',
modal: false
});
api.download({
url: "https://cdn.dianwutong.com/app/dianwutong_saas/app.apk",
cache: false,
allowResume: true
}, function (ret, err) {
api.hideProgress();
if (ret.state == 1) {
//下载成功
api.installApp({
appUri: ret.savePath
});
callback && callback();
} else {
api.alert({
title: '版本更新',
msg: '下载失败请到电务通官网扫码下载最新app',
buttons: ['确定']
}, function (ret, err) {
//coding...
callback && callback();
});
}
});
}
}
}
//检查是否更新
ctrl.checkHasUpdated = function () {
var lastVersion = $api.getStorage('lastVersion');
var currentVersion = api.readFile({
sync: true,
path: 'widget://resource/version.txt'
});
if (lastVersion && ctrl.cmpVersion(lastVersion, currentVersion) < 0) {
api.alert({
title: '提示',
msg: '版本更新成功,请重新登陆',
buttons: ['确定']
}, function (ret, err) {
api.sendEvent({
name: 'loginout'
});
setTimeout(function () {
api.closeWin({
animation: {
type: 'fade'
}
});
}, 500)
api.openWin({
name: 'login',
url: 'widget://html/user/login.html',
animation: {
type: 'fade'
}
});
})
} else {
$api.setStorage('lastVersion', currentVersion);
}
}
ctrl.init();
}