w-barcode.vue 5.3 KB

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