You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
348 lines
10 KiB
348 lines
10 KiB
const { loadModule, vueVersion } = window['vue2-sfc-loader'];
|
|
const options = {
|
|
moduleCache: {
|
|
vue: Vue,
|
|
myData: {
|
|
vueVersion,
|
|
}
|
|
},
|
|
async getFile(url) {
|
|
const res = await fetch(url);
|
|
if (!res.ok) {
|
|
throw Object.assign(new Error(url + ' ' + res.statusText), { res });
|
|
}
|
|
return await res.text();
|
|
},
|
|
addStyle(textContent) {
|
|
const style = Object.assign(document.createElement('style'), { textContent });
|
|
const ref = document.head.getElementsByTagName('style')[0] || null;
|
|
document.head.insertBefore(style, ref);
|
|
}
|
|
}
|
|
//eval解析vue组件
|
|
function parseModel(response) {
|
|
var html = new DOMParser().parseFromString(response.data, 'text/html');
|
|
var template = html.getElementsByTagName('template')[0];
|
|
var script = html.getElementsByTagName('script')[0].innerHTML;
|
|
script = '(' + script.replace(/^\s*export\s*default\s*/, '').replace(/;?\s*$/, '') + ')\n//# sourceURL=' + response.config.url;
|
|
var model = eval(script) || {};
|
|
if (html.getElementsByTagName('style').length > 0) {
|
|
var hash = response.config.url;
|
|
var style = html.getElementsByTagName('style')[0];
|
|
style.setAttribute('data-vue-style', hash);
|
|
var styleValue = style.outerHTML.replace('[component]', "[data-vue-style='" + hash + "']");
|
|
if ($("style[data-vue-style='" + hash + "']").length === 0) {
|
|
$('head').append(styleValue);
|
|
}
|
|
template.content.children[0].setAttribute('data-vue-style', hash);
|
|
model.template = template.outerHTML;
|
|
}
|
|
model.template = template;
|
|
return model;
|
|
}
|
|
//循环添加vue组件
|
|
function addVueComponents(cfg) {
|
|
if (window.location.search.indexOf('debug') > -1) {
|
|
addVueComponentsByEval(cfg);
|
|
}
|
|
else {
|
|
for (var i in cfg.list) {
|
|
(function () {
|
|
var item = cfg.list[i];
|
|
var ext = '.vue';
|
|
var url = cfg.prefix + item + ext;
|
|
var path = url.substring(config.path.length);
|
|
var name = path.substring(11, path.length - ext.length).replace(/\//g, "-");
|
|
Vue.component(name, function (resolve, reject) {
|
|
loadModule(url, options).then(function (model) {
|
|
resolve(model);
|
|
});
|
|
});
|
|
})();
|
|
}
|
|
}
|
|
}
|
|
//循环添加vue组件
|
|
function addVueComponentsByEval(cfg) {
|
|
for (var i in cfg.list) {
|
|
(function () {
|
|
var item = cfg.list[i];
|
|
var ext = '.vue';
|
|
var url = cfg.prefix + item + ext;
|
|
var path = url.substring(config.path.length);
|
|
var name = path.substring(11, path.length - ext.length).replace(/\//g, "-");
|
|
Vue.component(name, function (resolve, reject) {
|
|
axios.get(url, { headers: { 'Cache-Control': 'no-cache' } }).then(function (response) {
|
|
resolve(parseModel(response));
|
|
});
|
|
});
|
|
})();
|
|
}
|
|
}
|
|
//添加通用组件:
|
|
addVueComponents({
|
|
prefix: config.file("components/views/shared/"),
|
|
list: [
|
|
'layout',
|
|
'pagination',
|
|
'form'
|
|
]
|
|
});
|
|
|
|
//添加设备组件:
|
|
addVueComponents({
|
|
prefix: config.file("components/devices/"),
|
|
list: [
|
|
'gateway',
|
|
'humiture',
|
|
'light',
|
|
'infrared',
|
|
'smoke',
|
|
'switch1',
|
|
'socket',
|
|
'plug',
|
|
'curtain',
|
|
'door',
|
|
'lamp',
|
|
'camera',
|
|
'serialport',
|
|
'control',
|
|
'collector'
|
|
]
|
|
});
|
|
|
|
//util
|
|
function copy(from, to) {
|
|
for (var attr in to) {
|
|
if (from.hasOwnProperty(attr)) {
|
|
if (!(from[attr] instanceof Array)) {
|
|
to[attr] = from[attr];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
//获取设备数据
|
|
function getIoTData(device,name) {
|
|
var data = Enumerable.from(device.data).where(o => o.name === name).firstOrDefault();
|
|
if (data) {
|
|
return data['value'] + ' ' + data['unit'] + ' ' + data['description'];
|
|
}
|
|
return null;
|
|
}
|
|
Vue.prototype.getIoTData = getIoTData;
|
|
|
|
function getIoTDataValue(device, name) {
|
|
var data = Enumerable.from(device.data).where(o => o.name === name).firstOrDefault();
|
|
if (data) {
|
|
return data['value'];
|
|
}
|
|
return null;
|
|
};
|
|
Vue.prototype.getIoTDataValue = getIoTDataValue;
|
|
|
|
function getIoTDataUnit(device, name) {
|
|
var data = Enumerable.from(device.data).where(o => o.name === name).firstOrDefault();
|
|
if (data) {
|
|
return data['unit'];
|
|
}
|
|
return null;
|
|
}
|
|
function getIoTDataDescription(device, name) {
|
|
var data = Enumerable.from(device.data).where(o => o.name === name).firstOrDefault();
|
|
if (data) {
|
|
return data['description'];
|
|
}
|
|
return null;
|
|
}
|
|
//调用API
|
|
function execApi(number, method, query) {
|
|
loading.show();
|
|
axios.post(config.service('platform/api/v1/api/execApi'), { connectionId, number, method, query })
|
|
.then(function (response) {
|
|
})
|
|
.catch(function (error) {
|
|
console.error(error);
|
|
})
|
|
.finally(function () {
|
|
loading.hide();
|
|
});
|
|
}
|
|
Vue.prototype.execApi = execApi;
|
|
|
|
//调用场景
|
|
function execScene(id) {
|
|
loading.show();
|
|
axios.post(config.service('platform/api/v1/api/execScene'), { id, connectionId })
|
|
.then(function (response) {
|
|
//console.log(response);
|
|
})
|
|
.catch(function (error) {
|
|
console.error(error);
|
|
})
|
|
.finally(function () {
|
|
loading.hide();
|
|
});
|
|
}
|
|
Vue.prototype.execScene = execScene;
|
|
|
|
//更新列表项
|
|
function updateById(list, item) {
|
|
var key = 'id';
|
|
var to = Enumerable.from(list).where(function (o) { return o[key] === item[key]; }).firstOrDefault();
|
|
if (to) {
|
|
copy(item, to);
|
|
}
|
|
else {
|
|
list.push(item);
|
|
result = true;
|
|
}
|
|
}
|
|
//移除列表项
|
|
function removeById(list, item) {
|
|
var key = 'id';
|
|
for (var i = 0; i < list.length; i++) {
|
|
if (list[i][key] === item[key]) {
|
|
list.splice(i, 1);
|
|
result = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
//Signal
|
|
function connect() {
|
|
if (connection.state === signalR.HubConnectionState.Disconnected) {
|
|
connection.start().then(function () {
|
|
console.log('signalr connected');
|
|
}).catch(function (error) {
|
|
console.error(error);
|
|
setTimeout(connect, 5000);
|
|
});
|
|
}
|
|
}
|
|
//pubsub批量订阅取消订阅
|
|
PubSub.subscribes = function (events, action) {
|
|
if (!events || !action) {
|
|
return;
|
|
}
|
|
for (var i = 0; i < events.length; i++) {
|
|
PubSub.subscribe(events[i], function (m, d) { action(m, d); });
|
|
}
|
|
}
|
|
PubSub.unsubscribes = function (events) {
|
|
if (!events) {
|
|
return;
|
|
}
|
|
for (var i = 0; i < events.length; i++) {
|
|
PubSub.unsubscribe(events[i]);
|
|
}
|
|
}
|
|
//loading
|
|
var loading = {
|
|
show: function (msg) {
|
|
$('#loadingToast').show();
|
|
},
|
|
hide: function () {
|
|
$('#loadingToast').hide();
|
|
}
|
|
};
|
|
|
|
//message:
|
|
var connectionId;
|
|
var connection = new signalR.HubConnectionBuilder().withUrl(config.service('platform/hub?group=page')).build();
|
|
connection.onclose(function () {
|
|
connect();
|
|
});
|
|
|
|
connection.on('Connected', function (id) {
|
|
connectionId = id;
|
|
PubSub.publish('Connected');
|
|
});
|
|
|
|
connection.on("ServerToClient", function (method, message, to, from) {
|
|
PubSub.publish(method, { message: message, to: to, from: from });
|
|
});
|
|
//connect();
|
|
|
|
//router:
|
|
var routes = [];
|
|
var home = config.file('components/views/areas/default/index.vue');
|
|
var login = config.file('components/views/account/login.vue');
|
|
const router = new VueRouter();
|
|
router.beforeEach((to, from, next) => {
|
|
var url = to.path === '/' ? home : to.path;
|
|
var ext = '.vue';
|
|
var name = url.substring(1, url.length - ext.length).replace(/\//g, "-");
|
|
var route = routes[name];
|
|
if (!route) {
|
|
if (window.location.search.indexOf('debug') > -1) {
|
|
axios.get(url, { headers: { 'Cache-Control': 'no-cache' } }).then(function (response) {
|
|
var model = parseModel(response);
|
|
route = {
|
|
name: name,
|
|
path: to.path,
|
|
component: model,
|
|
meta: model.meta
|
|
};
|
|
router.addRoutes([route]);
|
|
routes[name] = route;
|
|
router.push({ path: to.path, query: to.query });
|
|
});
|
|
}
|
|
else {
|
|
loadModule(url, options).then(function (model) {
|
|
model.name = name;
|
|
route = {
|
|
name: name,
|
|
path: to.path,
|
|
component: model,
|
|
meta: model.meta
|
|
};
|
|
router.addRoutes([route]);
|
|
routes[name] = route;
|
|
router.push({ path: to.path, query: to.query });
|
|
});
|
|
}
|
|
}
|
|
else {
|
|
next();
|
|
}
|
|
});
|
|
|
|
//vuex
|
|
Vue.use(Vuex);
|
|
const store = new Vuex.Store({
|
|
strict: true,
|
|
state: {
|
|
token: {
|
|
accessToken: localStorage.getItem("accessToken"),
|
|
refreshToken: localStorage.getItem("refreshToken")
|
|
},
|
|
layout: null
|
|
},
|
|
mutations: {
|
|
set(state, data) {
|
|
state[data.key] = data.value;
|
|
},
|
|
login(state, data) {
|
|
localStorage.setItem('accessToken', data.accessToken);
|
|
localStorage.setItem('refreshToken', data.refreshToken);
|
|
state.token.accessToken = data.accessToken;
|
|
state.token.refreshToken = data.refreshToken;
|
|
},
|
|
logout(state) {
|
|
localStorage.removeItem('accessToken');
|
|
localStorage.removeItem('refreshToken');
|
|
state.token.accessToken = null;
|
|
state.token.refreshToken = null;
|
|
}
|
|
}
|
|
});
|
|
store.set = function (key, value) {
|
|
store.commit('set', { key: key, value: value });
|
|
}
|
|
Vue.prototype.store = store;
|
|
//antd tree 点击内容展开子节点
|
|
$('body').on('click', '.ant-tree-node-content-wrapper', function () {
|
|
$(this).parent('li').children('.ant-tree-switcher.ant-tree-switcher_close').click();
|
|
});
|