Skip to content

Commit

Permalink
2017/11/06更新
Browse files Browse the repository at this point in the history
1. 完成个人中心页--我的历程
2. 数据库结构变动很多,回去先清下数据库
  • Loading branch information
DiKang Li committed Nov 6, 2017
1 parent 7bd8d56 commit 20ec9c6
Showing 15 changed files with 442 additions and 113 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -64,5 +64,5 @@ callback到promise https://github.com/node-modules/qn/issues/82
累计上传多少张图片
累计注册了多少用户

登录页面按enter可登录
home页的时间轴http://www.jq22.com/yanshi164
<dd>登录页面按enter可登录</dd>
home页的时间轴http://www.jq22.com/yanshi164
72 changes: 36 additions & 36 deletions api/file.js
Original file line number Diff line number Diff line change
@@ -15,11 +15,11 @@ const client = qn.create({
origin: 'http://oyh0gj8ht.bkt.clouddn.com',
});

export default function(router) {
export default function (router) {
router.post('/api/uploader', convert(body({
uploadDir: path.join(__dirname, '/../uploads'),
keepExtensions: true
})), async(ctx, next) => {
})), async (ctx, next) => {
// 判断user中的setting是否勾选自动压缩
if (ctx.state.user) {
// 是否启用图片压缩
@@ -61,37 +61,37 @@ export default function(router) {
}
if (uploadType) {
return new Promise((resolve, reject) => {
client.uploadFile(compressUrl, { key: (new Date()).getTime() + uploadType }, function(err, result) {
client.uploadFile(compressUrl, { key: (new Date()).getTime() + uploadType }, function (err, result) {
if (err) {
ctx.throw(500, '图片上传失败')
return reject(err)
} else {
console.log('七牛上传成功!')
// save history
(async function() {
let history = new History({
filename: result.key,
old_filesize: fileName,
filesize: result['x:mtime'],
userid: await History.transId(ctx.state.user._id), // 所属人
tmp_url: compressUrl, // 腾讯云临时地址
remote_url: result.url, // 七牛永久地址
time: new Date()
})
let isOk = await History.add(history, true)
if (isOk == 1) {
ctx.body = {
ok: true,
msg: '图片上传成功',
data: result
// save history
(async function () {
let history = new History({
filename: result.key,
old_filename: fileName,
filesize: parseFloat(result['x:size']),
userid: await History.transId(ctx.state.user._id), // 所属人
tmp_url: compressUrl, // 腾讯云临时地址
remote_url: result.url, // 七牛永久地址
time: new Date()
})
let isOk = await History.add(history, true)
if (isOk == 1) {
ctx.body = {
ok: true,
msg: '图片上传成功',
data: result
}
resolve()
} else if (isOk == 0) {
ctx.throw(500, '更新History之后更新User.history失败')
} else if (isOk == -1) {
ctx.throw(500, '更新History失败')
}
resolve()
} else if (isOk == 0) {
ctx.throw(500, '更新History之后更新User.history失败')
} else if (isOk == -1) {
ctx.throw(500, '更新History失败')
}
})()
})()
}
})
})
@@ -124,17 +124,17 @@ export default function(router) {
}
if (uploadType) {
return new Promise((resolve, reject) => {
client.uploadFile(ctx.request.files[i].path, { key: (new Date()).getTime() + uploadType }, function(err, result) {
client.uploadFile(ctx.request.files[i].path, { key: (new Date()).getTime() + uploadType }, function (err, result) {
if (err) {
ctx.throw(500, '图片上传失败')
return reject(err)
} else {
// save history
(async function() {
(async function () {
let history = new History({
filename: result.key,
old_filesize: fileName,
filesize: result['x:mtime'],
old_filename: fileName,
filesize: parseFloat(result['x:size']),
userid: await History.transId(ctx.state.user._id), // 所属人
tmp_url: ctx.request.files[i].path, // 腾讯云临时地址
remote_url: result.url, // 七牛永久地址
@@ -187,17 +187,17 @@ export default function(router) {
}
if (uploadType) {
return new Promise((resolve, reject) => {
client.uploadFile(ctx.request.files[i].path, { key: (new Date()).getTime() + uploadType }, function(err, result) {
client.uploadFile(ctx.request.files[i].path, { key: (new Date()).getTime() + uploadType }, function (err, result) {
if (err) {
ctx.throw(500, '图片上传失败')
return reject(err)
} else {
// save history
(async function() {
(async function () {
let history = new History({
filename: result.key,
old_filesize: fileName,
filesize: result['x:mtime'],
old_filename: fileName,
filesize: parseFloat(result['x:size']),
userid: null, // 所属人
tmp_url: ctx.request.files[i].path, // 腾讯云临时地址
remote_url: result.url, // 七牛永久地址
@@ -224,4 +224,4 @@ export default function(router) {
}
}
})
}
}
3 changes: 2 additions & 1 deletion api/user.js
Original file line number Diff line number Diff line change
@@ -18,7 +18,8 @@ export default function (router) {
email: email,
password: password,
history: [],
settings: []
settings: [],
registe_time: new Date()
})
ctx.body = await User.add(ctx, user)
}
58 changes: 53 additions & 5 deletions models/history.js
Original file line number Diff line number Diff line change
@@ -3,15 +3,15 @@ const mongoose = require('mongoose')

const historySchema = new mongoose.Schema({
filename: String,
old_filesize: String,
filesize: String,
old_filename: String,
filesize: Number,
userid: mongoose.Schema.ObjectId, // 所属人
tmp_url: String, // 腾讯云临时地址
remote_url: String, // 七牛永久地址
time: Date
})

historySchema.statics.add = async function(history, needUpdateUser) {
historySchema.statics.add = async function (history, needUpdateUser) {
let a = await history.save()
if (a) {
if (needUpdateUser) {
@@ -30,10 +30,58 @@ historySchema.statics.add = async function(history, needUpdateUser) {
}
}

historySchema.statics.transId = async function(id) {
historySchema.statics.transId = async function (id) {
return mongoose.Types.ObjectId(id)
}

historySchema.statics.dayLoad = async function (userid) {
let a = await this.aggregate([
{
$match: {
"userid": mongoose.Types.ObjectId(userid)
}
},
{
$project: {
day: {
$substr: [{ "$add": ["$time", 28800000] }, 0, 10]
},
"old_filename": 1,
"remote_url": 1
}
},
{
$group: {
_id: "$day",
"upload_arr": { $push: { name: "$old_filename", link: "$remote_url" } }
}
},
{
$sort: {
"_id": -1
}
}
])

// 整理文档
let allYear = []
let result = []
a.forEach(item => {
let year = item._id.substring(0, 4)
let index = allYear.indexOf(year)
if (index > -1) {
result[index].items.push(item)
} else {
allYear.push(year)
result.push({
year: year,
items: [item]
})
}
})
return result
}

let History = mongoose.model('History', historySchema)

export { History }
export { History }
3 changes: 2 additions & 1 deletion models/user.js
Original file line number Diff line number Diff line change
@@ -7,7 +7,8 @@ const userSchema = new mongoose.Schema({
email: String,
password: String,
history: Array,
settings: Object
settings: Object,
registe_time: Date
})

userSchema.statics.add = async function (ctx, user) {
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -29,6 +29,7 @@
"koa-session2": "^2.2.5",
"koa-static": "^3.0.0",
"koa-views": "^5.2.1",
"moment": "^2.19.1",
"mongoose": "^4.12.4",
"pug": "^2.0.0-rc.1",
"qn": "^1.3.0",
26 changes: 17 additions & 9 deletions public/scripts/login.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
$(document).ready(function() {
$(document).ready(function () {
// 点击注册
$('#doLogin').bind('click', function() {
$('#doLogin').bind('click', function () {
var nameOrEmail = $('#inputEmail').val()
var password = $('#inputPassword').val()
if (nameOrEmail) {
@@ -16,18 +16,19 @@ $(document).ready(function() {
},
dataType: 'json',
timeout: 10000,
success: function(res) {
success: function (res) {
$('#doLogin').removeClass('loading').removeAttr('disabled')
if (res.ok) {
showAlert('success', '登陆成功!即将前往首页')
setTimeout(function() {
window.location.href = '/'
}, 1000)
showAlert('success', '登陆成功!')
var backUrl = getParam('backUrl')
setTimeout(function () {
window.location.href = backUrl || '/'
}, 800)
} else {
$('.err-tips').html(res.msg)
}
},
error: function(err) {
error: function (err) {
$('.err-tips').html('登陆失败')
$('#doLogin').removeClass('loading').removeAttr('disabled')
}
@@ -39,4 +40,11 @@ $(document).ready(function() {
$('.err-tips').html('请输入用户名或者密码')
}
})
})

$(document).keypress(function (e) {
// 回车键事件
if (e.which == 13) {
jQuery("#doLogin").click()
}
})
})
27 changes: 27 additions & 0 deletions public/scripts/personal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
$(document).ready(function () {
var $timeline_block = $('.content section')
//hide timeline blocks which are outside the viewport
$timeline_block.each(function () {
if ($(this).offset().top > $(window).scrollTop() + $(window).height() * 0.75) {
$(this).addClass('is-hidden')
}
})
//on scolling, show/animate timeline blocks when enter the viewport
$(window).on('scroll', function () {
$timeline_block.each(function () {
if ($(this).offset().top <= $(window).scrollTop() + $(window).height() * 0.75 && $(this).hasClass('is-hidden')) {
$(this).removeClass('is-hidden').addClass('bounce-in')
}
})
})

$('.content article > h3').on('click', function () {
if ($(this).hasClass('hide-more')) {
$(this).removeClass('hide-more')
$(this).parent().find('section').fadeIn()
} else {
$(this).addClass('hide-more')
$(this).parent().find('section').fadeOut()
}
})
})
Loading

0 comments on commit 20ec9c6

Please sign in to comment.