Skip to content

Commit 85a8d24

Browse files
committed
build: use tencent cloud instead
1 parent ae8485f commit 85a8d24

File tree

4 files changed

+92
-62
lines changed

4 files changed

+92
-62
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
"axios": "^0.26.1",
2828
"cli-progress": "^3.10.0",
2929
"commonmark": "^0.30.0",
30+
"cos-nodejs-sdk-v5": "^2.11.7",
3031
"form-data": "^4.0.0",
31-
"md5": "^2.3.0"
32+
"md5": "^2.3.0",
33+
"md5-file": "^5.0.0"
3234
}
3335
}

project.config.json

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"include": []
1717
},
1818
"setting": {
19-
"urlCheck": true,
19+
"urlCheck": false,
2020
"es6": true,
2121
"enhance": true,
2222
"postcss": true,
@@ -53,14 +53,18 @@
5353
"useCompilerPlugins": [
5454
"less"
5555
],
56-
"ignoreUploadUnusedFiles": true,
57-
"useStaticServer": true
56+
"ignoreUploadUnusedFiles": true
5857
},
5958
"compileType": "miniprogram",
6059
"libVersion": "2.22.1",
6160
"appid": "wx01462be634a0d447",
6261
"projectname": "HowToCookOnMiniprogram",
6362
"cloudfunctionTemplateRoot": "cloudfunctionTemplate/",
63+
"srcMiniprogramRoot": "miniprogram/",
64+
"editorSetting": {
65+
"tabIndent": "insertSpaces",
66+
"tabSize": 2
67+
},
6468
"condition": {
6569
"search": {
6670
"list": []
@@ -80,10 +84,5 @@
8084
"miniprogram": {
8185
"list": []
8286
}
83-
},
84-
"srcMiniprogramRoot": "miniprogram/",
85-
"editorSetting": {
86-
"tabIndent": "insertSpaces",
87-
"tabSize": 2
8887
}
8988
}

script/index.js

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -4,62 +4,11 @@ const glob = require('glob')
44
const {marked} = require('marked')
55
const MagicString = require('magic-string')
66
const md5 = require('md5')
7-
const axios = require('axios')
8-
const FormData = require('form-data');
97
const cliProgress = require('cli-progress');
10-
11-
const config = require('../config')
8+
const { uploadImage } = require('./upload')
129

1310
const bar = new cliProgress.SingleBar({}, cliProgress.Presets.shades_classic);
1411

15-
let accessToken = ''
16-
const getAccessToken = async() => {
17-
if (accessToken) return accessToken
18-
19-
const { data } = await axios.get('https://api.weixin.qq.com/cgi-bin/token', {
20-
params: {
21-
grant_type: 'client_credential',
22-
appid: config.appid,
23-
secret: config.secret
24-
}
25-
})
26-
if (data.access_token) {
27-
accessToken = data.access_token
28-
return accessToken
29-
}
30-
throw new TypeError(data)
31-
}
32-
33-
34-
const uploadImage = async (filePath) => {
35-
const cloudPath = 'cookbook'
36-
const token = await getAccessToken()
37-
const url = 'https://api.weixin.qq.com/tcb/uploadfile?access_token=' + token
38-
const { data } = await axios.post(url, {
39-
env: config.cloudEnvId,
40-
path: cloudPath
41-
})
42-
if (data.errcode == 0) {
43-
const { url, token, authorization, file_id, cos_file_id} = data;
44-
const form = new FormData()
45-
46-
form.append('key', authorization);
47-
form.append('Signature', authorization);
48-
form.append('x-cos-security-token', token);
49-
form.append('x-cos-meta-fileid', cos_file_id);
50-
form.append('file', fs.createReadStream(filePath));
51-
52-
return new Promise((resolve, reject) => {
53-
form.submit(url, (err) => {
54-
if (err) reject(err)
55-
resolve(file_id.replace(cloudPath, '') + authorization)
56-
})
57-
})
58-
} else {
59-
throw new TypeError(data.errmsg)
60-
}
61-
}
62-
6312
glob(path.resolve(__dirname, '../HowToCook/dishes/**/*.md'), {}, async (err, files) => {
6413
if (err) {
6514
console.log(err)
@@ -185,5 +134,5 @@ glob(path.resolve(__dirname, '../HowToCook/dishes/**/*.md'), {}, async (err, fil
185134

186135
s.prepend('export default ')
187136
fs.writeFileSync('./miniprogram/data.js', s.toString())
188-
fs.writeFileSync('./miniprogram/data.json', jsonData.toString())
137+
fs.writeFileSync('./data-v2.json', jsonData.toString())
189138
})

script/upload.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
const fs = require('fs');
2+
const COS = require('cos-nodejs-sdk-v5')
3+
const FormData = require('form-data');
4+
const axios = require('axios')
5+
const md5File = require('md5-file')
6+
const path = require('path')
7+
8+
const config = require('../config')
9+
10+
const cos = new COS({
11+
SecretId: config.cos.secretId,
12+
SecretKey: config.cos.secretKey
13+
});
14+
15+
let accessToken = ''
16+
const getAccessToken = async() => {
17+
if (accessToken) return accessToken
18+
19+
const { data } = await axios.get('https://api.weixin.qq.com/cgi-bin/token', {
20+
params: {
21+
grant_type: 'client_credential',
22+
appid: config.appid,
23+
secret: config.secret
24+
}
25+
})
26+
if (data.access_token) {
27+
accessToken = data.access_token
28+
return accessToken
29+
}
30+
throw new TypeError(data)
31+
}
32+
33+
const uploadWxCloud = async(filePath) => {
34+
const cloudPath = 'cookbook'
35+
const token = await getAccessToken()
36+
const url = 'https://api.weixin.qq.com/tcb/uploadfile?access_token=' + token
37+
const { data } = await axios.post(url, {
38+
env: config.cloudEnvId,
39+
path: cloudPath
40+
})
41+
if (data.errcode == 0) {
42+
const { url, token, authorization, file_id, cos_file_id} = data;
43+
const form = new FormData()
44+
45+
form.append('key', authorization);
46+
form.append('Signature', authorization);
47+
form.append('x-cos-security-token', token);
48+
form.append('x-cos-meta-fileid', cos_file_id);
49+
form.append('file', fs.createReadStream(filePath));
50+
51+
return new Promise((resolve, reject) => {
52+
form.submit(url, (err) => {
53+
if (err) reject(err)
54+
resolve(file_id.replace(cloudPath, '') + authorization)
55+
})
56+
})
57+
} else {
58+
throw new TypeError(data.errmsg)
59+
}
60+
}
61+
62+
const uploadImage = async (filePath) => {
63+
const body = fs.createReadStream(filePath);
64+
const key = md5File.sync(filePath);
65+
66+
return new Promise((resolve, reject) => {
67+
cos.putObject({
68+
Bucket: 'how-to-cook-1255404841',
69+
Region: 'ap-shanghai',
70+
Key: key,
71+
Body: body
72+
}, (err, data) => {
73+
if (err) reject(err)
74+
resolve('https://' + data.Location)
75+
})
76+
})
77+
}
78+
79+
module.exports.uploadWxCloud = uploadWxCloud;
80+
module.exports.uploadImage = uploadImage;

0 commit comments

Comments
 (0)