util.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. console.log('goToBookDetail option:',options)
  17. // 检查必要参数
  18. if (!options || (!options.bookId || !options.wxBookId)) {
  19. console.warn('书籍ID不能为空');
  20. return;
  21. }
  22. let url = '';
  23. // 根据不同参数构建不同的URL
  24. if (options.wxBookId) {
  25. // 微信书籍ID跳转
  26. url = `plugin-private://wx293c4b6097a8a4d0/pages/novel/index?bookId=${options.wxBookId}`;
  27. // 如果有章节ID,添加到URL
  28. if (options.chapterId) {
  29. url += `&chapterId=${options.chapterId}`;
  30. }
  31. if (options.bookId) {
  32. url += `&innerBookId=${options.bookId}`;
  33. }
  34. }
  35. wx.navigateTo({
  36. url,
  37. fail: (err) => {
  38. console.error('跳转书籍详情页失败:', err);
  39. console.log('url',url);
  40. wx.showToast({
  41. title: '跳转失败,请重试',
  42. icon: 'none'
  43. });
  44. }
  45. });
  46. };
  47. // 随机整数方法
  48. export const rnd = (min = 0, max = 1) => {
  49. const random = Math.floor(Math.random() * (max - min + 1) + min)
  50. return random
  51. };
  52. // 随机整数方法
  53. export const rndone = (self = []) => {
  54. return self.length > 0 ? self[rnd(0, self.length - 1)] : ''
  55. };
  56. /**
  57. * 拷贝
  58. */
  59. export function deepCopy(data) {
  60. let t = typeof data
  61. if (data instanceof Array) {
  62. t = 'array'
  63. }
  64. let o
  65. if (t === 'array') {
  66. o = []
  67. for (let i = 0; i < data.length; i++) {
  68. o.push(deepCopy(data[i]))
  69. }
  70. } else if (t === 'object') {
  71. o = {}
  72. for (let i in data) {
  73. o[i] = deepCopy(data[i])
  74. }
  75. } else {
  76. return data
  77. }
  78. return o
  79. }
  80. module.exports = {
  81. formatTime,
  82. goToBookDetail,
  83. rnd,
  84. rndone,
  85. deepCopy
  86. }