9c8d5f2cad
订单佣金只认关联用户;合伙人备注写入独立表;H5 增加用户管理与首页统计。 Co-authored-by: Cursor <cursoragent@cursor.com>
113 lines
3.2 KiB
JavaScript
113 lines
3.2 KiB
JavaScript
/**
|
|
* 上传小程序冷启动静态资源到 OSS(固定 key,不进小程序主包):
|
|
* static/mini-user/jiuzu-dragon.gif
|
|
* static/mini-user/jiuzu-dragon-mark.png
|
|
*
|
|
* 用法(在 server/dukang-api):
|
|
* node scripts/upload-mini-user-static.mjs
|
|
* 使用 server/dukang-api/.env 中的 OSS 凭证。
|
|
*/
|
|
import { readFileSync, existsSync } from 'fs';
|
|
import { resolve, dirname, extname } from 'path';
|
|
import { fileURLToPath } from 'url';
|
|
import { createRequire } from 'module';
|
|
|
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
const apiRoot = resolve(__dirname, '..');
|
|
const repoRoot = resolve(apiRoot, '../..');
|
|
const require = createRequire(resolve(apiRoot, 'package.json'));
|
|
const OSS = require('ali-oss');
|
|
|
|
function loadEnvFile(path) {
|
|
if (!existsSync(path)) return {};
|
|
const out = {};
|
|
for (const line of readFileSync(path, 'utf8').split(/\r?\n/)) {
|
|
const m = line.match(/^([A-Z0-9_]+)=(.*)$/);
|
|
if (!m) continue;
|
|
let v = m[2];
|
|
if (
|
|
(v.startsWith('"') && v.endsWith('"')) ||
|
|
(v.startsWith("'") && v.endsWith("'"))
|
|
) {
|
|
v = v.slice(1, -1);
|
|
}
|
|
out[m[1]] = v;
|
|
}
|
|
return out;
|
|
}
|
|
|
|
const env = {
|
|
...loadEnvFile(resolve(apiRoot, '.env.production')),
|
|
...loadEnvFile(resolve(apiRoot, '.env.development')),
|
|
...loadEnvFile(resolve(apiRoot, '.env')),
|
|
};
|
|
|
|
const accessKeyId = process.env.OSS_ACCESS_KEY_ID || env.OSS_ACCESS_KEY_ID || '';
|
|
const accessKeySecret = process.env.OSS_ACCESS_KEY_SECRET || env.OSS_ACCESS_KEY_SECRET || '';
|
|
const bucket = process.env.OSS_BUCKET || env.OSS_BUCKET || '';
|
|
const region = process.env.OSS_REGION || env.OSS_REGION || 'oss-cn-beijing';
|
|
const cdnBase = (process.env.OSS_CDN_BASE || env.OSS_CDN_BASE || '').replace(/\/$/, '');
|
|
|
|
if (!accessKeyId || !accessKeySecret || !bucket) {
|
|
console.error('缺少 OSS_ACCESS_KEY_ID / OSS_ACCESS_KEY_SECRET / OSS_BUCKET');
|
|
process.exit(1);
|
|
}
|
|
|
|
const mimeByExt = {
|
|
'.gif': 'image/gif',
|
|
'.png': 'image/png',
|
|
'.jpg': 'image/jpeg',
|
|
'.jpeg': 'image/jpeg',
|
|
'.webp': 'image/webp',
|
|
};
|
|
|
|
const files = [
|
|
{
|
|
local:
|
|
process.env.JIUZU_SPLASH_GIF_LOCAL ||
|
|
env.JIUZU_SPLASH_GIF_LOCAL ||
|
|
resolve(repoRoot, 'apps/mini-user/src/assets/jiuzu-dragon.gif'),
|
|
ossKey: 'static/mini-user/jiuzu-dragon.gif',
|
|
},
|
|
{
|
|
local:
|
|
process.env.JIUZU_SPLASH_MARK_LOCAL ||
|
|
env.JIUZU_SPLASH_MARK_LOCAL ||
|
|
resolve(repoRoot, 'apps/mini-user/src/assets/jiuzu-dragon-mark.png'),
|
|
ossKey: 'static/mini-user/jiuzu-dragon-mark.png',
|
|
},
|
|
];
|
|
|
|
const client = new OSS({
|
|
region,
|
|
accessKeyId,
|
|
accessKeySecret,
|
|
bucket,
|
|
});
|
|
|
|
for (const file of files) {
|
|
if (!existsSync(file.local)) {
|
|
console.error('本地文件不存在:', file.local);
|
|
process.exit(1);
|
|
}
|
|
const ext = extname(file.local).toLowerCase();
|
|
const mime = mimeByExt[ext];
|
|
if (!mime) {
|
|
console.error('不支持的文件类型:', file.local);
|
|
process.exit(1);
|
|
}
|
|
const buffer = readFileSync(file.local);
|
|
await client.put(file.ossKey, buffer, {
|
|
mime,
|
|
headers: {
|
|
'Content-Disposition': 'inline',
|
|
'Cache-Control': 'public, max-age=31536000',
|
|
},
|
|
});
|
|
const url = cdnBase
|
|
? `${cdnBase}/${file.ossKey}`
|
|
: `https://${bucket}.${region}.aliyuncs.com/${file.ossKey}`;
|
|
console.log('uploaded:', file.ossKey);
|
|
console.log('url:', url);
|
|
}
|