经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » jQuery » 查看文章
常用的几个方法封装 - angle-xiu
来源:cnblogs  作者:angle-xiu  时间:2020/12/8 9:05:15  对本文有异议

常用的方法封装

  1. $('document').ready(function () {
  2. $.extend({
  3. /*Function sendAjax
  4. * @param: obj Object
  5. * obj.url 地址必填
  6. * obj.modelData 数据选填
  7. */
  8. sendAjax: function (obj) {
  9. // 检测用户是否输入
  10. if(!obj.url){
  11. console.error('请填写url地址');
  12. return false;
  13. }
  14. // 请求参数格式化,均以json格式进行传参
  15. var data = JSON.stringify(obj.modelData) || {};
  16. $.ajax({
  17. url:obj.url,
  18. data:data,
  19. contentType:'application/json',
  20. dataType:'json',
  21. async:ture,
  22. }).done(function(res){
  23. switch(res.code){
  24. // 请求成功
  25. case 1:
  26. obj.success && obj.success(res);
  27. break;
  28. case 911:{
  29. // 无权限
  30. var redirectUrl = window.location.href;
  31. window.location.href = res.url+'?redirect'+encodeURIComponent(redirectUrl);
  32. break;
  33. }
  34. default:
  35. if(obj.fail()){
  36. obj.fail(res);
  37. }else
  38. alert('接口错误');
  39. }
  40. })
  41. },
  42. /*
  43. * @Function timeChange
  44. * @param:
  45. * source: String 需要转换的时间
  46. * inFormat: String 传入的时间格式
  47. * outFormat: Strign 输出的时间格式
  48. */
  49. timeChange:function(source,inFormat,outFormat){
  50. //个位补零
  51. var checkTime = function(){
  52. if(time <10){
  53. return "0"+time;
  54. }
  55. };
  56. // 根据输入的时间格式来进行转换,\2指的是第二个捕获组
  57. switch (inFormat){
  58. case 'Y-m-d H:i:s':
  59. var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;
  60. source = source.match(reg);
  61. source = new Date(source[1],source[3]-1,source[4],source[5],source[6],source[7]);
  62. break;
  63. case 'Y-m-d' :
  64. var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/;
  65. source = source.match(reg);
  66. source = new Date(source[1],source[3]-1,source[4])
  67. break;
  68. case 'timestamp':
  69. // 通常时间戳按秒记录,JavaScript是按毫秒计算
  70. source = new Date(parseInt(source)*1000);
  71. break;
  72. // 毫秒的时间戳
  73. case 'millisecond':
  74. source = new Date(parseInt(source));
  75. break;
  76. }
  77. // 输出时间
  78. switch(outFormat){
  79. case 'Y-m-d H:i:s':
  80. return source.getFullYear()
  81. +'-'
  82. +checkTime(source.getMonth()+1)
  83. +'-'
  84. +checkTime(source.getDate())
  85. +' '
  86. +checkTime(source.getHours())
  87. +':'
  88. +checkTime(source.getMinutes())
  89. +':'
  90. +checkTime(source.getSecond());
  91. break;
  92. case 'Y-m-d':
  93. return source.getFullYear()
  94. +'-'
  95. +checkTime(source.getMonth()+1)
  96. +'-'
  97. +checkTime(source.getDate());
  98. break;
  99. case 'Y-m-d H:i':
  100. return source.getFullYear()
  101. +'-'
  102. +checkTime(source.getMonth()+1)
  103. +'-'
  104. +checkTime(source.getDate())
  105. +' '
  106. +checkTime(source.getHours())
  107. +':'
  108. +checkTime(source.getMinutes());
  109. break;
  110. case 'Y.m.d':
  111. return source.getFullYear()
  112. +'.'
  113. +checkTime(source.getMonth()+1)
  114. +'.'
  115. +checkTime(source.getDate());
  116. break;
  117. }
  118. },
  119. // 比较数组全等
  120. /*Function compareJson
  121. * @param:
  122. * json1 Object 传入的对象
  123. * json2 Object
  124. */
  125. compareJson:function(json1,json2){
  126. for(let atr in json1){
  127. // 检测键是否一致
  128. if(json2[atr] === undefined)
  129. return false;
  130. // 检测值是否一致
  131. if(json1[atr] !== json2[atr])
  132. return false;
  133. }
  134. },
  135. // 数字转换
  136. /*Function numFormat
  137. * @param:
  138. * number Number 传入的数字
  139. * fixedNum Number 保留的小数点位数
  140. */
  141. numFormate:function(number,fixedNum){
  142. // 判断是否为数字
  143. if(typeof number !== Number){
  144. console.log('请检查输入的是否为数字');
  145. return false;
  146. }
  147. if(number<10000)
  148. return number;
  149. var level = null;
  150. if(number/10000 >1 )
  151. level = '万';
  152. if(number/10000000 > 1)
  153. level = '亿';
  154. // 默认保留两位
  155. if(!fixedNum)
  156. fixedNum = 2;
  157. switch(level){
  158. case '万':
  159. return (number/10000).toFixed(fixedNum)+'万';
  160. break;
  161. case '亿':
  162. return (num/100000000).toFixed(fixedNum)+'亿';
  163. }
  164. },
  165. })
  166. })

原文链接:http://www.cnblogs.com/angle-xiu/p/14030205.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号