index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. const url = `${process.env.LOCALHOSE}/auth?authUserId=${payload.authUserId}&type=${type}&appId=${appId}`
  29. const redirect_uri = encodeURIComponent(url)
  30. 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`
  31. }
  32. /** 时间格式化
  33. * @param {dateTime} date 标准时间格式 -> new Date()
  34. * @param {string} format 时间格式化的格式 'yyyy-MM-dd hh:mm:ss'
  35. * @returns {string} 格式化后的时间 '2017-01-01 01:00:00'
  36. */
  37. export function dateFormat(date = new Date(), format = 'yyyy-MM-dd hh:mm:ss') {
  38. var o = {
  39. 'M+': date.getMonth() + 1, // month
  40. 'd+': date.getDate(), // day
  41. 'h+': date.getHours(), // hour
  42. 'm+': date.getMinutes(), // minute
  43. 's+': date.getSeconds(), // second
  44. 'q+': Math.floor((date.getMonth() + 3) / 3), // quarter
  45. S: date.getMilliseconds(), // millisecond
  46. }
  47. if (/(y+)/.test(format)) {
  48. format = format.replace(
  49. RegExp.$1,
  50. (date.getFullYear() + '').substr(4 - RegExp.$1.length)
  51. )
  52. }
  53. for (var k in o) {
  54. if (new RegExp('(' + k + ')').test(format)) {
  55. format = format.replace(
  56. RegExp.$1,
  57. RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)
  58. )
  59. }
  60. }
  61. return format
  62. }
  63. /**
  64. * @param {string} url
  65. * @returns {Object}
  66. */
  67. export function getQueryObject(url) {
  68. url = url == null ? window.location.href : url
  69. const search = url.substring(url.lastIndexOf('?') + 1)
  70. const obj = {}
  71. const reg = /([^?&=]+)=([^?&=]*)/g
  72. search.replace(reg, (rs, $1, $2) => {
  73. const name = decodeURIComponent($1)
  74. let val = decodeURIComponent($2)
  75. val = String(val)
  76. obj[name] = val
  77. return rs
  78. })
  79. return obj
  80. }
  81. /**
  82. * 防抖
  83. * @param {Function} func 需要包装的函数
  84. * @param {string} wait 等待执行时间
  85. * @param {string} immediate 是否是立即执行 默认不立即执行
  86. * @returns {Function} 返回包装后的函数
  87. */
  88. export function debounce(func, wait, immediate) {
  89. let timeout, result
  90. return function () {
  91. const context = this
  92. const args = arguments
  93. if (timeout) clearTimeout(timeout)
  94. if (immediate) {
  95. const callNow = !timeout
  96. timeout = setTimeout(function () {
  97. timeout = null
  98. }, wait)
  99. if (callNow) result = func.apply(context, args)
  100. } else {
  101. timeout = setTimeout(function () {
  102. func.apply(context, args)
  103. }, wait)
  104. }
  105. return result
  106. }
  107. }
  108. // 生成a标签下载文件
  109. export function downloadUrlLink(url) {
  110. console.log(url)
  111. const a = document.createElement('a')
  112. a.setAttribute('download', true)
  113. a.setAttribute('href', url)
  114. a.setAttribute('target', '_blank')
  115. a.style.display = 'none'
  116. document.body.appendChild(a)
  117. a.click()
  118. document.body.removeChild(a)
  119. }