mongodb 用户地址模型
mongodb • lopo1983 发表了文章 • 0 个评论 • 144 次浏览 • 2020-09-28 14:20
const mongoose = app.mongoose;
const Schema = mongoose.Schema;
const { logger, helper } = app.createAnonymousContext();
const {
objToQuery
} = helper;
const conn = app.mongooseDB.get('mp');
const AccountUserAddressSchema = new Schema({
// 默认地址
default_id: {
type: Schema.Types.ObjectId,
default: null
},
// 用户id
_uid: {
type: Schema.Types.ObjectId
},
// 标签
tags: {
type: Array,
default: ['家', '公司', '学校']
},
// 地址列表
address: [{
// 收货人姓名
userName: {
type: String,
required: true
},
// 邮编
postalCode: {
type: String
},
// 省市
provinceName: {
type: String,
required: true
},
// 市
cityName: {
type: String,
required: true
},
// 区/县
countyName: {
type: String,
required: true
},
// 详细地址
detailInfo: {
type: String,
required: true
},
// 联系电话
telNumber: {
type: String,
required: true
},
// 状态 [[0,'删除'],[1,'正常']]
status: {
type: Number,
default: 1
},
// 标签
tag: String,
// 添加时间
addTime: {
type: Date,
default: new Date()
},
deleteTime: Date
}]
});
AccountUserAddressSchema.statics = {
// 添加
addOne: async function(_uid, BODYS) {
const { is_default } = BODYS;
const MODEL = this.findOneAndUpdate({
_uid
}, {
$push: {
'address': {...BODYS }
},
// ...isDefault && { default_id: _id }
}, {
new: true,
fields: 'cars default_id -_id'
});
try {
const RBD = await MODEL;
return !!RBD
} catch (error) {
console.log(error)
}
},
// 更新地址
updateOne: async function(_uid, _id, BODYS) {
// const { isDefault } = BODYS;
// delete BODYS.isDefault;
const MODEL = this.findOneAndUpdate({
_uid,
'address._id': _id
}, {
$set: {...objToQuery(BODYS, 'address') }
}, {
fields: 'address default_id -_id'
});
try {
// 修改数据
const DATAS = await MODEL;
// 修改默认
// !!isDefault && await this.setDefault(_uid, isDefault, _id);
return !!DATAS.address.length ? BODYS : { code: 211, message: '更新失败' }
} catch (error) {
return error
}
},
// 列表
list: async function(_uid) {
const MODEL = this.aggregate([{
$match: {
_uid: mongoose.Types.ObjectId(_uid)
}
}, {
$unwind: '$address'
}, {
$match: {
'address.deleteTime': { $exists: false }
}
}, {
$addFields: {
'address.isDefault': {
$eq: ['$default_id', '$address._id']
}
}
}, {
$replaceRoot: { newRoot: "$address" }
}]);
// const MODEL = this.find({ _uid })
try {
const RBD = await MODEL;
return RBD.length ? RBD : await this.initAddress(_uid)
} catch (error) {
console.log(error)
}
},
// 删除一个
delOne: async function(_uid, _id) {
const MODEL = this.findOneAndUpdate({
_uid,
'address._id': _id
}, {
'address.$.status': 0,
'address.$.deleteTime': new Date(),
});
try {
const RBD = await MODEL;
return { _id }
} catch (error) {
}
},
// 初始化用地址
initAddress: async function(_uid) {
const MODEL = this.create({ _uid: mongoose.Types.ObjectId(_uid) });
try {
const DATAS = await MODEL;
return DATAS
} catch (error) {
console.log(error)
}
},
// 获取单一数据
getOne: async function(_uid, _id) {
const MODEL = this.aggregate([{
$match: { _uid: mongoose.Types.ObjectId(_uid) }
}, {
$unwind: '$address'
}, {
$project: {
address: 1,
_id: 1,
default_id: 1,
}
}, {
$addFields: {
'address.isDefault': {
$eq: [{ $toString: '$default_id' }, _id]
}
}
}, {
$match: {
'address.deleteTime': { $exists: false },
'address._id': mongoose.Types.ObjectId(_id),
}
}, {
$replaceRoot: { newRoot: "$address" }
}]);
try {
if (_id !== "default") {
const DATAS = await MODEL;
return DATAS.length ? DATAS[0] : {}
}
} catch (error) {
console.log(error)
return error
}
},
// 获取默认地址
getDefault: async function(_uid) {
const MODEL = this.findOne({ _uid }, {}, {
fields: 'default_id -_id'
});
try {
const { default_id } = await MODEL;
return await this.getOne(_uid, default_id)
} catch (error) {
console.log(error);
return error
}
},
// 設置默認
setDefault: async function(_uid, default_id) {
// const default_id = !!isDefaults ? _id : '';
const MODEL = this.findOneAndUpdate({ _uid }, { default_id });
try {
const MB = await MODEL;
return { default_id }
} catch (error) {
console.log(error);
return error
}
},
};
return conn.model('account_mp_user_address', AccountUserAddressSchema)
}/**
* @name mongoDB update数组参数
*
*/
objToQuery(OBJ, KEY) {
return Object.keys(OBJ).reduce((a, b) => {
a[`${KEY}.$.${b}`] = OBJ[b];
return a
}, {});
}, 查看全部
module.exports = app => {
const mongoose = app.mongoose;
const Schema = mongoose.Schema;
const { logger, helper } = app.createAnonymousContext();
const {
objToQuery
} = helper;
const conn = app.mongooseDB.get('mp');
const AccountUserAddressSchema = new Schema({
// 默认地址
default_id: {
type: Schema.Types.ObjectId,
default: null
},
// 用户id
_uid: {
type: Schema.Types.ObjectId
},
// 标签
tags: {
type: Array,
default: ['家', '公司', '学校']
},
// 地址列表
address: [{
// 收货人姓名
userName: {
type: String,
required: true
},
// 邮编
postalCode: {
type: String
},
// 省市
provinceName: {
type: String,
required: true
},
// 市
cityName: {
type: String,
required: true
},
// 区/县
countyName: {
type: String,
required: true
},
// 详细地址
detailInfo: {
type: String,
required: true
},
// 联系电话
telNumber: {
type: String,
required: true
},
// 状态 [[0,'删除'],[1,'正常']]
status: {
type: Number,
default: 1
},
// 标签
tag: String,
// 添加时间
addTime: {
type: Date,
default: new Date()
},
deleteTime: Date
}]
});
AccountUserAddressSchema.statics = {
// 添加
addOne: async function(_uid, BODYS) {
const { is_default } = BODYS;
const MODEL = this.findOneAndUpdate({
_uid
}, {
$push: {
'address': {...BODYS }
},
// ...isDefault && { default_id: _id }
}, {
new: true,
fields: 'cars default_id -_id'
});
try {
const RBD = await MODEL;
return !!RBD
} catch (error) {
console.log(error)
}
},
// 更新地址
updateOne: async function(_uid, _id, BODYS) {
// const { isDefault } = BODYS;
// delete BODYS.isDefault;
const MODEL = this.findOneAndUpdate({
_uid,
'address._id': _id
}, {
$set: {...objToQuery(BODYS, 'address') }
}, {
fields: 'address default_id -_id'
});
try {
// 修改数据
const DATAS = await MODEL;
// 修改默认
// !!isDefault && await this.setDefault(_uid, isDefault, _id);
return !!DATAS.address.length ? BODYS : { code: 211, message: '更新失败' }
} catch (error) {
return error
}
},
// 列表
list: async function(_uid) {
const MODEL = this.aggregate([{
$match: {
_uid: mongoose.Types.ObjectId(_uid)
}
}, {
$unwind: '$address'
}, {
$match: {
'address.deleteTime': { $exists: false }
}
}, {
$addFields: {
'address.isDefault': {
$eq: ['$default_id', '$address._id']
}
}
}, {
$replaceRoot: { newRoot: "$address" }
}]);
// const MODEL = this.find({ _uid })
try {
const RBD = await MODEL;
return RBD.length ? RBD : await this.initAddress(_uid)
} catch (error) {
console.log(error)
}
},
// 删除一个
delOne: async function(_uid, _id) {
const MODEL = this.findOneAndUpdate({
_uid,
'address._id': _id
}, {
'address.$.status': 0,
'address.$.deleteTime': new Date(),
});
try {
const RBD = await MODEL;
return { _id }
} catch (error) {
}
},
// 初始化用地址
initAddress: async function(_uid) {
const MODEL = this.create({ _uid: mongoose.Types.ObjectId(_uid) });
try {
const DATAS = await MODEL;
return DATAS
} catch (error) {
console.log(error)
}
},
// 获取单一数据
getOne: async function(_uid, _id) {
const MODEL = this.aggregate([{
$match: { _uid: mongoose.Types.ObjectId(_uid) }
}, {
$unwind: '$address'
}, {
$project: {
address: 1,
_id: 1,
default_id: 1,
}
}, {
$addFields: {
'address.isDefault': {
$eq: [{ $toString: '$default_id' }, _id]
}
}
}, {
$match: {
'address.deleteTime': { $exists: false },
'address._id': mongoose.Types.ObjectId(_id),
}
}, {
$replaceRoot: { newRoot: "$address" }
}]);
try {
if (_id !== "default") {
const DATAS = await MODEL;
return DATAS.length ? DATAS[0] : {}
}
} catch (error) {
console.log(error)
return error
}
},
// 获取默认地址
getDefault: async function(_uid) {
const MODEL = this.findOne({ _uid }, {}, {
fields: 'default_id -_id'
});
try {
const { default_id } = await MODEL;
return await this.getOne(_uid, default_id)
} catch (error) {
console.log(error);
return error
}
},
// 設置默認
setDefault: async function(_uid, default_id) {
// const default_id = !!isDefaults ? _id : '';
const MODEL = this.findOneAndUpdate({ _uid }, { default_id });
try {
const MB = await MODEL;
return { default_id }
} catch (error) {
console.log(error);
return error
}
},
};
return conn.model('account_mp_user_address', AccountUserAddressSchema)
}
/**
* @name mongoDB update数组参数
*
*/
objToQuery(OBJ, KEY) {
return Object.keys(OBJ).reduce((a, b) => {
a[`${KEY}.$.${b}`] = OBJ[b];
return a
}, {});
},
mongodb 分页封装(传统分页)【2020-10-30 修正】
mongodb • lopo1983 发表了文章 • 0 个评论 • 178 次浏览 • 2020-09-22 11:47
return [{
$facet: {
'list': [
...MATCH,
{
$skip: (page - 1) * limit
},
{
$limit: limit * 1
},
...OTHERMATCH.length ? OTHERMATCH : []
],
'page': [
...MATCH,
{
$count: 'count'
}
],
...OTHERFACET
}
}, {
$project: {
list: {
$cond: [{ $eq: [{ $size: '$list' }, 0] },
[], '$list'
]
},
page: {
$cond: [{ $eq: [{ $size: '$page' }, 0] },
[{ count: 0 }], '$page'
]
},
count: 1,
...FACET_KEY
}
}, {
$unwind: '$page'
}, {
$addFields: {
'page.limit': limit * 1,
'page.page': page * 1
}
}]
};
module.exports = pageFn 查看全部
const pageFn = ({ MATCH = [], OTHERMATCH = [], OTHERFACET = {}, FACET_KEY = {}, limit = 20, page = 1 } = {}) => {
return [{
$facet: {
'list': [
...MATCH,
{
$skip: (page - 1) * limit
},
{
$limit: limit * 1
},
...OTHERMATCH.length ? OTHERMATCH : []
],
'page': [
...MATCH,
{
$count: 'count'
}
],
...OTHERFACET
}
}, {
$project: {
list: {
$cond: [{ $eq: [{ $size: '$list' }, 0] },
[], '$list'
]
},
page: {
$cond: [{ $eq: [{ $size: '$page' }, 0] },
[{ count: 0 }], '$page'
]
},
count: 1,
...FACET_KEY
}
}, {
$unwind: '$page'
}, {
$addFields: {
'page.limit': limit * 1,
'page.page': page * 1
}
}]
};
module.exports = pageFn
linux 根据端口查进程id
每天进步一点点 • lopo1983 发表了文章 • 0 个评论 • 205 次浏览 • 2020-07-19 22:04
kill [port]下面是windows的netstat -ano |findstr [port]
taskkill /f /t /im "进程id或者进程名称" 查看全部
netstat -tpln | grep [port]下面是windows的
kill [port]
netstat -ano |findstr [port]
taskkill /f /t /im "进程id或者进程名称"
egg 自动同步关系型数据库model
Nodejs • lopo1983 发表了文章 • 0 个评论 • 261 次浏览 • 2020-05-07 12:39
module.exports = app => {
app.beforeStart(async () => {
// 从配置中心获取 MySQL 的配置
// { host: 'mysql.com', port: '3306', user: 'test_user', password: 'test_password', database: 'test' }
await app.model.sync({ force: true });
});
}; 查看全部
// {app_root}/app.js
module.exports = app => {
app.beforeStart(async () => {
// 从配置中心获取 MySQL 的配置
// { host: 'mysql.com', port: '3306', user: 'test_user', password: 'test_password', database: 'test' }
await app.model.sync({ force: true });
});
};
centos7.x 防火墙常用操作
每天进步一点点 • lopo1983 发表了文章 • 0 个评论 • 328 次浏览 • 2020-03-20 10:43
2、查看防火墙所有开放的端口firewall-cmd --zone=public --list-ports
3.、关闭防火墙
如果要开放的端口太多,嫌麻烦,可以关闭防火墙,安全性自行评估systemctl stop firewalld.service
4、查看防火墙状态 firewall-cmd --state
5、查看监听的端口netstat -lnpt
PS:centos7默认没有 netstat 命令,需要安装 net-tools 工具,yum install -y net-tools
6、检查端口被哪个进程占用netstat -lnpt |grep 5672
7、查看进程的详细信息
ps 6832
8、中止进程
kill -9 6832 查看全部
firewall-cmd --zone=public --add-port=5672/tcp --permanent# 开放5672端口
firewall-cmd --zone=public --remove-port=5672/tcp --permanent#关闭5672端口
firewall-cmd --reload# 配置立即生效
2、查看防火墙所有开放的端口
firewall-cmd --zone=public --list-ports
3.、关闭防火墙
如果要开放的端口太多,嫌麻烦,可以关闭防火墙,安全性自行评估
systemctl stop firewalld.service
4、查看防火墙状态
firewall-cmd --state
5、查看监听的端口
netstat -lnpt
PS:centos7默认没有 netstat 命令,需要安装 net-tools 工具,yum install -y net-tools
6、检查端口被哪个进程占用
netstat -lnpt |grep 5672
7、查看进程的详细信息
ps 6832
8、中止进程
kill -9 6832
AMQP
每天进步一点点 • lopo1983 发表了文章 • 0 个评论 • 303 次浏览 • 2020-03-15 23:12
从 AMQP 协议可以看出,Queue、Exchange 和 Binding 构成了 AMQP 协议的核心
Producer:消息生产者,即投递消息的程序Broker:消息队列服务器实体。
Exchange:消息交换机,它指定消息按什么规则,路由到哪个队列。Binding:绑定,它的作用就是把 Exchange 和 Queue 按照路由规则绑定起来。Queue:消息队列载体,每个消息都会被投入到一个或多个队列。
Consumer:消息消费者,即接受消息的程序。 查看全部
centos 安装最新版 redis
每天进步一点点 • lopo1983 发表了文章 • 0 个评论 • 298 次浏览 • 2020-03-14 23:53
yum --enablerepo=remi install redis设置开机启动启动
systemctl enable redis.service修改配置
vim /etc/redis.conf
# 设置可外网访问 DEV 模式用 上线慎用
bind 0.0.0.0
# 设置访问密码
requirepass ***********
:wq
/bin/systemctl restart redis.service
记住默认只有16个 chanceDb 查看全部
yum install -y http://rpms.famillecollet.com/ ... 7.rpm安装
yum --enablerepo=remi install redis设置开机启动启动
systemctl enable redis.service修改配置
vim /etc/redis.conf
# 设置可外网访问 DEV 模式用 上线慎用
bind 0.0.0.0
# 设置访问密码
requirepass ***********
:wq
/bin/systemctl restart redis.service
记住默认只有16个 chanceDb
妈妈再也不用担心我烧CPU 和内存了 Puppeteer 并发
每天进步一点点 • lopo1983 发表了文章 • 0 个评论 • 698 次浏览 • 2020-03-11 22:29
let WSE_LIST = ; //存储browserWSEndpoint列表
init();
app.get('/', function (req, res) {
let tmp = Math.floor(Math.random()* MAX_WSE);
(async () => {
let browserWSEndpoint = WSE_LIST[tmp];
const browser = await puppeteer.connect({browserWSEndpoint});
const page = await browser.newPage();
await page.goto('file://code/screen/index.html');
await page.setViewport({
width: 600,
height: 400
});
await page.screenshot({path: 'example.png'});
await page.close();
res.send('Hello World!');
})();
});
function init(){
(async () => {
for(var i=0;i<MAX_WSE;i++){
const browser = await puppeteer.launch({headless:true,
args: [
'--disable-gpu',
'--disable-dev-shm-usage',
'--disable-setuid-sandbox',
'--no-first-run',
'--no-sandbox',
'--no-zygote',
'--single-process'
]});
browserWSEndpoint = await browser.wsEndpoint();
WSE_LIST[i] = browserWSEndpoint;
}
console.log(WSE_LIST);
})();
}[/i]啰嗦几句
开启几个浏览器 随机在浏览器上打开tab 关闭headless 看更明显 使用express 是为了更方便看到结果 查看全部
const MAX_WSE = 4; //启动几个浏览器啰嗦几句
let WSE_LIST = ; //存储browserWSEndpoint列表
init();
app.get('/', function (req, res) {
let tmp = Math.floor(Math.random()* MAX_WSE);
(async () => {
let browserWSEndpoint = WSE_LIST[tmp];
const browser = await puppeteer.connect({browserWSEndpoint});
const page = await browser.newPage();
await page.goto('file://code/screen/index.html');
await page.setViewport({
width: 600,
height: 400
});
await page.screenshot({path: 'example.png'});
await page.close();
res.send('Hello World!');
})();
});
function init(){
(async () => {
for(var i=0;i<MAX_WSE;i++){
const browser = await puppeteer.launch({headless:true,
args: [
'--disable-gpu',
'--disable-dev-shm-usage',
'--disable-setuid-sandbox',
'--no-first-run',
'--no-sandbox',
'--no-zygote',
'--single-process'
]});
browserWSEndpoint = await browser.wsEndpoint();
WSE_LIST[i] = browserWSEndpoint;
}
console.log(WSE_LIST);
})();
}[/i]
开启几个浏览器 随机在浏览器上打开tab 关闭headless 看更明显 使用express 是为了更方便看到结果
centos 无法启动 Puppeteer 解决办法
Nodejs • lopo1983 发表了文章 • 0 个评论 • 425 次浏览 • 2020-03-10 22:46
(node:21019) UnhandledPromiseRejectionWarning: Error: Failed to launch the browser process!
/www/pup/node_modules/puppeteer/.local-chromium/linux-722234/chrome-linux/chrome: error while loading shared libraries: libX11.so.6: cannot open shared object file: No such file or directory解决办法yum install ipa-gothic-fonts xorg-x11-fonts-100dpi xorg-x11-fonts-75dpi xorg-x11-utils xorg-x11-fonts-cyrillic xorg-x11-fonts-Type1 xorg-x11-fonts-misc -y
yum install pango.x86_64 libXcomposite.x86_64 libXcursor.x86_64 libXdamage.x86_64 libXext.x86_64 libXi.x86_64 libXtst.x86_64 cups-libs.x86_64 libXScrnSaver.x86_64 libXrandr.x86_64 GConf2.x86_64 alsa-lib.x86_64 atk.x86_64 gtk3.x86_64 -y
# 再安装NSS的依赖:
yum install nss.x86_64
错误2
(node:25375) UnhandledPromiseRejectionWarning: Error: Failed to launch the browser process!
[0310/223602.052200:ERROR:zygote_host_impl_linux.cc(89)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
解决办法# puppeteer的执行文件中去沙箱运行:
browser = await puppeteer.launch({
headless: true,
args: ['--no-sandbox', '--disable-setuid-sandbox']
}); 查看全部
无法启动 错误
(node:21019) UnhandledPromiseRejectionWarning: Error: Failed to launch the browser process!解决办法
/www/pup/node_modules/puppeteer/.local-chromium/linux-722234/chrome-linux/chrome: error while loading shared libraries: libX11.so.6: cannot open shared object file: No such file or directory
yum install ipa-gothic-fonts xorg-x11-fonts-100dpi xorg-x11-fonts-75dpi xorg-x11-utils xorg-x11-fonts-cyrillic xorg-x11-fonts-Type1 xorg-x11-fonts-misc -y
yum install pango.x86_64 libXcomposite.x86_64 libXcursor.x86_64 libXdamage.x86_64 libXext.x86_64 libXi.x86_64 libXtst.x86_64 cups-libs.x86_64 libXScrnSaver.x86_64 libXrandr.x86_64 GConf2.x86_64 alsa-lib.x86_64 atk.x86_64 gtk3.x86_64 -y
# 再安装NSS的依赖:
yum install nss.x86_64
错误2
(node:25375) UnhandledPromiseRejectionWarning: Error: Failed to launch the browser process!
[0310/223602.052200:ERROR:zygote_host_impl_linux.cc(89)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
解决办法
# puppeteer的执行文件中去沙箱运行:
browser = await puppeteer.launch({
headless: true,
args: ['--no-sandbox', '--disable-setuid-sandbox']
});
vm 退出保存文件(新建)
每天进步一点点 • lopo1983 发表了文章 • 0 个评论 • 330 次浏览 • 2020-03-06 13:31
:wq
:filename:file
:wq