util.js 2.2 KB

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