w-barcode.vue 5.5 KB

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