/*var*/ var isApp = location.href.indexOf('http') !== 0; var server = isApp ? localStorage.getItem('server') : null; var iotCenter = isApp ? server + '/IoTCenter' : ''; var dataUrl = iotCenter + '/App/GetProducts'; var token = isApp ? localStorage.getItem('token') : null; var wsUrl = iotCenter + '/hub?group=page'; var connectionId; const connection = new signalR.HubConnectionBuilder() .withUrl(wsUrl) .build(); var app; var vm; // if (isApp) { Framework7.use(Framework7Vue); } toastr.options.timeOut = 500; toastr.options.positionClass = "toast-top-center"; /*fun*/ function alert(message, title) { if (isApp) { title = title || '消息'; app.dialog.alert(message, title); } else { alert(message); } } function hideLoading(message) { toastr.success(message || '请求成功'); $('.overlay').hide(); } function ajax(url, data, type) { url = iotCenter + url console.log(url); type = type || 'get'; $('.overlay').show(); $.ajax({ type: type, url: url, data: data, success: AjaxCallBack }).fail(function (result) { toastr.error('请求发送失败!'); console.log(result); }).always(function () { $('.overlay').hide(); }); } function AjaxCallBack(response) { var result = response; if (result.code === 0) { if (result.type === 0) { if (result.format === 1) { console.log('format/1/base64 jpeg image'); $('#callback .page-content').html(''); } else { console.log('format/0/json object'); $('#callback .page-content').html(result.data); } app.popup.open('#callback'); } } } /*ws*/ function connect() { if (debug) { console.log('start connect to server:' + Date()); } if (connection.state === signalR.HubConnectionState.Disconnected) { connection.start().then(function () { toastr.success('客户端与服务器连接成功!'); }).catch(function (err) { toastr.error('客户端与服务器连接失败!'); setTimeout(connect, 15 * 1000); }); } } connection.on('Connected', function (id) { connectionId = id; }); connection.onclose(function (err) { setTimeout(connect, 15 * 1000); }); connection.on("ServerToClient", function (method, json, to, from) { console.log(method + ':' + json); onMessage(method, json, to, from); }); function onMessage(method, json, to, from) { var item = JSON.parse(json); if (method === 'ExecApiRsponse') { console.log(json); toastr.success('操作调用成功'); } else if (method === 'ExecSceneRsponse') { console.log(json); toastr.success('场景调用成功'); } else if (method == 'ProductEntityInserted' || method == 'ProductEntityUpdated' || method == 'ProductEntityDeleted' || method == 'DeviceEntityInserted' || method == 'DeviceEntityDeleted' || method == 'SceneEntityInserted' || method == 'SceneEntityUpdated' || method == 'SceneEntityDeleted') { vm.load(); } } /*vue*/ function loadData() { vm = new Vue({ el: '#app', data() { return { f7params: { routes: [], name: 'My App', id: 'com.myapp.test', theme: 'ios', }, model: null }; }, mounted() { if (isApp) { if (!server) { location.href = "config.html"; } if (!token) { location.href = "login.html"; } this.$f7ready((f7) => { app = this.$f7; }); } this.load(); }, methods: { load() { connect(); axios.post(dataUrl) .then(function (response) { vm.model = response.data; }) .catch(function (error) { toastr.error('数据加载失败!'); }) .then(hideLoading); }, CallApi(number, method, query) { ajax('/App/ExecApi', { token: token, connectionId: connectionId, number: number, method: method, query: query }, 'post'); }, GetScenes() { return Enumerable.from(this.model.Scenes).orderBy('o=>o.DisplayOrder').toArray(); }, CallScene(id) { ajax('/App/ExecGlobalScene', { token: token, connectionId: connectionId, id: id }, 'post'); } } }); }