金诚优选前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.0 KiB

  1. <template>
  2. <view>
  3. <!-- #ifdef MP-WEIXIN -->
  4. <canvas :canvas-id="item.id" :id="item.id" :style="{width:width,height: height}" v-for="(item,index) in listCode" :key="item.id" />
  5. <!-- #endif -->
  6. <!-- #ifndef MP-WEIXIN -->
  7. <canvas :canvas-id="id" :id="id" :style="{width:width,height: height}" />
  8. <!-- #endif -->
  9. </view>
  10. </template>
  11. <script>
  12. import QRCODE from './index.js';
  13. import { getUUid, deepClone } from './helper.js'
  14. export default {
  15. name: 'WBarcode',
  16. props:{
  17. options:{
  18. type: Object,
  19. required: true,
  20. default: () =>{
  21. return {
  22. }
  23. }
  24. }
  25. },
  26. data () {
  27. return {
  28. listCode: [],
  29. id: getUUid(),
  30. height: null,
  31. width: null,
  32. }
  33. },
  34. created() {
  35. // https://ext.dcloud.net.cn/plugin?id=4662
  36. this.height = this.options.height + 'rpx';
  37. this.width = this.options.width + 'rpx';
  38. this.SpecialTreatment(this.options)
  39. this.$nextTick(()=>{
  40. this.generateCode();
  41. })
  42. },
  43. watch: {
  44. options:{
  45. deep: true,
  46. handler (val) {
  47. this.height = val.height + 'rpx';
  48. this.width = val.width + 'rpx';
  49. this.SpecialTreatment(val)
  50. // #ifdef H5
  51. setTimeout(()=>{// h5平台动态改变canvas大小
  52. this.generateCode();
  53. },50)
  54. // #endif
  55. // #ifdef APP-NVUE || APP-PLUS || MP || QUICKAPP-WEBVIEW || QUICKAPP-WEBVIEW-HUAWEI || QUICKAPP-WEBVIEW-UNION
  56. this.generateCode();
  57. // #endif
  58. }
  59. }
  60. },
  61. methods: {
  62. SpecialTreatment (val) {//微信小程序渲染多个canvas特殊处理
  63. // #ifdef MP-WEIXIN
  64. let obj = deepClone(val);
  65. this.id = obj.id = getUUid();
  66. this.listCode = [obj]
  67. // #endif
  68. },
  69. generateCode () {
  70. try{
  71. const parameter = {...this.options,id: this.id,ctx: this};
  72. QRCODE.BarCode(parameter,(res)=>{
  73. this.$emit('generate',res)
  74. })
  75. }catch(err){}
  76. },
  77. async saveImg (){
  78. try{
  79. const res = await QRCODE.SaveImg({id: this.id,width: this.width,height: this.width,ctx: this});
  80. return res
  81. }catch(e){}
  82. }
  83. }
  84. }
  85. </script>