index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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(
  50. RegExp.$1,
  51. (date.getFullYear() + '').substr(4 - RegExp.$1.length)
  52. )
  53. }
  54. for (var k in o) {
  55. if (new RegExp('(' + k + ')').test(format)) {
  56. format = format.replace(
  57. RegExp.$1,
  58. RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)
  59. )
  60. }
  61. }
  62. return format
  63. }
  64. /**
  65. * @param {string} url
  66. * @returns {Object}
  67. */
  68. export function getQueryObject(url) {
  69. url = url == null ? window.location.href : url
  70. const search = url.substring(url.lastIndexOf('?') + 1)
  71. const obj = {}
  72. const reg = /([^?&=]+)=([^?&=]*)/g
  73. search.replace(reg, (rs, $1, $2) => {
  74. const name = decodeURIComponent($1)
  75. let val = decodeURIComponent($2)
  76. val = String(val)
  77. obj[name] = val
  78. return rs
  79. })
  80. return obj
  81. }
  82. /**
  83. * 防抖
  84. * @param {Function} func 需要包装的函数
  85. * @param {string} wait 等待执行时间
  86. * @param {string} immediate 是否是立即执行 默认不立即执行
  87. * @returns {Function} 返回包装后的函数
  88. */
  89. export function debounce(func, wait, immediate) {
  90. let timeout, result
  91. return function () {
  92. const context = this
  93. const args = arguments
  94. if (timeout) clearTimeout(timeout)
  95. if (immediate) {
  96. const callNow = !timeout
  97. timeout = setTimeout(function () {
  98. timeout = null
  99. }, wait)
  100. if (callNow) result = func.apply(context, args)
  101. } else {
  102. timeout = setTimeout(function () {
  103. func.apply(context, args)
  104. }, wait)
  105. }
  106. return result
  107. }
  108. }
  109. export function callMobile(mobile) {
  110. if (!mobile) return
  111. const a = document.createElement('a')
  112. a.href = 'tel:' + mobile
  113. a.click()
  114. }
  115. /**
  116. * Merges one object to another object
  117. * @param {Object} target
  118. * @param {(Object)} source
  119. * @returns {Object}
  120. */
  121. export function objectCover(target, source) {
  122. for (const key in source) {
  123. if (Object.hasOwnProperty.call(target, key)) {
  124. target[key] = source[key]
  125. }
  126. }
  127. return target
  128. }