util.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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) {
  62. //获取用户的code
  63. var usercode = getUserCode();
  64. data.userCode = usercode;
  65. $.ajax({
  66. type: 'GET',
  67. url: url,
  68. data: data,
  69. success: success,
  70. error:error,
  71. dataType: "json"
  72. });
  73. }
  74. //post方法
  75. function do_post(url, data, success) {
  76. //获取用户的code
  77. var usercode = getUserCode();
  78. //data.userCode = usercode;
  79. if(data==''){
  80. data="userCode="+usercode;
  81. }else{
  82. data+="&userCode="+usercode;
  83. }
  84. $.ajax({
  85. type: 'POST',
  86. url: url,
  87. data: data,
  88. success: success,
  89. dataType: "json"
  90. });
  91. }
  92. //put方法
  93. function do_put(url, data, success) {
  94. //获取用户的code
  95. var usercode = getUserCode();
  96. data.userCode = usercode;
  97. data._method="PUT";
  98. $.ajax({
  99. type: 'POST',
  100. url: url,
  101. data: data,
  102. success: success,
  103. dataType: "json"
  104. });
  105. }
  106. //delete方法
  107. function do_delete(url, data, success) {
  108. //获取用户的code
  109. var usercode = getUserCode();
  110. data.userCode = usercode;
  111. data._method="DELETE";
  112. $.ajax({
  113. type: 'POST',
  114. url: url,
  115. data: data,
  116. success: success,
  117. dataType: "json"
  118. });
  119. }
  120. function getUserCode() {
  121. var code = window.localStorage.getItem("userCode");
  122. if (!code) {
  123. alert("请重新登陆")
  124. window.location.href = '/login/login.html';
  125. }
  126. return code
  127. }
  128. function setUserCode(code) {
  129. window.localStorage.setItem("userCode", code);
  130. }
  131. function cleanUserCode(){
  132. window.localStorage.setItem("userCode", null);
  133. }
  134. /**
  135. * 判断是否有登陆
  136. * @param data
  137. */
  138. function isLogin(data){
  139. if(data.errorCode==not_login){
  140. alert("请重新登陆")
  141. window.location.href = '/login/login.html';
  142. }
  143. }