sard-uniapp 1.9.0 → 1.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/changelog.md CHANGED
@@ -1,3 +1,12 @@
1
+ ## [1.9.1](https://github.com/sutras/sard-uniapp/compare/v1.9.0...v1.9.1) (2024-12-22)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * 修复qrcode 在app端报错的问题 ([c74cad5](https://github.com/sutras/sard-uniapp/commit/c74cad5cbe7dd733843713fd3500e15236719583))
7
+
8
+
9
+
1
10
  # [1.9.0](https://github.com/sutras/sard-uniapp/compare/v1.8.2...v1.9.0) (2024-12-22)
2
11
 
3
12
 
@@ -1,9 +1,10 @@
1
1
  import { type StyleValue } from 'vue';
2
+ export type QrcodeECL = 'L' | 'M' | 'Q' | 'H';
2
3
  export interface QrcodeProps {
3
4
  rootStyle?: StyleValue;
4
5
  rootClass?: string;
5
6
  text?: string;
6
- ecl?: 'L' | 'M' | 'Q' | 'H';
7
+ ecl?: QrcodeECL;
7
8
  type?: 'canvas' | 'image';
8
9
  size?: string;
9
10
  canvasSize?: number;
@@ -21,3 +22,6 @@ export declare const defaultQrcodeProps: {
21
22
  bgColor: string;
22
23
  quietZoneModules: number;
23
24
  };
25
+ export interface QrcodeSlots {
26
+ default?(props: Record<string, never>): any;
27
+ }
@@ -1 +1 @@
1
- export type { QrcodeProps } from './common';
1
+ export type { QrcodeProps, QrcodeSlots, QrcodeECL } from './common';
@@ -3,10 +3,15 @@
3
3
  @include bem(qrcode) {
4
4
  @include b() {
5
5
  @include universal;
6
+ display: inline-flex;
6
7
  }
7
8
 
8
- @include e(canvas) {
9
- display: none;
9
+ @include e(canvas-wrapper) {
10
+ position: fixed;
11
+ left: -999999px;
12
+ opacity: 0;
13
+ pointer-events: none;
14
+ touch-action: none;
10
15
  }
11
16
 
12
17
  @include e(image) {
@@ -1,7 +1,10 @@
1
1
  import { type QrcodeProps } from './common';
2
- declare const _default: import("vue").DefineComponent<QrcodeProps, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<QrcodeProps> & Readonly<{}>, {
2
+ declare function __VLS_template(): {
3
+ default?(_: {}): any;
4
+ };
5
+ declare const __VLS_component: import("vue").DefineComponent<QrcodeProps, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<QrcodeProps> & Readonly<{}>, {
3
6
  text: string;
4
- ecl: "L" | "M" | "Q" | "H";
7
+ ecl: import("./common").QrcodeECL;
5
8
  size: string;
6
9
  type: "canvas" | "image";
7
10
  canvasSize: number;
@@ -9,4 +12,10 @@ declare const _default: import("vue").DefineComponent<QrcodeProps, {}, {}, {}, {
9
12
  bgColor: string;
10
13
  quietZoneModules: number;
11
14
  }, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
15
+ declare const _default: __VLS_WithTemplateSlots<typeof __VLS_component, ReturnType<typeof __VLS_template>>;
12
16
  export default _default;
17
+ type __VLS_WithTemplateSlots<T, S> = T & {
18
+ new (): {
19
+ $slots: S;
20
+ };
21
+ };
@@ -1,19 +1,37 @@
1
1
  <template>
2
2
  <view :class="qrcodeClass" :style="qrcodeStyle">
3
- <canvas type="2d" :class="bem.e('canvas')" :id="canvasId"></canvas>
3
+ <view :class="bem.e('canvas-wrapper')">
4
+ <canvas
5
+ type="2d"
6
+ :width="canvasSize"
7
+ :height="canvasSize"
8
+ :style="{ width: canvasSize + 'px', height: canvasSize + 'px' }"
9
+ :canvas-id="canvasId"
10
+ :id="canvasId"
11
+ ></canvas>
12
+ </view>
4
13
  <image :src="dataURL" mode="aspectFit" :class="bem.e('image')" />
14
+ <slot></slot>
5
15
  </view>
6
16
  </template>
7
17
 
8
18
  <script>
9
19
  import { mergeDefaults as _mergeDefaults, defineComponent as _defineComponent } from "vue";
10
- import { computed, getCurrentInstance, onMounted, shallowRef } from "vue";
20
+ import {
21
+ computed,
22
+ getCurrentInstance,
23
+ onMounted,
24
+ ref,
25
+ shallowRef,
26
+ watch
27
+ } from "vue";
11
28
  import {
12
29
  classNames,
13
30
  stringifyStyle,
14
31
  createBem,
15
32
  uniqid,
16
- qrcode
33
+ qrcode,
34
+ isApp
17
35
  } from "../../utils";
18
36
  import { defaultQrcodeProps } from "./common";
19
37
  export default _defineComponent({
@@ -40,30 +58,72 @@ export default _defineComponent({
40
58
  __expose();
41
59
  const props = __props;
42
60
  const bem = createBem("qrcode");
61
+ const instance = getCurrentInstance();
43
62
  const canvasId = uniqid();
63
+ const contextRef = shallowRef();
44
64
  const canvasRef = shallowRef();
45
65
  const qrcodeMap = computed(() => {
46
66
  return qrcode(props.text, {
47
67
  ecl: props.ecl
48
68
  });
49
69
  });
50
- const dataURL = computed(() => {
70
+ const dataURL = ref("");
71
+ const drawQrcodeInApp = () => {
72
+ const context = contextRef.value;
73
+ if (!context) {
74
+ return;
75
+ }
76
+ const map = qrcodeMap.value;
77
+ const size = props.canvasSize;
78
+ const moduleSize = size / (map.length + props.quietZoneModules * 2);
79
+ const margin = moduleSize * props.quietZoneModules;
80
+ context.clearRect(0, 0, size, size);
81
+ context.setFillStyle(props.bgColor);
82
+ context.fillRect(0, 0, size, size);
83
+ context.setFillStyle(props.color);
84
+ map.forEach((row, rowIndex) => {
85
+ row.forEach((col, colIndex) => {
86
+ if (col === 1) {
87
+ context.fillRect(
88
+ colIndex * moduleSize + margin,
89
+ rowIndex * moduleSize + margin,
90
+ moduleSize,
91
+ moduleSize
92
+ );
93
+ }
94
+ });
95
+ });
96
+ context.draw();
97
+ uni.canvasToTempFilePath({
98
+ x: 0,
99
+ y: 0,
100
+ width: size,
101
+ height: size,
102
+ destWidth: size,
103
+ destHeight: size,
104
+ canvasId,
105
+ success(res) {
106
+ dataURL.value = res.tempFilePath;
107
+ }
108
+ });
109
+ };
110
+ const drawQrcodeInOthers = () => {
51
111
  const canvas = canvasRef.value;
52
112
  if (!canvas) {
53
- return "";
113
+ return;
54
114
  }
55
115
  const map = qrcodeMap.value;
56
116
  const size = props.canvasSize;
57
117
  canvas.width = size;
58
118
  canvas.height = size;
59
- const context = canvas.getContext("2d");
60
119
  const moduleSize = size / (map.length + props.quietZoneModules * 2);
61
120
  const margin = moduleSize * props.quietZoneModules;
62
- const path = canvas.createPath2D ? canvas.createPath2D() : new Path2D();
121
+ const context = canvas.getContext("2d");
122
+ const path2D = canvas.createPath2D ? canvas.createPath2D() : new Path2D();
63
123
  map.forEach((row, rowIndex) => {
64
124
  row.forEach((col, colIndex) => {
65
125
  if (col === 1) {
66
- path.rect(
126
+ path2D.rect(
67
127
  colIndex * moduleSize + margin,
68
128
  rowIndex * moduleSize + margin,
69
129
  moduleSize,
@@ -76,16 +136,37 @@ export default _defineComponent({
76
136
  context.fillStyle = props.bgColor;
77
137
  context.fillRect(0, 0, size, size);
78
138
  context.fillStyle = props.color;
79
- context.fill(path);
80
- return canvas.toDataURL();
81
- });
82
- const instance = getCurrentInstance();
83
- onMounted(() => {
84
- uni.createSelectorQuery().in(instance).select(`#${canvasId}`).node((res) => {
85
- if (res && res.node) {
86
- canvasRef.value = res.node;
139
+ context.fill(path2D);
140
+ dataURL.value = canvas.toDataURL();
141
+ };
142
+ watch(
143
+ [
144
+ contextRef,
145
+ canvasRef,
146
+ qrcodeMap,
147
+ () => props.canvasSize,
148
+ () => props.color,
149
+ () => props.bgColor,
150
+ () => props.quietZoneModules
151
+ ],
152
+ () => {
153
+ if (isApp) {
154
+ drawQrcodeInApp();
155
+ } else {
156
+ drawQrcodeInOthers();
87
157
  }
88
- }).exec();
158
+ }
159
+ );
160
+ onMounted(() => {
161
+ if (isApp) {
162
+ contextRef.value = uni.createCanvasContext(canvasId, instance);
163
+ } else {
164
+ uni.createSelectorQuery().in(instance).select(`#${canvasId}`).node((res) => {
165
+ if (res && res.node) {
166
+ canvasRef.value = res.node;
167
+ }
168
+ }).exec();
169
+ }
89
170
  });
90
171
  const qrcodeClass = computed(() => {
91
172
  return classNames(bem.b(), props.rootClass);
@@ -96,7 +177,7 @@ export default _defineComponent({
96
177
  height: props.size
97
178
  });
98
179
  });
99
- const __returned__ = { props, bem, canvasId, canvasRef, qrcodeMap, dataURL, instance, qrcodeClass, qrcodeStyle };
180
+ const __returned__ = { props, bem, instance, canvasId, contextRef, canvasRef, qrcodeMap, dataURL, drawQrcodeInApp, drawQrcodeInOthers, qrcodeClass, qrcodeStyle };
100
181
  return __returned__;
101
182
  }
102
183
  });
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "id": "sard-uniapp",
3
3
  "name": "sard-uniapp",
4
- "version": "1.9.0",
4
+ "displayName": "sard-uniapp",
5
+ "version": "1.9.1",
5
6
  "description": "sard-uniapp 是一套基于 Uniapp + Vue3 框架开发的兼容多端的 UI 组件库",
6
7
  "main": "index.js",
7
8
  "scripts": {
package/utils/index.d.ts CHANGED
@@ -7,3 +7,4 @@ export * from './is';
7
7
  export * from './utils';
8
8
  export * from './lwa.slim';
9
9
  export * from './qrcode';
10
+ export * from './system';
package/utils/index.js CHANGED
@@ -7,3 +7,4 @@ export * from './is';
7
7
  export * from './utils';
8
8
  export * from './lwa.slim';
9
9
  export * from './qrcode';
10
+ export * from './system';
@@ -0,0 +1,4 @@
1
+ export declare const systemInfo: UniApp.GetSystemInfoResult;
2
+ export declare const isApp: boolean;
3
+ export declare const isWeb: boolean;
4
+ export declare const isMp: boolean;
@@ -0,0 +1,4 @@
1
+ export const systemInfo = uni.getSystemInfoSync();
2
+ export const isApp = systemInfo.uniPlatform === 'app';
3
+ export const isWeb = systemInfo.uniPlatform === 'web';
4
+ export const isMp = systemInfo.uniPlatform.startsWith('mp-');