util.js 1.7 KB

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