123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- <template>
- <view @longtap.stop="longtap">
- <canvas
- :width="info.orient == 'vertical' ? info.destHeight : info.destWidth "
- :height="info.orient == 'vertical' ? info.destWidth : info.destHeight"
- :canvas-id="item.id"
- :id="item.id"
- :style="{width:info.orient == 'vertical' ? info.height : info.width,height: info.orient == 'vertical' ? info.width : info.height}"
- v-for="item in info.listCode"
- :key="item.id"
- @error="handleError"></canvas>
- </view>
- </template>
- <!-- #ifdef VUE3 -->
- <script setup name="WBarcode">
- import {
- reactive,
- watch,
- onMounted,
- nextTick,
- getCurrentInstance,
- } from 'vue';
- import {
- BarCode,
- GetImg,
- GetPixelRatio,
- GetPx ,
- } from '@/uni_modules/wmf-code/js_sdk/index.js';
- import {
- getUUid,
- deepClone,
- platform,
- } from '../../common/helper.js'
- //定义props
- const props = defineProps({
- options:{
- type: Object,
- required: true,
- default: () =>{
- return {}
- }
- }
- });
- const emits = defineEmits(['generate','press','error'])
- const opt = props.options;
- const that = getCurrentInstance();
- const HSize = opt.text ? opt.text.size || 40 + opt.text.padding || 20 : 0;
- let info = reactive({
- id: getUUid(),
- destWidth: GetPixelRatio() * GetPx(opt.width) + 'px',
- destHeight: GetPixelRatio() * GetPx(opt.height + HSize) + 'px',
- width: GetPx(opt.width) + 'px',
- height: GetPx(opt.height + HSize) + 'px',
- orient: opt.orient || 'horizontal',
- listCode: []
- })
- onMounted(()=>{
- SpecialTreatment(opt);
- nextTick(()=>{
- generateCode(opt)
- })
- });
- watch(()=>props.options,(val)=>{
- SpecialTreatment(val);
- const HSize = val.text ? val.text.size || 40 + val.text.padding || 20 : 0;
- info.destWidth= GetPixelRatio() * GetPx(val.width) + 'px',
- info.destHeight= GetPixelRatio() * GetPx(val.height + HSize) + 'px',
- info.orient = val.orient || 'horizontal',
- info.width= GetPx(val.width) + 'px',
- info.height= GetPx(val.height + HSize) + 'px',
- setTimeout(()=>{
- generateCode(val)
- },100)
- },{ deep: true })
- const generateCode = (val)=> {
- try{
- const parameter = {...val,source: platform(),id: info.id,ctx: that};
- BarCode(parameter,(res)=>{
- emits('generate',res)
- })
- }catch(err){console.warn(err)}
- }
- const GetCodeImg = async ()=> {
- try{
- return await GetImg({id: info.id,width: opt.orient == 'vertical' ? opt.height : opt.width,height: opt.orient == 'vertical' ? opt.width : opt.height,ctx: that});
- }catch(e){console.warn(e)}
- };
- const SpecialTreatment = (val) => {//渲染多个canvas特殊处理
- let obj = deepClone(val);
- obj.id = info.id;
- info.listCode = [obj];
- };
- // 长按事件
- const longtap = (e)=>{
- emits('press',e)
- }
- // canvas创建错误 触发
- const handleError = (e)=>{
- emits('error',e.detail)
- }
- </script>
- <!-- #endif -->
- <!-- #ifndef VUE3 -->
- <script>
- import { BarCode, GetImg,GetPixelRatio,GetPx } from '@/uni_modules/wmf-code/js_sdk/index.js';
- import { getUUid, deepClone,platform } from '../../common/helper.js'
- export default {
- name: 'WBarcode',
- props:{
- options:{
- type: Object,
- required: true,
- default: () =>{
- return {
-
- }
- }
- }
- },
- data () {
- return {
- info:{
- destHeight: 0,
- destWidth: 0,
- width: 0,
- height: 0,
- listCode: [],
- orient: 'horizontal'
- },
- id: getUUid()
- }
- },
- mounted() {
- const HSize = this.options.text ? ((this.options.text.size || 40) + ( this.options.text.padding || 20)) : 0;
- this.info.height = GetPx(this.options.height + HSize) + 'px';
- this.info.orient = this.options.orient || 'horizontal';
- this.info.width = GetPx(this.options.width) + 'px';
- this.info.destHeight = GetPx(this.options.height + HSize) * GetPixelRatio() + 'px';
- this.info.destWidth = GetPx(this.options.width) * GetPixelRatio() + 'px';
- this.SpecialTreatment(this.options)
- this.$nextTick(()=>{
- this.generateCode();
- })
- },
- watch: {
- options:{
- deep: true,
- handler (val) {
- const HSize = val.text ? val.text.size || 40 + val.text.padding || 20 : 0;
- this.info.height = GetPx(val.height + HSize) + 'px';
- this.info.width = GetPx(val.width) + 'px';
- this.info.destHeight = GetPx(val.height + HSize) * GetPixelRatio() + 'px';
- this.info.destWidth = GetPx(val.width) * GetPixelRatio() + 'px';
- this.info.orient = val.orient || 'horizontal'
- this.SpecialTreatment(val)
- setTimeout(()=>{// h5平台动态改变canvas大小
- this.generateCode();
- },100)
- }
- }
- },
- methods: {
- longtap (e){
- this.$emit('press',e)
- },
- handleError (e) {//当发生错误时触发 error 事件,字节跳动小程序与飞书小程序不支持
- this.$emit('error',e.detail)
- },
- SpecialTreatment (val) {//微信小程序渲染多个canvas特殊处理
- let obj = deepClone(val);
- obj.id = this.id;
- this.info.listCode = [obj]
- },
- generateCode () {
- try{
- const parameter = {...this.options,source: platform(),id: this.id,ctx: this};
- BarCode(parameter,(res)=>{
- this.$emit('generate',res)
- })
- }catch(err){}
- },
- async GetCodeImg (){
- try{
- return await GetImg({id: this.id,width: this.options.orient == 'vertical' ? this.options.height : this.options.width,height: this.options.orient == 'vertical' ? this.options.width : this.options.height,ctx: this});
- }catch(e){}
- }
- }
- }
- </script>
- <!-- #endif -->
|