util.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. $.ajax({
  92. type: 'PUT',
  93. url: url,
  94. data: data,
  95. success: success,
  96. dataType: "json"
  97. });
  98. }
  99. //delete方法
  100. function do_delete(url, data, success) {
  101. //获取用户的code
  102. var usercode = getUserCode();
  103. data.userCode = usercode;
  104. $.ajax({
  105. type: 'DELETE',
  106. url: url,
  107. data: data,
  108. success: success,
  109. dataType: "json"
  110. });
  111. }
  112. function getUserCode() {
  113. var code = window.localStorage.getItem("userCode");
  114. if (!code) {
  115. alert("请重新登陆")
  116. window.location.href = server+'/login/login.html';
  117. }
  118. return code
  119. }
  120. function setUserCode(code) {
  121. window.localStorage.setItem("userCode", code);
  122. }
  123. /**
  124. * 判断是否有登陆
  125. * @param data
  126. */
  127. function isLogin(data){
  128. if(data.errorCode==not_login){
  129. alert("请重新登陆")
  130. window.location.href = server+'/login/login.html';
  131. }
  132. }