删除具体js实例
This commit is contained in:
parent
f37b7a693d
commit
5f886955af
|
@ -1,7 +1,7 @@
|
||||||
node_modules
|
node_modules
|
||||||
/target/
|
/target/
|
||||||
|
|
||||||
.env*
|
# .env*
|
||||||
!.env.template
|
!.env.template
|
||||||
|
|
||||||
upload/*
|
upload/*
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -4,6 +4,7 @@
|
||||||
"description": "init a project with me",
|
"description": "init a project with me",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
"start": "pm2 start ./src/index.js",
|
||||||
"dev": "nodemon ./src/index.js",
|
"dev": "nodemon ./src/index.js",
|
||||||
"test": "node ./test/redis.test.js"
|
"test": "node ./test/redis.test.js"
|
||||||
},
|
},
|
||||||
|
@ -21,6 +22,7 @@
|
||||||
"mariadb": "^3.0.0",
|
"mariadb": "^3.0.0",
|
||||||
"mongoose": "^6.3.2",
|
"mongoose": "^6.3.2",
|
||||||
"mysql2": "^2.3.3",
|
"mysql2": "^2.3.3",
|
||||||
|
"pm2": "^5.2.0",
|
||||||
"redis": "^4.1.0",
|
"redis": "^4.1.0",
|
||||||
"redis-connection-pool": "^2.0.2",
|
"redis-connection-pool": "^2.0.2",
|
||||||
"sequelize": "^6.19.0"
|
"sequelize": "^6.19.0"
|
||||||
|
|
|
@ -6,8 +6,8 @@ const KoaStatic = require("koa-static");
|
||||||
const parameter = require("koa-parameter");
|
const parameter = require("koa-parameter");
|
||||||
|
|
||||||
require("../config/env.config"); // 加载配置
|
require("../config/env.config"); // 加载配置
|
||||||
const router = require("../router"); // 加载路由
|
const router = require("../route"); // 加载路由
|
||||||
const errHandler = require("./err.handler"); // 加载自定义异常
|
const errHandler = require("./error.handler"); // 加载自定义异常
|
||||||
|
|
||||||
const app = new Koa();
|
const app = new Koa();
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,87 @@
|
||||||
|
module.exports = {
|
||||||
|
userFormateError: {
|
||||||
|
code: '10001',
|
||||||
|
message: '用户名或密码为空',
|
||||||
|
result: '',
|
||||||
|
},
|
||||||
|
userAlreadyExited: {
|
||||||
|
code: '10002',
|
||||||
|
message: '用户已经存在',
|
||||||
|
result: '',
|
||||||
|
},
|
||||||
|
userRegisterError: {
|
||||||
|
code: '10003',
|
||||||
|
message: '用户注册错误',
|
||||||
|
result: '',
|
||||||
|
},
|
||||||
|
userDoesNotExist: {
|
||||||
|
code: '10004',
|
||||||
|
message: '用户不存在',
|
||||||
|
result: '',
|
||||||
|
},
|
||||||
|
userLoginError: {
|
||||||
|
code: '10005',
|
||||||
|
message: '用户登录失败',
|
||||||
|
result: '',
|
||||||
|
},
|
||||||
|
invalidPassword: {
|
||||||
|
code: '10006',
|
||||||
|
message: '密码不匹配',
|
||||||
|
result: '',
|
||||||
|
},
|
||||||
|
tokenExpiredError: {
|
||||||
|
code: '10101',
|
||||||
|
message: 'token已过期',
|
||||||
|
result: '',
|
||||||
|
},
|
||||||
|
invalidToken: {
|
||||||
|
code: '10102',
|
||||||
|
message: '无效的token',
|
||||||
|
result: '',
|
||||||
|
},
|
||||||
|
hasNotAdminPermission: {
|
||||||
|
code: '10103',
|
||||||
|
message: '没有管理员权限',
|
||||||
|
result: '',
|
||||||
|
},
|
||||||
|
fileUploadError: {
|
||||||
|
code: '10201',
|
||||||
|
message: '商品图片上传失败',
|
||||||
|
result: '',
|
||||||
|
},
|
||||||
|
unSupportedFileType: {
|
||||||
|
code: '10202',
|
||||||
|
message: '不支持的文件格式',
|
||||||
|
result: '',
|
||||||
|
},
|
||||||
|
goodsFormatError: {
|
||||||
|
code: '10203',
|
||||||
|
message: '商品参数格式错误',
|
||||||
|
result: '',
|
||||||
|
},
|
||||||
|
publishGoodsError: {
|
||||||
|
code: '10204',
|
||||||
|
message: '发布商品失败',
|
||||||
|
result: '',
|
||||||
|
},
|
||||||
|
invalidGoodsID: {
|
||||||
|
code: '10205',
|
||||||
|
message: '无效的商品id',
|
||||||
|
result: '',
|
||||||
|
},
|
||||||
|
cartFormatError: {
|
||||||
|
code: '10301',
|
||||||
|
message: '购物车数据格式错误',
|
||||||
|
result: '',
|
||||||
|
},
|
||||||
|
addrFormatError: {
|
||||||
|
code: '10401',
|
||||||
|
message: '地址数据格式错误',
|
||||||
|
result: '',
|
||||||
|
},
|
||||||
|
orderFormatError: {
|
||||||
|
code: '10501',
|
||||||
|
message: '订单数据格式错误',
|
||||||
|
result: '',
|
||||||
|
},
|
||||||
|
}
|
|
@ -1,3 +1,5 @@
|
||||||
|
const {d} = require('../model/example')
|
||||||
|
|
||||||
class UserController {
|
class UserController {
|
||||||
async register(ctx, next) {
|
async register(ctx, next) {
|
||||||
ctx.json({ code: 0, message: "success" });
|
ctx.json({ code: 0, message: "success" });
|
|
@ -1,9 +0,0 @@
|
||||||
const mongoose = require("mongoose");
|
|
||||||
|
|
||||||
const { MONGO_HOST, MONGO_PORT, MONGO_DBNAME, MONGO_USER, MONGO_PASSWD } =
|
|
||||||
process.env;
|
|
||||||
const URI = `mongodb://${MONGO_USER}:${MONGO_PASSWD}@${MONGO_HOST}:${MONGO_PORT}/${MONGO_DBNAME}`;
|
|
||||||
|
|
||||||
mongoose.connect(URI);
|
|
||||||
|
|
||||||
module.exports = { mongoose };
|
|
|
@ -1,49 +0,0 @@
|
||||||
const redis = require("redis");
|
|
||||||
// require("../config/env.config");
|
|
||||||
|
|
||||||
const { REDIS_HOST, REDIS_PORT, REDIS_DBNAME, REDIS_USER, REDIS_PASSWD } =
|
|
||||||
process.env; // REDIS_USER 和 REDIS_PASSWD,需要服务器配置ACL用户名密码
|
|
||||||
const URL = `redis://${REDIS_USER}:${REDIS_PASSWD}@${REDIS_HOST}:${REDIS_PORT}/${REDIS_DBNAME}`;
|
|
||||||
|
|
||||||
// https://github.com/redis/node-redis/tree/master/docs
|
|
||||||
// module.exports = redis.createClient({URL});
|
|
||||||
const client = redis.createClient({
|
|
||||||
// url: URL,
|
|
||||||
socket: { host: REDIS_HOST, port: REDIS_PORT },
|
|
||||||
password: REDIS_PASSWD,
|
|
||||||
// database: REDIS_DBNAME,
|
|
||||||
// legacyMode: true, // 语法向后(v3)部分兼容
|
|
||||||
});
|
|
||||||
|
|
||||||
client.on("error", (err) => console.log(`redis client ~ Error ${err}`));
|
|
||||||
client.on("connect", () => console.log("redis client ~ connect"));
|
|
||||||
client.on("reconnecting", () => console.log("redis client ~ reconnecting"));
|
|
||||||
client.on("ready", () => console.log("redis client ~ ready"));
|
|
||||||
client.on("end", () => console.log("redis client ~ end"));
|
|
||||||
|
|
||||||
client.connect();
|
|
||||||
client.ping();
|
|
||||||
// try {
|
|
||||||
// } catch (error) {
|
|
||||||
// console.log(error);
|
|
||||||
// }
|
|
||||||
|
|
||||||
module.exports = client;
|
|
||||||
|
|
||||||
//* 方式二:redis 连接池
|
|
||||||
//! unusable
|
|
||||||
/* var redisPool = require("redis-connection-pool")("myRedisPool", {
|
|
||||||
host: REDIS_HOST, // default
|
|
||||||
port: REDIS_PORT, //default
|
|
||||||
max_clients: 30, // defalut
|
|
||||||
perform_checks: false, // checks for needed push/pop functionality
|
|
||||||
database: 0, // database number to use
|
|
||||||
options: {
|
|
||||||
auth_pass: REDIS_PASSWD,
|
|
||||||
}, //options for createClient of node-redis, optional
|
|
||||||
});
|
|
||||||
redisPool.set("test-key", "foobar", function (err) {
|
|
||||||
redisPool.get("test-key", function (err, reply) {
|
|
||||||
console.log(reply); // 'foobar'
|
|
||||||
});
|
|
||||||
}); */
|
|
|
@ -1,26 +0,0 @@
|
||||||
const { Sequelize } = require("sequelize");
|
|
||||||
|
|
||||||
const {
|
|
||||||
MARIA_DIALECT,
|
|
||||||
MARIA_HOST,
|
|
||||||
MARIA_PORT,
|
|
||||||
MARIA_DBNAME,
|
|
||||||
MARIA_USRNAME,
|
|
||||||
MARIA_PASSWD,
|
|
||||||
} = process.env;
|
|
||||||
|
|
||||||
const seq = new Sequelize(MARIA_DBNAME, MARIA_USRNAME, MARIA_PASSWD, {
|
|
||||||
host: MARIA_HOST,
|
|
||||||
port: MARIA_PORT,
|
|
||||||
dialect: 'mariadb',
|
|
||||||
});
|
|
||||||
|
|
||||||
seq.authenticate()
|
|
||||||
.then(() => {
|
|
||||||
console.log('数据库连接成功')
|
|
||||||
})
|
|
||||||
.catch(err => {
|
|
||||||
console.log('数据库连接失败', err)
|
|
||||||
})
|
|
||||||
|
|
||||||
module.exports = seq;
|
|
|
@ -1,27 +0,0 @@
|
||||||
const { Sequelize } = require("sequelize");
|
|
||||||
|
|
||||||
const {
|
|
||||||
MYSQL_DIALECT,
|
|
||||||
MYSQL_HOST,
|
|
||||||
MYSQL_PORT,
|
|
||||||
MYSQL_DBNAME,
|
|
||||||
MYSQL_USRNAME,
|
|
||||||
MYSQL_PASSWD,
|
|
||||||
} = process.env;
|
|
||||||
|
|
||||||
const seq = new Sequelize(MYSQL_DBNAME, MYSQL_USRNAME, MYSQL_PASSWD, {
|
|
||||||
host: MYSQL_HOST,
|
|
||||||
port: MYSQL_PORT,
|
|
||||||
dialect: MYSQL_DIALECT,
|
|
||||||
});
|
|
||||||
|
|
||||||
// seq
|
|
||||||
// .authenticate()
|
|
||||||
// .then(() => {
|
|
||||||
// console.log('数据库连接成功')
|
|
||||||
// })
|
|
||||||
// .catch(err => {
|
|
||||||
// console.log('数据库连接失败', err)
|
|
||||||
// })
|
|
||||||
|
|
||||||
module.exports = seq;
|
|
|
@ -1,4 +1,4 @@
|
||||||
import app from "./app/index.js";
|
const app = require("./app/index.js");
|
||||||
|
|
||||||
const { APP_PORT } = process.env;
|
const { APP_PORT } = process.env;
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,7 @@ const {
|
||||||
tokenExpiredError,
|
tokenExpiredError,
|
||||||
invalidToken,
|
invalidToken,
|
||||||
hasNotAdminPermission,
|
hasNotAdminPermission,
|
||||||
} = require('../constant/err.type')
|
} = require('../constant/error.type')
|
||||||
|
|
||||||
const auth = async (ctx, next) => {
|
const auth = async (ctx, next) => {
|
||||||
const { authorization = '' } = ctx.request.header
|
const { authorization = '' } = ctx.request.header
|
||||||
|
@ -31,6 +31,7 @@ const auth = async (ctx, next) => {
|
||||||
await next()
|
await next()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 判断:是否有管理员权限
|
||||||
const hadAdminPermission = async (ctx, next) => {
|
const hadAdminPermission = async (ctx, next) => {
|
||||||
const { is_admin } = ctx.state.user
|
const { is_admin } = ctx.state.user
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,7 @@ const {
|
||||||
userDoesNotExist,
|
userDoesNotExist,
|
||||||
userLoginError,
|
userLoginError,
|
||||||
invalidPassword,
|
invalidPassword,
|
||||||
} = require('../constant/err.type')
|
} = require('../constant/error.type')
|
||||||
|
|
||||||
const userValidator = async (ctx, next) => {
|
const userValidator = async (ctx, next) => {
|
||||||
const { user_name, password } = ctx.request.body
|
const { user_name, password } = ctx.request.body
|
||||||
|
|
|
@ -1,30 +0,0 @@
|
||||||
const { DataTypes } = require('sequelize')
|
|
||||||
|
|
||||||
const seq = require('../../db/seq.maria')
|
|
||||||
|
|
||||||
// 创建模型(Model zd_user -> 表 zd_users)
|
|
||||||
const User = seq.define('zd_user', {
|
|
||||||
// id 会被sequelize自动创建, 管理
|
|
||||||
user_name: {
|
|
||||||
type: DataTypes.STRING,
|
|
||||||
allowNull: false,
|
|
||||||
unique: true,
|
|
||||||
comment: '用户名, 唯一',
|
|
||||||
},
|
|
||||||
password: {
|
|
||||||
type: DataTypes.CHAR(64),
|
|
||||||
allowNull: false,
|
|
||||||
comment: '密码',
|
|
||||||
},
|
|
||||||
is_admin: {
|
|
||||||
type: DataTypes.BOOLEAN,
|
|
||||||
allowNull: false,
|
|
||||||
defaultValue: 0,
|
|
||||||
comment: '是否为管理员, 0: 不是管理员(默认); 1: 是管理员',
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
// 强制同步数据库(创建数据表)
|
|
||||||
// User.sync({ force: true })
|
|
||||||
|
|
||||||
module.exports = User
|
|
|
@ -1,24 +0,0 @@
|
||||||
const { mongoose } = require("../../db/mgs.mongo");
|
|
||||||
|
|
||||||
const BookSchema = mongoose.Schema({
|
|
||||||
name: {
|
|
||||||
type: String,
|
|
||||||
require: true,
|
|
||||||
},
|
|
||||||
author: {
|
|
||||||
type: String,
|
|
||||||
},
|
|
||||||
wordCount: {
|
|
||||||
type: Number,
|
|
||||||
},
|
|
||||||
publishTime: {
|
|
||||||
type: Date,
|
|
||||||
default: Date.now,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const Book = mongoose.model("Book", BookSchema);
|
|
||||||
|
|
||||||
// let book = new Book({});
|
|
||||||
|
|
||||||
module.exports = { Book };
|
|
|
@ -1,42 +0,0 @@
|
||||||
const User = require('../../model/maria/use.model')
|
|
||||||
|
|
||||||
class UserService {
|
|
||||||
async createUser(user_name, password) {
|
|
||||||
// 插入数据
|
|
||||||
// await表达式: promise对象的值
|
|
||||||
const res = await User.create({ user_name, password })
|
|
||||||
// console.log(res)
|
|
||||||
return res.dataValues
|
|
||||||
}
|
|
||||||
|
|
||||||
async getUerInfo({ id, user_name, password, is_admin }) {
|
|
||||||
const whereOpt = {}
|
|
||||||
|
|
||||||
id && Object.assign(whereOpt, { id })
|
|
||||||
user_name && Object.assign(whereOpt, { user_name })
|
|
||||||
password && Object.assign(whereOpt, { password })
|
|
||||||
is_admin && Object.assign(whereOpt, { is_admin })
|
|
||||||
|
|
||||||
const res = await User.findOne({
|
|
||||||
attributes: ['id', 'user_name', 'password', 'is_admin'],
|
|
||||||
where: whereOpt,
|
|
||||||
})
|
|
||||||
|
|
||||||
return res ? res.dataValues : null
|
|
||||||
}
|
|
||||||
|
|
||||||
async updateById({ id, user_name, password, is_admin }) {
|
|
||||||
const whereOpt = { id }
|
|
||||||
const newUser = {}
|
|
||||||
|
|
||||||
user_name && Object.assign(newUser, { user_name })
|
|
||||||
password && Object.assign(newUser, { password })
|
|
||||||
is_admin && Object.assign(newUser, { is_admin })
|
|
||||||
|
|
||||||
const res = await User.update(newUser, { where: whereOpt })
|
|
||||||
// console.log(res)
|
|
||||||
return res[0] > 0 ? true : false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = new UserService()
|
|
|
@ -1,5 +0,0 @@
|
||||||
const { Book } = require("../../model/mongo/book.model");
|
|
||||||
|
|
||||||
let book = new Book({});
|
|
||||||
|
|
||||||
module.exports = { book };
|
|
|
@ -1,25 +0,0 @@
|
||||||
const client = require("../../db/redis");
|
|
||||||
|
|
||||||
function save2Redis(key, value) {
|
|
||||||
client.set(key, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
function getByKey(key, callback = null) {
|
|
||||||
if (callback) {
|
|
||||||
client.get(key, callback);
|
|
||||||
} else {
|
|
||||||
client.get(key, (err, value) => {
|
|
||||||
if (err) return null;
|
|
||||||
console.log(value)
|
|
||||||
// return value;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
Object.prototype.toString = function () {
|
|
||||||
return JSON.stringify(this);
|
|
||||||
};
|
|
||||||
*/
|
|
||||||
|
|
||||||
module.exports = { client, save2Redis, getByKey };
|
|
|
@ -1,2 +0,0 @@
|
||||||
const { book } = require("../src/service/mongo/book.service");
|
|
||||||
|
|
Loading…
Reference in New Issue