util.js 2.3 KB

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