util.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. const formatTime = date => {
  2. // 确保date是Date对象
  3. const dateObj = date instanceof Date ? date : new Date(date)
  4. const year = dateObj.getFullYear()
  5. const month = dateObj.getMonth() + 1
  6. const day = dateObj.getDate()
  7. const hour = dateObj.getHours()
  8. const minute = dateObj.getMinutes()
  9. const second = dateObj.getSeconds()
  10. return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
  11. }
  12. const formatNumber = n => {
  13. n = n.toString()
  14. return n[1] ? n : `0${n}`
  15. }
  16. /**
  17. * 导航到webview页面
  18. * @param {string} path - 页面路径
  19. */
  20. const navigateToWebview = (path, mode) => {
  21. // 检查链接中是否包含type=activity
  22. if (path.includes('type=activity') && !mode) {
  23. // 提取链接中的id参数
  24. const idMatch = path.match(/[?&]id=([^&]*)/);
  25. if (idMatch) {
  26. const activityId = idMatch[1];
  27. console.log('检测到activity类型链接,跳转到活动详情页,id:', activityId);
  28. wx.navigateTo({
  29. url: `/pages/exhibition/activeDetails/index?isFrom=weixin&id=${activityId}&type=activity`
  30. });
  31. return;
  32. }
  33. }
  34. let fullUrl;
  35. // 如果mode为'open',直接使用原始链接
  36. if (mode === 'open' || path.includes('https://')) {
  37. fullUrl = path;
  38. } else {
  39. // 配置webview链接
  40. const envConfig = {
  41. // 开发环境和测试环境
  42. development: 'https://sit-kelamayi.4dage.com/mini/#',
  43. // 正式环境
  44. production: 'https://kelamayi.4dage.com/mini/#'
  45. };
  46. // 获取当前环境
  47. const currentEnv = wx.getAccountInfoSync().miniProgram.envVersion || 'develop';
  48. let baseUrl = envConfig.development;
  49. if (currentEnv === 'release') {
  50. baseUrl = envConfig.production;
  51. }
  52. // 检查path中是否已经包含isFrom参数,避免重复添加
  53. if (path.includes('isFrom=weixin')) {
  54. fullUrl = `${baseUrl}${path}`;
  55. } else {
  56. const separator = path.includes('?') ? '&' : '?';
  57. fullUrl = `${baseUrl}${path}${separator}isFrom=weixin`;
  58. }
  59. }
  60. wx.navigateTo({
  61. url: `/pages/webview/index?url=${encodeURIComponent(fullUrl)}`
  62. });
  63. };
  64. module.exports = {
  65. formatTime,
  66. navigateToWebview
  67. }