utils.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import { http } from './http'
  2. import { store } from '../store/index'
  3. /**
  4. * 时间格式化
  5. * @param {Date} date - 时间
  6. */
  7. export const formatTime = date => {
  8. const year = date.getFullYear()
  9. const month = date.getMonth() + 1
  10. const day = date.getDate()
  11. const hour = date.getHours()
  12. const minute = date.getMinutes()
  13. const second = date.getSeconds()
  14. return (
  15. [year, month, day].map(formatNumber).join('/') +
  16. ' ' +
  17. [hour, minute, second].map(formatNumber).join(':')
  18. )
  19. }
  20. export const formatNumber = n => {
  21. n = n.toString()
  22. return n[1] ? n : '0' + n
  23. }
  24. /**
  25. *
  26. */
  27. export const queryString = query => {
  28. let qry = []
  29. for (const key in query) {
  30. qry.push(`${key}=${query[key]}`)
  31. }
  32. return qry.join('&')
  33. }
  34. export const queryObject = url => {
  35. const u = decodeURIComponent(decodeURIComponent(url))
  36. let query = ''
  37. if (u.includes('?')) {
  38. query = u.split('?')[1].split('&')
  39. } else {
  40. query = u.split('?')[0].split('&')
  41. }
  42. const queryObject = {}
  43. query.map(item => {
  44. if (!item.includes('=')) return
  45. const obj = item.split('=')
  46. const key = obj[0]
  47. const val = obj[1]
  48. queryObject[key] = val
  49. })
  50. return queryObject
  51. }
  52. export const loginRedirect = () => {
  53. // wx.showToast({ title: '请先登录', icon: 'none' })
  54. const paths = getCurrentPages()
  55. const currentPage = paths[paths.length - 1]
  56. const uri = encodeURIComponent(
  57. `/${currentPage.route}?${queryString(currentPage.options)}`
  58. )
  59. wx.redirectTo({
  60. url: `/pages/login/login?uri=${uri}`,
  61. })
  62. }
  63. /**
  64. * html 实体符 map
  65. * 其他:替换rich-text不支持的字符,图片宽度限制 配合 css class raw-image
  66. * @type {Object}
  67. */
  68. const unescapeHTMLMap = {
  69. '&': /&/g,
  70. '<': /&lt;/g,
  71. '>': /&gt;/g,
  72. '"': /&quot;/g,
  73. '”': /&rdquo;/g,
  74. '“': /&ldquo;/g,
  75. "'": /&apos;/g,
  76. '/': /&#x2f;/g,
  77. ' ': /&(.{2,7});/g,
  78. '<div': /<section/g,
  79. '</div>': /<\/section>/g,
  80. '<img$1 class="raw-image" />': /<img+(.*?)\/>/g,
  81. }
  82. /**
  83. * 解码 html 实体符
  84. * @param {String} str html
  85. * @returns {String}
  86. */
  87. export const unescapeHTML = str => {
  88. let htmlStr = str.replace(/&#(x)?([\w\d]{0,5});/gi, (full, hex, code) =>
  89. String.fromCharCode(parseInt(code, hex ? 16 : 10))
  90. )
  91. Object.keys(unescapeHTMLMap).map(src => {
  92. htmlStr = htmlStr.replace(unescapeHTMLMap[src], src)
  93. // htmlStr = htmlStr.replace(/<img/g, '<img width=100%')
  94. return htmlStr
  95. })
  96. return htmlStr
  97. }
  98. /**
  99. * 跳转直播间
  100. * @param {*} id -直播间id
  101. * @param {*} status -直播间状态
  102. */
  103. export const goLive = (id, status) => {
  104. if (status == 101) {
  105. http.post('/live/live/roomAction', { room_id: id, type: 1 })
  106. wx.navigateTo({
  107. url: `${store.config.live_plugin}?room_id=${id}`,
  108. })
  109. } else {
  110. let title = ''
  111. switch (status * 1) {
  112. case 102:
  113. title = '未开始'
  114. break
  115. case 103:
  116. title = '已结束'
  117. break
  118. case 104:
  119. title = '已被禁播'
  120. break
  121. case 105:
  122. title = '暂停中'
  123. break
  124. case 106:
  125. title = '异常'
  126. break
  127. case 107:
  128. title = '已过期'
  129. break
  130. }
  131. wx.showToast({
  132. title: '直播' + title,
  133. icon: 'none',
  134. })
  135. }
  136. }
  137. /**
  138. * 新人专享活动 0 距离开始大于1天 1 未开始(24小时内) 2 进行中 3 已结束
  139. * @param start -开始时间戳
  140. * @param end -结束时间戳
  141. */
  142. export const actStatus = (start, end) => {
  143. let now = new Date().getTime() / 1000
  144. start = start * 1
  145. end = end * 1
  146. // 未开始状态
  147. if (now < start) {
  148. if (start - now > 86400) {
  149. return 0
  150. } else {
  151. return 1
  152. }
  153. } else if (now >= end) {
  154. return 3
  155. } else {
  156. return 2
  157. }
  158. }