userauth.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. wsUri = "ws://" + authServiceIP + ":" + authServicePort + "/wsaccess";
  2. ws = new WebSocket(wsUri);
  3. userDic = {};
  4. var socket;
  5. $(function () {
  6. checkUserLogin();
  7. });
  8. var newSocket = function () {
  9. // ws = new WebSocket(wsUri);
  10. ws.onopen = function (evt) {
  11. }
  12. ws.onclose = function (evt) {
  13. reconnect();
  14. }
  15. ws.onmessage = function (evt) {
  16. var result = JSON.parse(evt.data);
  17. if (result.success) {
  18. switch (result.api) {
  19. case "user.readuser": //判断当前缓存的用户在数据库是否存在
  20. if (result.body.account.created < 0) {
  21. GD.createErrorDialog(420, 160, "当前用户在数据库已不存在,请用其他用户登录");
  22. return;
  23. }
  24. break;
  25. }
  26. } else {
  27. }
  28. }
  29. ws.onerror = function (evt) {
  30. reconnect();
  31. console.log(evt);
  32. //return;
  33. }
  34. };
  35. var reconnect = function () {
  36. ws = new WebSocket(wsUri);
  37. ws.onopen = function (evt) {}
  38. ws.onmessage = function (evt) {}
  39. }
  40. newSocket();
  41. (function () {
  42. var socketOption = {
  43. path: '/wsaccess/socket.io/'
  44. };
  45. if(isAuthEnable=="True"){
  46. if (typeof (socket) == "undefined") {
  47. socket = GD.io("http://" + authServiceIP + ":" + authServicePort, socketOption);
  48. socket.on('connection', function () {
  49. });
  50. socket.on('disconnect', function () {
  51. });
  52. socket.on('close', function () {
  53. socket = null;
  54. })
  55. socket.on('message', function (result) {
  56. });
  57. socket.on('error', function (data) {
  58. console.log(data);
  59. // alert('后台服务连接失败');
  60. })
  61. }
  62. }else{
  63. socket="";
  64. return;
  65. }
  66. })();
  67. function logIn(username, pwd, callback) {
  68. if (!ws) {
  69. return false;
  70. }
  71. var msg = '{"header": {"token": "TOKEN"},"api": "auth.authenticate","request": { "name": "' + username + '","password": "' + pwd + '"}}'
  72. localStorage.clear();
  73. ws.send(msg);
  74. var login = function (evt) {
  75. var result = JSON.parse(evt.data);
  76. if (result.success) {
  77. switch (result.api) {
  78. case "auth.authenticate": //如果用户登陆成功,将用户信息缓存下来
  79. localStorage.setItem("user", JSON.stringify(result.body.user));
  80. localStorage.setItem("token", JSON.stringify(result.body.token));
  81. localStorage.setItem("userGroup", typeof (result.body.user.groupid) == "undefined" ? 0 : result.body.user.groupid);
  82. localStorage.setItem("extra", typeof (result.body.user.extra) == "undefined" ? '""' : JSON.stringify(result.body.user.extra));
  83. getAuthInfoByGroupId(result.body.user.groupid, function (data) {
  84. if (data) {
  85. // alert("登录成功");
  86. eval(callback);
  87. }
  88. });
  89. window.alarmInit && alarmInit();
  90. break;
  91. case "user.readgroup":
  92. localStorage.setItem("userLevel", typeof (result.body.group.level) == "undefined" ? 0 : result.body.group.level);
  93. ws.removeEventListener('message', login);
  94. break;
  95. }
  96. } else {
  97. if (result.api == "auth.authenticate") {
  98. GD.createErrorDialog(300, 160, '用户名或密码不正确!', function (){}, function () {});
  99. ws.removeEventListener('message', login);
  100. return;
  101. }
  102. }
  103. }
  104. ws.addEventListener('message', login);
  105. }
  106. //用户退出
  107. function loginOut() {
  108. if (!ws) {
  109. return false;
  110. }
  111. var logout = function (evt) {
  112. var result = JSON.parse(evt.data);
  113. ws.removeEventListener('message', logout);
  114. if (result.success) {
  115. if (result.api == "auth.logout") {
  116. //localStorage.removeItem("user"); //用户退出登陆
  117. localStorage.clear();
  118. //alert("退出登陆");
  119. }
  120. }
  121. ws.removeEventListener('message', logout);
  122. }
  123. if (localStorage.getItem("token") != null) {
  124. var msg = '{"header": {"token": ' + localStorage.getItem("token") + '},"api": "auth.logout"}';
  125. ws.send(msg);
  126. ws.addEventListener('message', logout);
  127. } else {
  128. // alert("用户未登录!");
  129. return;
  130. }
  131. }
  132. //获取操作对应的权限
  133. function getAuthInfoByGroupId(groupid, callback) {
  134. var authdata = function (evt) {
  135. var result = JSON.parse(evt.data);
  136. if (result.success) {
  137. if (result.api == "user.readgroup") {
  138. var userLevel = parseInt(localStorage.getItem("userLevel"));
  139. var userGroup = parseInt(localStorage.getItem("userGroup"));
  140. var operLevel = typeof (result.body.group.level) == "undefined" ? 0 : parseInt(result.body.group.level);
  141. var operGroup = typeof (result.body.group.id) == "undefined" ? 0 : parseInt(result.body.group.id);
  142. // if (userGroup != operGroup && userLevel < operLevel) {
  143. // GD.createErrorDialog(300, 160, "没有该操作的权限");
  144. // return;
  145. // } else {
  146. // callback(true);
  147. // }
  148. if (userLevel < operLevel) {
  149. GD.createErrorDialog(300, 160, "没有该操作的权限");
  150. return;
  151. } else if(userLevel == operLevel&&userGroup != operGroup){
  152. GD.createErrorDialog(300, 160, "没有该操作的权限");
  153. return;
  154. }else{
  155. callback(true);
  156. }
  157. }
  158. ws.removeEventListener('message', authdata);
  159. }
  160. }
  161. if (!ws) {
  162. return false;
  163. }
  164. //unescape(getCookieValue("token"))
  165. if (localStorage.getItem("token") != null) {
  166. var msg = '{"header": {"token": ' + localStorage.getItem("token") + '},"api": "user.readgroup","request": { "id": ' + groupid + ' }}';
  167. ws.send(msg);
  168. ws.addEventListener('message', authdata);
  169. }
  170. }
  171. //判断用户是否登陆
  172. function checkUserLogin() {
  173. var userinfo = localStorage.getItem("user");
  174. // alert(unescape(userinfo));
  175. //console.log(getCookieValue("user")+" token: "+getCookieValue("token")+" userGroup: "+getCookieValue("userGroup"))
  176. if (userinfo == "" || userinfo == null) {
  177. //alert("用户未登录!");
  178. return;
  179. }
  180. }
  181. function checkUserExistByUserId(userid) {
  182. if (!ws) {
  183. return false;
  184. }
  185. if (localStorage.getItem("token") != null) {
  186. var msg = '{"header": { "token":' + localStorage.getItem("token") + ' }, "api": "user.readuser", "request": { "id": ' + userid + ' }}';
  187. ws.send(msg);
  188. } else {
  189. GD.createErrorDialog(300, 160, "用户未登录!");
  190. }
  191. }
  192. //获取所有的用户
  193. function getAllUsers(callback) {
  194. if (!socket) {
  195. callback();
  196. return false;
  197. }
  198. socket.post('user.searchuser', {
  199. "offset": 0,
  200. "limit": 10
  201. }, function (result) {
  202. if (result.success) {
  203. if (result.api == "user.searchuser") {
  204. localStorage.setItem("allusers", JSON.stringify(result.body.accounts));
  205. callback(true);
  206. }
  207. } else {
  208. callback();
  209. }
  210. })
  211. }
  212. //写入操作记录
  213. function addOpera(tagName, areaName, result, groupid, desc, quality, callback) {
  214. if (!ws) {
  215. return false;
  216. }
  217. if (typeof quality === 'function') {
  218. callback = quality;
  219. quality = '';
  220. }
  221. var writeopera = function (evt) {
  222. var result = JSON.parse(evt.data);
  223. if (result.success) {
  224. if (result.api == "operate.add") {
  225. callback(true);
  226. }
  227. }
  228. ws.removeEventListener('message', writeopera);
  229. }
  230. if (localStorage.getItem("token") != null) {
  231. $.ajax({
  232. url: "http://" + webServiceIP + ":" + webServicePort + "/api/Table/?tableName=" + areaName,
  233. type: "GET",
  234. dataType: 'JSON',
  235. contentType: "application/json; charset=utf-8",
  236. success: function (data) {
  237. areaName = data.Desc || areaName;
  238. var msg = '{"header": { "token":' + localStorage.getItem("token") + ' }, "api": "operate.add",';
  239. msg += '"request": {"operate": {"tag": "' + tagName + '", "device": "' + quality + '.' + areaName + '","result": ' + result + ',"desc": "' + desc + '" }}}';
  240. ws.send(msg);
  241. ws.addEventListener('message', writeopera);
  242. },
  243. error: function () {
  244. GD.createErrorDialog(300, 160, "写入操作记录时发生错误!");
  245. },
  246. complete: function (xhr) {
  247. xhr = null;
  248. }
  249. })
  250. } else {
  251. GD.createErrorDialog(300, 160, "用户未登录!");
  252. }
  253. }
  254. //把数据保存到后端服务
  255. function cacheKeyValueData(key, value, callback) {
  256. if (!socket) {
  257. callback();
  258. return false;
  259. }
  260. // var cacheSet = function (evt) {
  261. // var result = JSON.parse(evt.data);
  262. // if (result.success) {
  263. // if (result.api == "cache.set") {
  264. // callback(true);
  265. // }
  266. // } else {
  267. // callback();
  268. // }
  269. // ws.removeEventListener('message', cacheSet);
  270. // }
  271. // if(ws.readyState == 1){
  272. // if (localStorage.getItem("token") != null) {
  273. // var msg = '{"header": {"token": ' + localStorage.getItem("token") + '},"api": "cache.set","request": { "key": "' + key + '","value":"' + value + '" }}';
  274. // ws.send(msg);
  275. // ws.addEventListener('message', cacheSet);
  276. // } else {
  277. // alert("用户未登录!");
  278. // callback();
  279. // }
  280. // }
  281. socket.post('cache.set', {
  282. "key": key,
  283. "value": value
  284. }, function (result) {
  285. if (result.success) {
  286. if (result.api == "cache.set") {
  287. callback(true);
  288. }
  289. } else {
  290. callback();
  291. }
  292. })
  293. }
  294. //读取后端key/value数据
  295. function getKeyValueData(key, callback) {
  296. if (!socket) {
  297. callback();
  298. return false;
  299. }
  300. // var cacheget = function (evt) {
  301. // var result = JSON.parse(evt.data);
  302. // if (result.success) {
  303. // if (result.api == "cache.get") {
  304. // callback(result.body.value);
  305. // }
  306. // } else {
  307. // callback();
  308. // }
  309. // ws.removeEventListener('message', cacheget);
  310. // }
  311. // if (ws.readyState == 1) {
  312. // if (localStorage.getItem("token") != null) {
  313. // var msg = '{"header": {"token": ' + localStorage.getItem("token") + '},"api": "cache.get","request": { "key": "' + key + '"}}';
  314. // ws.send(msg);
  315. // ws.addEventListener('message', cacheget);
  316. // } else {
  317. // alert("用户未登录!");
  318. // callback();
  319. // }
  320. // }
  321. socket.post('cache.get', {
  322. "key": key
  323. }, function (result) {
  324. if (result.success) {
  325. if (result.api == "cache.get") {
  326. callback(result.body.value);
  327. }
  328. } else {
  329. callback();
  330. }
  331. })
  332. }