util.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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) => {
  21. // 配置webview链接
  22. const envConfig = {
  23. // 开发环境和测试环境
  24. development: 'https://sit-kelamayi.4dage.com/mini/#',
  25. // 正式环境
  26. production: 'https://kelamayi.4dage.com/mini/#'
  27. };
  28. // 获取当前环境
  29. const currentEnv = wx.getAccountInfoSync().miniProgram.envVersion || 'develop';
  30. let baseUrl = envConfig.development;
  31. if (currentEnv === 'release') {
  32. baseUrl = envConfig.production;
  33. }
  34. // 检查path中是否已经包含isFrom参数,避免重复添加
  35. let fullUrl;
  36. if (path.includes('isFrom=weixin')) {
  37. fullUrl = `${baseUrl}${path}`;
  38. } else {
  39. const separator = path.includes('?') ? '&' : '?';
  40. fullUrl = `${baseUrl}${path}${separator}isFrom=weixin`;
  41. }
  42. wx.navigateTo({
  43. url: `/pages/webview/index?url=${encodeURIComponent(fullUrl)}`
  44. });
  45. };
  46. module.exports = {
  47. formatTime,
  48. navigateToWebview
  49. }