utils.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. function BeforeNavigateTo(params, needLogin) { //页面跳转勾子
  2. if (needLogin) {
  3. if (uni.getStorageSync('xj-storage_token')) {
  4. uni.navigateTo({
  5. animationType: 'pop-in',
  6. animationDuration: 300,
  7. ...params,
  8. fail: (e) => {
  9. plus.nativeUI.toast(e.errMsg);
  10. }
  11. })
  12. } else {
  13. uni.navigateTo({
  14. animationType: 'pop-in',
  15. animationDuration: 300,
  16. url: '/pages/native/login'
  17. })
  18. }
  19. } else {
  20. uni.navigateTo({
  21. animationType: 'pop-in',
  22. animationDuration: 300,
  23. ...params,
  24. })
  25. }
  26. }
  27. function BeforeSwitchTab(params) { //页面跳转勾子 针对tab
  28. if (true) {
  29. uni.switchTab({
  30. animationType: 'pop-in',
  31. animationDuration: 300,
  32. ...params,
  33. })
  34. } else {
  35. alert('未登录')
  36. }
  37. }
  38. function utf16toEntities(str = "") {
  39. var patt = /[\ud800-\udbff][\udc00-\udfff]/g;
  40. str = str.replace(patt, function(char) {
  41. var H, L, code;
  42. if (char.length === 2) {
  43. H = char.charCodeAt(0);
  44. L = char.charCodeAt(1);
  45. code = (H - 0xD800) * 0x400 + 0x10000 + L - 0xDC00;
  46. return "&#" + code + ";";
  47. } else {
  48. return char;
  49. }
  50. });
  51. return str;
  52. }
  53. function format(data,fmt) {
  54. const o = {
  55. "y+": data.getFullYear(),
  56. "M+": data.getMonth()+1,
  57. "d+": data.getDate(),
  58. "H+": data.getHours(),
  59. "m+": data.getMinutes(),
  60. "s+": data.getSeconds(),
  61. "S+": data.getMilliseconds(),
  62. "q+": Math.floor(data.getMonth()/3) + 1,
  63. "h+": (()=>{
  64. const hour=data.getHours()%12;
  65. return hour==0?12:hour;
  66. })(),
  67. "E+": (()=>{
  68. const week = {"0":"Sunday","1":"Monday","2":"Tuesday","3":"Wednesday","4":"Thursday","5":"Friday","6":"Saturday"};
  69. return week[data.getDay()+""];
  70. })(),
  71. "x1": (()=>{
  72. const week = {"0":"周日","1":"周一","2":"周二","3":"周三","4":"周四","5":"周五","6":"周六"};
  73. return week[data.getDay()+""];
  74. })(),
  75. "x2": (()=>{
  76. const hour = ["凌晨","早上","下午","晚上"];
  77. const h=data.getHours();
  78. if(h==12) return "中午";
  79. return hour[parseInt(h/6)];
  80. })(),
  81. }
  82. for(var k in o) {
  83. if(new RegExp("("+k+")", "g").test(fmt)) {
  84. const len = RegExp.$1.length;
  85. fmt = fmt.replace(RegExp.$1, len==1?o[k]:("00"+o[k]).substr(-len));
  86. }
  87. }
  88. return fmt;
  89. }
  90. function toWeiXinString(data) {
  91. let str;
  92. const now = new Date();
  93. const today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
  94. const yesterday = new Date(now.getFullYear(), now.getMonth(), now.getDate()-1);
  95. const beforeYesterday = new Date(now.getFullYear(), now.getMonth(), now.getDate()-2);
  96. const monday = new Date(today);
  97. monday.setDate(today.getDate()-(today.getDay()?today.getDay()-1:6));
  98. if(data.getTime() > today.getTime()) {
  99. str = "";
  100. } else if(data.getTime() > yesterday.getTime()) {
  101. str = "昨天";
  102. } else if(data.getTime() > beforeYesterday.getTime()) {
  103. str = "前天";
  104. } else if(data.getTime() > monday.getTime()) {
  105. const week = {"0":"周日","1":"周一","2":"周二","3":"周三","4":"周四","5":"周五","6":"周六"};
  106. str = week[data.getDay()+""];
  107. } else {
  108. const hour = ["凌晨","早上","下午","晚上"];
  109. const h=data.getHours();
  110. if(h==12) str = "中午";
  111. else str = hour[parseInt(h/6)];
  112. str = format(data,"MM月dd ") + str;
  113. }
  114. str += format(data,"HH:mm");
  115. return str;
  116. }
  117. export {
  118. BeforeNavigateTo,
  119. BeforeSwitchTab,
  120. utf16toEntities,
  121. toWeiXinString
  122. }