util.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. // 随机整数方法
  47. export const rnd = (min = 0, max = 1) => {
  48. const random = Math.floor(Math.random() * (max - min + 1) + min)
  49. return random
  50. };
  51. // 随机整数方法
  52. export const rndone = (self = []) => {
  53. return self.length > 0 ? self[rnd(0, self.length - 1)] : ''
  54. };
  55. /**
  56. * 拷贝
  57. */
  58. export function deepCopy(data) {
  59. let t = typeof data
  60. if (data instanceof Array) {
  61. t = 'array'
  62. }
  63. let o
  64. if (t === 'array') {
  65. o = []
  66. for (let i = 0; i < data.length; i++) {
  67. o.push(deepCopy(data[i]))
  68. }
  69. } else if (t === 'object') {
  70. o = {}
  71. for (let i in data) {
  72. o[i] = deepCopy(data[i])
  73. }
  74. } else {
  75. return data
  76. }
  77. return o
  78. }
  79. module.exports = {
  80. formatTime,
  81. goToBookDetail,
  82. rnd,
  83. rndone,
  84. deepCopy
  85. }