util.js 3.8 KB

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