123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- wsUri = "ws://" + authServiceIP + ":" + authServicePort + "/wsaccess";
- ws = new WebSocket(wsUri);
- userDic = {};
- var socket;
- $(function () {
- checkUserLogin();
- });
- var newSocket = function () {
- // ws = new WebSocket(wsUri);
- ws.onopen = function (evt) {
- }
- ws.onclose = function (evt) {
- reconnect();
- }
- ws.onmessage = function (evt) {
- var result = JSON.parse(evt.data);
- if (result.success) {
- switch (result.api) {
- case "user.readuser": //判断当前缓存的用户在数据库是否存在
- if (result.body.account.created < 0) {
- GD.createErrorDialog(420, 160, "当前用户在数据库已不存在,请用其他用户登录");
- return;
- }
- break;
- }
- } else {
- }
- }
- ws.onerror = function (evt) {
- reconnect();
- console.log(evt);
- //return;
- }
- };
- var reconnect = function () {
- ws = new WebSocket(wsUri);
- ws.onopen = function (evt) {}
- ws.onmessage = function (evt) {}
- }
- newSocket();
- (function () {
- var socketOption = {
- path: '/wsaccess/socket.io/'
- };
- if(isAuthEnable=="True"){
- if (typeof (socket) == "undefined") {
- socket = GD.io("http://" + authServiceIP + ":" + authServicePort, socketOption);
- socket.on('connection', function () {
- });
- socket.on('disconnect', function () {
- });
- socket.on('close', function () {
- socket = null;
- })
- socket.on('message', function (result) {
- });
- socket.on('error', function (data) {
- console.log(data);
- // alert('后台服务连接失败');
- })
- }
- }else{
- socket="";
- return;
- }
-
- })();
- function logIn(username, pwd, callback) {
- if (!ws) {
- return false;
- }
- var msg = '{"header": {"token": "TOKEN"},"api": "auth.authenticate","request": { "name": "' + username + '","password": "' + pwd + '"}}'
- localStorage.clear();
- ws.send(msg);
- var login = function (evt) {
- var result = JSON.parse(evt.data);
- if (result.success) {
- switch (result.api) {
- case "auth.authenticate": //如果用户登陆成功,将用户信息缓存下来
- localStorage.setItem("user", JSON.stringify(result.body.user));
- localStorage.setItem("token", JSON.stringify(result.body.token));
- localStorage.setItem("userGroup", typeof (result.body.user.groupid) == "undefined" ? 0 : result.body.user.groupid);
- localStorage.setItem("extra", typeof (result.body.user.extra) == "undefined" ? '""' : JSON.stringify(result.body.user.extra));
- getAuthInfoByGroupId(result.body.user.groupid, function (data) {
- if (data) {
- // alert("登录成功");
- eval(callback);
- }
- });
- window.alarmInit && alarmInit();
- break;
- case "user.readgroup":
- localStorage.setItem("userLevel", typeof (result.body.group.level) == "undefined" ? 0 : result.body.group.level);
- ws.removeEventListener('message', login);
- break;
- }
- } else {
- if (result.api == "auth.authenticate") {
- GD.createErrorDialog(300, 160, '用户名或密码不正确!', function (){}, function () {});
- ws.removeEventListener('message', login);
- return;
- }
- }
- }
- ws.addEventListener('message', login);
- }
- //用户退出
- function loginOut() {
- if (!ws) {
- return false;
- }
- var logout = function (evt) {
- var result = JSON.parse(evt.data);
- ws.removeEventListener('message', logout);
- if (result.success) {
- if (result.api == "auth.logout") {
- //localStorage.removeItem("user"); //用户退出登陆
- localStorage.clear();
- //alert("退出登陆");
- }
- }
- ws.removeEventListener('message', logout);
- }
- if (localStorage.getItem("token") != null) {
- var msg = '{"header": {"token": ' + localStorage.getItem("token") + '},"api": "auth.logout"}';
- ws.send(msg);
- ws.addEventListener('message', logout);
- } else {
- // alert("用户未登录!");
- return;
- }
- }
- //获取操作对应的权限
- function getAuthInfoByGroupId(groupid, callback) {
- var authdata = function (evt) {
- var result = JSON.parse(evt.data);
- if (result.success) {
- if (result.api == "user.readgroup") {
- var userLevel = parseInt(localStorage.getItem("userLevel"));
- var userGroup = parseInt(localStorage.getItem("userGroup"));
- var operLevel = typeof (result.body.group.level) == "undefined" ? 0 : parseInt(result.body.group.level);
- var operGroup = typeof (result.body.group.id) == "undefined" ? 0 : parseInt(result.body.group.id);
- // if (userGroup != operGroup && userLevel < operLevel) {
- // GD.createErrorDialog(300, 160, "没有该操作的权限");
- // return;
- // } else {
- // callback(true);
- // }
- if (userLevel < operLevel) {
- GD.createErrorDialog(300, 160, "没有该操作的权限");
- return;
- } else if(userLevel == operLevel&&userGroup != operGroup){
- GD.createErrorDialog(300, 160, "没有该操作的权限");
- return;
- }else{
- callback(true);
- }
- }
- ws.removeEventListener('message', authdata);
- }
- }
- if (!ws) {
- return false;
- }
- //unescape(getCookieValue("token"))
- if (localStorage.getItem("token") != null) {
- var msg = '{"header": {"token": ' + localStorage.getItem("token") + '},"api": "user.readgroup","request": { "id": ' + groupid + ' }}';
- ws.send(msg);
- ws.addEventListener('message', authdata);
- }
- }
- //判断用户是否登陆
- function checkUserLogin() {
- var userinfo = localStorage.getItem("user");
- // alert(unescape(userinfo));
- //console.log(getCookieValue("user")+" token: "+getCookieValue("token")+" userGroup: "+getCookieValue("userGroup"))
- if (userinfo == "" || userinfo == null) {
- //alert("用户未登录!");
- return;
- }
- }
- function checkUserExistByUserId(userid) {
- if (!ws) {
- return false;
- }
- if (localStorage.getItem("token") != null) {
- var msg = '{"header": { "token":' + localStorage.getItem("token") + ' }, "api": "user.readuser", "request": { "id": ' + userid + ' }}';
- ws.send(msg);
- } else {
- GD.createErrorDialog(300, 160, "用户未登录!");
- }
- }
- //获取所有的用户
- function getAllUsers(callback) {
- if (!socket) {
- callback();
- return false;
- }
- socket.post('user.searchuser', {
- "offset": 0,
- "limit": 10
- }, function (result) {
- if (result.success) {
- if (result.api == "user.searchuser") {
- localStorage.setItem("allusers", JSON.stringify(result.body.accounts));
- callback(true);
- }
- } else {
- callback();
- }
- })
- }
- //写入操作记录
- function addOpera(tagName, areaName, result, groupid, desc, quality, callback) {
- if (!ws) {
- return false;
- }
- if (typeof quality === 'function') {
- callback = quality;
- quality = '';
- }
- var writeopera = function (evt) {
- var result = JSON.parse(evt.data);
- if (result.success) {
- if (result.api == "operate.add") {
- callback(true);
- }
- }
- ws.removeEventListener('message', writeopera);
- }
- if (localStorage.getItem("token") != null) {
- $.ajax({
- url: "http://" + webServiceIP + ":" + webServicePort + "/api/Table/?tableName=" + areaName,
- type: "GET",
- dataType: 'JSON',
- contentType: "application/json; charset=utf-8",
- success: function (data) {
- areaName = data.Desc || areaName;
- var msg = '{"header": { "token":' + localStorage.getItem("token") + ' }, "api": "operate.add",';
- msg += '"request": {"operate": {"tag": "' + tagName + '", "device": "' + quality + '.' + areaName + '","result": ' + result + ',"desc": "' + desc + '" }}}';
- ws.send(msg);
- ws.addEventListener('message', writeopera);
- },
- error: function () {
- GD.createErrorDialog(300, 160, "写入操作记录时发生错误!");
- },
- complete: function (xhr) {
- xhr = null;
- }
- })
- } else {
- GD.createErrorDialog(300, 160, "用户未登录!");
- }
- }
- //把数据保存到后端服务
- function cacheKeyValueData(key, value, callback) {
- if (!socket) {
- callback();
- return false;
- }
- // var cacheSet = function (evt) {
- // var result = JSON.parse(evt.data);
- // if (result.success) {
- // if (result.api == "cache.set") {
- // callback(true);
- // }
- // } else {
- // callback();
- // }
- // ws.removeEventListener('message', cacheSet);
- // }
- // if(ws.readyState == 1){
- // if (localStorage.getItem("token") != null) {
- // var msg = '{"header": {"token": ' + localStorage.getItem("token") + '},"api": "cache.set","request": { "key": "' + key + '","value":"' + value + '" }}';
- // ws.send(msg);
- // ws.addEventListener('message', cacheSet);
- // } else {
- // alert("用户未登录!");
- // callback();
- // }
- // }
- socket.post('cache.set', {
- "key": key,
- "value": value
- }, function (result) {
- if (result.success) {
- if (result.api == "cache.set") {
- callback(true);
- }
- } else {
- callback();
- }
- })
- }
- //读取后端key/value数据
- function getKeyValueData(key, callback) {
- if (!socket) {
- callback();
- return false;
- }
- // var cacheget = function (evt) {
- // var result = JSON.parse(evt.data);
- // if (result.success) {
- // if (result.api == "cache.get") {
- // callback(result.body.value);
- // }
- // } else {
- // callback();
- // }
- // ws.removeEventListener('message', cacheget);
- // }
- // if (ws.readyState == 1) {
- // if (localStorage.getItem("token") != null) {
- // var msg = '{"header": {"token": ' + localStorage.getItem("token") + '},"api": "cache.get","request": { "key": "' + key + '"}}';
- // ws.send(msg);
- // ws.addEventListener('message', cacheget);
- // } else {
- // alert("用户未登录!");
- // callback();
- // }
- // }
- socket.post('cache.get', {
- "key": key
- }, function (result) {
- if (result.success) {
- if (result.api == "cache.get") {
- callback(result.body.value);
- }
- } else {
- callback();
- }
- })
- }
|