Commit 7ac8f13d authored by wty's avatar wty

更改连接池,尝试更改路由方法

parent 60c96dd7
...@@ -3,37 +3,30 @@ const MYSQL = require('mysql') ...@@ -3,37 +3,30 @@ const MYSQL = require('mysql')
const config = require('../config/dev.json') const config = require('../config/dev.json')
const request = require('request') const request = require('request')
// request(config['remote_config_url'],{},(error, request, body)=>{ const getMysql = new Promise((resolve)=>{
// const data = JSON.parse(body) request(config['remote_config_url'],{},(error, request, body)=>{
// const source = data['propertySources'][0]['source'] const data = JSON.parse(body)
// const username = source['apollo.mysql.duiba_web_tool.username'] const source = data['propertySources'][0]['source']
// const password = source['apollo.mysql.duiba_web_tool.password'] const username = source['apollo.mysql.duiba_web_tool.username']
// const database = source['apollo.mysql.duiba_web_tool.database'] const password = source['apollo.mysql.duiba_web_tool.password']
// const host = source['apollo.mysql.duiba_web_tool.url'].split(':')[0] const database = source['apollo.mysql.duiba_web_tool.database']
// const port = source['apollo.mysql.duiba_web_tool.url'].split(':')[1] const host = source['apollo.mysql.duiba_web_tool.url'].split(':')[0]
// const Mysql = MYSQL.createPool({ const port = source['apollo.mysql.duiba_web_tool.url'].split(':')[1]
// 'host': host, const Mysql = MYSQL.createPool({
// 'port': port, 'host': host,
// 'user': username, 'port': port,
// 'password': password, 'user': username,
// 'database': database, 'password': password,
// 'connectionLimit': 1000 'database': database,
// }) 'connectionLimit': 1000
// console.info('MysqlConfig:',Mysql.config) })
// console.info('OBJMysql',Mysql) console.info('MysqlConfig:',Mysql.config)
// module.exports = Mysql console.info('OBJMysql',Mysql)
// }) resolve(Mysql)
const Mysql = MYSQL.createPool({ })
'host': 'rm-bp1jglyqbhi8qwf0a.mysql.rds.aliyuncs.com',
'port': 3306,
'user': "duiba_dev",
'password': "dbseccode3qTecHjXZCTSNmptANeMVnAURVVzgSoj",
'database': "duiba_web_tool",
'connectionLimit': 1000
}) })
console.info('MysqlConfig:', Mysql.config)
console.info('OBJMysql', Mysql) module.exports = getMysql
module.exports = Mysql
......
const express = require('express') const express = require('express')
const request = require("request"); const request = require("request");
const Mysql = require('../SQLPool') // const Mysql = require('../SQLPool')
const getMysql = require('../SQLPool')
const router = express.Router() const router = express.Router()
getMysql.then(Mysql => {
router.get('/queryType', (req, res) => { router.get('/queryType', (req, res) => {
const SQL = 'SELECT * FROM type' const SQL = 'SELECT * FROM type'
Mysql.query(SQL, (err, result) => { Mysql.query(SQL, (err, result) => {
if (err) { if (err) {
res.send({
success: false,
err
})
}
res.send({ res.send({
success: false, success: true,
err data: result
}) })
}
res.send({
success: true,
data: result
}) })
}) });
});
router.get('/queryChildType', (req, res) => { router.get('/queryChildType', (req, res) => {
const {type_id} = req.query const {type_id} = req.query
const SQL = 'SELECT child_type.child_type_id, child_type.child_type_name, child_type.type_id FROM child_type INNER JOIN type ON child_type.type_id = type.type_id WHERE type.type_id = ?' const SQL = 'SELECT child_type.child_type_id, child_type.child_type_name, child_type.type_id FROM child_type INNER JOIN type ON child_type.type_id = type.type_id WHERE type.type_id = ?'
Mysql.query(SQL,[type_id], (err, result) => { Mysql.query(SQL,[type_id], (err, result) => {
if (err) { if (err) {
res.send({
success: false,
err
})
}
res.send({ res.send({
success: false, success: true,
err data: result
}) })
}
res.send({
success: true,
data: result
}) })
}) });
});
router.get('/queryToolBox', (req, res) => { router.get('/queryToolBox', (req, res) => {
let {isFollow, type_id} = req.query let {isFollow, type_id} = req.query
let SQL = `SELECT toolbox.*, type_childtype_toolbox.child_type_id FROM type_childtype_toolbox INNER JOIN toolbox ON type_childtype_toolbox.toolbox_id = toolbox.toolbox_id WHERE type_childtype_toolbox.type_id = ?` let SQL = `SELECT toolbox.*, type_childtype_toolbox.child_type_id FROM type_childtype_toolbox INNER JOIN toolbox ON type_childtype_toolbox.toolbox_id = toolbox.toolbox_id WHERE type_childtype_toolbox.type_id = ?`
if (!isFollow && !type_id){ if (!isFollow && !type_id){
SQL = `SELECT * FROM toolbox` SQL = `SELECT * FROM toolbox`
}
if (isFollow) {
type_id = JSON.parse(type_id)
SQL = 'SELECT * FROM toolbox WHERE toolbox_id IN (?)'
}
Mysql.query(SQL, [type_id], (err, result) => {
if (err) {
res.send({
success: false,
err
})
throw err
} }
if (isFollow) {
type_id = JSON.parse(type_id)
SQL = 'SELECT * FROM toolbox WHERE toolbox_id IN (?)'
}
Mysql.query(SQL, [type_id], (err, result) => {
if (err) {
res.send({
success: false,
err
})
throw err
}
res.send({ res.send({
success: true, success: true,
data: result data: result
})
}) })
}) });
}); }).catch(e => console.error(e))
// router.get('/queryToolBoxSortId', (req, res) => { // router.get('/queryToolBoxSortId', (req, res) => {
// const SQL = 'SELECT sortId FROM toolbox' // const SQL = 'SELECT sortId FROM toolbox'
......
const express = require('express'); const express = require('express');
const Mysql = require('../SQLPool')
const router = express.Router(); const router = express.Router();
const getMysql = require('../SQLPool')
const ErrorCode = { const ErrorCode = {
USER_NOT_LOGIN: 70001 USER_NOT_LOGIN: 70001
} }
getMysql.then(Mysql => {
/* GET users listing. */ /* GET users listing. */
router.get('/getUser', function (req, res, next) { router.get('/getUser', function (req, res, next) {
let {uid} = req.cookies let {uid} = req.cookies
if (!uid){ if (!uid){
uid = req.query.uid uid = req.query.uid
}
// if (!uid) {
// res.send({
// success: false,
// code: ErrorCode.USER_NOT_LOGIN,
// data: '用户尚未登录'
// })
// return
// }
const SQL = 'SELECT * FROM `user` WHERE `user`.uid = ?'
Mysql.query(SQL, [uid], (err, result) => {
if (err || !result || !result.length) {
res.send({
success: false,
err
})
return
} }
console.log(result) // if (!uid) {
const _result = result.slice() // res.send({
_result[0].follow = JSON.parse(_result[0].follow) // success: false,
res.send({ // code: ErrorCode.USER_NOT_LOGIN,
success: true, // data: '用户尚未登录'
data: { // })
..._result[0], // return
// }
const SQL = 'SELECT * FROM `user` WHERE `user`.uid = ?'
Mysql.query(SQL, [uid], (err, result) => {
if (err || !result || !result.length) {
res.send({
success: false,
err
})
return
} }
}) console.log(result)
}) const _result = result.slice()
}); _result[0].follow = JSON.parse(_result[0].follow)
router.get('/login', (req, res) => {
const {uid} = req.query
const SQL = 'SELECT * FROM `user` WHERE `user`.uid = ?'
Mysql.query(SQL, [uid], (err, result) => {
if (err) {
res.send({
success: false,
err
})
throw err
}
if (!!result && result[0].uid) {
res.cookie('uid', result[0].uid)
res.send({ res.send({
success: true, success: true,
data: '登录成功!' data: {
}) ..._result[0],
} else { }
res.send({
success: false,
data: '登录失败,用户不存在'
}) })
} })
});
router.get('/login', (req, res) => {
const {uid} = req.query
const SQL = 'SELECT * FROM `user` WHERE `user`.uid = ?'
Mysql.query(SQL, [uid], (err, result) => {
if (err) {
res.send({
success: false,
err
})
throw err
}
if (!!result && result[0].uid) {
res.cookie('uid', result[0].uid)
res.send({
success: true,
data: '登录成功!'
})
} else {
res.send({
success: false,
data: '登录失败,用户不存在'
})
}
})
}) })
})
router.get('/follow', (req, res) => { router.get('/follow', (req, res) => {
const {follow, uid} = req.query const {follow, uid} = req.query
const SQL = 'UPDATE user SET follow = ? WHERE uid = ?' const SQL = 'UPDATE user SET follow = ? WHERE uid = ?'
Mysql.query(SQL, [follow, uid], (err, result) => { Mysql.query(SQL, [follow, uid], (err, result) => {
if (err) { if (err) {
res.send({
success: false,
err
})
return
}
res.send({ res.send({
success: false, success: true,
err
}) })
return
}
res.send({
success: true,
}) })
}) })
}).catch(e => {
console.error(e)
}) })
module.exports = router; module.exports = router;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment