index.vue 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <template>
  2. <view @longtap.stop="longtap">
  3. <canvas
  4. :width="destWidth"
  5. :height="destHeight"
  6. :canvas-id="item.id"
  7. :id="item.id"
  8. :style="{width:width,height: height}"
  9. v-for="(item,index) in listCode"
  10. :key="item.id"
  11. @error="handleError"></canvas>
  12. </view>
  13. </template>
  14. <script>
  15. import {QRCode,GetCodeImg,GetPixelRatio,GetPx} from '../../js_sdk';
  16. import { getUUid, deepClone, platform } from '../../common/helper.js'
  17. export default {
  18. name: 'WQrcode',
  19. props:{
  20. options:{
  21. type: Object,
  22. required: true,
  23. default: () =>{
  24. return {
  25. }
  26. }
  27. }
  28. },
  29. data () {
  30. return {
  31. destHeight: 0,
  32. destWidth: 0,
  33. width: 0,
  34. height: 0,
  35. listCode:[],
  36. id: getUUid(),
  37. }
  38. },
  39. mounted() {
  40. this.height = this.width = GetPx(this.options.size) + 'px';
  41. this.destHeight = this.destWidth = GetPx(this.options.size) * GetPixelRatio() + 'px';
  42. this.SpecialTreatment(this.options)
  43. this.$nextTick(()=>{
  44. this.generateCode();
  45. })
  46. },
  47. watch: {
  48. options:{
  49. deep: true,
  50. handler (val) {
  51. this.height = this.width = GetPx(val.size) + 'px';
  52. this.destHeight = this.destWidth = GetPx(val.size) * GetPixelRatio() + 'px';
  53. this.SpecialTreatment(val)
  54. setTimeout(()=>{// h5平台动态改变canvas大小
  55. this.generateCode();
  56. },50)
  57. }
  58. }
  59. },
  60. methods: {
  61. longtap (e){
  62. this.$emit('press',e)
  63. },
  64. handleError (e) {//当发生错误时触发 error 事件,字节跳动小程序与飞书小程序不支持
  65. this.$emit('error',e.detail)
  66. },
  67. SpecialTreatment (val) {//微信小程序渲染多个canvas特殊处理
  68. let obj = deepClone(val);
  69. obj.id = this.id;
  70. this.listCode = [obj]
  71. },
  72. generateCode () {
  73. try{
  74. const parameter = {...this.options,source: platform(),id: this.id,ctx: this};
  75. QRCode(parameter,(res)=>{
  76. this.$emit('generate',res)
  77. })
  78. }catch(err){}
  79. },
  80. async GetCodeImg (){
  81. try{
  82. return await GetCodeImg({id: this.id,width: this.options.size,height: this.options.size,ctx: this});
  83. }catch(e){}
  84. }
  85. }
  86. }
  87. </script>