util.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import dayjs from 'dayjs'
  2. import duration from 'dayjs/plugin/duration'
  3. dayjs.extend(duration);
  4. const systemInfo = uni.getSystemInfoSync();
  5. <<<<<<< HEAD
  6. const Language = (systemInfo.language == 'zh-CN' || systemInfo.language == 'zh_CN') ? 'zh' : 'en' // 'zh' | 'en'
  7. =======
  8. // const Language = systemInfo.language == 'zh_CN' ? 'zh' : 'en' // 'zh' | 'en'
  9. const Language = 'en' // 'zh' | 'en'
  10. >>>>>>> xxq_test
  11. const formatTime = (date) => {
  12. const year = date.getFullYear();
  13. const month = date.getMonth() + 1;
  14. const day = date.getDate();
  15. const hour = date.getHours();
  16. const minute = date.getMinutes();
  17. const second = date.getSeconds();
  18. return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(
  19. ':');
  20. };
  21. const getLocation = async () => {
  22. let res = await uni.getLocation({
  23. type: 'wgs84',
  24. // success: function (res) {
  25. // console.log('当前位置的经度:' + res.longitude);
  26. // console.log('当前位置的纬度:' + res.latitude);
  27. // }
  28. });
  29. return res[1]
  30. }
  31. const msg = (str) => {
  32. uni.showToast({
  33. title: str,
  34. icon:'none'
  35. })
  36. }
  37. /**
  38. * 计算剩余时间
  39. * @param {string|dayjs.Dayjs} startTime - 开始时间(字符串或dayjs对象)
  40. * @param {string|dayjs.Dayjs} endTime - 结束时间(字符串或dayjs对象)
  41. * @returns {string} - 返回剩余时间,格式为 "X天Y小时Z分钟" 或 "<小于1分钟"
  42. */
  43. function getRemainingTime(startTime, endTime,type = 0) {
  44. // 确保时间是dayjs对象
  45. const start = dayjs(startTime);
  46. const end = dayjs(endTime);
  47. // 计算时间差
  48. const diff = dayjs.duration(end.diff(start));
  49. // 如果小于1分钟
  50. if (diff.asMinutes() < 1) {
  51. return '<小于1分钟';
  52. }
  53. // 获取天数、小时数和分钟数
  54. const days = diff.days();
  55. const hours = diff.hours();
  56. const minutes = diff.minutes();
  57. if(type){
  58. return {
  59. days,
  60. hours,
  61. minutes,
  62. }
  63. }
  64. // 返回格式化后的字符串
  65. return `${days}天${hours}小时${minutes}分`;
  66. }
  67. //价格转换
  68. function fenToYuan(fen) {
  69. const roundedFen = Math.round(Number(fen));
  70. return Number((roundedFen / 100).toFixed(2));
  71. }
  72. const strJoin = (str) => {
  73. if (!str) return []
  74. return JSON.parse(str)
  75. }
  76. const formatNumber = (n) => {
  77. n = n.toString();
  78. return n[1] ? n : '0' + n;
  79. };
  80. function isEmail(email) {
  81. if(!email) return false
  82. // 正则表达式用于匹配邮箱格式
  83. const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
  84. return emailRegex.test(email);
  85. }
  86. module.exports = {
  87. Language,
  88. isEmail,
  89. formatTime: formatTime,
  90. getLocation,
  91. msg,
  92. strJoin,
  93. fenToYuan,
  94. getRemainingTime
  95. };