From dfc4b7a84bc9273445b8e3a787a3a68cfebf1dfb Mon Sep 17 00:00:00 2001 From: aixianling Date: Fri, 23 Dec 2022 18:02:51 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E7=89=88=E5=B0=8F=E7=A8=8B=E5=BA=8F?= =?UTF-8?q?=E6=89=93=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/rest/wechat/add.js | 26 +++++++------------------- src/rest/wechat/download.js | 10 +++++----- src/rest/wechat/getZip.js | 18 ++++++++---------- src/rest/wechat/list.js | 28 ++++++++-------------------- src/rest/wechat/sync.js | 21 +++++++++++++++++++++ 5 files changed, 49 insertions(+), 54 deletions(-) create mode 100644 src/rest/wechat/sync.js diff --git a/src/rest/wechat/add.js b/src/rest/wechat/add.js index 902c6d2..243896d 100644 --- a/src/rest/wechat/add.js +++ b/src/rest/wechat/add.js @@ -3,26 +3,14 @@ module.exports = { action: "/node/wxmp/addOrUpdate", method: "post", execute: (request, response) => { - let {appid, privateKey, projectPath, version, miniapp_appid, id: pid, npmScript} = request.body, - form = {appid, privateKey, projectPath, version, pid, npmScript}, sql - if (form.appid) {//编辑 - let arr = Object.keys(form).filter(e => form[e] != undefined).map(e => `${e}='${form[e]}'`) - sql = `update node_wxmp_config set ${arr.join(",")} where appid='${form.appid}' and pid='${pid}'` - } else {//新增 - let cols = [], arr = [] - form.appid = miniapp_appid - Object.keys(form).map(e => { - if (form[e]) { - cols.push(e) - arr.push(`'${form[e]}'`) - } - }) - sql = `insert into node_wxmp_config (${cols.join(",")}) values(${arr.join(",")})` - } - dbUtils.query(sql).then(() => { - response.send({code: 0}) + let {appid, privateKey, projectPath, version, versionName, id, npmScript} = request.body, + form = {appid, privateKey, projectPath, version, versionName, id, npmScript} + dbUtils.addOrUpdate({ + table: 'node_wxmp_deploy', form + }).then(data => { + response.send({code: 0, data}) }).catch(err => { - response.send({code: 1, err: err.sqlMessage}) + response.send({code: 1, err: err?.sqlMessage || err || ""}) }) } } diff --git a/src/rest/wechat/download.js b/src/rest/wechat/download.js index b59ee9c..452a34d 100644 --- a/src/rest/wechat/download.js +++ b/src/rest/wechat/download.js @@ -4,15 +4,15 @@ module.exports = { action: "/node/wxmp/download", method: "post", execute: (request, response) => { - let {pid, appid: aid} = request.query, appid = [aid, pid].join("_") - if (appid) { - let path = `/home/deploy/wxmpZips/${appid}/`, zipPath = `/home/deploy/wxmpZips/${appid}.zip` + let {id} = request.query + if (id) { + let path = `/home/deploy/node_deploy/zips/${id}/`, zipPath = `/home/deploy/node_deploy/zips/${id}.zip` fse.removeSync(zipPath) fse.pathExists(path, (err, exists) => { console.log(`${path}=========>${exists}`) if (exists) { - execute(`cd ${path}&&zip -r ${appid}.zip .`) - .then(() => execute(`cd ${path}&&mv ${appid}.zip /home/deploy/wxmpZips`)) + execute(`cd ${path}&&zip -r ${id}.zip .`) + .then(() => execute(`cd ${path}&&mv ${id}.zip /home/deploy/node_deploy/zips`)) .then(() => { console.log('压缩完成!') setTimeout(() => { diff --git a/src/rest/wechat/getZip.js b/src/rest/wechat/getZip.js index ebf961d..74df7b4 100644 --- a/src/rest/wechat/getZip.js +++ b/src/rest/wechat/getZip.js @@ -7,19 +7,17 @@ module.exports = { action: "/node/wxmp/getZip", method: "post", execute: (request, response) => { - let {appid, id} = request.query, uniCon = `appid='${appid}' and pid='${id}'`, uid = [appid, id].join("_") - db.query(`select * from node_dvcp_config where id='${id}'`).then(res => { + const {id, appid} = request.query + db.query(`select * from node_wxmp_deploy where id='${id}'`).then(res => { let info = res?.[0], sql - if (info?.appid && info?.pid) { - sql = `update node_wxmp_config set error=null where appid='${info.appid}' and pid='${info.pid}'` - } else if (info?.miniapp_appid) { - sql = `insert into node_wxmp_config ('appid','pid') values('${info?.miniapp_appid}','${info.id}')` + if (info?.id) { + sql = `update node_wxmp_deploy set error=null where id='${id}'` } else return response.send({code: 1, err: "无法找到小程序信息"}) db.query(sql).then(() => setTimeout(() => { response.send({code: 0}) }, 2000)) - const path = info.projectPath || (isDev ? 'E:\\code\\cunwei\\dvcp_v2_wechat' : '/home/deploy/node-service/dvcp_v2_wxmp'), - dest = (isDev ? `E:\\wxmpZips\\${uid}` : `/home/deploy/wxmpZips/${uid}/`), + const path = info.projectPath || (isDev ? 'E:\\code\\cunwei\\dvcp_v2_wechat' : '/home/deploy/node_deploy/dvcp_v2_wechat'), + dest = (isDev ? `E:\\wxmpZips\\${id}` : `/home/deploy/node_deploy/zips/${id}/`), processEnv = info.npmScript || 'build', dist = info.npmScript ? `${path}/dist/${info.npmScript}/` : `${path}/dist/build/mp-weixin/` execute(`cd ${path}&&npm run apps&&node bin/pages.js ${appid} ${id}&&npm run ${processEnv}`) @@ -28,10 +26,10 @@ module.exports = { .then(() => fse.copy(dist, dest)) .then(() => fse.emptyDir(dist)) .then(() => { - db.query(`update node_wxmp_config set error='打包时间:${dayjs().format("YYYY-MM-DD HH:mm:ss")}' where ${uniCon}`) + db.query(`update node_wxmp_deploy set error='打包时间:${dayjs().format("YYYY-MM-DD HH:mm:ss")}' where id='${id}'`) }).catch(err => { console.log(err) - db.query(`update node_wxmp_config set error='${err}' where ${uniCon}`) + db.query(`update node_wxmp_deploy set error='${err}' where id='${id}'`) }) }).catch(err => { console.log(err) diff --git a/src/rest/wechat/list.js b/src/rest/wechat/list.js index 44aba36..3738afd 100644 --- a/src/rest/wechat/list.js +++ b/src/rest/wechat/list.js @@ -3,26 +3,14 @@ module.exports = { action: "/node/wxmp/list", method: "post", execute: (request, response) => { - let total = 0, records = [], {size, current, name} = request.query - Promise.all([ - dbUtils.query(`select 1 from node_dvcp_config`).then(res => { - return total = res.length - }), - new Promise(resolve => { - let sql = `select * from node_dvcp_config where name like '%${name}%' or version like '%${name}%' or appid like '%${name}%' limit ${(current-1)*size},${size}` - dbUtils.query(sql).then(res => { - records = res - resolve() - }).catch(err => { - response.send({code: 1, err: err.sqlMessage}) - }) - }) - - ]).then(() => { - response.send({ - code: 0, - data: {records, total} - }) + const {size, current, name, type} = request.query + dbUtils.list({ + table: 'node_wxmp_deploy', con: name, + search: {size, current, type} + }).then(data => { + response.send({code: 0, data}) + }).catch(err => { + response.send({code: 1, err: err?.sqlMessage || err || ""}) }) } } diff --git a/src/rest/wechat/sync.js b/src/rest/wechat/sync.js new file mode 100644 index 0000000..1d6e59d --- /dev/null +++ b/src/rest/wechat/sync.js @@ -0,0 +1,21 @@ +const dbUtils = require("../../utils/dbUitls"); +uid = e => [e.appid, e.corpId].join("_") +module.exports = { + action: "/node/wxmp/sync", + method: "post", + execute: (request, response) => { + let records = {}, meta = [] + Promise.all([ + dbUtils.query("select * from app_dvcp_config").then(res => meta = res), + dbUtils.query("select * from node_wxmp_deploy").then(res => res?.map(e => records[uid(e)] = e)) + ]).then(() => Promise.all(meta.map(e => { + const {name, miniapp_appid: appid, corp_id: corpId, web_url: webUrl} = e + const id = records[uid({appid, corpId})]?.id + delete e.system_info + return dbUtils.addOrUpdate({ + table: 'node_wxmp_deploy', + form: {id, name, appid, corpId, webUrl, dvcpConfig: e} + }) + }))).finally(() => response.send({code: 0, data: ""})) + } +}