app-xiangsonghua/app-saas-src/script/index.js
2024-12-26 17:00:06 +08:00

420 lines
12 KiB
JavaScript
Raw Permalink 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.

apiready = function () {
app = {
init: {},
bind: {},
initUser: {},
initDB: {},
jump: {}, // 跳转
getMessages: {},
listenMessage: {},
unListenMessage: {},
getPowerRooms: {},
getPowerRoomsByEmployee: {},// 员工登陆,获取配电室列表
getAlarmList: {}
}
app.init = function () {
this.bind();
// 初始化数据库
this.initDB(this.jump);
}
// 事件绑定
app.bind = function () {
//应用回到前台之后 获取消息
api.addEventListener({
name: 'resume'
}, function (ret, err) {
if ($api.getStorage("userId")) {
app.getMessages();
}
});
// 收到登录成功的消息 初始化客户
api.addEventListener({
name: 'logined'
}, function (ret, err) {
$api.disabledRequest = false; // 取消禁止调用接口标志
// edit start 2023/2/27
if ($api.getStorage("userType") == 2) {
app.getPowerRoomsByEmployee();
app.getAlarmList();
return
}
// edit end 2023/2/27
app.initUser();
});
// 收到登录失败的消息 1、清空客户消息 2、取消监听推送 3、重置badge(ios有效)
api.addEventListener({
name: 'loginout'
}, function (ret, err) {
if (rtAlarmTimer) {
clearInterval(rtAlarmTimer);
}
// add start 2019/4/29 kangzhi
// 在清除所有的storage信息时若选择了记住密码则重新存储账号密码
var remember = $api.getStorage('remember');
var account = $api.getStorage('account');
var password = $api.getStorage('password');
// add end 2019/4/29 kangzhi
$api.clearStorage();
// add start 2019/4/29 kangzhi
if (remember) {
$api.setStorage('remember', remember);
$api.setStorage("account", account);
$api.setStorage("password", password);
}
// add end 2019/4/29 kangzhi
app.unListenMessage();
api.setAppIconBadge({
badge: 0
});
});
api.addEventListener({
name: 'removeMessage',
}, function (ret, err) {
if (ret) {
var messageId = ret.value.messageId;
if (messageId) {
messageDao.deleteOne(messageId);
}
}
});
// add by guoyuanyuan 2018/9/25
// 监听客户改变的消息(项目改变)
api.addEventListener({
name: 'projectChanged'
}, function (ret, err) {
app.getPowerRooms();
app.getAlarmList();
});
// add s 2023/7/20
// 员工登陆时,角色切换
// api.addEventListener({
// name: 'roleChanged'
// }, function (ret, err) {
// app.getPowerRooms();
// app.getAlarmList();
// });
}
//初始化用户
app.initUser = function () {
messageDao.initTable(app.getMessages);
app.listenMessage();
app.getPowerRooms();
app.getAlarmList();
}
//初始化数据库
app.initDB = function (callback) {
messageDao.initTable(function (ret, err) {
if (!err) {
// 创建message表
userMaxMessageIdDao.initTable(function (ret, err) {
messageDao.getMaxIds(function (list) {
for (var i = 0; i < list.length; i++) {
userMaxMessageIdDao.updateUserMessageId(list[i].user_id, list[i].msg_id, function (ret, err) {
});
}
setTimeout(function () {
if (callback) {
callback();
}
}, 100);
})
});
}
});
}
// 初始化完成之后 跳转
app.jump = function () {
//标识已经登陆过,直接跳转到登陆后的首页
var useId = $api.getStorage('userId');
if ($api.getStorage("token") && !$api.disabledRequest && useId) {
app.getAlarmList();
var edge = api.winWidth * 0.2;
(edge < 0) && (edge = 60);
api.openDrawerLayout({
name: 'index',
url: 'widget://html/index/index.html',
leftPane: {
edge: edge,
name: 'index.leftPanel',
url: 'widget://html/index/leftPanel.html'
},
reload: true,
slidBackEnabled: false,
animation: {
type: "fade",
duration: 300
}
});
} else {
//没有登陆的话,跳转到登陆页面
api.openWin({
name: 'login',
url: 'widget://html/user/login.html',
animation: {
type: 'fade'
}
});
}
}
//从数据库上获取消息并保存到本地
app.getMessages = function () {
messageDao.updateData();
}
//监听消息推送
app.listenMessage = function () {
var ajpush = api.require('ajpush');
//如果是android 初始化jpush
if (api.systemType == "android") {
ajpush.setAuth({
auth:true
},function(ret) {
if (ret && ret.status){
//success
console.log(ret.status, 'status')
ajpush.init(function (ret) {
if (ret && ret.status) {
console.log(ret.status, 'ret.status')
ajpush.getRegistrationId(function (ret) {
var registrationId = ret.id;
});
listen();
}
});
}
});
} else {
listen();
}
function listen() {
var userId = $api.getStorage("userId");
var alias = 'CUSTOMER_' + userId;
var tags = ['CUSTOMER', alias];
var param = {
alias: alias,
// tags: tags
};
ajpush.resumePush(function (ret) {
if (ret && ret.status) {
var count = 0
var bindInterval = setInterval(function () {
ajpush.bindAliasAndTags(param, function (ret, err) {
count++;
ajpush.setListener(function (ret) {
messageDao.updateData();
});
if (ret && ret.statusCode != 6002) {
clearInterval(bindInterval)
}
if (count > 3) {
clearInterval(bindInterval)
}
});
}, 5000)
}
});
// ajpush.resumePush(function (ret) {
// if (ret && ret.status) {
// ajpush.bindAliasAndTags(param, function (ret, err) {
// ajpush.setListener(function (ret) {
// messageDao.updateData();
// });
// });
// }
// });
}
}
//取消监听消息推送
app.unListenMessage = function () {
var ajpush = api.require('ajpush');
ajpush.removeListener();
ajpush.stopPush(function (ret) {
if (ret && ret.status) {
//success
// console.log('out123')
}
});
}
//获取配电室列表
app.getPowerRooms = function () {
var url = "/ems/rest/power/room/list";
var data = {
"cusId": $api.getStorage("cusId")
}
$api.get(url, data, callback);
function callback(ret, err) {
if (!err) {
// 先更新体验客户名
var guestPrName = $api.guestPrName;
//判断是否是体验账号,如果是体验账号,则对云端返回的数据进行拦截
var cus = $api.getStorage('cus');
var powerRooms = [];
var list = ret.body;
var length = list.length;
for (var i = 0; i < length; i++) {
if (cus) {
list[i].prName = guestPrName;
}
powerRooms.push({
"prId": list[i].prId,
"prName": list[i].prName,
"configId": list[i].configId,
})
}
if (length > 0) {
$api.setStorage("prList", powerRooms);
$api.setStorage("powerRoom", powerRooms[0]);
}
}
}
}
// 员工登陆 获取配电室列表
//获取配电室列表
app.getPowerRoomsByEmployee = function () {
var url = "/ems/rest/power/room/page";
var data = {
"employeeId": $api.getStorage("employeeId"),
"roleId": $api.getStorage("roleId"),
"page": 1,
"pageSize": 9999
}
$api.get(url, data, callback);
function callback(ret, err) {
if (!err) {
// 先更新体验客户名
var guestPrName = $api.guestPrName;
//判断是否是体验账号,如果是体验账号,则对云端返回的数据进行拦截
var cus = $api.getStorage('cus');
var powerRooms = [];
var list = ret.body.records;
var length = list.length;
for (var i = 0; i < length; i++) {
if (cus) {
list[i].prName = guestPrName;
}
powerRooms.push({
"prId": list[i].prId,
"prName": list[i].prName,
"configId": list[i].configId,
})
}
if (length > 0) {
$api.setStorage("prList", powerRooms);
$api.setStorage("powerRoom", powerRooms[0]);
}
}
}
}
//获取实时报警列表
var rtAlarmTimer;
app.getAlarmList = function () {
get();
if (rtAlarmTimer) {
clearInterval(rtAlarmTimer);
}
rtAlarmTimer = window.setInterval(function () {
get();
}, 10000);
//避免重复监听
api.removeEventListener({
name: 'requestRtAlarm'
});
api.addEventListener({
name: 'requestRtAlarm'
}, function (ret, err) {
get();
});
function get() {
var url = '/ems/rest/alarm/page';
var data = {
"page": 1,
"pageSize": 999999,
"cusId": $api.getStorage('cusId'),
"alarmStatus": 1 // 实时报警
};
// add s 2023/7/10
if ($api.getStorage("userType") == 2) { // 员工
data = {
"employeeId": $api.getStorage("employeeId"),
"roleId": $api.getStorage("roleId"),
"page": 1,
"pageSize": 999999,
"alarmStatus": 1 // 实时报警
}
}
// add e 2023/7/10
$api.get(url, data, function (res, err) {
if (res && res.code == 200) {
$api.setStorage('rtAlarmList', res.body.records);
//发出登录成功的消息
api.sendEvent({
name: 'rtAlarmListUpdated'
});
}
});
}
}
app.init();
}