let express = require('express');
let app = express();
let mysql = require('mysql');
let url = require("url"); // 格式化url
let port = 8080;
let db_config = {
host: 'localhost',
user: 'root',
port: '3306',
password: '123456789',
database: 'mini'
};
class MySQL {
constructor() {
return mysql.createConnection(db_config);
}
static getInstance() {
if (!MySQL.conn) {
MySQL.conn = new MySQL();
MySQL.conn.connect();
}
return MySQL.conn;
}
static query(sql) {
let conn = MySQL.getInstance();
return new Promise(function (resolve, reject) {
conn.query(sql, function (error, results, fields) {
if (error) reject(error);
resolve(results);
});
});
}
}
class Data {
loadByData(data) {
for (let key in this.columns) {
this[key] = data[this.columns[key]];
}
return this;
}
async find(nodeLevel, key, name) {
let condition = "";
let where = "";
var number = 0;
if (nodeLevel) {
number++;
condition += `nod_level ='${nodeLevel}'`;
}
if(name == 1 || name == 2 || name == 3){
if (number != 0) {
condition += ` AND`;
}
number++;
if(name == 2){
condition += ` tpc_pparea ='${key}'`;
}else if(name == 3){
condition += ` tpc_area='${key}'`;
}else{
condition += ` tpc_ppparea='${key}'`;
}
}else if (key) {
if (number != 0) {
condition += ` AND`;
}
number++;
condition += ` ${name} ='${key}'`;
}
if (number > 0) {
where = 'where';
}
let sql = `SELECT * FROM ${this.table} ${where} ${condition} LIMIT 100`;
let res = await MySQL.query(sql);
let objs = [];
for (let i in res) {
let obj = new this.constructor();
obj.loadByData(res[i]);
objs.push(obj);
}
return objs;
}
}
class Tree extends Data {
async parent() {
if (this[this.pkey] <= 0) return null;
let parent = new this.constructor();
parent[this.key] = this[this.pkey];
return parent.load();
}
async parents() {
let parent = await this.parent();
if (!parent) return [this];
let ps = await parent.parents();
ps.push(this);
return ps;
}
async children(nodeLevel,key,name) {
let obj = new this.constructor();
return obj.find(nodeLevel,key,name);
}
// async children(nodeLevel, nod_pid, node_id, content_id, content_cid, tpc_id, reply_id, user_id, rpl_usr_id, area_id, area_level, level_id, level) {
// let obj = new this.constructor();
// return obj.find(nodeLevel, nod_pid, node_id, content_id, content_cid, tpc_id, reply_id, user_id, rpl_usr_id, area_id, area_level, level_id, level);
// }
}
class Category extends Tree {
constructor() {
super();
this.key = "id";
this.pkey = "pid";
this.table = "babel_node";
this.columns = {
"nid": "nid",
"node_id": "node_id",
"nod_pid": "nod_pid",
"nod_level": "nod_level",
"nod_name": "nod_name",
"nod_title": "nod_title",
};
}
}
class Topic extends Tree {
constructor() {
super();
this.key = "id";
this.pkey = "pid";
this.table = "babel_topic";
this.columns = {
"tpc_id": "tpc_id",
"tpc_pid": "tpc_pid",
"tpc_ppid": "tpc_ppid",
"tpc_pname": "tpc_pname",
"tpc_title": "tpc_title",
"tpc_content": "tpc_content",
"tpc_description": "tpc_description",
"tpc_area": "tpc_area",
"tpc_pparea": "tpc_pparea",
"tpc_ppparea": "tpc_ppparea",
};
}
}
class Reply extends Tree {
constructor() {
super();
this.key = "id";
this.pkey = "pid";
this.table = "babel_reply";
this.columns = {
"rpl_tpc_id": "rpl_tpc_id",
"rpl_usr_id": "rpl_usr_id",
"rpl_post_nick": "rpl_post_nick",
"rpl_content": "rpl_content",
"rpl_post_usr_id": "rpl_post_usr_id",
};
}
}
class User extends Tree {
constructor() {
super();
this.key = "id";
this.pkey = "pid";
this.table = "babel_user";
this.columns = {
"usr_id": "usr_id",
"usr_nick": "usr_nick",
"usr_email": "usr_email",
"usr_full": "usr_full",
};
}
}
class Area extends Tree {
constructor() {
super();
this.key = "id";
this.pkey = "pid";
this.table = "babel_area";
this.columns = {
"area_id": "area_id",
"area_level": "area_level",
"area_pid": "area_pid",
"area_title": "area_title",
};
}
}
app.get('/', async function (req, res) {
html = '<h1>首页</h1>';
cate = new Category();
cate.nod_level = 100;
let children = await cate.children(100);
for (let i in children) {
html += `<hr><a href="/list?id=${children[i].node_id}">${children[i].nod_title}</a>`;
let subChildren = await children[i].children(101, children[i].node_id,'nod_pid');
for (let j in subChildren) {
html += `<li><a href="/content?id=${children[i].node_id}&&cid=${subChildren[j].node_id}">${subChildren[j].nod_title}</a></li>`;
}
}
res.send(html);
});
app.get('/list', async function (req, res) {
html = '<h1>列表</h1><a href="/">返回首页</a><br/>';
cate = new Category();
let resData = url.parse(req.url, true).query;
if (!resData.id) {
res.send('传参失败');
}
let children = await cate.children(100, resData.id,'node_id');
for (let i in children) {
html += `<hr><a href="/list?id=${children[i].node_id}">${children[i].nod_title}</a>`;
let subChildren = await children[i].children(101, children[i].node_id,'nod_pid');
for (let j in subChildren) {
html += `<li><a href="/content?id=${resData.id}&&cid=${subChildren[j].node_id}">${subChildren[j].nod_title}</a></li>`;
}
}
res.send(html);
});
app.get('/content', async function (req, res) {
html = '<h1>子列表</h1><a href="/">返回首页</a><br/>';
cate = new Topic();
let resData = url.parse(req.url, true).query;
if (!resData.id || !resData.cid) {
res.send('传参失败');
}
let children = await cate.children(null, resData.cid,'tpc_pid');
for (let i in children) {
html += `<hr><a href="/ad?id=${children[i].tpc_id}">${children[i].tpc_pname}</a>`;
}
res.send(html);
});
app.get('/ad', async function (req, res) {
html = '<h1>广告查看页面</h1><a href="/">返回首页</a><br/>';
cate = new Topic();
let resData = url.parse(req.url, true).query;
if (!resData.id) {
res.send('传参失败');
}
let children = await cate.children(null, resData.id,'tpc_id');
cate_are = new Area();
if (children[0].tpc_area !== 21) {
var tpc_area = await cate_are.children(null,children[0].tpc_area, 'area_id');
}
if (children[0].tpc_pparea !== 21) {
var tpc_pparea = await cate_are.children(null,children[0].tpc_pparea,'area_id');
}
html += `<a href="/area?ppparea=21">上海</a> `;
if (children[0].tpc_pparea !== 21 && tpc_pparea) {
html += `>> <a href="/area?pparea=${tpc_pparea[0].area_id}">${tpc_pparea[0].area_title}</a> `;
}
if (children[0].tpc_area !== 21) {
if (tpc_area[0]) {
var tpcId = tpc_area[0].area_id;
var tpcTitle = tpc_area[0].area_title;
html += `>> <a href="/area?pparea=${tpc_pparea[0].area_id}&&area=${tpcId}">${tpcTitle}</a>`;
}
}
for (let i in children) {
html += `<hr>标题:${children[i].tpc_title}`;
html += `<hr>内容:${children[i].tpc_content}`;
html += `<hr>描述信息:${children[i].tpc_description}`;
}
cate = new Reply();
let childrenReply = await cate.children(null, resData.id,'rpl_tpc_id');
for (let i in childrenReply) {
html += `<hr><a href="/user?id=${childrenReply[i].rpl_post_usr_id}">${childrenReply[i].rpl_post_nick}</a>:${childrenReply[i].rpl_content}`;
}
res.send(html);
});
app.get('/user', async function (req, res) {
html = '<h1>用户页面</h1><a href="/">返回首页</a>';
cate = new User();
let resData = url.parse(req.url, true).query;
if (!resData.id) {
res.send('传参失败');
}
let childrenUser = await cate.children(null,resData.id,'usr_id');
for (let i in childrenUser) {
html += `<hr>id:${childrenUser[i].usr_id}`;
html += `<hr>昵称:${childrenUser[i].usr_nick}`;
html += `<hr>邮箱:${childrenUser[i].usr_email}`;
html += `<hr>真实姓名:${childrenUser[i].usr_full}`;
}
cate = new Topic();
let childrenAd = await cate.children(null, resData.id,'tpc_uid');
for (let i in childrenAd) {
html += `<br/><a href="/ad?id=${childrenAd[i].tpc_id}">${childrenAd[i].tpc_title}</a>`;
}
res.send(html);
});
app.get('/area', async function (req, res) {
html = '<h1>地域页面</h1><a href="/">返回首页</a><br/>';
cate_are = new Area();
let resData = url.parse(req.url, true).query;
if (resData.area !== 21) {
var tpc_area = await cate_are.children(null, resData.area, 'area_id');
}
if (resData.pparea !== 21) {
var tpc_pparea = await cate_are.children(null,resData.pparea, 'area_id');
}
html += `<a href="/area?ppparea=21">上海</a> `;
if (resData.pparea !== 21 && resData.pparea) {
html += `>> <a href="/area?pparea=${tpc_pparea[0].area_id}">${tpc_pparea[0].area_title}</a> `;
}
if (resData.area !== 21 && resData.area) {
html += `>> <a href="/area?pparea=${tpc_pparea[0].area_id}&&area=${tpc_area[0].area_id}">${tpc_area[0].area_title}</a>`;
}
cate = new Topic();
let resDatas = url.parse(req.url, true).query;
var sss = resDatas.ppparea;
var ss = resDatas.pparea;
var s = resDatas.area;
var level = 0;
var level_id = '';
if (s) {
level = 3;
level_id = s;
} else if (ss) {
level = 2;
level_id = ss;
} else {
level = 1;
level_id = sss;
}
let children = await cate.children(null, level_id, level);
for (let i in children) {
html += `<hr><a href="/ad?id=${children[i].tpc_id}">${children[i].tpc_pname}</a>`;
}
res.send(html);
});
app.listen(port, () => console.log(`http://localhost:${port}/`))