w-qrcode.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <template>
  2. <view @longtap.stop="longtap">
  3. <canvas
  4. :width="info.destWidth"
  5. :height="info.destHeight"
  6. :canvas-id="item.id"
  7. :id="item.id"
  8. :style="{width:info.width,height: info.height}"
  9. v-for="item in info.listCode"
  10. :key="item.id"
  11. @error="handleError"></canvas>
  12. </view>
  13. </template>
  14. <!-- #ifdef VUE3 -->
  15. <script setup name="WQrcode">
  16. import {reactive, watch,onMounted,nextTick,getCurrentInstance } from 'vue';
  17. import { QRCode, GetImg,GetPixelRatio,GetPx } from '@/uni_modules/wmf-code/js_sdk/index.js';
  18. import { getUUid, deepClone,platform } from '../../common/helper.js'
  19. //定义props
  20. const props = defineProps({
  21. options:{
  22. type: Object,
  23. required: true,
  24. default: () =>{
  25. return {}
  26. }
  27. }
  28. });
  29. const emits = defineEmits(['generate','press','error'])
  30. const opt = props.options;
  31. const that = getCurrentInstance();
  32. const SIZE = GetPx(opt.size);
  33. let info = reactive({
  34. destHeight: SIZE * GetPixelRatio() + 'px',
  35. destWidth: SIZE * GetPixelRatio() + 'px',
  36. width: SIZE + 'px',
  37. height: SIZE + 'px',
  38. listCode:[],
  39. id: getUUid(),
  40. })
  41. onMounted(()=>{
  42. SpecialTreatment(opt);
  43. nextTick(()=>{
  44. generateCode(opt)
  45. })
  46. });
  47. watch(()=>props.options,(val)=>{
  48. SpecialTreatment(val);
  49. const SIZE_Dynamic = GetPx(val.size);
  50. info.destWidth= GetPixelRatio() * SIZE_Dynamic + 'px',
  51. info.destHeight= GetPixelRatio() * SIZE_Dynamic + 'px',
  52. info.width= SIZE_Dynamic + 'px',
  53. info.height= SIZE_Dynamic + 'px',
  54. setTimeout(()=>{
  55. generateCode(val)
  56. },50)
  57. },{ deep: true })
  58. const SpecialTreatment =(val)=> {//渲染多个canvas特殊处理
  59. let obj = deepClone(val);
  60. obj.id = info.id;
  61. info.listCode = [obj]
  62. };
  63. const generateCode = (val)=>{
  64. try{
  65. const parameter = {...val,source: platform(),id: info.id,ctx: that};
  66. QRCode(parameter,(res)=>{
  67. emits('generate',res)
  68. })
  69. }catch(err){console.warn(err)}
  70. };
  71. const GetCodeImg = async ()=> {
  72. try{
  73. return await GetImg({id: info.id,width: opt.width,height: opt.height,ctx: that});
  74. }catch(e){console.warn(e)}
  75. };
  76. // 长按事件
  77. const longtap = (e)=>{
  78. emits('press',e)
  79. }
  80. // canvas创建错误 触发
  81. const handleError = (e)=>{
  82. emits('error',e.detail)
  83. }
  84. </script>
  85. <!-- #endif -->
  86. <!-- #ifndef VUE3 -->
  87. <script>
  88. import { QRCode, GetImg,GetPixelRatio,GetPx } from '@/uni_modules/wmf-code/js_sdk/index.js';
  89. import { getUUid, deepClone,platform } from '../../common/helper.js'
  90. export default {
  91. name: 'WQrcode',
  92. props:{
  93. options:{
  94. type: Object,
  95. required: true,
  96. default: () =>{
  97. return {
  98. }
  99. }
  100. }
  101. },
  102. data () {
  103. return {
  104. info:{
  105. destHeight: 0,
  106. destWidth: 0,
  107. width: 0,
  108. height: 0,
  109. listCode:[],
  110. },
  111. destHeight: 0,
  112. destWidth: 0,
  113. width: 0,
  114. height: 0,
  115. listCode:[],
  116. id: getUUid(),
  117. }
  118. },
  119. mounted() {
  120. this.info.height = this.info.width = GetPx(this.options.size) + 'px';
  121. this.info.destHeight = this.info.destWidth = GetPx(this.options.size) * GetPixelRatio() + 'px';
  122. this.SpecialTreatment(this.options)
  123. this.$nextTick(()=>{
  124. this.generateCode();
  125. })
  126. },
  127. watch: {
  128. options:{
  129. deep: true,
  130. handler (val) {
  131. this.info.height = this.info.width = GetPx(val.size) + 'px';
  132. this.info.destHeight = this.info.destWidth = GetPx(val.size) * GetPixelRatio() + 'px';
  133. this.SpecialTreatment(val)
  134. setTimeout(()=>{// h5平台动态改变canvas大小
  135. this.generateCode();
  136. },50)
  137. }
  138. }
  139. },
  140. methods: {
  141. longtap (e){// 长按事件
  142. this.$emit('press',e);
  143. },
  144. handleError (e) {//当发生错误时触发 error 事件,字节跳动小程序与飞书小程序不支持
  145. this.$emit('error',e.detail)
  146. },
  147. SpecialTreatment (val) {//微信小程序渲染多个canvas特殊处理
  148. let obj = deepClone(val);
  149. obj.id = this.id;
  150. this.info.listCode = [obj]
  151. },
  152. // 生成二维码
  153. generateCode () {
  154. try{
  155. const parameter = {...this.options,source: platform(),id: this.id,ctx: this};
  156. QRCode(parameter,(res)=>{
  157. this.$emit('generate',res)
  158. })
  159. }catch(err){console.warn(err)}
  160. },
  161. // 获取二维码图片
  162. async GetCodeImg (){
  163. try{
  164. return await GetImg({id: this.id,width: this.options.size,height: this.options.size,ctx: this});
  165. }catch(e){console.warn(e)}
  166. }
  167. }
  168. }
  169. </script>
  170. <!-- #endif -->