这两个月:
这两个月闲的,写个了论坛,PS:只实现了基本功能,还有各种bug,写来玩玩,并没有在意那些细节(弃坑)。这次主要来说说最近的情况。
涉猎:http权威指南 再次看mongodb权威指南 1/2(加深印象)
论坛:
收获:基本的前端页面,对自适应有初步的认识。mongo+koa2实现的基本论坛功能。采用vue.js前端渲染,知道了一些渲染逻辑和做法。
数据库设计
var messageSchema = new Schema({
type: { type: String },
master_id: { type: ObjectId ,ref:'User'},//自身
author_id: { type: ObjectId ,ref:'User'},//被回复人ID
topic_id: { type: ObjectId ,ref:'Topic'},
reply: { type: ObjectId ,ref: 'User'},//回复主题Id
reply_id: { type: ObjectId ,ref:'User'},
has_read: { type: Boolean, default: false },
create_at: { type: Date, default: Date.now }
});
const productSchema = new Schema({
name: {type: String},
series: {type: ObjectId, ref :'Series'}, //系列
grade: {type: Number}, //评分
product: {type: String, default:'earphone'},//产品类型
type: {type: String},//标识类型
depict: {type :String, default: ''}, //描述
num: {type: String, default: ''}, //数量
price: {type :String, default: ''}, //价格
create_at: { type: Date, default: Date.now },
is_sale: {type:String, default:false},
author_id: {type: ObjectId, ref:'User'}
})
let replySchema = new Schema({
content: { type: String },
topic_id: { type: ObjectId ,ref: 'Topic'}, //话题Id
toreply_id:{ type: ObjectId ,ref: 'Reply'},//被回复topicId
author_id: { type: ObjectId ,ref: 'User'}, //被回复Id作者
reply_id: { type: ObjectId ,ref: 'User'},//回复主题Id
create_at: { type: Date, default: Date.now },
update_at: { type: Date, default: Date.now },
ups:[ObjectId],
ups_count:{type:Number,default:0},
deleted: {type: Boolean, default: false} //删除需要保留概要
})
let seriesSchema = new Schema({
brand: {type: String},
product: [{type:ObjectId ,ref :'Product'}],
iden: {type: String}, //标识码
author_id: [{type: ObjectId ,ref :'User'}],
depict: {type: String},
create_at: { type: Date, default: Date.now }
})
let topicSchema = new Schema({
title: { type: String },
content: { type: String },
author_id: { type: ObjectId ,ref :'User'},
top: { type: Boolean, default: false }, // 置顶帖
good: {type: Boolean, default: false}, // 精华帖
lock: {type: Boolean, default: false}, // 被锁定主题
reply_count: { type: Number, default: 0 },
visit_count: { type: Number, default: 1 },
collect_count: { type: Number, default: 0 },
create_at: { type: Date, default: Date.now },
update_at: { type: Date, default: Date.now },
last_reply: { type: ObjectId ,ref :'User'},
last_reply_at: { type: Date, default: Date.now },
tab: {type: String},
imge:{type:String,default: Date.now}, //缩略图
deleted: {type: Boolean, default: false}
});
let userSchema = new Schema({
name: { type: String},
loginName: { type: String},
pass: { type: String },
email: { type: String},
city: {type: String},
url: { type: String },
score: {type: Number ,default: 0},
depict: {type:String},
avatar: {type: String},
seal:[], //印章
update_at: { type: Date, default: Date.now },
medal:[
{type: String}
]//勋章
});
注意的问题:
分mongo分页可参考:http://blog.sina.com.cn/s/blog_56545fd30101442b.html
点赞:查询reply ups[],返回ups[],前端判断存在ups,处于点亮状态。添加使用addToSet。若考虑分页问题,也可以返回所有被点亮评论,因为一个人不会点主题下多少回复的。
连表查询:连表查询其实使用shell是进行2次查询,要尽量一次性尽量返回数据。 mongoose使用populate()。用空间换取时间和性能,可以将一些关联数据在集合中多次出现。schema定义时避免使用驼峰命名法,不要出现大写字母(来自狗哥:这是规定,好处是方便记忆书写和维护),可以看到上面的schema又有问题+1。
在设置session时可以直接设置sid,代替loginName。可以看到上面的user集合设计的就有问题。
已读和未读(虽然没做):匹配SID#SID,读取session,若前SID(回复主题)后者SID(回复别人)为当前user的标识,更改信息未读状态。
权限管理(没整):简单的就是设置管理员,复杂的就要增加权限集合,根据需求会比较复杂,自己有点想法,网上貌似没人用mongo撸这种….
其实最大的收获还是学会了布局,和vue.js前端渲染(PS:pug和vue混用,但是前端渲染蜘蛛是很难抓取到的),前端会了点。
完成效果:
主页(注册页什么的也写了,不要在意那些细节。。。。)

信息管理

@和主题回复等提示

发表话题

话题页面和评论页面
评论页面可以点赞和继续@消息

后记:
话说这个东西各种bug,就比如session过期页面跳转功能都没写,最近学习态度有点问题,需要跳坑,于是就草草结束。花在前端的时间太多,而且貌似也是浪费时间。跳坑~!还有koa2 async/await真的比较酸爽~
代码停靠: https://github.com/fangker/cube/ 。
