From a211a1ab6a9773f301d9acd35837e0ab8193468c Mon Sep 17 00:00:00 2001 From: chenxf Date: Sun, 4 Jan 2026 17:49:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BE=9D=E8=B5=96=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E5=A3=B0=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build/genapi.js | 140 +++++++++++++++++++++++++++++++++++++++++++++ build/increpack.js | 96 ++++++++++++++++++++++++------- package.json | 2 +- 3 files changed, 216 insertions(+), 22 deletions(-) create mode 100644 build/genapi.js diff --git a/build/genapi.js b/build/genapi.js new file mode 100644 index 0000000..0166fe5 --- /dev/null +++ b/build/genapi.js @@ -0,0 +1,140 @@ +const axios = require('axios').default +const fs = require('fs') +const Path = require('path') + +function mkdirsSync(dirname) { + if (fs.existsSync(dirname)) { + return true + } else { + if (mkdirsSync(Path.dirname(dirname))) { + fs.mkdirSync(dirname) + return true + } + } +} +const fileTemplate = `/** + * 该文件由插件根据doc.html在启动项目时自动生成,请勿修改(修改了也会被覆盖 + */ + +import request from '@/utils/request' +` +const functionCodeGener = function(meta) { + const functionParams = [] + if (Array.isArray(meta.params) && meta.params.length > 0) { + functionParams.push('params') + } + if (Array.isArray(meta.data) && meta.data.length > 0) { + functionParams.push('data') + } + if (Array.isArray(meta.formData) && meta.formData.length > 0) { + functionParams.push('formData') + } + return ` +/** + * ${meta.comment} + */ +export function ${meta.name}(${functionParams.join(', ')}) { + return request({ + method: '${meta.method}', + url: '${meta.url}'${meta.requestType ? ',\n requestType: \'' + meta.requestType + '\'' : ''}${functionParams.includes('params') ? ',\n params' : ''}${functionParams.includes('data') || functionParams.includes('formData') ? ',\n data' + (functionParams.includes('formData') ? ': formData' : '') : ''} + }) +} +` +} +module.exports = class Genapi { + constructor(apiUrl, output = 'build') { + this.apiUrl = apiUrl[apiUrl.length - 1] === '/' ? apiUrl.slice(0, apiUrl.length - 1) : apiUrl + this.output = output + } + apply(compiler) { + compiler.hooks.environment.tap('Genapi', async(compilation, callback) => { + const projectPath = Path.join(__dirname, '../') + const apiPath = Path.join(projectPath, 'src/api/', this.output) + axios({ + url: this.apiUrl + '/swagger-resources', + method: 'get' + }).then(({ data }) => { + const resultFile = {} + Promise.all(data.map((apiScope) => { + return axios({ + url: this.apiUrl + '/v2/api-docs', + params: { + group: apiScope.name + }, + method: 'get' + }).then(({ data }) => { + const { + basePath, + paths + } = data + for (const _path in paths) { + const path = _path.replace(new RegExp('^' + basePath, 'g'), '') + const pathSplit = path.split('/').slice('/') + const apiName = pathSplit[pathSplit.length - 1] + const fileName = pathSplit.length > 2 ? pathSplit[pathSplit.length - 2] : '' + const mkdirPath = pathSplit.slice(0, pathSplit.length - 2).join('/') || '/' + const fileApiSet = new Set() + const filePath = Path.join(mkdirPath, fileName) + if (!resultFile[filePath]) { + resultFile[filePath] = fileTemplate + } + for (const method in paths[_path]) { + const apiMeta = paths[_path][method] + const parameters = apiMeta.parameters || [] + const formData = parameters.filter((item) => { + return item.in === 'formData' + }) + const data = parameters.filter((item) => { + return item.in === 'body' + }) + const params = parameters.filter((item) => { + return item.in === 'query' + }) + resultFile[filePath] += functionCodeGener({ + name: (() => { + const filterApiName = apiName.replace(/\.do$/g, '') + let result = Object.keys(paths[_path]).length > 1 ? `${method}_${filterApiName}` : filterApiName + if (fileApiSet.has(result)) { + let i = 1 + while (fileApiSet.has(`${result}_${i}`)) { + i++ + } + result = `${result}_${i}` + } + return result + })(), + method, + url: path, + comment: apiMeta.summary, + data, + params, + formData, + requestType: (() => { + if (method !== 'get') { + if (formData.length > 0) { + return 'formdata' + } else { + return 'json' + } + } + })() + }) + } + } + }).catch((e) => { + console.warn(`无法读取api文档: ${apiScope.name}`) + console.warn(e.message) + }) + })).then(() => { + for (const path in resultFile) { + mkdirsSync(Path.join(apiPath, path)) + const filepath = Path.join(apiPath, path, 'index.js') + if (fs.existsSync(filepath) && fs.readFileSync(filepath).toString() !== resultFile[path]) { + fs.writeFileSync(filepath, resultFile[path]) + } + } + }) + }).catch((e) => { }) + }) + } +} diff --git a/build/increpack.js b/build/increpack.js index ca70170..902d0bd 100644 --- a/build/increpack.js +++ b/build/increpack.js @@ -1,9 +1,9 @@ const path = require('path') const fs = require('fs') -var compressing = require('compressing') +const compressing = require('compressing') const axios = require('axios') const child_process = require('child_process') - +const packageJson = require('../package.json') function parseTime(time, format = 'yyyy-MM-dd hh:mm:ss') { const date = time instanceof Date ? time : new Date(time) @@ -32,29 +32,83 @@ function parseTime(time, format = 'yyyy-MM-dd hh:mm:ss') { * youfool */ class IncrePack { - constructor(options) { - this.options = options - } apply(compiler) { + if (process.env.VUE_APP_WEBPACK_CMD !== 'build') { + return + } + const date = new Date() const projectPath = path.join(__dirname, '../') const distPath = projectPath + 'dist' - compiler.hooks.done.tap('IncrePack', async (compilation, callback) => { - // 排除开发环境 - if (process.env.ENV !== 'development') { - - const increName = `${this.options.name}.zip` - // 打包压缩 - const tempFile = projectPath + '/' + increName - compressing.zip.compressDir(distPath, tempFile, { ignoreBase: true }) - .then(() => { - // 剪切回dist目录下 - fs.renameSync(tempFile, distPath + '/' + increName) - console.log('------------------打包成功------------------') - }) - .catch(err => { - console.error(err) - }) + const versionPath = `${projectPath}src/version.js` + let hash = '000000' + try { + hash = child_process.execSync('git rev-parse HEAD').slice(0, 6) + } catch (e) { + console.log('------------------读取版本Hash失败---------------') + console.warn(e) + } + const codeVersion = `${parseTime(date, 'yyyy-MM-dd hh:mm:ss')} ${hash} ${process.env.VUE_APP_ENVIRONMENT}` + compiler.hooks.beforeRun.tapAsync('SetVersion', async(compilation, callback) => { + const data = `/** 本文件由打包插件自动生成和修改 */\nexport default '${codeVersion}'\n` + fs.writeFileSync(versionPath, data) + callback() + }) + compiler.hooks.done.tap('SetVersion', async(compilation) => { + const data = `/** 本文件由打包插件自动生成和修改 */\nexport default ''\n` + fs.writeFileSync(versionPath, data) + }) + compiler.hooks.done.tap('IncrePack', async(compilation) => { + let versionJson = { + artifactId: packageJson.name, + version: packageJson.version, + appId: packageJson.appId, + codeId: packageJson.codeId, + codeVersion } + if (packageJson.increApi) { + // 获取版本信息 + try { + console.log('------------------正在写入升级记录---------------') + const res = await axios.default({ + url: packageJson.increApi + '?appId=' + packageJson.appId + '&codeId=' + packageJson.codeId, + method: 'get', + timeout: 5000 + }) + const { code, data } = res.data + if (code === 0 && data) { + versionJson = { + ...versionJson, + artifactId: packageJson.name, + upgradeId: data.upgradeId, + version: data.version, + releaseTime: data.releaseTime, + fromCommit: data.fromCommit, + untilCommit: data.untilCommit, + newFeatures: data.newFeatures, + repair: data.repair, + other: data.other + } + console.log('------------------写入升级记录成功---------------') + } + } catch (e) { + console.log(e.message) + console.log('------------------提交升级记录到远端失败---------------') + } + } + fs.mkdirSync(distPath + '/increpack-config') + fs.writeFileSync(distPath + '/increpack-config/version.json', JSON.stringify(versionJson), 'utf8') + const increName = `${packageJson.name}-${process.env.VUE_APP_ENVIRONMENT}-${parseTime(date, 'yyyy-MM-dd')}-${hash}.zip` + // 打包压缩 + const tempFile = projectPath + '/' + increName + compressing.zip.compressDir(distPath, tempFile, { ignoreBase: true }) + .then(() => { + // 剪切回dist目录下 + fs.renameSync(tempFile, distPath + '/' + increName) + console.log('------------------打包成功------------------') + }) + .catch(err => { + console.error(err) + }) }) } } diff --git a/package.json b/package.json index e370b73..aa3e753 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ }, "dependencies": { "@amap/amap-jsapi-loader": "^1.0.1", - "@geoscene/core": "^4.29.10", + "@geoscene/core": "4.29.10", "axios": "^0.21.1", "babel-polyfill": "^6.26.0", "echarts": "^4.8.0",