w-qrcode.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 ,defineExpose } 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,source: platform(),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. defineExpose({
  85. GetCodeImg
  86. })
  87. </script>
  88. <!-- #endif -->
  89. <!-- #ifndef VUE3 -->
  90. <script>
  91. import { QRCode, GetImg,GetPixelRatio,GetPx } from '@/uni_modules/wmf-code/js_sdk/index.js';
  92. import { getUUid, deepClone,platform } from '../../common/helper.js'
  93. export default {
  94. name: 'WQrcode',
  95. props:{
  96. options:{
  97. type: Object,
  98. required: true,
  99. default: () =>{
  100. return {
  101. }
  102. }
  103. }
  104. },
  105. data () {
  106. return {
  107. info:{
  108. destHeight: 0,
  109. destWidth: 0,
  110. width: 0,
  111. height: 0,
  112. listCode:[],
  113. },
  114. destHeight: 0,
  115. destWidth: 0,
  116. width: 0,
  117. height: 0,
  118. listCode:[],
  119. id: getUUid(),
  120. }
  121. },
  122. mounted() {
  123. this.info.height = this.info.width = GetPx(this.options.size) + 'px';
  124. this.info.destHeight = this.info.destWidth = GetPx(this.options.size) * GetPixelRatio() + 'px';
  125. this.SpecialTreatment(this.options)
  126. this.$nextTick(()=>{
  127. this.generateCode();
  128. })
  129. },
  130. watch: {
  131. options:{
  132. deep: true,
  133. handler (val) {
  134. this.info.height = this.info.width = GetPx(val.size) + 'px';
  135. this.info.destHeight = this.info.destWidth = GetPx(val.size) * GetPixelRatio() + 'px';
  136. this.SpecialTreatment(val)
  137. setTimeout(()=>{// h5平台动态改变canvas大小
  138. this.generateCode();
  139. },50)
  140. }
  141. }
  142. },
  143. methods: {
  144. longtap (e){// 长按事件
  145. this.$emit('press',e);
  146. },
  147. handleError (e) {//当发生错误时触发 error 事件,字节跳动小程序与飞书小程序不支持
  148. this.$emit('error',e.detail)
  149. },
  150. SpecialTreatment (val) {//微信小程序渲染多个canvas特殊处理
  151. let obj = deepClone(val);
  152. obj.id = this.id;
  153. this.info.listCode = [obj]
  154. },
  155. // 生成二维码
  156. generateCode () {
  157. try{
  158. const parameter = {...this.options,source: platform(),id: this.id,ctx: this};
  159. QRCode(parameter,(res)=>{
  160. this.$emit('generate',res)
  161. })
  162. }catch(err){console.warn(err)}
  163. },
  164. // 获取二维码图片
  165. async GetCodeImg (){
  166. try{
  167. return await GetImg({id: this.id,source: platform(),width: this.options.size,height: this.options.size,ctx: this});
  168. }catch(e){console.warn(e)}
  169. }
  170. }
  171. }
  172. </script>
  173. <!-- #endif -->