helper.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * desc: 辅助方法
  3. * author: wangyang
  4. * date: 2015-04-11
  5. */
  6. define(['config'], function(config) {
  7. return {
  8. //根据系统调用不同的函数
  9. osProxy: function(fn) {
  10. var os = '';
  11. if (config.isIOS) {
  12. os = 'ios';
  13. } else if (config.isAndroid) {
  14. os = 'android';
  15. } else if (config.isWX) {
  16. os = 'wx';
  17. } else if (config.isChubao) {
  18. os = 'cb'
  19. }
  20. if (!!fn[os]) {
  21. return fn[os].call();
  22. } else {
  23. // console.log(os + '-没有定义方法');
  24. return false;
  25. }
  26. },
  27. //动态载入样式
  28. includeStyleElement: function(styles, styleId) {
  29. if (document.getElementById(styleId)) {
  30. return false;
  31. }
  32. var style = document.createElement('style');
  33. style.id = styleId;
  34. (document.getElementsByTagName('head')[0] || document.body).appendChild(style);
  35. if (style.styleSheet) { //for ie
  36. style.styleSheet.cssText = styles;
  37. } else {//for w3c
  38. style.appendChild(document.createTextNode(styles));
  39. }
  40. },
  41. //获取当前的日期字符串
  42. getDateStr: function(options) {
  43. var defaultOptions = {
  44. date: new Date(),
  45. separator: ''
  46. };
  47. var finalOptions = $.extend(true, defaultOptions, options);
  48. var year = finalOptions.date.getFullYear();
  49. var month = finalOptions.date.getMonth() + 1;
  50. if (month < 10) {
  51. month = '0' + month;
  52. }
  53. var day = finalOptions.date.getDate();
  54. if (day < 10) {
  55. day = '0' + day;
  56. }
  57. return year + finalOptions.separator + month + finalOptions.separator + day;
  58. },
  59. //返回倒计时字符串
  60. formatCountDown: function(left_value) {
  61. var hour, minite, second;
  62. hour = parseInt(left_value / 3600);
  63. left_value -= 3600 * hour;
  64. minite = parseInt(left_value / 60);
  65. second = left_value - 60 * minite;
  66. if (minite < 10) {
  67. minite = '0' + minite;
  68. }
  69. if (second < 10) {
  70. second = '0' + second;
  71. }
  72. return hour + ' : ' + minite + ' : ' + second;
  73. }
  74. }
  75. })