index.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import { Progress } from 'element-ui'
  2. // 绘制logo
  3. export function drawLogo(text = '', len = 4) {
  4. if (text.length > 4) {
  5. text = text.split('').filter((item) => /^[\u4E00-\u9FA5]+$/.test(item))
  6. }
  7. if (!document) return
  8. const canvas = document.createElement('canvas')
  9. canvas.width = 400
  10. canvas.height = 400
  11. if (canvas.getContext) {
  12. // 绘制流程
  13. var ctx = canvas.getContext('2d')
  14. ctx.fillStyle = '#130f40'
  15. ctx.fillRect(0, 0, 400, 400)
  16. // 绘制第一个字
  17. ctx.fillStyle = '#eee'
  18. ctx.font = '80px 黑体'
  19. text[0] && ctx.fillText(text[0], 80, 160)
  20. text[1] && ctx.fillText(text[1], 220, 160)
  21. text[2] && ctx.fillText(text[2], 80, 300)
  22. text[3] && ctx.fillText(text[3], 220, 300)
  23. }
  24. return canvas.toDataURL()
  25. }
  26. // 跳转到授权页面
  27. export function toAuthorization(appId, payload) {
  28. const type = payload.routePrefix.split('/')[2]
  29. console.log(process.env.LOCALHOSE)
  30. const url = `${process.env.LOCALHOSE}/auth?authUserId=${payload.authUserId}&type=${type}&appId=${appId}`
  31. const redirect_uri = encodeURIComponent(url)
  32. 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`
  33. }
  34. /** 时间格式化
  35. * @param {dateTime} date 标准时间格式 -> new Date()
  36. * @param {string} format 时间格式化的格式 'yyyy-MM-dd hh:mm:ss'
  37. * @returns {string} 格式化后的时间 '2017-01-01 01:00:00'
  38. */
  39. export function dateFormat(date = new Date(), format = 'yyyy-MM-dd hh:mm:ss') {
  40. var o = {
  41. 'M+': date.getMonth() + 1, // month
  42. 'd+': date.getDate(), // day
  43. 'h+': date.getHours(), // hour
  44. 'm+': date.getMinutes(), // minute
  45. 's+': date.getSeconds(), // second
  46. 'q+': Math.floor((date.getMonth() + 3) / 3), // quarter
  47. S: date.getMilliseconds(), // millisecond
  48. }
  49. if (/(y+)/.test(format)) {
  50. format = format.replace(
  51. RegExp.$1,
  52. (date.getFullYear() + '').substr(4 - RegExp.$1.length)
  53. )
  54. }
  55. for (var k in o) {
  56. if (new RegExp('(' + k + ')').test(format)) {
  57. format = format.replace(
  58. RegExp.$1,
  59. RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)
  60. )
  61. }
  62. }
  63. return format
  64. }
  65. /**
  66. * @param {string} url
  67. * @returns {Object}
  68. */
  69. export function getQueryObject(url) {
  70. url = url == null ? window.location.href : url
  71. const search = url.substring(url.lastIndexOf('?') + 1)
  72. const obj = {}
  73. const reg = /([^?&=]+)=([^?&=]*)/g
  74. search.replace(reg, (rs, $1, $2) => {
  75. const name = decodeURIComponent($1)
  76. let val = decodeURIComponent($2)
  77. val = String(val)
  78. obj[name] = val
  79. return rs
  80. })
  81. return obj
  82. }
  83. /**
  84. * 防抖
  85. * @param {Function} func 需要包装的函数
  86. * @param {string} wait 等待执行时间
  87. * @param {string} immediate 是否是立即执行 默认不立即执行
  88. * @returns {Function} 返回包装后的函数
  89. */
  90. export function debounce(func, wait, immediate) {
  91. let timeout, result
  92. return function () {
  93. const context = this
  94. const args = arguments
  95. if (timeout) clearTimeout(timeout)
  96. if (immediate) {
  97. const callNow = !timeout
  98. timeout = setTimeout(function () {
  99. timeout = null
  100. }, wait)
  101. if (callNow) result = func.apply(context, args)
  102. } else {
  103. timeout = setTimeout(function () {
  104. func.apply(context, args)
  105. }, wait)
  106. }
  107. return result
  108. }
  109. }
  110. // 生成a标签下载文件
  111. export function downloadUrlLink(url) {
  112. console.log(url)
  113. const a = document.createElement('a')
  114. a.setAttribute('download', true)
  115. a.setAttribute('href', url)
  116. a.setAttribute('target', '_blank')
  117. a.style.display = 'none'
  118. document.body.appendChild(a)
  119. a.click()
  120. document.body.removeChild(a)
  121. }
  122. // 下载方式2
  123. export async function downloadWithUrl(downUrl, fileName, self) {
  124. const h = self.$createElement
  125. const notification = self.$notify({
  126. title: '提示',
  127. message: h('div', {}, [
  128. h('div', {}, `正在下载${fileName},请勿重复操作!`),
  129. h(Progress, { props: { percentage: 100 } }),
  130. ]),
  131. duration: 0,
  132. })
  133. try {
  134. const data = await fetch(downUrl)
  135. const res = await data.blob()
  136. const a = document.createElement('a')
  137. a.href = URL.createObjectURL(res)
  138. a.download = fileName
  139. a.click()
  140. } catch (err) {
  141. self.$message.error(`下载${fileName}失败`)
  142. } finally {
  143. notification.close()
  144. }
  145. }
  146. export function callMobile(mobile) {
  147. if (!mobile) return
  148. const a = document.createElement('a')
  149. a.href = 'tel:' + mobile
  150. a.click()
  151. }
  152. /**
  153. * Merges one object to another object
  154. * @param {Object} target
  155. * @param {(Object)} source
  156. * @returns {Object}
  157. */
  158. export function objectCover(target, source) {
  159. for (const key in source) {
  160. if (Object.hasOwnProperty.call(target, key)) {
  161. target[key] = source[key]
  162. }
  163. }
  164. return target
  165. }