1. html2canvas

  1. import html2canvas from 'html2canvas'
  2. import { Toast } from 'vant'
  3. export const doCut = function (shareDom, state) {
  4. console.log(shareDom.offsetHeight)
  5. html2canvas(shareDom, {
  6. dpi: window.devicePixelRatio * 2,
  7. scale: 1,
  8. allowTaint: true,
  9. useCORS: true,
  10. height: shareDom.offsetHeight,
  11. width: shareDom.offsetWidth,
  12. scrollY: 0,
  13. scrollX: 0
  14. }).then((canvas) => {
  15. const img = new Image()
  16. img.src = canvas.toDataURL('png')
  17. img.setAttribute('crossOrigin', 'anonymous')
  18. img.onload = function () {
  19. const imgUrl = canvas.toDataURL('image/png')
  20. // console.log(imgUrl)
  21. state.imageUrl = imgUrl
  22. Toast('海报已生成,长按后分享给好友')
  23. // saveFile(imgUrl, 'test.png')
  24. // 根据生成的图片地址imgUrl(base64)进行后续保存操作
  25. }
  26. })
  27. }
  28. // 保存图片
  29. // const saveFile = function (data, filename) {
  30. // const saveLink = document.createElementNS(
  31. // 'http://www.w3.org/1999/xhtml',
  32. // 'a'
  33. // )
  34. // saveLink.href = data
  35. // saveLink.download = filename
  36. // var event = document.createEvent('MouseEvents')
  37. // event.initMouseEvent(
  38. // 'click',
  39. // true,
  40. // false,
  41. // window,
  42. // 0,
  43. // 0,
  44. // 0,
  45. // 0,
  46. // 0,
  47. // false,
  48. // false,
  49. // false,
  50. // false,
  51. // 0,
  52. // null
  53. // )
  54. // saveLink.dispatchEvent(event)
  55. // }

2. cookie

  1. /*
  2. * @Description:
  3. * @Author: Harry
  4. * @Date: 2021-12-02 22:33:52
  5. * @Url: https://u.mr90.top
  6. * @github: https://github.com/rr210
  7. * @LastEditTime: 2021-12-18 20:53:35
  8. * @LastEditors: Harry
  9. */
  10. const setCookie = function (name, value) {
  11. const expdate = new Date() // 初始化时间
  12. expdate.setTime(expdate.getTime() + 30 * 60 * 1000) // 时间单位毫秒
  13. document.cookie = name + '=' + escape(value) + ';expires=' + expdate.toUTCString() + ';path=/'
  14. // document.cookie = name + "=" + value + ";path=/"; // 时间默认为当前会话可以不要,但路径要填写,因为JS的默认路径是当前页,如果不填,此cookie只在当前页面生效!
  15. }
  16. const getCookie = function (a) {
  17. // console.log(a)
  18. let d
  19. const b = document.cookie
  20. const c = b.split(';')
  21. // console.log(c);
  22. // console.log(escape('YWRtaW4='));
  23. for (let e = 0; e < c.length; e++) {
  24. const f = c[e].split('=')
  25. if (a === f[0].toString().trim()) {
  26. d = f[1]
  27. break
  28. }
  29. // eslint-disable-next-line no-void
  30. } if (void 0 === d || d == null) {
  31. return ''
  32. } else {
  33. const g = unescape(d.trim())
  34. return g
  35. }
  36. }
  37. const delCookie = function (a) {
  38. const b = new Date(0).toUTCString()
  39. document.cookie = a + '=;expires=' + b + ';path=/'
  40. }
  41. export default {
  42. setCookie,
  43. getCookie,
  44. delCookie
  45. }

3. Base64

  1. /***
  2. * @Description:
  3. * @Author: Harry
  4. * @Date: 2021-12-03 20:01:23
  5. * @Url: https://u.mr90.top
  6. * @github: https://github.com/rr210
  7. * @LastEditTime: 2021-12-27 17:51:06
  8. * @LastEditors: Harry
  9. */
  10. class Base64 {
  11. _keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  12. // eslint-disable-next-line no-useless-constructor
  13. constructor() {}
  14. encode = (input) => {
  15. var output = ''
  16. var chr1, chr2, chr3, enc1, enc2, enc3, enc4
  17. var i = 0
  18. input = this._utf8_encode(input)
  19. while (i < input.length) {
  20. chr1 = input.charCodeAt(i++)
  21. chr2 = input.charCodeAt(i++)
  22. chr3 = input.charCodeAt(i++)
  23. enc1 = chr1 >> 2
  24. enc2 = ((chr1 & 3) << 4) | (chr2 >> 4)
  25. enc3 = ((chr2 & 15) << 2) | (chr3 >> 6)
  26. enc4 = chr3 & 63
  27. if (isNaN(chr2)) {
  28. enc3 = enc4 = 64
  29. } else if (isNaN(chr3)) {
  30. enc4 = 64
  31. }
  32. output = output +
  33. this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
  34. this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4)
  35. }
  36. return output
  37. }
  38. decode = (input) => {
  39. var output = ''
  40. var chr1, chr2, chr3
  41. var enc1, enc2, enc3, enc4
  42. var i = 0
  43. // eslint-disable-next-line no-useless-escape
  44. input = input.replace(/[^A-Za-z0-9\+\/\=]/g, '')
  45. while (i < input.length) {
  46. enc1 = this._keyStr.indexOf(input.charAt(i++))
  47. enc2 = this._keyStr.indexOf(input.charAt(i++))
  48. enc3 = this._keyStr.indexOf(input.charAt(i++))
  49. enc4 = this._keyStr.indexOf(input.charAt(i++))
  50. chr1 = (enc1 << 2) | (enc2 >> 4)
  51. chr2 = ((enc2 & 15) << 4) | (enc3 >> 2)
  52. chr3 = ((enc3 & 3) << 6) | enc4
  53. output = output + String.fromCharCode(chr1)
  54. if (enc3 !== 64) {
  55. output = output + String.fromCharCode(chr2)
  56. }
  57. if (enc4 !== 64) {
  58. output = output + String.fromCharCode(chr3)
  59. }
  60. }
  61. output = this._utf8_decode(output)
  62. return output
  63. }
  64. // eslint-disable-next-line camelcase
  65. _utf8_encode = function (string) {
  66. string = string.replace(/\r\n/g, '\n')
  67. var utftext = ''
  68. for (var n = 0; n < string.length; n++) {
  69. var c = string.charCodeAt(n)
  70. if (c < 128) {
  71. utftext += String.fromCharCode(c)
  72. } else if ((c > 127) && (c < 2048)) {
  73. utftext += String.fromCharCode((c >> 6) | 192)
  74. utftext += String.fromCharCode((c & 63) | 128)
  75. } else {
  76. utftext += String.fromCharCode((c >> 12) | 224)
  77. utftext += String.fromCharCode(((c >> 6) & 63) | 128)
  78. utftext += String.fromCharCode((c & 63) | 128)
  79. }
  80. }
  81. return utftext
  82. }
  83. // eslint-disable-next-line camelcase
  84. _utf8_decode = function (utftext) {
  85. var string = ''
  86. var i = 0
  87. let c = 0
  88. let c2 = 0
  89. // console.log(object);
  90. while (i < utftext.length) {
  91. c = utftext.charCodeAt(i)
  92. if (c < 128) {
  93. string += String.fromCharCode(c)
  94. i++
  95. } else if ((c > 191) && (c < 224)) {
  96. c2 = utftext.charCodeAt(i + 1)
  97. string += String.fromCharCode(((c & 31) << 6) | (c2 & 63))
  98. i += 2
  99. } else {
  100. c2 = utftext.charCodeAt(i + 1)
  101. const c3 = utftext.charCodeAt(i + 2)
  102. string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63))
  103. i += 3
  104. }
  105. }
  106. return string
  107. }
  108. }
  109. export default Base64