From 8f3c1774bf67020bf9a767a8a2fd870ed411e3e0 Mon Sep 17 00:00:00 2001 From: Fritz Lin Date: Sat, 7 Nov 2015 23:52:38 +0800 Subject: [PATCH] =?UTF-8?q?#=E5=9C=A8=E7=BA=BF=E4=BA=91=E7=B2=98=E8=B4=B4#?= =?UTF-8?q?=20channel=20=3D>=20paste?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 17 +++------ bower.json | 2 +- config-default/index.js | 3 +- package.json | 4 +-- src/server/api.js | 74 +++++++++++++++++++++------------------- src/server/view.js | 53 +++++++++++++++------------- src/web/channel-view.hbs | 17 +++++---- src/web/channel-view.js | 10 +++--- 8 files changed, 94 insertions(+), 86 deletions(-) diff --git a/README.md b/README.md index d7e86aa..7de6b70 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,8 @@ -轻应用:#在线云频道# +轻应用:#在线云粘贴# === -- 复制于:[#微信话题墙#](https://github.com/fritx/wxtopic) -- 在线演示: -- 相关博文:[跟我一起来升级#在线云频道#(1)](http://fritx.github.io/blog/?2015/05/channel-dev-together-01/) - - - - - - +- 精简于:[#在线云频道#](https://github.com/fritx/channel) +- 在线演示: ## 项目亮点 @@ -38,8 +31,8 @@ ``` $ npm install -g gulp $ npm install -g bower -$ git clone git@github.com:fritx/channel.git -$ cd channel +$ git clone git@github.com:fritx/paste.git +$ cd paste $ npm install $ bower install $ cp -r config-default config diff --git a/bower.json b/bower.json index 557bbb3..58a877e 100644 --- a/bower.json +++ b/bower.json @@ -1,5 +1,5 @@ { - "name": "channel", + "name": "paste", "private": true, "dependencies": { "jquery": "~2.1.3", diff --git a/config-default/index.js b/config-default/index.js index f7d7173..2e264cf 100644 --- a/config-default/index.js +++ b/config-default/index.js @@ -8,8 +8,9 @@ module.exports = { //有通过微信认证的微信公众号 则填入 //wxAppId: 'wxXXXXXXXXXXXXXXXX', //wxAppSecret: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', + listLimit: 50, urlPrefix: '', dbFile: dbFile, statsHtml: fs.readFileSync(statsFile).toString().trim(), - port: 9118 + port: 9119 } diff --git a/package.json b/package.json index aade7a7..7f2f8a0 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { - "name": "channel", + "name": "paste", "version": "0.0.2", - "description": "在线云频道", + "description": "在线云粘贴", "private": true, "main": "src/server", "scripts": { diff --git a/src/server/api.js b/src/server/api.js index cbe73f6..a995bb4 100644 --- a/src/server/api.js +++ b/src/server/api.js @@ -2,7 +2,7 @@ var uuid = require('node-uuid').v4 var bodyParser = require('body-parser') var multer = require('multer') var db = require('./db') -var dbChannels = db.dbChannels +// var dbChannels = db.dbChannels var dbComments = db.dbComments var tss = require('./lib/tss') var fs = require('fs-extra') @@ -15,15 +15,16 @@ module.exports = function (app) { app.use('/api', bodyParser.urlencoded({ extended: false })) app.use('/api', multer({ limits: bytes('4mb') })) - app.post('/api/channels/:key/comments', function (req, res, next) { - var channel = dbChannels.find({ - key: req.params.key - }) - if (!channel || channel._del) { - return res.status(404).send({ - error: 'channel not found with key: ' + req.params.key - }) - } + // app.post('/api/channels/:key/comments', function (req, res, next) { + app.post('/api/comments', function (req, res, next) { + // var channel = dbChannels.find({ + // key: req.params.key + // }) + // if (!channel || channel._del) { + // return res.status(404).send({ + // error: 'channel not found with key: ' + req.params.key + // }) + // } var text = req.body['text'] || '' text = text.trim().slice(0, 2000) var media = req.files['media'] @@ -54,14 +55,15 @@ module.exports = function (app) { }) } } - var followed = dbComments.chain().filter({ - channel_id: channel.id - }).last().value() + // var followed = dbComments.chain().filter({ + // channel_id: channel.id + // }).last().value() + var followed = dbComments.chain().last().value() var nextFloor = followed ? followed.floor + 1 : 1 var comment = { id: getNextId(dbComments), floor: nextFloor, - channel_id: channel.id, + // channel_id: channel.id, ip: req.ip, timestamp: tss() } @@ -77,28 +79,28 @@ module.exports = function (app) { res.send({ floor: comment.floor }) }) - app.post('/api/channels', function (req, res) { - var title = req.body['title'] || '' - title = title.trim().slice(0, 100) - if (title.length < 1) { - return res.status(400).send({ - error: 'empty channel title' - }) - } - var channel = dbChannels.find({ title: title }) - if (!channel) { - channel = { - id: getNextId(dbChannels), - key: uuid(), - title: title, - ip: req.ip, - timestamp: tss() - } - dbChannels.push(channel) - db.save() - } - res.send({ key: channel.key }) - }) + // app.post('/api/channels', function (req, res) { + // var title = req.body['title'] || '' + // title = title.trim().slice(0, 100) + // if (title.length < 1) { + // return res.status(400).send({ + // error: 'empty channel title' + // }) + // } + // var channel = dbChannels.find({ title: title }) + // if (!channel) { + // channel = { + // id: getNextId(dbChannels), + // key: uuid(), + // title: title, + // ip: req.ip, + // timestamp: tss() + // } + // dbChannels.push(channel) + // db.save() + // } + // res.send({ key: channel.key }) + // }) } diff --git a/src/server/view.js b/src/server/view.js index e77ec0d..0eb4f49 100644 --- a/src/server/view.js +++ b/src/server/view.js @@ -6,10 +6,12 @@ var _ = require('lodash') var config = require('../../config') var wx = require('./wx') var db = require('./db') -var dbChannels = db.dbChannels +// var dbChannels = db.dbChannels var dbComments = db.dbComments var hbs = exphbs.create() var Handlebars = hbs.handlebars +var listLimit = config.listLimit +var floorBits = Math.ceil(Math.log(listLimit+1) / Math.log(10)) module.exports = function (app) { //app.engine('jade', jade.__express) @@ -17,10 +19,10 @@ module.exports = function (app) { app.set('view engine', 'hbs') app.set('views', path.resolve(__dirname, '../web')) - app.get('/', addPathSlash, function (req, res) { - //res.redirect(prefixUrl('/channels')) - res.redirect('open') - }) + // app.get('/', addPathSlash, function (req, res) { + // //res.redirect(prefixUrl('/channels')) + // res.redirect('open') + // }) //app.get('/channels', function (req, res) { // res.send('Top Channels') //}) @@ -30,29 +32,34 @@ module.exports = function (app) { next() }) - app.get('/open', dropPathSlash, function (req, res, next) { - res.__tmpl = 'channel-open' - next() - }) + // app.get('/open', dropPathSlash, function (req, res, next) { + // res.__tmpl = 'channel-open' + // next() + // }) - app.get('/channels/:key', dropPathSlash, function (req, res, next) { - var channel = dbChannels.find({ - key: req.params.key - }) - if (!channel) { - return res.redirect('../open') - } - var comments = dbComments.filter({ - channel_id: channel.id - }).reverse() + // app.get('/channels/:key', dropPathSlash, function (req, res, next) { + app.get('/', addPathSlash, function (req, res, next) { + // var channel = dbChannels.find({ + // key: req.params.key + // }) + // if (!channel) { + // return res.redirect('../open') + // } + // var comments = dbComments.filter({ + // channel_id: channel.id + // }).reverse() + var comments = dbComments.value().slice(-listLimit).reverse() res.__tmpl = 'channel-view' res.__data = { comments: comments, - channel: channel, - channel_json: JSON.stringify( - _.pick(channel, ['key', 'title']) - ), + // channel: channel, + // channel_json: JSON.stringify( + // _.pick(channel, ['key', 'title']) + // ), helpers: { + flr: function (floor) { + return floor.toString().slice(-floorBits) + }, format: function (text) { var html = Handlebars.Utils.escapeExpression(text) html = html.replace(/[\r\n]+/g, '
') diff --git a/src/web/channel-view.hbs b/src/web/channel-view.hbs index 38f4ff8..7b90d5d 100644 --- a/src/web/channel-view.hbs +++ b/src/web/channel-view.hbs @@ -3,14 +3,16 @@ - ##{{channel.title}}## + + #在线云粘贴#
-

##{{channel.title}}##

+ +

#在线云粘贴#

@@ -25,7 +27,7 @@ {{#each comments}} {{#if _del}}{{else}}
  • -
    +
    @@ -56,7 +58,7 @@ @@ -108,8 +110,9 @@ }) wx.ready(function() { var messageData = { - title: '#在线云频道#', - desc: '我正在频道 ##' + channel.title + '##', + title: '#在线云粘贴#', + // desc: '我正在频道 ##' + channel.title + '##', + desc: '我正在频道 #在线云粘贴#', link: location.href, imgUrl: location.origin + urlPrefix + '/static/logo.jpg', trigger: function (res) { diff --git a/src/web/channel-view.js b/src/web/channel-view.js index 8eeb7a8..afe6270 100644 --- a/src/web/channel-view.js +++ b/src/web/channel-view.js @@ -16,7 +16,8 @@ $(function initPage() { }) var myComment = dbMyComments - .findLast({ channel_key: channel.key }).value() + // .findLast({ channel_key: channel.key }).value() + .last().value() if (myComment) { //document.title = '我参与了 ##' + channel.title + '##' } @@ -26,7 +27,8 @@ $(function initPage() { if (submitted) return alert('稍安勿躁') var form = $form_comment.serializeJSON() if (!form['text'] && !$input_media.val()) return - var url = 'api/channels/' + channel.key + '/comments' + // var url = 'api/channels/' + channel.key + '/comments' + var url = 'api/comments' submitted = true if ($input_media.val()) { $input_media.ajaxfileupload({ @@ -53,8 +55,8 @@ $(function initPage() { return alert('发送失败,为毛?') } dbMyComments.push({ - floor: d.floor, - channel_key: channel.key + // channel_key: channel.key, + floor: d.floor }) dbMyComments.save() $form_comment[0].reset()
  • AltStyle によって変換されたページ (->オリジナル) /