utils.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import html2canvas from 'html2canvas'
  2. import QRCode from 'qrcode'
  3. import { ScreenShot } from './typing'
  4. import { getCWXSignature } from '@/api/common'
  5. import wx from 'weixin-js-sdk'
  6. /**
  7. * @description 屏幕截图
  8. * @param el 需要截图的dom节点
  9. * @returns img img 的标签
  10. */
  11. export const screenShot: ScreenShot = async (el) => {
  12. const _el = typeof el === 'string' ? document.getElementById(el) : el
  13. const scale = window.devicePixelRatio
  14. // canvas.width = 1000 * scale;
  15. // canvas.height = 1000 * scale;
  16. // ctx.scale(scale, scale);
  17. return new Promise((resolve) => {
  18. html2canvas(_el!, {
  19. useCORS: true,
  20. scale
  21. }).then(canvas => {
  22. const img = document.createElement('img')
  23. img.src = canvas.toDataURL('image/png')
  24. img.style.width = '100vw'
  25. resolve(img)
  26. })
  27. })
  28. }
  29. export const createQrcode = (text: string): Promise<string> => {
  30. console.log('text:', text)
  31. return new Promise((resolve) => {
  32. QRCode.toDataURL(text, { errorCorrectionLevel: 'H' }, function (err, url) {
  33. if (err) {
  34. throw new Error(err.message)
  35. } else {
  36. resolve(url)
  37. }
  38. })
  39. })
  40. }
  41. /**
  42. * @description 注册wx-open-launch-app
  43. */
  44. export async function registerWxopenButton (request: () => any) {
  45. return new Promise(async resolve => {
  46. const { data } = await request()
  47. console.log('register: wxopenButto:', data)
  48. const { signature, appId, nonceStr, timestamp } = data
  49. wx.config({
  50. debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印
  51. appId, // 必填,公众号的唯一标识
  52. timestamp, // 必填,生成签名的时间戳
  53. nonceStr, // 必填,生成签名的随机串
  54. signature, // 必填,签名
  55. jsApiList: ['openLocation', 'getLocation'], // 必填,需要使用的JS接口列表
  56. openTagList: ['wx-open-launch-app'] // 可选,需要使用的开放标签列表,例如['wx-open-launch-app']
  57. })
  58. wx.error((res: any) => {
  59. console.log(res)
  60. })
  61. wx.ready(() => {
  62. resolve(true)
  63. })
  64. })
  65. }