├── views ├── 404.jade ├── index.jade ├── error.jade └── layout.jade ├── public └── stylesheets │ └── style.css ├── routes ├── index.js ├── users.js ├── util.js ├── config.js └── philosophers.js ├── config └── sequelize.js ├── middlewares └── check.js ├── package.json ├── models ├── code.js ├── user.js ├── comment.js ├── work.js ├── philosopher.js ├── data.js ├── idea.js ├── paper.js └── index.js ├── README.md ├── LICENSE ├── .gitignore ├── app.js └── bin └── www /views/404.jade: -------------------------------------------------------------------------------- 1 | extends layout 2 | 3 | block content 4 | h1= 404 -------------------------------------------------------------------------------- /views/index.jade: -------------------------------------------------------------------------------- 1 | extends layout 2 | 3 | block content 4 | h1= title 5 | p Welcome to #{title} 6 | -------------------------------------------------------------------------------- /views/error.jade: -------------------------------------------------------------------------------- 1 | extends layout 2 | 3 | block content 4 | h1= message 5 | h2= error.status 6 | pre #{error.stack} 7 | -------------------------------------------------------------------------------- /public/stylesheets/style.css: -------------------------------------------------------------------------------- 1 | body { 2 | padding: 50px; 3 | font: 14px "Lucida Grande", Helvetica, Arial, sans-serif; 4 | } 5 | 6 | a { 7 | color: #00B7FF; 8 | } 9 | -------------------------------------------------------------------------------- /views/layout.jade: -------------------------------------------------------------------------------- 1 | doctype html 2 | html 3 | head 4 | title= title 5 | link(rel='stylesheet', href='/stylesheets/style.css') 6 | body 7 | block content 8 | -------------------------------------------------------------------------------- /routes/index.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var router = express.Router(); 3 | 4 | /* GET home page. */ 5 | router.get('/', function (req, res, next) { 6 | res.render('index', {title: 'Express'}); 7 | }); 8 | 9 | module.exports = router; 10 | -------------------------------------------------------------------------------- /config/sequelize.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by airing on 2017/3/3. 3 | */ 4 | var Sequelize = require('sequelize'); 5 | var SQL_PASSWORD = require('../routes/config').SQL_PASSWORD; 6 | 7 | exports.sequelize = function () { 8 | return new Sequelize('philosopher', 'root', SQL_PASSWORD, {'dialect': 'mysql', host: 'localhost', port: 3306}); 9 | } 10 | -------------------------------------------------------------------------------- /middlewares/check.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by airing on 2017/3/3. 3 | */ 4 | module.exports = { 5 | checkLogin: function checkLogin(req, res, next) { 6 | if (!req.session.user) { 7 | req.flash('error', '未登录'); 8 | return res.redirect('/signin'); 9 | } 10 | next(); 11 | }, 12 | 13 | checkNotLogin: function checkNotLogin(req, res, next) { 14 | if (req.session.user) { 15 | req.flash('error', '已登录'); 16 | return res.redirect('back');//返回之前的页面 17 | } 18 | next(); 19 | } 20 | }; 21 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "philosopher-back-end", 3 | "version": "1.0.0", 4 | "private": true, 5 | "scripts": { 6 | "start": "node ./bin/www" 7 | }, 8 | "dependencies": { 9 | "body-parser": "~1.16.0", 10 | "cookie-parser": "~1.4.3", 11 | "debug": "~2.6.0", 12 | "express": "~4.14.1", 13 | "jade": "~1.11.0", 14 | "jpush-sdk": "^3.4.1", 15 | "md5": "^2.2.1", 16 | "morgan": "~1.7.0", 17 | "mysql": "^2.13.0", 18 | "qiniu": "^6.1.13", 19 | "sequelize": "^3.30.2", 20 | "serve-favicon": "~2.3.2", 21 | "sha1": "^1.1.1" 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /models/code.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by airing on 2017/4/25. 3 | */ 4 | module.exports = function (sequelize, DataTypes) { 5 | return sequelize.define( 6 | 'code', 7 | { 8 | 'user_account': { 9 | 'type': DataTypes.STRING(20), 10 | 'allowNull': false 11 | }, 12 | 'timestamp': { 13 | 'type': DataTypes.DOUBLE, 14 | 'allowNull': false 15 | }, 16 | 'used': { 17 | 'type': DataTypes.BOOLEAN, 18 | 'allowNull': false 19 | }, 20 | 'code': { 21 | 'type': DataTypes.STRING(20), 22 | 'allowNull': false 23 | } 24 | } 25 | ); 26 | }; -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Sophia:哲学知识共享社区 2 | 3 | > 线上地址:[https://sopher.group](https://sopher.group) 4 | 5 | ![](http://airing.ursb.me/image/cover/sophia-shot.png) 6 | 7 | > 注:目前暂不适配移动端。 8 | 9 | ## GitHub 10 | 11 | * 前端:[https://github.com/airingursb/sophia](https://github.com/airingursb/sophia) 12 | 13 | * 服务端:[https://github.com/airingursb/sophia-back-end](https://github.com/airingursb/sophia-back-end) 14 | 15 | ## 技术点 16 | 17 | * 前端:vue-cil + vue2 + vuex + vue-router 18 | 19 | * 服务端:express + sequelize 20 | 21 | * 服务器环境:ubuntu14.04 + nginx + mysql + nodejs + pm2 22 | 23 | ## Installation 24 | 25 | ``` bash 26 | # install dependencies 27 | npm install 28 | 29 | # serve at localhost:9001 30 | npm start 31 | ``` 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Airing 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (http://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # Typescript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | .idea/ 61 | -------------------------------------------------------------------------------- /models/user.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by airing on 2017/3/3. 3 | */ 4 | module.exports = function (sequelize, DataTypes) { 5 | return sequelize.define( 6 | 'user', 7 | { 8 | 'username': { 9 | 'type': DataTypes.STRING(45), 10 | 'allowNull': false 11 | }, 12 | 'password': { 13 | 'type': DataTypes.STRING(125), 14 | 'allowNull': false 15 | }, 16 | 'face': { 17 | 'type': DataTypes.STRING(250), 18 | 'allowNull': false 19 | }, 20 | 'sex': { 21 | 'type': DataTypes.INTEGER, 22 | 'allowNull': true 23 | }, 24 | 'school': { 25 | 'type': DataTypes.STRING(45), 26 | 'allowNull': true 27 | }, 28 | 'className': { 29 | 'type': DataTypes.STRING(20), 30 | 'allowNull': true 31 | }, 32 | 'phonenumber': { 33 | 'type': DataTypes.STRING(20), 34 | 'allowNull': true 35 | }, 36 | 'state': { 37 | 'type': DataTypes.STRING(20), 38 | 'allowNull': true 39 | }, 40 | 'createdAt': { 41 | 'type': DataTypes.DOUBLE, 42 | 'allowNull': true 43 | } 44 | } 45 | ); 46 | }; 47 | -------------------------------------------------------------------------------- /models/comment.js: -------------------------------------------------------------------------------- 1 | module.exports = function (sequelize, DataTypes) { 2 | return sequelize.define( 3 | 'comment', 4 | { 5 | 'uid': { 6 | 'type': DataTypes.INTEGER, 7 | 'allowNull': false 8 | }, 9 | 'userId': { 10 | 'type': DataTypes.INTEGER, 11 | 'allowNull': true 12 | }, 13 | 'username': { 14 | 'type': DataTypes.STRING(45), 15 | 'allowNull': false 16 | }, 17 | 'className': { 18 | 'type': DataTypes.STRING(45), 19 | 'allowNull': false 20 | }, 21 | 'philosopherId': { 22 | 'type': DataTypes.INTEGER, 23 | 'allowNull': false 24 | }, 25 | 'msg': { 26 | 'type': DataTypes.TEXT, 27 | 'allowNull': false 28 | }, 29 | 'face_url': { 30 | 'type': DataTypes.STRING(125), 31 | 'allowNull': false 32 | }, 33 | 'createdAt': { 34 | 'type': DataTypes.DOUBLE, 35 | 'allowNull': true 36 | } 37 | }, 38 | { 39 | indexes: [ 40 | { 41 | name: 'philosopher_id', 42 | method: 'BTREE', 43 | fields: ['philosopherId'] 44 | }, 45 | { 46 | name: 'user_id', 47 | method: 'BTREE', 48 | fields: ['userId'] 49 | } 50 | ] 51 | } 52 | ); 53 | }; 54 | 55 | -------------------------------------------------------------------------------- /models/work.js: -------------------------------------------------------------------------------- 1 | module.exports = function (sequelize, DataTypes) { 2 | return sequelize.define( 3 | 'work', 4 | { 5 | 'uid': { 6 | 'type': DataTypes.INTEGER, 7 | 'allowNull': false 8 | }, 9 | 'userId': { 10 | 'type': DataTypes.INTEGER, 11 | 'allowNull': true 12 | }, 13 | 'username': { 14 | 'type': DataTypes.STRING(45), 15 | 'allowNull': false 16 | }, 17 | 'face_url': { 18 | 'type': DataTypes.STRING(125), 19 | 'allowNull': false 20 | }, 21 | 'philosopherId': { 22 | 'type': DataTypes.INTEGER, 23 | 'allowNull': false 24 | }, 25 | 'worksname': { 26 | 'type': DataTypes.STRING(500), 27 | 'allowNull': false 28 | }, 29 | 'worksurl': { 30 | 'type': DataTypes.STRING(125), 31 | 'allowNull': true 32 | }, 33 | 'rating': { 34 | 'type': DataTypes.DOUBLE, 35 | 'allowNull': true 36 | } 37 | }, 38 | { 39 | indexes: [ 40 | { 41 | name: 'philosopher_id', 42 | method: 'BTREE', 43 | fields: ['philosopherId'] 44 | }, 45 | { 46 | name: 'user_id', 47 | method: 'BTREE', 48 | fields: ['userId'] 49 | } 50 | ] 51 | } 52 | ); 53 | }; 54 | 55 | -------------------------------------------------------------------------------- /models/philosopher.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by airing on 2017/3/3. 3 | */ 4 | module.exports = function (sequelize, DataTypes) { 5 | return sequelize.define( 6 | 'philosopher', 7 | { 8 | 'name': { 9 | 'type': DataTypes.STRING(45), 10 | 'allowNull': false 11 | }, 12 | 'englishname': { 13 | 'type': DataTypes.STRING(125), 14 | 'allowNull': true 15 | }, 16 | 'mainidea': { 17 | 'type': DataTypes.STRING(45), 18 | 'allowNull': true 19 | }, 20 | 'avatar': { 21 | 'type': DataTypes.STRING(1000), 22 | 'allowNull': true 23 | }, 24 | 'place': { 25 | 'type': DataTypes.STRING(20), 26 | 'allowNull': true 27 | }, 28 | 'time': { 29 | 'type': DataTypes.STRING(45), 30 | 'allowNull': true 31 | }, 32 | 'wiki': { 33 | 'type': DataTypes.STRING(125), 34 | 'allowNull': true 35 | }, 36 | 'introduce': { 37 | 'type': DataTypes.TEXT, 38 | 'allowNull': true 39 | }, 40 | 'contributor': { 41 | 'type': DataTypes.TEXT, 42 | 'allowNull': true 43 | }, 44 | 'tags': { 45 | 'type': DataTypes.TEXT, 46 | 'allowNull': true 47 | }, 48 | 'createdAt': { 49 | 'type': DataTypes.DOUBLE, 50 | 'allowNull': true 51 | } 52 | } 53 | ); 54 | } 55 | -------------------------------------------------------------------------------- /models/data.js: -------------------------------------------------------------------------------- 1 | module.exports = function (sequelize, DataTypes) { 2 | return sequelize.define( 3 | 'data', 4 | { 5 | 'uid': { 6 | 'type': DataTypes.INTEGER, 7 | 'allowNull': false 8 | }, 9 | 'userId': { 10 | 'type': DataTypes.INTEGER, 11 | 'allowNull': true 12 | }, 13 | 'username': { 14 | 'type': DataTypes.STRING(45), 15 | 'allowNull': false 16 | }, 17 | 'face_url': { 18 | 'type': DataTypes.STRING(125), 19 | 'allowNull': false 20 | }, 21 | 'philosopherId': { 22 | 'type': DataTypes.INTEGER, 23 | 'allowNull': false 24 | }, 25 | 'name': { 26 | 'type': DataTypes.STRING(500), 27 | 'allowNull': false 28 | }, 29 | 'url': { 30 | 'type': DataTypes.STRING(500), 31 | 'allowNull': true 32 | }, 33 | 'rating': { 34 | 'type': DataTypes.DOUBLE, 35 | 'allowNull': true 36 | }, 37 | 'createdAt': { 38 | 'type': DataTypes.DOUBLE, 39 | 'allowNull': true 40 | } 41 | }, 42 | { 43 | indexes: [ 44 | { 45 | name: 'philosopher_id', 46 | method: 'BTREE', 47 | fields: ['philosopherId'] 48 | }, 49 | { 50 | name: 'user_id', 51 | method: 'BTREE', 52 | fields: ['userId'] 53 | } 54 | ] 55 | } 56 | ); 57 | }; 58 | -------------------------------------------------------------------------------- /models/idea.js: -------------------------------------------------------------------------------- 1 | module.exports = function (sequelize, DataTypes) { 2 | return sequelize.define( 3 | 'idea', 4 | { 5 | 'uid': { 6 | 'type': DataTypes.INTEGER, 7 | 'allowNull': false 8 | }, 9 | 'userId': { 10 | 'type': DataTypes.INTEGER, 11 | 'allowNull': true 12 | }, 13 | 'username': { 14 | 'type': DataTypes.STRING(45), 15 | 'allowNull': false 16 | }, 17 | 'face_url': { 18 | 'type': DataTypes.STRING(125), 19 | 'allowNull': false 20 | }, 21 | 'philosopherId': { 22 | 'type': DataTypes.INTEGER, 23 | 'allowNull': false 24 | }, 25 | 'content': { 26 | 'type': DataTypes.STRING(500), 27 | 'allowNull': false 28 | }, 29 | 'frombook': { 30 | 'type': DataTypes.STRING(125), 31 | 'allowNull': true 32 | }, 33 | 'frompaper': { 34 | 'type': DataTypes.STRING(125), 35 | 'allowNull': true 36 | }, 37 | 'rating': { 38 | 'type': DataTypes.DOUBLE, 39 | 'allowNull': true 40 | }, 41 | 'createdAt': { 42 | 'type': DataTypes.DOUBLE, 43 | 'allowNull': true 44 | } 45 | }, 46 | { 47 | indexes: [ 48 | { 49 | name: 'philosopher_id', 50 | method: 'BTREE', 51 | fields: ['philosopherId'] 52 | }, 53 | { 54 | name: 'user_id', 55 | method: 'BTREE', 56 | fields: ['userId'] 57 | } 58 | ] 59 | } 60 | ); 61 | }; 62 | 63 | -------------------------------------------------------------------------------- /models/paper.js: -------------------------------------------------------------------------------- 1 | module.exports = function (sequelize, DataTypes) { 2 | return sequelize.define( 3 | 'paper', 4 | { 5 | 'uid': { 6 | 'type': DataTypes.INTEGER, 7 | 'allowNull': false 8 | }, 9 | 'userId': { 10 | 'type': DataTypes.INTEGER, 11 | 'allowNull': true 12 | }, 13 | 'username': { 14 | 'type': DataTypes.STRING(45), 15 | 'allowNull': false 16 | }, 17 | 'face_url': { 18 | 'type': DataTypes.STRING(125), 19 | 'allowNull': false 20 | }, 21 | 'philosopherId': { 22 | 'type': DataTypes.INTEGER, 23 | 'allowNull': false 24 | }, 25 | 'paper_name': { 26 | 'type': DataTypes.STRING(500), 27 | 'allowNull': false 28 | }, 29 | 'paper_url': { 30 | 'type': DataTypes.STRING(500), 31 | 'allowNull': true 32 | }, 33 | 'author': { 34 | 'type': DataTypes.STRING(125), 35 | 'allowNull': true 36 | }, 37 | 'rating': { 38 | 'type': DataTypes.DOUBLE, 39 | 'allowNull': true 40 | }, 41 | 'createdAt': { 42 | 'type': DataTypes.DOUBLE, 43 | 'allowNull': true 44 | } 45 | }, 46 | { 47 | indexes: [ 48 | { 49 | name: 'philosopher_id', 50 | method: 'BTREE', 51 | fields: ['philosopherId'] 52 | }, 53 | { 54 | name: 'user_id', 55 | method: 'BTREE', 56 | fields: ['userId'] 57 | } 58 | ] 59 | } 60 | ); 61 | }; 62 | 63 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var path = require('path'); 3 | var favicon = require('serve-favicon'); 4 | var logger = require('morgan'); 5 | var cookieParser = require('cookie-parser'); 6 | var bodyParser = require('body-parser'); 7 | 8 | var index = require('./routes/index'); 9 | var users = require('./routes/users'); 10 | var philosophers = require('./routes/philosophers'); 11 | var util = require('./routes/util'); 12 | 13 | var app = express(); 14 | 15 | // view engine setup 16 | app.set('views', path.join(__dirname, 'views')); 17 | app.set('view engine', 'jade'); 18 | 19 | // uncomment after placing your favicon in /public 20 | // app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); 21 | app.use(logger('dev')); 22 | app.use(bodyParser.json()); 23 | app.use(bodyParser.urlencoded({extended: false})); 24 | app.use(cookieParser()); 25 | app.use(express.static(path.join(__dirname, 'public'))); 26 | 27 | app.use('/', index); 28 | app.use('/users', users); 29 | app.use('/philosophers', philosophers); 30 | app.use('/util', util); 31 | 32 | // catch 404 and forward to error handler 33 | app.use(function (req, res, next) { 34 | var err = new Error('Not Found'); 35 | err.status = 404; 36 | next(err); 37 | }); 38 | 39 | // error handler 40 | app.use(function (err, req, res, next) { 41 | // set locals, only providing error in development 42 | res.locals.message = err.message; 43 | res.locals.error = req.app.get('env') === 'development' ? err : {}; 44 | 45 | // render the error page 46 | res.status(err.status || 500); 47 | res.render('error'); 48 | }); 49 | 50 | module.exports = app; 51 | -------------------------------------------------------------------------------- /models/index.js: -------------------------------------------------------------------------------- 1 | var sequelize = require('../config/sequelize').sequelize(); 2 | var User = sequelize.import('./user'); 3 | var Philosopher = sequelize.import('./philosopher'); 4 | var Comment = sequelize.import('./comment'); 5 | var Paper = sequelize.import('./paper'); 6 | var Work = sequelize.import('./work'); 7 | var Data = sequelize.import('./data'); 8 | var Idea = sequelize.import('./idea'); 9 | var Code = sequelize.import('./code'); 10 | 11 | User.hasMany(Comment, {foreignKey: 'userId', targetKey: 'userId'}); 12 | User.hasMany(Paper, {foreignKey: 'userId', targetKey: 'userId'}); 13 | User.hasMany(Work, {foreignKey: 'userId', targetKey: 'userId'}); 14 | User.hasMany(Data, {foreignKey: 'userId', targetKey: 'userId'}); 15 | User.hasMany(Idea, {foreignKey: 'userId', targetKey: 'userId'}); 16 | 17 | Philosopher.hasMany(Comment, {foreignKey: 'philosopherId', targetKey: 'philosopherId'}); 18 | Philosopher.hasMany(Paper, {foreignKey: 'philosopherId', targetKey: 'philosopherId'}); 19 | Philosopher.hasMany(Work, {foreignKey: 'philosopherId', targetKey: 'philosopherId'}); 20 | Philosopher.hasMany(Data, {foreignKey: 'philosopherId', targetKey: 'philosopherId'}); 21 | Philosopher.hasMany(Idea, {foreignKey: 'philosopherId', targetKey: 'philosopherId'}); 22 | 23 | Comment.belongsTo(Philosopher); 24 | Paper.belongsTo(Philosopher); 25 | Work.belongsTo(Philosopher); 26 | Data.belongsTo(Philosopher); 27 | Idea.belongsTo(Philosopher); 28 | 29 | sequelize.sync(); 30 | 31 | exports.User = User; 32 | exports.Philosopher = Philosopher; 33 | exports.Comment = Comment; 34 | exports.Paper = Paper; 35 | exports.Work = Work; 36 | exports.Data = Data; 37 | exports.Idea = Idea; 38 | exports.Code = Code; 39 | -------------------------------------------------------------------------------- /bin/www: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | /** 4 | * Module dependencies. 5 | */ 6 | 7 | var app = require('../app'); 8 | var debug = require('debug')('rideread:server'); 9 | var http = require('http'); 10 | 11 | /** 12 | * Get port from environment and store in Express. 13 | */ 14 | 15 | var port = normalizePort(process.env.PORT || '9001'); 16 | app.set('port', port); 17 | 18 | /** 19 | * Create HTTP server. 20 | */ 21 | 22 | var server = http.createServer(app); 23 | 24 | /** 25 | * Listen on provided port, on all network interfaces. 26 | */ 27 | 28 | server.listen(port); 29 | server.on('error', onError); 30 | server.on('listening', onListening); 31 | 32 | /** 33 | * Normalize a port into a number, string, or false. 34 | */ 35 | 36 | function normalizePort(val) { 37 | var port = parseInt(val, 10); 38 | 39 | if (isNaN(port)) { 40 | // named pipe 41 | return val; 42 | } 43 | 44 | if (port >= 0) { 45 | // port number 46 | return port; 47 | } 48 | 49 | return false; 50 | } 51 | 52 | /** 53 | * Event listener for HTTP server "error" event. 54 | */ 55 | 56 | function onError(error) { 57 | if (error.syscall !== 'listen') { 58 | throw error; 59 | } 60 | 61 | var bind = typeof port === 'string' 62 | ? 'Pipe ' + port 63 | : 'Port ' + port; 64 | 65 | // handle specific listen errors with friendly messages 66 | switch (error.code) { 67 | case 'EACCES': 68 | console.error(bind + ' requires elevated privileges'); 69 | process.exit(1); 70 | break; 71 | case 'EADDRINUSE': 72 | console.error(bind + ' is already in use'); 73 | process.exit(1); 74 | break; 75 | default: 76 | throw error; 77 | } 78 | } 79 | 80 | /** 81 | * Event listener for HTTP server "listening" event. 82 | */ 83 | 84 | function onListening() { 85 | var addr = server.address(); 86 | var bind = typeof addr === 'string' 87 | ? 'pipe ' + addr 88 | : 'port ' + addr.port; 89 | debug('Listening on ' + bind); 90 | } 91 | -------------------------------------------------------------------------------- /routes/users.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var router = express.Router(); 3 | var sha1 = require('sha1'); 4 | var md5 = require('md5'); 5 | 6 | var UserModel = require('../models').User; 7 | var CodeModel = require('../models').Code; 8 | 9 | var KEY = require('./config').KEY; 10 | var MESSAGE = require('./config').MESSAGE; 11 | 12 | router.get('/login', function (req, res, next) { 13 | 14 | var timestamp = new Date().getTime(); 15 | 16 | if (req.query.phonenumber == null 17 | || req.query.password == null) { 18 | return res.jsonp({status: 1000, msg: MESSAGE.PARAMETER_ERROR}) 19 | } 20 | 21 | UserModel.findOne({ 22 | where: { 23 | phonenumber: req.query.phonenumber 24 | } 25 | }).then(function (user) { 26 | if (!user) { 27 | return res.jsonp({status: 1002, msg: MESSAGE.USER_NOT_EXIST}); 28 | } 29 | if (user.password !== req.query.password) { 30 | return res.jsonp({status: 1003, msg: MESSAGE.PASSWORD_ERROR}); 31 | } 32 | var token = md5(user.id + timestamp + KEY); 33 | return res.jsonp({status: 0, timestamp: timestamp, token: token, data: user, msg: MESSAGE.SUCCESS}); 34 | }); 35 | }); 36 | 37 | router.get('/register', function (req, res, next) { 38 | 39 | var timestamp = new Date().getTime(); 40 | 41 | if (req.query.password == null 42 | || req.query.username == null 43 | || req.query.phonenumber == null) { 44 | return res.jsonp({status: 1000, msg: MESSAGE.PARAMETER_ERROR}) 45 | } 46 | 47 | var user = { 48 | phonenumber: req.query.phonenumber, 49 | password: sha1(req.query.password), 50 | face: 'http://airing.ursb.me/image/cover/philosopherlogo.png', 51 | username: req.query.username, 52 | sex: req.query.sex ? parseInt(req.query.sex) : 0, 53 | school: req.query.school ? req.query.school : '', 54 | className: req.query.className ? req.query.className : '', 55 | state: '0', 56 | createdAt: timestamp 57 | }; 58 | CodeModel.findOne({ 59 | where: { 60 | user_account: req.query.phonenumber, 61 | code: req.query.code, 62 | used: false, 63 | timestamp: req.query.timestamp 64 | } 65 | }).then(function (result) { 66 | if (!result) { 67 | UserModel.findOne({ 68 | where: { 69 | phonenumber: user.phonenumber 70 | } 71 | }).then(function (result) { 72 | if (!result) { 73 | UserModel.create(user).then(function (user) { 74 | var token = md5(user.id + timestamp + KEY); 75 | return res.jsonp({status: 0, timestamp: timestamp, data: user, token: token, msg: MESSAGE.SUCCESS}); 76 | }); 77 | } else { 78 | return res.jsonp({status: 1005, msg: MESSAGE.USER_ALREADY_EXIST}); 79 | } 80 | }); 81 | } else { 82 | return res.jsonp({status: 1003, msg: MESSAGE.CODE_ERROR}); 83 | } 84 | }); 85 | }); 86 | 87 | router.get('/update_face', function (req, res, next) { 88 | if (req.query.uid == null 89 | || req.query.token == null 90 | || req.query.timestamp == null 91 | || req.query.face == null) { 92 | return res.jsonp({status: 1000, msg: MESSAGE.PARAMETER_ERROR}) 93 | } 94 | UserModel.update({ 95 | face: req.query.face 96 | }, { 97 | where: { 98 | id: req.query.uid 99 | } 100 | }).then(function () { 101 | UserModel.findOne({ 102 | where: { 103 | id: req.query.uid 104 | } 105 | }).then(function (user) { 106 | return res.jsonp({status: 0, user: user, msg: MESSAGE.SUCCESS}) 107 | }); 108 | }) 109 | }); 110 | 111 | module.exports = router; 112 | -------------------------------------------------------------------------------- /routes/util.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var router = express.Router(); 3 | var qiniu = require('qiniu'); 4 | var https = require('https'); 5 | var querystring = require('querystring'); 6 | 7 | var QINIU_ACCESS = require('./config').QINIU_ACCESS; 8 | var QINIU_SECRET = require('./config').QINIU_SECRET; 9 | var BUCKET = require('./config').BUCKET; 10 | var MESSAGE = require('./config').MESSAGE; 11 | var ADMIN_USER = require('./config').ADMIN_USER; 12 | var ADMIN_PASSWORD = require('./config').ADMIN_PASSWORD; 13 | var YUNPIAN_APIKEY = require('./config').YUNPIAN_APIKEY; 14 | 15 | var UserModel = require('../models').User; 16 | var CodeModel = require('../models').Code; 17 | var VersionModel = require('../models').Version; 18 | 19 | qiniu.conf.ACCESS_KEY = QINIU_ACCESS; 20 | qiniu.conf.SECRET_KEY = QINIU_SECRET; 21 | 22 | router.post('/upload', function (req, res, next) { 23 | var form = new multiparty.Form({uploadDir: './public/files/'}); 24 | form.parse(req, function (err, fields, files) { 25 | var filesTmp = JSON.stringify(files, null, 2); 26 | if (err) { 27 | console.log('parse error: ' + err); 28 | } else { 29 | console.log('parse files: ' + filesTmp); 30 | var inputFile = files.inputFile[0]; 31 | var uploadedPath = inputFile.path; 32 | var dstPath = './public/files/' + inputFile.originalFilename; 33 | fs.rename(uploadedPath, dstPath, function (err) { 34 | if (err) { 35 | console.log('rename error: ' + err); 36 | } else { 37 | console.log('rename ok'); 38 | } 39 | }); 40 | } 41 | 42 | res.writeHead(200, {'content-type': 'text/plain;charset=utf-8'}); 43 | res.write('received upload:\n\n'); 44 | res.end(util.inspect({fields: fields, files: filesTmp})); 45 | }); 46 | }); 47 | 48 | function uptoken(bucket, key) { 49 | var putPolicy = new qiniu.rs.PutPolicy(bucket + ":" + key); 50 | return putPolicy.token(); 51 | } 52 | 53 | router.get('/qiniu_token', function (req, res, next) { 54 | 55 | if (req.query.token == null || req.query.uid == null || req.query.timestamp == null || req.query.filename == null) { 56 | return res.jsonp({status: 1000, msg: MESSAGE.PARAMETER_ERROR}) 57 | } 58 | var qiniu_token = uptoken(BUCKET, req.query.filename); 59 | return res.jsonp({status: 0, qiniu_token: qiniu_token, msg: MESSAGE.SUCCESS}); 60 | }); 61 | 62 | router.get('/code', function (req, res, next) { 63 | 64 | var timestamp = new Date().getTime(); 65 | if (req.query.timestamp == null || req.query.phonenumber == null) { 66 | return res.jsonp({status: 1000, msg: MESSAGE.PARAMETER_ERROR}) 67 | } 68 | var code = Math.floor(Math.random() * 899999 + 100000); 69 | 70 | var postData = { 71 | mobile: req.query.phonenumber, 72 | text: '【双生APP】您的验证码是' + code, 73 | apikey: YUNPIAN_APIKEY 74 | }; 75 | 76 | var content = querystring.stringify(postData); 77 | 78 | var options = { 79 | host: 'sms.yunpian.com', 80 | path: '/v2/sms/single_send.json', 81 | method: 'POST', 82 | agent: false, 83 | rejectUnauthorized: false, 84 | headers: { 85 | 'Content-Type': 'application/x-www-form-urlencoded', 86 | 'Content-Length': content.length 87 | } 88 | }; 89 | 90 | var model = { 91 | user_account: req.query.phonenumber, 92 | code: code, 93 | timestamp: timestamp, 94 | used: false 95 | }; 96 | 97 | CodeModel.findAll({ 98 | where: { 99 | user_account: req.query.phonenumber, 100 | used: false 101 | } 102 | }).then(function (results) { 103 | if (results[0] !== undefined) { 104 | console.log('连续请求:' + (timestamp - results[0].timestamp)); 105 | if (timestamp - results[0].timestamp < 600000) { 106 | return res.jsonp({status: 5000, msg: MESSAGE.REQUEST_ERROR}); 107 | } 108 | } 109 | CodeModel.create(model).then(function () { 110 | 111 | var req = https.request(options, function (res) { 112 | res.setEncoding('utf8'); 113 | res.on('data', function (chunk) { 114 | console.log(JSON.parse(chunk)); 115 | }); 116 | res.on('end', function () { 117 | console.log('over'); 118 | }); 119 | }); 120 | req.write(content); 121 | req.end(); 122 | }); 123 | 124 | return res.jsonp({status: 0, msg: MESSAGE.SUCCESS, data: code}); 125 | }); 126 | }); 127 | 128 | module.exports = router; 129 | -------------------------------------------------------------------------------- /routes/config.js: -------------------------------------------------------------------------------- 1 | const MESSAGE = { 2 | SUCCESS: '请求成功', // 0 3 | PARAMETER_ERROR: '参数错误', // 1000 4 | USER_NOT_EXIST: '用户不存在', // 1002 5 | PASSWORD_ERROR: '账号密码错误', // 1003 6 | CODE_ERROR: '验证码错误', // 1004 7 | USER_ALREADY_EXIST: '用户已被注册', // 1005 8 | USER_ALREADY_CONNECT: '用户已被匹配', // 9 | USER_ALREADY_LOGIN: '用户已被登录', // 1017 10 | USER_NOT_LOGIN: '用户尚未登录', // 11 | TYPE_ERROR: 'type参数有误', // 1001 12 | PICTURE_IS_NULL: '图片为空', // 1002 13 | VIDEO_IS_NULL: '视频为空', // 1003 14 | MOMENT_IS_NULL: '附近没有阅圈', // 4000 15 | REQUEST_ERROR: '请求时间间隔过短', // 5000 16 | FOLLOWER_IS_EXISE: '已经存在该粉丝', // 5001 17 | }; 18 | 19 | const KEY = ''; 20 | const SQL_PASSWORD = ''; 21 | const YUNPIAN_APIKEY = ''; 22 | const QINIU_ACCESS = ''; 23 | const QINIU_SECRET = ''; 24 | const BUCKET = ''; 25 | const ADMIN_USER = ''; 26 | const ADMIN_PASSWORD = ''; 27 | 28 | var JPush = require('jpush-sdk'); 29 | var client = JPush.buildClient('', ''); 30 | 31 | function getNowFormatDate() { 32 | var date = new Date(); 33 | var seperator1 = "-"; 34 | var seperator2 = ":"; 35 | var month = date.getMonth() + 1; 36 | var strDate = date.getDate(); 37 | var strHours = date.getHours(); 38 | var strMinutes = date.getMinutes(); 39 | var strSeconds = date.getSeconds(); 40 | if (month >= 1 && month <= 9) { 41 | month = "0" + month; 42 | } 43 | if (strDate >= 0 && strDate <= 9) { 44 | strDate = "0" + strDate; 45 | } 46 | if (strHours >= 0 && strHours <= 9) { 47 | strHours = "0" + strHours; 48 | } 49 | if (strMinutes >= 0 && strMinutes <= 9) { 50 | strMinutes = "0" + strMinutes; 51 | } 52 | if (strSeconds >= 0 && strSeconds <= 9) { 53 | strSeconds = "0" + strSeconds; 54 | } 55 | var currentDate = date.getFullYear() + seperator1 + month + seperator1 + strDate 56 | + 'T' + strHours + seperator2 + strMinutes 57 | + seperator2 + strSeconds + '.000Z'; 58 | return currentDate; 59 | } 60 | 61 | function LantitudeLongitudeDist(lon1, lat1, lon2, lat2) { 62 | var EARTH_RADIUS = 6378137; 63 | var radLat1 = lat1 * Math.PI / 180.0; 64 | var radLat2 = lat2 * Math.PI / 180.0; 65 | 66 | var radLon1 = lon1 * Math.PI / 180.0; 67 | var radLon2 = lon2 * Math.PI / 180.0; 68 | 69 | if (radLat1 < 0) 70 | radLat1 = Math.PI / 2 + Math.abs(radLat1);// south 71 | if (radLat1 > 0) 72 | radLat1 = Math.PI / 2 - Math.abs(radLat1);// north 73 | if (radLon1 < 0) 74 | radLon1 = Math.PI * 2 - Math.abs(radLon1);// west 75 | if (radLat2 < 0) 76 | radLat2 = Math.PI / 2 + Math.abs(radLat2);// south 77 | if (radLat2 > 0) 78 | radLat2 = Math.PI / 2 - Math.abs(radLat2);// north 79 | if (radLon2 < 0) 80 | radLon2 = Math.PI * 2 - Math.abs(radLon2);// west 81 | var x1 = EARTH_RADIUS * Math.cos(radLon1) * Math.sin(radLat1); 82 | var y1 = EARTH_RADIUS * Math.sin(radLon1) * Math.sin(radLat1); 83 | var z1 = EARTH_RADIUS * Math.cos(radLat1); 84 | 85 | var x2 = EARTH_RADIUS * Math.cos(radLon2) * Math.sin(radLat2); 86 | var y2 = EARTH_RADIUS * Math.sin(radLon2) * Math.sin(radLat2); 87 | var z2 = EARTH_RADIUS * Math.cos(radLat2); 88 | 89 | var d = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2)); 90 | //余弦定理求夹角 91 | var theta = Math.acos((EARTH_RADIUS * EARTH_RADIUS + EARTH_RADIUS * EARTH_RADIUS - d * d) / (2 * EARTH_RADIUS * EARTH_RADIUS)); 92 | var dist = theta * EARTH_RADIUS; 93 | return dist.toFixed(3) + 'km'; 94 | } 95 | 96 | var log = function (api) { 97 | console.log('POST: ' + api); 98 | console.log('TIME: ' + getNowFormatDate()); 99 | }; 100 | 101 | function JiGuangPush(user_id) { 102 | client.push().setPlatform('ios', 'android') 103 | .setAudience(JPush.alias(user_id.toString())) 104 | .setNotification('骑阅通知', JPush.ios('ios alert'), JPush.android('android alert', null, 1)) 105 | .setMessage('您有一条未读动态!') 106 | .setOptions(null, 60) 107 | .send(function (err, res) { 108 | if (err) { 109 | if (err instanceof JPush.APIConnectionError) { 110 | console.log(err.message); 111 | // Response Timeout means your request to the server may have already received, 112 | // please check whether or not to push 113 | console.log(err.isResponseTimeout) 114 | } else if (err instanceof JPush.APIRequestError) { 115 | console.log(err.message) 116 | } 117 | } else { 118 | console.log('Sendno: ' + res.sendno); 119 | console.log('Msg_id: ' + res.msg_id); 120 | } 121 | }) 122 | } 123 | 124 | exports.JiGuangPush = JiGuangPush; 125 | exports.MESSAGE = MESSAGE; 126 | exports.KEY = KEY; 127 | exports.SQL_PASSWORD = SQL_PASSWORD; 128 | exports.YUNPIAN_APIKEY = YUNPIAN_APIKEY; 129 | exports.QINIU_ACCESS = QINIU_ACCESS; 130 | exports.QINIU_SECRET = QINIU_SECRET; 131 | exports.ADMIN_USER = ADMIN_USER; 132 | exports.ADMIN_PASSWORD = ADMIN_PASSWORD; 133 | exports.BUCKET = BUCKET; 134 | exports.log = log; 135 | exports.LantitudeLongitudeDist = LantitudeLongitudeDist; -------------------------------------------------------------------------------- /routes/philosophers.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var router = express.Router(); 3 | var sha1 = require('sha1'); 4 | var md5 = require('md5'); 5 | 6 | var UserModel = require('../models').User; 7 | var PhilosopherModel = require('../models').Philosopher; 8 | var CommentModel = require('../models').Comment; 9 | var WorkModel = require('../models').Work; 10 | var PaperModel = require('../models').Paper; 11 | var DataModel = require('../models').Data; 12 | var IdeaModel = require('../models').Idea; 13 | 14 | var MESSAGE = require('./config').MESSAGE; 15 | 16 | router.get('/show_list', function (req, res, next) { 17 | 18 | PhilosopherModel.findAll().then(function (results) { 19 | 20 | var philosophers = []; 21 | results.forEach(function (result) { 22 | var philosopher = {}; 23 | philosopher.pid = result.id; 24 | philosopher.avatar = result.avatar; 25 | philosopher.name = result.name; 26 | philosophers.push(philosopher) 27 | }); 28 | 29 | return res.jsonp({status: 0, data: philosophers, msg: MESSAGE.SUCCESS}); 30 | }); 31 | }); 32 | 33 | router.get('/show_tags', function (req, res, next) { 34 | 35 | PhilosopherModel.findAll({ 36 | where: { 37 | tags: { 38 | '$like': '%' + req.query.tag + '%' 39 | } 40 | } 41 | }).then(function (results) { 42 | 43 | var philosophers = []; 44 | results.forEach(function (result) { 45 | var philosopher = {}; 46 | philosopher.pid = result.id; 47 | philosopher.avatar = result.avatar; 48 | philosopher.name = result.name; 49 | philosophers.push(philosopher) 50 | }); 51 | 52 | return res.jsonp({status: 0, data: philosophers, msg: MESSAGE.SUCCESS}); 53 | }); 54 | }); 55 | 56 | router.get('/show_detail', function (req, res, next) { 57 | 58 | if (req.query.uid == null 59 | || req.query.token == null 60 | || req.query.timestamp == null 61 | || req.query.pid == null) { 62 | return res.jsonp({status: 1000, msg: MESSAGE.PARAMETER_ERROR}) 63 | } 64 | 65 | PhilosopherModel.findOne({ 66 | where: { 67 | id: req.query.pid 68 | }, 69 | include: [CommentModel, PaperModel, WorkModel, DataModel, IdeaModel] 70 | }).then(function (philosopher) { 71 | return res.jsonp({status: 0, data: philosopher, msg: MESSAGE.SUCCESS}); 72 | }); 73 | }); 74 | 75 | router.get('/add_philosopher', function (req, res, next) { 76 | 77 | if (req.query.uid == null 78 | || req.query.token == null 79 | || req.query.timestamp == null 80 | || req.query.name == null) { 81 | return res.jsonp({status: 1000, msg: MESSAGE.PARAMETER_ERROR}) 82 | } 83 | 84 | var philosopher = { 85 | name: req.query.name, 86 | englishname: req.query.englishname ? req.query.englishname : '', 87 | mainidea: req.query.mainidea ? req.query.mainidea : '', 88 | avatar: req.query.avatar ? req.query.avatar : '', 89 | place: req.query.place ? req.query.place : '', 90 | time: req.query.time ? req.query.time : '', 91 | wiki: req.query.wiki ? req.query.wiki : '', 92 | introduce: req.query.introduce ? req.query.introduce : '', 93 | createdAt: new Date().getTime(), 94 | tags: '哲学' 95 | }; 96 | 97 | PhilosopherModel.create(philosopher).then(function (philosopher) { 98 | return res.jsonp({status: 0, data: philosopher, msg: MESSAGE.SUCCESS}); 99 | }); 100 | }); 101 | 102 | router.get('/add_comment', function (req, res, next) { 103 | 104 | var timestamp = new Date().getTime(); 105 | 106 | if (req.query.uid == null 107 | || req.query.token == null 108 | || req.query.timestamp == null 109 | || req.query.pid == null 110 | || req.query.msg == null) { 111 | return res.jsonp({status: 1000, msg: MESSAGE.PARAMETER_ERROR}) 112 | } 113 | 114 | var comment = { 115 | uid: req.query.uid, 116 | philosopherId: req.query.pid, 117 | username: '', 118 | face_url: '', 119 | className: '', 120 | msg: req.query.msg ? req.query.msg : '', 121 | philosopher: {}, 122 | user: {}, 123 | createdAt: timestamp 124 | }; 125 | 126 | UserModel.findOne({ 127 | where: { 128 | id: req.query.uid 129 | } 130 | }).then(function (user) { 131 | comment.user = user; 132 | comment.username = user.username; 133 | comment.face_url = user.face; 134 | comment.className = user.className; 135 | 136 | PhilosopherModel.findOne({ 137 | where: { 138 | id: req.query.pid 139 | } 140 | }).then(function (philosopher) { 141 | comment.philosopher = philosopher; 142 | CommentModel.create(comment).then(function (result) { 143 | return res.jsonp({status: 0, data: result, msg: MESSAGE.SUCCESS}); 144 | }) 145 | }) 146 | }); 147 | }); 148 | 149 | router.get('/add_paper', function (req, res, next) { 150 | 151 | var timestamp = new Date().getTime(); 152 | 153 | if (req.query.uid == null 154 | || req.query.token == null 155 | || req.query.timestamp == null 156 | || req.query.pid == null 157 | || req.query.papername == null) { 158 | return res.jsonp({status: 1000, msg: MESSAGE.PARAMETER_ERROR}) 159 | } 160 | 161 | var paper = { 162 | uid: req.query.uid, 163 | philosopherId: req.query.pid, 164 | username: '', 165 | face_url: '', 166 | paper_name: req.query.papername ? req.query.papername : '', 167 | paper_url: req.query.paperurl ? req.query.paperurl : '', 168 | author: req.query.author ? req.query.author : '', 169 | rating: req.query.rating ? req.query.rating : '', 170 | philosopher: {}, 171 | user: {}, 172 | createdAt: timestamp 173 | }; 174 | 175 | UserModel.findOne({ 176 | where: { 177 | id: req.query.uid 178 | } 179 | }).then(function (user) { 180 | paper.user = user; 181 | paper.username = user.username; 182 | paper.face_url = user.face; 183 | 184 | PhilosopherModel.findOne({ 185 | where: { 186 | id: req.query.pid 187 | } 188 | }).then(function (philosopher) { 189 | paper.philosopher = philosopher; 190 | PaperModel.create(paper).then(function (result) { 191 | return res.jsonp({status: 0, data: result, msg: MESSAGE.SUCCESS}); 192 | }) 193 | }) 194 | }); 195 | }); 196 | 197 | router.get('/add_works', function (req, res, next) { 198 | 199 | var timestamp = new Date().getTime(); 200 | 201 | if (req.query.uid == null 202 | || req.query.token == null 203 | || req.query.timestamp == null 204 | || req.query.pid == null 205 | || req.query.worksname == null) { 206 | return res.jsonp({status: 1000, msg: MESSAGE.PARAMETER_ERROR}) 207 | } 208 | 209 | var works = { 210 | uid: req.query.uid, 211 | philosopherId: req.query.pid, 212 | username: '', 213 | face_url: '', 214 | worksname: req.query.worksname ? req.query.worksname : '', 215 | worksurl: req.query.worksurl ? req.query.worksurl : '', 216 | rating: req.query.rating ? req.query.rating : '', 217 | philosopher: {}, 218 | user: {}, 219 | createdAt: timestamp 220 | }; 221 | 222 | UserModel.findOne({ 223 | where: { 224 | id: req.query.uid 225 | } 226 | }).then(function (user) { 227 | works.user = user; 228 | works.username = user.username; 229 | works.face_url = user.face; 230 | 231 | PhilosopherModel.findOne({ 232 | where: { 233 | id: req.query.pid 234 | } 235 | }).then(function (philosopher) { 236 | works.philosopher = philosopher; 237 | WorkModel.create(works).then(function (result) { 238 | return res.jsonp({status: 0, data: result, msg: MESSAGE.SUCCESS}); 239 | }) 240 | }) 241 | }); 242 | }); 243 | 244 | router.get('/add_data', function (req, res, next) { 245 | 246 | var timestamp = new Date().getTime(); 247 | 248 | if (req.query.uid == null 249 | || req.query.token == null 250 | || req.query.timestamp == null 251 | || req.query.pid == null 252 | || req.query.name == null) { 253 | return res.jsonp({status: 1000, msg: MESSAGE.PARAMETER_ERROR}) 254 | } 255 | 256 | var data = { 257 | uid: req.query.uid, 258 | philosopherId: req.query.pid, 259 | username: '', 260 | face_url: '', 261 | name: req.query.name ? req.query.name : '', 262 | url: req.query.url ? req.query.url : '', 263 | rating: req.query.rating ? req.query.rating : '', 264 | philosopher: {}, 265 | user: {}, 266 | createdAt: timestamp 267 | }; 268 | 269 | UserModel.findOne({ 270 | where: { 271 | id: req.query.uid 272 | } 273 | }).then(function (user) { 274 | data.user = user; 275 | data.username = user.username; 276 | data.face_url = user.face; 277 | 278 | PhilosopherModel.findOne({ 279 | where: { 280 | id: req.query.pid 281 | } 282 | }).then(function (philosopher) { 283 | data.philosopher = philosopher; 284 | DataModel.create(data).then(function (result) { 285 | return res.jsonp({status: 0, data: result, msg: MESSAGE.SUCCESS}); 286 | }) 287 | }) 288 | }); 289 | }); 290 | 291 | router.get('/add_idea', function (req, res, next) { 292 | 293 | var timestamp = new Date().getTime(); 294 | 295 | if (req.query.uid == null 296 | || req.query.token == null 297 | || req.query.timestamp == null 298 | || req.query.pid == null 299 | || req.query.content == null) { 300 | return res.jsonp({status: 1000, msg: MESSAGE.PARAMETER_ERROR}) 301 | } 302 | 303 | var idea = { 304 | uid: req.query.uid, 305 | userId: req.query.uid, 306 | philosopherId: req.query.pid, 307 | username: '', 308 | face_url: '', 309 | content: req.query.content ? req.query.content : '', 310 | frombook: req.query.frombook ? req.query.frombook : '', 311 | frompaper: req.query.frompaper ? req.query.frompaper : '', 312 | rating: req.query.rating ? req.query.rating : '', 313 | philosopher: {}, 314 | user: {}, 315 | createdAt: timestamp 316 | }; 317 | 318 | UserModel.findOne({ 319 | where: { 320 | id: req.query.uid 321 | } 322 | }).then(function (user) { 323 | idea.user = user; 324 | idea.username = user.username; 325 | idea.face_url = user.face; 326 | 327 | PhilosopherModel.findOne({ 328 | where: { 329 | id: req.query.pid 330 | } 331 | }).then(function (philosopher) { 332 | idea.philosopher = philosopher; 333 | console.log(idea); 334 | IdeaModel.create(idea).then(function (result) { 335 | return res.jsonp({status: 0, data: result, msg: MESSAGE.SUCCESS}); 336 | }) 337 | }) 338 | }); 339 | }); 340 | 341 | router.get('/add_tag', function (req, res, next) { 342 | 343 | var timestamp = new Date().getTime(); 344 | 345 | if (req.query.uid == null 346 | || req.query.token == null 347 | || req.query.timestamp == null 348 | || req.query.pid == null 349 | || req.query.tag == null) { 350 | return res.jsonp({status: 1000, msg: MESSAGE.PARAMETER_ERROR}) 351 | } 352 | 353 | PhilosopherModel.findOne({ 354 | where: { 355 | id: req.query.pid 356 | } 357 | }).then(function (philosopher) { 358 | var tags = philosopher.tags.split(','); 359 | if (tags.indexOf(req.query.tag) === -1) { 360 | tags.push(req.query.tag); 361 | PhilosopherModel.update({ 362 | tags: tags.join(',') 363 | }, { 364 | where: { 365 | id: req.query.pid 366 | } 367 | }).then(function () { 368 | return res.jsonp({status: 0, msg: MESSAGE.SUCCESS}); 369 | }) 370 | } else { 371 | return res.jsonp({status: 202}); 372 | } 373 | 374 | }) 375 | }); 376 | 377 | router.get('/search', function (req, res, next) { 378 | 379 | var timestamp = new Date().getTime(); 380 | 381 | if (req.query.search == null) { 382 | return res.jsonp({status: 1000, msg: MESSAGE.PARAMETER_ERROR}) 383 | } 384 | 385 | if (req.query.search.substr(0, 1) === '#') { 386 | PhilosopherModel.findAll({ 387 | where: { 388 | tags: { 389 | '$like': '%' + req.query.search.substring(1, req.query.search.length) + '%' 390 | } 391 | } 392 | }).then(function (results) { 393 | var philosophers = []; 394 | results.forEach(function (result) { 395 | var philosopher = {}; 396 | philosopher.pid = result.id; 397 | philosopher.avatar = result.avatar; 398 | philosopher.name = result.name; 399 | philosophers.push(philosopher) 400 | }); 401 | return res.jsonp({status: 0, data: philosophers, msg: MESSAGE.SUCCESS}); 402 | }) 403 | } else { 404 | PhilosopherModel.findAll({ 405 | where: { 406 | name: { 407 | '$like': '%' + req.query.search + '%' 408 | } 409 | } 410 | }).then(function (results) { 411 | var philosophers = []; 412 | results.forEach(function (result) { 413 | var philosopher = {}; 414 | philosopher.pid = result.id; 415 | philosopher.avatar = result.avatar; 416 | philosopher.name = result.name; 417 | philosophers.push(philosopher) 418 | }); 419 | return res.jsonp({status: 0, data: philosophers, msg: MESSAGE.SUCCESS}); 420 | }) 421 | } 422 | 423 | }); 424 | 425 | module.exports = router; 426 | --------------------------------------------------------------------------------