index.js 3.5 KB

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