index.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // 绘制logo
  2. export function drawLogo(text = '', len = 4) {
  3. if (text.length > 4) {
  4. text = text.split('').filter((item) => /^[\u4E00-\u9FA5]+$/.test(item))
  5. }
  6. if (!document) return
  7. const canvas = document.createElement('canvas')
  8. canvas.width = 400
  9. canvas.height = 400
  10. if (canvas.getContext) {
  11. // 绘制流程
  12. var ctx = canvas.getContext('2d')
  13. ctx.fillStyle = '#130f40'
  14. ctx.fillRect(0, 0, 400, 400)
  15. // 绘制第一个字
  16. ctx.fillStyle = '#eee'
  17. ctx.font = '80px 黑体'
  18. text[0] && ctx.fillText(text[0], 80, 160)
  19. text[1] && ctx.fillText(text[1], 220, 160)
  20. text[2] && ctx.fillText(text[2], 80, 300)
  21. text[3] && ctx.fillText(text[3], 220, 300)
  22. }
  23. return canvas.toDataURL()
  24. }
  25. // 跳转到授权页面
  26. export function toAuthorization(appId, payload) {
  27. const type = payload.routePrefix.split('/')[2]
  28. console.log(process.env.LOCALHOSE)
  29. const url = `${process.env.LOCALHOSE}/auth?authUserId=${payload.authUserId}&type=${type}&appId=${appId}`
  30. const redirect_uri = encodeURIComponent(url)
  31. window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`
  32. }
  33. /** 时间格式化
  34. * @param {dateTime} date 标准时间格式 -> new Date()
  35. * @param {string} format 时间格式化的格式 'yyyy-MM-dd hh:mm:ss'
  36. * @returns {string} 格式化后的时间 '2017-01-01 01:00:00'
  37. */
  38. export function dateFormat(date = new Date(), format = 'yyyy-MM-dd hh:mm:ss') {
  39. var o = {
  40. 'M+': date.getMonth() + 1, // month
  41. 'd+': date.getDate(), // day
  42. 'h+': date.getHours(), // hour
  43. 'm+': date.getMinutes(), // minute
  44. 's+': date.getSeconds(), // second
  45. 'q+': Math.floor((date.getMonth() + 3) / 3), // quarter
  46. S: date.getMilliseconds(), // millisecond
  47. }
  48. if (/(y+)/.test(format)) {
  49. format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  50. }
  51. for (var k in o) {
  52. if (new RegExp('(' + k + ')').test(format)) {
  53. format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length))
  54. }
  55. }
  56. return format
  57. }
  58. /**
  59. * @param {string} url
  60. * @returns {Object}
  61. */
  62. export function getQueryObject(url) {
  63. url = url == null ? window.location.href : url
  64. const search = url.substring(url.lastIndexOf('?') + 1)
  65. const obj = {}
  66. const reg = /([^?&=]+)=([^?&=]*)/g
  67. search.replace(reg, (rs, $1, $2) => {
  68. const name = decodeURIComponent($1)
  69. let val = decodeURIComponent($2)
  70. val = String(val)
  71. obj[name] = val
  72. return rs
  73. })
  74. return obj
  75. }
  76. /**
  77. * 防抖
  78. * @param {Function} func 需要包装的函数
  79. * @param {string} wait 等待执行时间
  80. * @param {string} immediate 是否是立即执行 默认不立即执行
  81. * @returns {Function} 返回包装后的函数
  82. */
  83. export function debounce(func, wait, immediate) {
  84. let timeout, result
  85. return function () {
  86. const context = this
  87. const args = arguments
  88. if (timeout) clearTimeout(timeout)
  89. if (immediate) {
  90. const callNow = !timeout
  91. timeout = setTimeout(function () {
  92. timeout = null
  93. }, wait)
  94. if (callNow) result = func.apply(context, args)
  95. } else {
  96. timeout = setTimeout(function () {
  97. func.apply(context, args)
  98. }, wait)
  99. }
  100. return result
  101. }
  102. }
  103. export function callMobile(mobile) {
  104. if (!mobile) return
  105. const a = document.createElement('a')
  106. a.href = 'tel:' + mobile
  107. a.click()
  108. }
  109. /**
  110. * Merges one object to another object
  111. * @param {Object} target
  112. * @param {(Object)} source
  113. * @returns {Object}
  114. */
  115. export function objectCover(target, source) {
  116. for (const key in source) {
  117. if (Object.hasOwnProperty.call(target, key)) {
  118. target[key] = source[key]
  119. }
  120. }
  121. return target
  122. }
  123. // 获取视频封面
  124. export function getVideoBase64(url) {
  125. return new Promise((resolve, reject) => {
  126. const video = document.createElement('video')
  127. video.setAttribute('crossOrigin', 'anonlymous')
  128. video.setAttribute('preload', 'auto')
  129. video.setAttribute('src', url)
  130. video.style.display = 'none'
  131. if (/iPad|iPhone|iPod/.test(navigator.userAgent)) {
  132. video.setAttribute('autoplay', true)
  133. }
  134. video.onloadeddata = () => {
  135. const canvas = document.createElement('canvas')
  136. const width = video.videoWidth
  137. const height = video.videoHeight
  138. canvas.width = width
  139. canvas.height = height
  140. canvas.getContext('2d').drawImage(video, 0, 0, width, height)
  141. // dataUrl = canvas.toDataURL('image/jpeg')
  142. canvas.toBlob((data) => {
  143. resolve(data)
  144. })
  145. }
  146. })
  147. }
  148. /**
  149. * 加载图片
  150. * @param {string} url 图片链接
  151. */
  152. export function loadImage(url, width, height) {
  153. return new Promise((resolve, reject) => {
  154. const image = new Image()
  155. image.width = width
  156. image.height = height
  157. image.src = url
  158. image.onload = () => {
  159. resolve(image)
  160. }
  161. image.onerror = (err) => {
  162. reject(err)
  163. }
  164. })
  165. }