util.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. const formatTime = date => {
  2. const year = date.getFullYear()
  3. const month = date.getMonth() + 1
  4. const day = date.getDate()
  5. const hour = date.getHours()
  6. const minute = date.getMinutes()
  7. const second = date.getSeconds()
  8. return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
  9. }
  10. const formatNumber = n => {
  11. n = n.toString()
  12. return n[1] ? n : `0${n}`
  13. }
  14. // 跳转到书籍详情页
  15. export const goToBookDetail = (options) => {
  16. // 检查必要参数
  17. if (!options || (!options.bookId || !options.wxBookId)) {
  18. console.warn('书籍ID不能为空');
  19. return;
  20. }
  21. let url = '';
  22. // 根据不同参数构建不同的URL
  23. if (options.wxBookId) {
  24. // 微信书籍ID跳转
  25. url = `plugin-private://wx293c4b6097a8a4d0/pages/novel/index?bookId=${options.wxBookId}`;
  26. // 如果有章节ID,添加到URL
  27. if (options.chapterId) {
  28. url += `&chapterId=${options.chapterId}`;
  29. }
  30. if (options.bookId) {
  31. url += `&innerBookId=${options.bookId}`;
  32. }
  33. }
  34. wx.navigateTo({
  35. url,
  36. fail: (err) => {
  37. console.error('跳转书籍详情页失败:', err);
  38. console.log('url',url);
  39. wx.showToast({
  40. title: '跳转失败,请重试',
  41. icon: 'none'
  42. });
  43. }
  44. });
  45. };
  46. module.exports = {
  47. formatTime,
  48. goToBookDetail
  49. }