From a49d799afe2a4dfaf383a0cf1ecbb51955dc2550 Mon Sep 17 00:00:00 2001 From: aixianling Date: Mon, 16 May 2022 10:08:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=BA=94=E7=94=A8=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/rest/wechatapps/add.js | 39 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/src/rest/wechatapps/add.js b/src/rest/wechatapps/add.js index 4bc8547..d9a4b0c 100644 --- a/src/rest/wechatapps/add.js +++ b/src/rest/wechatapps/add.js @@ -1,39 +1,38 @@ const dbUtils = require("../../utils/dbUitls"); -const {v4: uuid} = require('uuid'); const addOrUpdate = form => { - let sql - if (form.id) {//编辑 - let arr = Object.keys(form).filter(e => form[e]).map(e => `${e}='${form[e]}'`) - sql = `update node_wechat_apps set ${arr.join(",")} where id='${form.name}'` - } else {//新增 - let cols = [], arr = [] - Object.keys(form).map(e => { - if (form[e]) { - cols.push(e) - arr.push(`'${form[e]}'`) - } - }) - sql = `insert into node_wechat_apps (id,${cols.join(",")}) values('${form.name}',${arr.join(",")})` - } - return sql + return dbUtils.query(`select 1 from node_wechat_apps where id=${form.id}`).then(res => { + if (res.length > 0) {//编辑 + let arr = Object.keys(form).filter(e => form[e]).map(e => `${e}='${form[e]}'`) + return `update node_wechat_apps set ${arr.join(",")} where id='${form.name}'` + } else {//新增 + let cols = [], arr = [] + Object.keys(form).map(e => { + if (form[e]) { + cols.push(e) + arr.push(`'${form[e]}'`) + } + }) + return `insert into node_wechat_apps (id,${cols.join(",")}) values('${form.name}',${arr.join(",")})` + } + }) + } module.exports = { action: "/node/wechatapps/addOrUpdate", method: "post", execute: (request, response) => { - let form = request.body, sql + let form = request.body if (form.list?.length > 0) { - Promise.all(form.list.map(e => dbUtils.query(addOrUpdate(e)))).then(() => { + Promise.all(form.list.map(e => addOrUpdate(e).then(sql => dbUtils.query(sql)))).then(() => { response.send({code: 0}) }).catch(err => { response.send({code: 1, err: err.sqlMessage}) }) } else { if (form.name) { - sql = addOrUpdate(form) - sql && dbUtils.query(sql).then(() => { + addOrUpdate(form).then(sql => dbUtils.query(sql)).then(() => { response.send({code: 0}) }).catch(err => { response.send({code: 1, err: err.sqlMessage})