services.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. define([ 'angular' ], function(angular) {
  2. 'use strict';
  3. angular.module('common.services', [ ]).factory('SessionService', function() {
  4. return {
  5. get : function(key) {
  6. var storage = window.sessionStorage;
  7. if(storage)
  8. return sessionStorage.getItem(key);
  9. return null;
  10. },
  11. set : function(key, val) {
  12. var storage = window.sessionStorage;
  13. if(storage)
  14. return sessionStorage.setItem(key, val);
  15. return null;
  16. },
  17. unset : function(key) {
  18. var storage = window.sessionStorage;
  19. if(storage)
  20. return sessionStorage.removeItem(key);
  21. return null;
  22. },
  23. getCookie: function(key) {
  24. var storage = window.localStorage;
  25. if(storage)
  26. return storage.getItem(key);
  27. else {
  28. var val = document.cookie.match(new RegExp("(^| )" + key + "=([^;]*)(;|$)"));
  29. if (val != null) {
  30. return unescape(val[2]);
  31. }
  32. return null
  33. }
  34. },
  35. setCookie: function(key, val) {
  36. var storage = window.localStorage;
  37. if(storage)
  38. storage.setItem(key, val);
  39. else {
  40. var date = new Date(new Date().getTime() + 30 * 24 * 60 * 60 * 1000);
  41. document.cookie = key + "=" + escape(val) + ";expires=" + date.toGMTString();
  42. }
  43. },
  44. removeCookie: function(key) {
  45. var storage = window.localStorage;
  46. if(storage)
  47. storage.removeItem(key);
  48. else {
  49. var val = this.getCookie(key);
  50. if (val != null) {
  51. var date = new Date(new Date().getTime() - 1);
  52. document.cookie = key + "=" + val + ";expires=" + date.toGMTString()
  53. }
  54. }
  55. }
  56. };
  57. }).factory('BaseService', function() {
  58. return {
  59. getRootPath : function() {
  60. var fullPath = window.document.location.href;
  61. var path = window.document.location.pathname;
  62. var pos = fullPath.indexOf(path);
  63. return fullPath.substring(0, pos) + path.substring(0, path.substr(1).indexOf('/') + 1);
  64. }
  65. };
  66. }).factory('AuthenticationService',['$http', 'SessionService', 'BaseService', 'SerializerUtil', function($http, SessionService, BaseService, SerializerUtil) {
  67. var cacheSession = function() {
  68. SessionService.set('authenticated', true);
  69. };
  70. var uncacheSession = function() {
  71. SessionService.unset('authenticated');
  72. };
  73. var rootPath = BaseService.getRootPath();
  74. return {
  75. root : function() {
  76. return rootPath;
  77. },
  78. login : function(user) {
  79. var payload = SerializerUtil.param(user);
  80. var config = {
  81. headers : {
  82. 'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8'
  83. }
  84. };
  85. var login = $http.post(rootPath + "/pda/login.action", payload, config);
  86. login.success(cacheSession);
  87. return login;
  88. },
  89. logout : function() {
  90. var logout = $http.get(rootPath + "/pda/logout.action");
  91. logout.success(uncacheSession);
  92. return logout;
  93. },
  94. isAuthed : function() {
  95. return SessionService.get('authenticated');
  96. },
  97. getAuthentication : function() {
  98. var request = $http.get(rootPath + '/pda/authentication.action');
  99. request.success(function(data){
  100. if(data)
  101. cacheSession();
  102. else
  103. uncacheSession();
  104. });
  105. request.error(uncacheSession);
  106. return request;
  107. },
  108. getMasters : function(){
  109. var request = $http.get(rootPath +'/mobile/getAllMasters.action');
  110. request.success(function(data){
  111. });
  112. return request;
  113. }
  114. };
  115. }]).factory('SerializerUtil', function() {
  116. return {
  117. /**
  118. * @description 将元素值转换为序列化的字符串表示
  119. */
  120. param : function(obj) {
  121. var query = '', name, value, fullSubName, subName, subValue, innerObj, i, me = this;
  122. for (name in obj) {
  123. value = obj[name];
  124. if (value instanceof Array) {
  125. for (i = 0; i < value.length; ++i) {
  126. subValue = value[i];
  127. fullSubName = name + '[' + i + ']';
  128. innerObj = {};
  129. innerObj[fullSubName] = subValue;
  130. query += me.param(innerObj) + '&';
  131. }
  132. } else if (value instanceof Object) {
  133. for (subName in value) {
  134. subValue = value[subName];
  135. fullSubName = name + '[' + subName + ']';
  136. innerObj = {};
  137. innerObj[fullSubName] = subValue;
  138. query += me.param(innerObj) + '&';
  139. }
  140. } else if (value !== undefined && value !== null)
  141. query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
  142. }
  143. return query.length ? query.substr(0, query.length - 1) : query;
  144. }
  145. };
  146. });
  147. });