util.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //用来获取请求中的参数的工具类
  2. UrlParm = function () { // url参数
  3. var data, index;
  4. (function init() {
  5. data = [];
  6. index = {};
  7. var u = window.location.search.substr(1);
  8. if (u != '') {
  9. var parms = decodeURIComponent(u).split('&');
  10. for (var i = 0, len = parms.length; i < len; i++) {
  11. if (parms[i] != '') {
  12. var p = parms[i].split("=");
  13. if (p.length == 1 || (p.length == 2 && p[1] == '')) {
  14. data.push(['']);
  15. index[p[0]] = data.length - 1;
  16. } else if (typeof(p[0]) == 'undefined' || p[0] == '') {
  17. data[0] = [p[1]];
  18. } else if (typeof(index[p[0]]) == 'undefined') { // c=aaa
  19. data.push([p[1]]);
  20. index[p[0]] = data.length - 1;
  21. } else {// c=aaa
  22. data[index[p[0]]].push(p[1]);
  23. }
  24. }
  25. }
  26. }
  27. })();
  28. return {
  29. // 获得参数,类似request.getParameter()
  30. parm: function (o) { // o: 参数名或者参数次序
  31. try {
  32. return (typeof(o) == 'number' ? data[o][0] : data[index[o]][0]);
  33. } catch (e) {
  34. }
  35. },
  36. //获得参数组, 类似request.getParameterValues()
  37. parmValues: function (o) { // o: 参数名或者参数次序
  38. try {
  39. return (typeof(o) == 'number' ? data[o] : data[index[o]]);
  40. } catch (e) {
  41. }
  42. },
  43. //是否含有parmName参数
  44. hasParm: function (parmName) {
  45. return typeof(parmName) == 'string' ? typeof(index[parmName]) != 'undefined' : false;
  46. },
  47. // 获得参数Map ,类似request.getParameterMap()
  48. parmMap: function () {
  49. var map = {};
  50. try {
  51. for (var p in index) {
  52. map[p] = data[index[p]];
  53. }
  54. } catch (e) {
  55. }
  56. return map;
  57. }
  58. }
  59. }();
  60. //get方法
  61. function do_get(url, data, success,error,bool) {
  62. if(bool==undefined){
  63. bool=true;
  64. }
  65. //获取用户的code
  66. var usercode = getUserCode();
  67. data.userCode = usercode;
  68. data.saasId = getSaasId();
  69. $.ajax({
  70. type: 'GET',
  71. url: url,
  72. data: data,
  73. success: success,
  74. error:error,
  75. dataType: "json",
  76. async:bool
  77. });
  78. }
  79. //post方法
  80. function do_post(url, data, success ,error,bool) {
  81. if(bool==undefined){
  82. bool=true;
  83. }
  84. //获取用户的code
  85. if(typeof (data)=="string"){
  86. if(data==''){
  87. data="userCode="+getUserCode()+"&saasId="+ getSaasId();
  88. }else{
  89. data+="&userCode="+getUserCode()+"&saasId="+ getSaasId();
  90. }
  91. }else{
  92. data.userCode = getUserCode();
  93. data.saasId = getSaasId();
  94. }
  95. $.ajax({
  96. type: 'POST',
  97. url: url,
  98. data: data,
  99. success: success,
  100. error:error,
  101. dataType: "json",
  102. async:bool
  103. });
  104. }
  105. //put方法
  106. function do_put(url, data, success ,error,bool) {
  107. if(bool==undefined){
  108. bool=true;
  109. }
  110. //获取用户的code
  111. if(typeof (data)=="string"){
  112. if(data==''){
  113. data="userCode="+usercode+"&saasId="+ getSaasId()+"&_method=PUT";
  114. }else{
  115. data+="&userCode="+usercode+"&saasId="+ getSaasId()+"&_method=PUT";
  116. }
  117. }else{
  118. data.userCode = getUserCode();
  119. data.saasId = getSaasId();
  120. data._method="PUT";
  121. }
  122. $.ajax({
  123. type: 'POST',
  124. url: url,
  125. data: data,
  126. success: success,
  127. error:error,
  128. dataType: "json",
  129. async:bool
  130. });
  131. }
  132. //delete方法
  133. function do_delete(url, data, success,error,bool) {
  134. if(bool==undefined){
  135. bool=true;
  136. }
  137. //获取用户的code
  138. var usercode = getUserCode();
  139. data.userCode = usercode;
  140. data.saasId = getSaasId();
  141. data._method="DELETE";
  142. $.ajax({
  143. type: 'POST',
  144. url: url,
  145. data: data,
  146. success: success,
  147. error:error,
  148. dataType: "json",
  149. async:bool
  150. });
  151. }
  152. function getUserCode() {
  153. var code = window.localStorage.getItem("userCode");
  154. if (!code) {
  155. alert("请重新登陆")
  156. window.location.href = server+ '/login/login.html';
  157. }
  158. return code
  159. }
  160. function setUserCode(code) {
  161. window.localStorage.setItem("userCode", code);
  162. }
  163. function cleanUserCode(){
  164. window.localStorage.setItem("userCode", null);
  165. }
  166. function getSaasId() {
  167. var code = window.localStorage.getItem("saasId");
  168. return code
  169. }
  170. function setSaasId(code) {
  171. window.localStorage.setItem("saasId", code);
  172. }
  173. function cleanSaasId(){
  174. window.localStorage.setItem("saasId", null);
  175. }
  176. /**
  177. * 判断是否有登陆
  178. * @param data
  179. */
  180. function isLogin(data){
  181. if(data.errorCode==not_login){
  182. alert("请重新登陆")
  183. window.location.href = '/login/login.html';
  184. }
  185. }