张延森 3 years ago
parent
commit
c09be1e425

+ 2
- 2
config/dev.js View File

3
     NODE_ENV: '"development"'
3
     NODE_ENV: '"development"'
4
   },
4
   },
5
   defineConstants: {
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
     // HOST: '"http://127.0.0.1:8567"',
8
     // HOST: '"http://127.0.0.1:8567"',
9
     WSS_HOST: '"wss://xlk.njyz.tech"',
9
     WSS_HOST: '"wss://xlk.njyz.tech"',
10
     OSS_PATH: '"https://xlk-assets.oss-cn-shanghai.aliyuncs.com/"',
10
     OSS_PATH: '"https://xlk-assets.oss-cn-shanghai.aliyuncs.com/"',

+ 18
- 1
src/components/Poster/Poster.jsx View File

64
       const ticker = setInterval(() => {
64
       const ticker = setInterval(() => {
65
         if (cnt >= 3) {
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
           const image = sdk.toDataURL()
84
           const image = sdk.toDataURL()
68
           onEnd(image, sdk)
85
           onEnd(image, sdk)
69
           clearInterval(ticker)
86
           clearInterval(ticker)
73
     }
90
     }
74
   }, [sdk, dataSource, onStart, onEnd, onError])
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 View File

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

+ 16
- 5
src/components/Poster/index.jsx View File

38
 
38
 
39
     // 保存到相册
39
     // 保存到相册
40
     setLoading(true)
40
     setLoading(true)
41
+    // save2Album(img).then(() => {
42
+    //   setLoading(false)
43
+    //   onSuccess()
44
+    //   Taro.showToast({ title: '保存成功', icon: 'none' })
45
+    // }, () => setLoading(false))
41
     sdkRef.current.download().then((tempPath) => {
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
     }).catch((err) => {
58
     }).catch((err) => {
48
       setLoading(false)
59
       setLoading(false)
49
       console.error(err)
60
       console.error(err)

+ 18
- 6
src/components/Poster/sdk.js View File

1
 import Taro from '@tarojs/taro'
1
 import Taro from '@tarojs/taro'
2
 
2
 
3
+const sysInfo = Taro.getSystemInfoSync()
4
+
3
 export default function factory(selector) {
5
 export default function factory(selector) {
4
   return new Promise((resolve) => {
6
   return new Promise((resolve) => {
5
     const query = Taro.createSelectorQuery()
7
     const query = Taro.createSelectorQuery()
13
 }
15
 }
14
 
16
 
15
 export function posterSDK(canvas) {
17
 export function posterSDK(canvas) {
18
+  let dpr;
16
   let onError = (err) => console.error(err);
19
   let onError = (err) => console.error(err);
17
   const ctx = canvas.getContext('2d')
20
   const ctx = canvas.getContext('2d')
18
 
21
 
21
    * @param {*} config 
24
    * @param {*} config 
22
    */
25
    */
23
   function initCanvas(config) {
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
     ctx.fillStyle="#FFFFFF";
34
     ctx.fillStyle="#FFFFFF";
107
    * @param {*} options 
113
    * @param {*} options 
108
    * @returns 
114
    * @returns 
109
    */
115
    */
110
-  function download(options) {
116
+  function download(canvasId) {
111
     return new Promise((resovle, reject) => {
117
     return new Promise((resovle, reject) => {
112
       Taro.canvasToTempFilePath({
118
       Taro.canvasToTempFilePath({
119
+        canvasId,
113
         canvas,
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
         success: (res) => resovle(res.tempFilePath),
129
         success: (res) => resovle(res.tempFilePath),
118
         fail: (err) => {
130
         fail: (err) => {
119
           onError('exportImage', err)
131
           onError('exportImage', err)

+ 5
- 0
src/utils/image.js View File

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