张延森 3 år sedan
förälder
incheckning
c09be1e425

+ 2
- 2
config/dev.js Visa fil

@@ -3,8 +3,8 @@ module.exports = {
3 3
     NODE_ENV: '"development"'
4 4
   },
5 5
   defineConstants: {
6
-    HOST: '"https://xlk.njyz.tech"',
7
-    // HOST: '"https://www.newhousehold.cn"',
6
+    // HOST: '"https://xlk.njyz.tech"',
7
+    HOST: '"https://www.newhousehold.cn"',
8 8
     // HOST: '"http://127.0.0.1:8567"',
9 9
     WSS_HOST: '"wss://xlk.njyz.tech"',
10 10
     OSS_PATH: '"https://xlk-assets.oss-cn-shanghai.aliyuncs.com/"',

+ 18
- 1
src/components/Poster/Poster.jsx Visa fil

@@ -64,6 +64,23 @@ export default (props) => {
64 64
       const ticker = setInterval(() => {
65 65
         if (cnt >= 3) {
66 66
           // 图片全部处理完
67
+          // // console.log('--------download--------->')
68
+          // sdk.download('canvas-poster').then((tempPath) => {
69
+          //   onEnd(tempPath, sdk)
70
+          //   clearInterval(ticker)
71
+          // }).catch(() => {
72
+          //   clearInterval(ticker)
73
+          // })
74
+
75
+          // const image = sdk.toDataURL();
76
+          // Taro.getImageInfo({
77
+          //   src: image,
78
+          //   success: (res => {
79
+          //     onEnd(res.path, sdk)
80
+          //     clearInterval(ticker)
81
+          //   })
82
+          // })
83
+
67 84
           const image = sdk.toDataURL()
68 85
           onEnd(image, sdk)
69 86
           clearInterval(ticker)
@@ -73,5 +90,5 @@ export default (props) => {
73 90
     }
74 91
   }, [sdk, dataSource, onStart, onEnd, onError])
75 92
 
76
-  return <View style={{width: 0, height: 0}}><Canvas type='2d' id='canvas-poster' /></View>
93
+  return <View style={{ width: '1px', height: '1px' }}><Canvas type='2d' id='canvas-poster' /></View>
77 94
 }

+ 17
- 17
src/components/Poster/config.js Visa fil

@@ -1,4 +1,4 @@
1
-import Taro, { preloadData } from '@tarojs/taro'
1
+import Taro from '@tarojs/taro'
2 2
 
3 3
 /**
4 4
  * 获取配置
@@ -7,13 +7,13 @@ import Taro, { preloadData } from '@tarojs/taro'
7 7
 export function getConfig() {
8 8
   // https://developers.weixin.qq.com/miniprogram/dev/api/base/system/wx.getSystemInfo.html
9 9
   const systemInfo = Taro.getStorageSync('systemInfo')
10
-  const { windowWidth, pixelRatio: dpr } = systemInfo
10
+  const { windowWidth } = systemInfo
11 11
 
12 12
   // canvas
13 13
   const canvas = {
14
-    width: windowWidth * dpr,
14
+    width: windowWidth,
15 15
     // 设计稿宽高比 2.16  = height / width
16
-    height: windowWidth * dpr * 2.16,
16
+    height: windowWidth * 2.16,
17 17
   }
18 18
 
19 19
   // 图片
@@ -24,31 +24,31 @@ export function getConfig() {
24 24
     y: 0,
25 25
   }
26 26
 
27
-  const marign = 22.5 * dpr;
28
-  const innerMargin = 15 * dpr
27
+  const marign = 22.5;
28
+  const innerMargin = 15
29 29
 
30 30
   // 头像
31 31
   const avatar = {
32 32
     arc: true,  // arc 代表裁剪为圆形
33
-    width: 66 * dpr,
34
-    height: 66 * dpr,
33
+    width: 66,
34
+    height: 66,
35 35
     x: marign,
36
-    y: poster.height + (39 * dpr),
36
+    y: poster.height + 39,
37 37
   }
38 38
 
39 39
   // 小程序码
40 40
   const miniCode = {
41 41
     arc: true,
42
-    width: 99 * dpr,
43
-    height: 99 * dpr,
44
-    x: canvas.width - (99 * dpr) - marign,  // 99 是宽度
45
-    y: poster.height + (20 * dpr),
42
+    width: 99,
43
+    height: 99,
44
+    x: canvas.width - 99 - marign,  // 99 是宽度
45
+    y: poster.height + 20,
46 46
   }
47 47
 
48 48
   // 姓名
49 49
   const name = {
50 50
     // https://developer.mozilla.org/zh-CN/docs/Web/CSS/font
51
-    font: `${15 * dpr}px serif`,
51
+    font: '15px serif',
52 52
     // https://developer.mozilla.org/zh-CN/docs/Web/API/CanvasRenderingContext2D/fillStyle
53 53
     fillStyle: '#333333',
54 54
 
@@ -57,16 +57,16 @@ export function getConfig() {
57 57
     maxWidth: canvas.width - (marign * 2) - avatar.width - miniCode.width - (innerMargin * 2),
58 58
 
59 59
     x: marign + avatar.width + innerMargin,
60
-    y: poster.height + (65 * dpr),
60
+    y: poster.height + 65,
61 61
   }
62 62
 
63 63
   // 说明
64 64
   const desc = {
65
-    font: `${10 * dpr}px serif`,
65
+    font: '10px serif',
66 66
     fillStyle: '#666666',
67 67
     maxWidth: name.maxWidth,
68 68
     x: name.x,
69
-    y: poster.height + (89.5 * dpr),
69
+    y: poster.height + (89.5),
70 70
   }
71 71
 
72 72
   return {

+ 16
- 5
src/components/Poster/index.jsx Visa fil

@@ -38,12 +38,23 @@ export default (props) => {
38 38
 
39 39
     // 保存到相册
40 40
     setLoading(true)
41
+    // save2Album(img).then(() => {
42
+    //   setLoading(false)
43
+    //   onSuccess()
44
+    //   Taro.showToast({ title: '保存成功', icon: 'none' })
45
+    // }, () => setLoading(false))
41 46
     sdkRef.current.download().then((tempPath) => {
42
-      save2Album(tempPath).then(() => {
43
-        setLoading(false)
44
-        onSuccess()
45
-        Taro.showToast({ title: '保存成功', icon: 'none' })
46
-      }, () => setLoading(false))
47
+      console.log('------poster----tempPath---', tempPath, new Date().valueOf())
48
+
49
+      Taro.previewImage({
50
+        urls: [tempPath]
51
+      })
52
+
53
+      // save2Album(tempPath).then(() => {
54
+      //   setLoading(false)
55
+      //   onSuccess()
56
+      //   Taro.showToast({ title: '保存成功', icon: 'none' })
57
+      // }, () => setLoading(false))
47 58
     }).catch((err) => {
48 59
       setLoading(false)
49 60
       console.error(err)

+ 18
- 6
src/components/Poster/sdk.js Visa fil

@@ -1,5 +1,7 @@
1 1
 import Taro from '@tarojs/taro'
2 2
 
3
+const sysInfo = Taro.getSystemInfoSync()
4
+
3 5
 export default function factory(selector) {
4 6
   return new Promise((resolve) => {
5 7
     const query = Taro.createSelectorQuery()
@@ -13,6 +15,7 @@ export default function factory(selector) {
13 15
 }
14 16
 
15 17
 export function posterSDK(canvas) {
18
+  let dpr;
16 19
   let onError = (err) => console.error(err);
17 20
   const ctx = canvas.getContext('2d')
18 21
 
@@ -21,8 +24,11 @@ export function posterSDK(canvas) {
21 24
    * @param {*} config 
22 25
    */
23 26
   function initCanvas(config) {
24
-    canvas.width = config.canvas.width
25
-    canvas.height = config.canvas.height
27
+    dpr = config.systemInfo.pixelRatio;
28
+
29
+    canvas.width = config.canvas.width * dpr;
30
+    canvas.height = config.canvas.height * dpr;
31
+    ctx.scale(dpr, dpr);
26 32
 
27 33
     // 默认白色背景
28 34
     ctx.fillStyle="#FFFFFF";
@@ -107,13 +113,19 @@ export function posterSDK(canvas) {
107 113
    * @param {*} options 
108 114
    * @returns 
109 115
    */
110
-  function download(options) {
116
+  function download(canvasId) {
111 117
     return new Promise((resovle, reject) => {
112 118
       Taro.canvasToTempFilePath({
119
+        canvasId,
113 120
         canvas,
114
-        fileType: 'jpg',
115
-        quality: 0.8,
116
-        ...(options || {}),
121
+        fileType: 'png',
122
+        quality: 0.9,
123
+        x: 0,
124
+        y: 0,
125
+        width: canvas.width,
126
+        height: canvas.height,
127
+        destWidth: canvas.width,
128
+        destHeight: canvas.height,
117 129
         success: (res) => resovle(res.tempFilePath),
118 130
         fail: (err) => {
119 131
           onError('exportImage', err)

+ 5
- 0
src/utils/image.js Visa fil

@@ -40,6 +40,8 @@ export function getImgURL(img) {
40 40
 export function save2Album(tempPath) {
41 41
   return new Promise((resolve, reject) => {
42 42
     authorize('scope.writePhotosAlbum').then(() => {
43
+      console.log('-----------save2Album---------->', tempPath)
44
+
43 45
       // 再保存到相册
44 46
       Taro.saveImageToPhotosAlbum({
45 47
         filePath: tempPath,
@@ -57,6 +59,9 @@ export function save2Album(tempPath) {
57 59
             icon: 'none',
58 60
           })
59 61
           reject(err)
62
+        },
63
+        complete: (res) => {
64
+          console.log('---------complete----------->', res)
60 65
         }
61 66
       })
62 67
     }).catch((err) => {