Commit ae864859 authored by 陈志良's avatar 陈志良

分享 - 功能添加

parent a1b4cb97
'use strict'; 'use strict';
var express = require('express'), var express = require('express'),
router = express.Router(), router = express.Router(),
returnCode = require('../../utils/returnCode'), _ = require('lodash'),
_ = require('lodash'); then = require('thenjs'),
mongoose = require('mongoose');
var mongoose = require('mongoose'); var forumThreadService = require('../../service/forumThreadService'),
var forumThreadService = require('../../service/forumThreadService'); returnCode = require('../../utils/returnCode'),
var forumShareConfigService = require('../../service/forumShareConfigService'); forumShareConfigService = require('../../service/forumShareConfigService'),
var forumShareService = require('../../service/forumShareService'); forumShareService = require('../../service/forumShareService'),
var config = require('../../../config/config'); config = require('../../../config/config'),
var httpService=require('../../service/httpService'); httpService=require('../../service/httpService'),
var then = require('thenjs'); forumInfoService = require('../../service/forumInfoService');
var forumInfoService = require('../../service/forumInfoService');
module.exports = function(app) { module.exports = function(app) {
app.use('/v1/forum', router); app.use('/v1/forum', router);
...@@ -24,19 +24,17 @@ function write(res, title, desc, link, imgUrl, mid, ent_code) { ...@@ -24,19 +24,17 @@ function write(res, title, desc, link, imgUrl, mid, ent_code) {
'Cache-Control': 'no-cache', 'Cache-Control': 'no-cache',
'Pragma': 'no-cache' 'Pragma': 'no-cache'
}); });
// result.debug = 'true' === isDebug ? true : false;
// result.jsApiList = jsApiList;
res.write('$(function(){'); res.write('$(function(){');
// var title = type+'类型分享', var success_singlemessage = '$.ajax({'+
// link = 'http://sandbox.wxpai.cn/app/aubywx/100041', 'type:\'GET\','+
// imgUrl = 'http://dev.fs.wxpai.cn/upload/100001_2015410_1428671539587_40.jpg', 'url: \'/v1/forum/'+ent_code+'/share/singlemessage\''+
// desc = '测试描述'; '});';
// var string = ''; var success_timeline = '$.ajax({'+
var success = '$.ajax({'+
'type:\'GET\','+ 'type:\'GET\','+
'url: \'/v1/forum/'+ent_code+'/timeline\''+ 'url: \'/v1/forum/'+ent_code+'/share/timeline\''+
'});'; '});';
var string = 'wx.onMenuShareTimeline({' + '\"title\": \"' + title + '\",' + '\"link\":\"' + link + '\",' + '\"imgUrl\": \"' + imgUrl + '\", ' + 'success: function(){'+success+'},' + 'cancel: function () {}' + ' });' + ' wx.onMenuShareAppMessage({' + '\"title\": \"' + title + '\", ' + '\"desc\": \"' + desc + '\", ' + '\"link\":\"' + link + '\", ' + '\"imgUrl\": \"' + imgUrl + '\", ' + '\"type\":\'\' ,' + '\"dataUrl\": \'\', ' + 'success: function(){},' + 'cancel: function () { ' + '}' + ' });' var string = 'wx.onMenuShareTimeline({' + '\"title\": \"' + title + '\",' + '\"link\":\"' + link + '\",' + '\"imgUrl\": \"' + imgUrl + '\", ' + 'success: function(){'+success_timeline+'},' + 'cancel: function () {}' + ' });' +
' wx.onMenuShareAppMessage({' + '\"title\": \"' + title + '\", ' + '\"desc\": \"' + desc + '\", ' + '\"link\":\"' + link + '\", ' + '\"imgUrl\": \"' + imgUrl + '\", ' + '\"type\":\'\' ,' + '\"dataUrl\": \'\', ' + 'success: function(){'+success_singlemessage+'},' + 'cancel: function () { ' + '}' + ' });'
res.write('wx.ready(function(){' + string + '});'); res.write('wx.ready(function(){' + string + '});');
res.write('wx.error(function(res){console.log(res);});'); res.write('wx.error(function(res){console.log(res);});');
...@@ -73,179 +71,176 @@ function getWX(res, title, desc, link, imgUrl, mid,ent_code, id,type,forumThread ...@@ -73,179 +71,176 @@ function getWX(res, title, desc, link, imgUrl, mid,ent_code, id,type,forumThread
} }
} }
//微信分享接口 //微信分享接口
router.get('/:ent_code/share.js', function(req, res, next) { router.get('/:ent_code/share.js', function(req, res, next) {
var rs = {}; req.session.mobileForumUser = {
var type = req.query.type; userId :123,
var id = req.query.id; openId:'123123'
var uId= req.session.mobileForumUser.userId; };
var mid = 0; req.session.openUser = {
mid:1
}
req.session.user = {
ent_code:100001
}
var rs = {},
type = req.query.type,
id = req.query.id,
uId= req.session.mobileForumUser.userId,
ent_code = req.session.user.ent_code,
mid = 0,
link = '/app/forum/' + ent_code + '/index?pageUrl=',
share_log = {};
if (req.session.openUser && req.session.openUser.mid) { if (req.session.openUser && req.session.openUser.mid) {
mid = req.session.openUser.mid; mid = req.session.openUser.mid;
} }
var ent_code = req.params.ent_code; delete req.session.mobileForumUser.share_log;
var link = '/app/forum/' + ent_code + '/index?pageUrl=';
if (type) {
//板块微信接口 //板块微信接口
if (type === 'Info') { if(!type || !id){
console.error('请求参数错误')
then(function(cont) { res.json(_.assign({result:false},returnCode.WRONG_PARAM));
}else if (type === 'Info') {
//分享记录对象
share_log.type = 2;
share_log.info = id;
//查询板块默认分享设置
forumShareConfigService.getByKey(ent_code, 'share', function(err, result) { forumShareConfigService.getByKey(ent_code, 'share', function(err, result) {
var title = '', if(err){
desc = '', console.error(err);
imgUrl = ''; res.json(_.assign({result:false},returnCode.BUSY));
}else{
var title = '',desc = '',imgUrl = '';
if (result) { if (result) {
//匹配板块默认分享设置
result = result.meta_value; result = result.meta_value;
for (var i = 0; i < result.length; i++) { _.forEach(result,function(e){
if (result[i].type === 'info') { if (e.type === 'info') {
title = result[i].title; title = e.title;
desc = result[i].desc; desc = e.desc;
imgUrl = result[i].imgUrl; imgUrl = e.imgUrl;
return;
} }
} });
if (id) { link = link + 'index&infoId='+ id+'&uId='+uId;
link = link + 'index&infoId=' + id;
} else {
link = link + 'index';
}
link = link +'&uId='+uId;
getWX(res, title, desc, link, imgUrl, mid,ent_code, id,type,null); getWX(res, title, desc, link, imgUrl, mid,ent_code, id,type,null);
} else { }else{
cont(err); //没找到板块默认分享设置
console.error('info默认分享设置不存在');
res.json(_.assign({result:false},returnCode.DATA_NOTEXITS));
}
} }
}); });
}).fail(function(cont, err) { }else if (type === 'Thread') {
console.error(err); share_log.type = 3;
res.json({ share_log.thread = id;
result: false,
code: '10002'
});
});
} else if (!id) {
console.error("ID不能为空");
res.json({
message: 'ID不能为空',
result: false,
code: '10002'
});
} else if (type === 'Thread') {
then(function(cont) { then(function(cont) {
//获得文章 //获得文章
forumThreadService.getById(id, function(err, result) { forumThreadService.getById(id,cont);
if (result) { }).then(function(cont, thread) {
cont(null, result); if(!thread){
} else { //文章个性化分享设置不存在
cont(err); console.error('分享文章不存在');
res.json(_.assign({result:false},returnCode.DATA_NOTEXITS));
}else{
share_log.p_thread = thread.pid;
share_log.info = thread.info;
share_log.thread_type = thread.pid?4:thread.type;
//组装分享路径与判断分享类型
//判断分享类型 1、文章 2、话题 3、照片墙
var share_type = '';
if (thread.type === 1 || (thread.type === 2 && thread.level === 2)) {
link = link + 'thread&infoId=' + thread.info + '&ent_code=' + ent_code + '&id=' + thread._id;
share_type = 'article';
} else if (thread.type === 2 && thread.level === 1) {
link = link + 'topicList&infoId=' + thread.info + '&ent_code=' + ent_code + '&id=' + thread._id;
share_type = 'topic';
} else if (thread.type === 3) {
link = link + 'photoList&infoId=' + thread.info + '&ent_code=' + ent_code + '&id=' + thread._id;
share_type = 'photo';
} }
}); link = link +'&uId='+uId;
}).then(function(cont, result) { //获取分享设置
//如果没有share值则用默认值 if (!thread.share || thread.share_type==2) {
//获取默认文章分享设置
if (!result.share||result.share_type==2) {
forumShareConfigService.getByKey(ent_code, 'share', function(err, sc) { forumShareConfigService.getByKey(ent_code, 'share', function(err, sc) {
var title = '',desc = '',imgUrl = '';
var title = '', if(err || !sc){
desc = '', cont(err || share_type+'默认分享设置不存在');
imgUrl = ''; }else{
if (sc) {
if (result.type === 1 || (result.type === 2 && result.level === 2)) {
link = link + 'thread&infoId=' + result.info + '&ent_code=' + ent_code + '&id=' + result._id;
} else if (result.type === 2 && result.level === 1) {
link = link + 'topicList&infoId=' + result.info + '&ent_code=' + ent_code + '&id=' + result._id;
} else if (result.type === 3) {
link = link + 'photoList&infoId=' + result.info + '&ent_code=' + ent_code + '&id=' + result._id;
}
//1、文章 2、话题 3、照片墙
sc = sc.meta_value; sc = sc.meta_value;
for (var i = 0; i < sc.length; i++) { //设置分享信息
_.forEach(sc,function(e){
if (result.type === 1 || (result.type === 2 && result.level === 2)) { if(share_type === e.type){
title = e.title;
if (sc[i].type === 'article') { desc = e.desc;
imgUrl = e.imgUrl;
title = sc[i].title; return;
desc = sc[i].desc;
imgUrl = sc[i].imgUrl;
}
} else if (result.type === 2 && result.level === 1) {
if (sc[i].type === 'topic') {
title = sc[i].title;
desc = sc[i].desc;
imgUrl = sc[i].imgUrl;
}
} else if (result.type === 3) {
if (sc[i].type === 'photo') {
title = sc[i].title;
desc = sc[i].desc;
imgUrl = sc[i].imgUrl;
} }
});
} getWX(res, title, desc, link, imgUrl, mid,ent_code,thread._id,type,thread);
}
link = link +'&uId='+uId;
getWX(res, title, desc, link, imgUrl, mid,ent_code,result._id,type,result);
} else {
cont(err);
} }
}); });
} else { } else {
//如果有分享设置,则返回分享设置 //获取个性化文章分享设置
forumShareService.getById(result.share, function(err, forumShare) { forumShareService.getById(thread.share, function(err, forumShare) {
//1、文章 2、话题 3、照片墙 if(err || !forumShare){
//文章个性化分享设置不存在
console.error(err || '文章个性化分享设置不存在');
if (result.type === 1 || (result.type === 2 && result.level === 2)) { res.json(_.assign({result:false},returnCode.DATA_NOTEXITS));
link = link + 'thread&infoId=' + result.info + '&ent_code=' + ent_code + '&id=' + result._id; }else{
//key = 'article'; getWX(res, forumShare.title, forumShare.description, link, forumShare.icon, mid,ent_code,thread._id,type,thread);
} else if (result.type === 2 && result.level === 1) {
link = link + 'topicList&infoId=' + result.info + '&ent_code=' + ent_code + '&id=' + result._id;
//key = 'topic';
} else if (result.type === 3) {
link = link + 'photoList&infoId=' + result.info + '&ent_code=' + ent_code + '&id=' + result._id;
//key = 'photo';
} }
link = link +'&uId='+uId;
getWX(res, forumShare.title, forumShare.description, link, forumShare.icon, mid,ent_code,result._id,type,result);
}); });
} }
}
}).fail(function(cont, err) { }).fail(function(cont, err) {
console.error(err); console.error(err);
res.json({ res.json(_.assign({result:false},returnCode.WRONG_PARAM));
result: false,
code: '10002'
});
}); });
} }
req.session.mobileForumUser.share_log = share_log;
});
var getClientIP = function(req) {
var ipAddress;
var headers = req.headers;
var forwardedIpsStr = headers['x-real-ip'] || headers['x-forwarded-for'];
if (forwardedIpsStr) {
ipAddress = forwardedIpsStr;
} else { } else {
res.json({ ipAddress = null;
result: false,
code: '10002'
});
} }
}); if (!ipAddress) {
ipAddress = req.connection.remoteAddress;
}
return ipAddress;
};
//微信分享接口 //微信分享朋友圈接口
router.get('/:ent_code/timeline', function(req, res, next) { router.get('/:ent_code/share/timeline', function(req, res, next) {
var mid = req.session.openUser.mid, var mid = req.session.openUser.mid,
action = 'share_timeline', action = 'share_timeline',
ent_code = req.session.user.ent_code; ent_code = req.session.user.ent_code,
user = req.session.mobileForumUser.userId,
open_id = req.session.mobileForumUser.openId,
share_type = 1,
ip = getClientIP(req),
userAgent = req.headers['user-agent'];
httpService.createShareLog(req);
httpService.sendRequest(ent_code,mid,action); httpService.sendRequest(ent_code,mid,action);
res.status(200).end(); res.status(200).end();
}); });
//微信分享单人接口
router.get('/:ent_code/share/singlemessage', function(req, res, next) {
var mid = req.session.openUser.mid,
action = 'share_singlemessage',
ent_code = req.session.user.ent_code,
user = req.session.mobileForumUser.userId,
open_id = req.session.mobileForumUser.openId,
share_type = 1,
ip = getClientIP(req),
userAgent = req.headers['user-agent'];
httpService.createShareLog(req);
httpService.sendRequest(ent_code,mid,action);
res.status(200).end();
});
...@@ -9,30 +9,66 @@ var ForumShareLogSchema = new Schema({ ...@@ -9,30 +9,66 @@ var ForumShareLogSchema = new Schema({
require: true, require: true,
index: true index: true
}, },
user: { //分享 user: { //访问
type: Schema.Types.ObjectId, type: String,
require: true, require: true,
index: true, index: true,
ref: 'ForumUser' ref: 'ForumUser'
}, },
thread: { //分享内容 mid: { //会员ID
type: Schema.Types.ObjectId, type: Number,
require: true, require: true,
index: true, index: true
ref: 'ForumThread'
}, },
destination: { //分享的目标,微信好友,微信朋友圈,QQ空间,微博等等 open_id: { //微信公众号唯一识别
type: String, type: String,
require: true, require: true,
index: true index: true
}, },
ip: { //IP地址 info: { //板块ID
type: String,
index: true,
ref: 'ForumInfo'
},
thread: { //话题、文章、照片墙的ID
type: String, type: String,
index: true,
ref: 'forumThread'
},
type: { //页面:1.主页 2.板块 3.文章
type: Number,
index: true,
require: true require: true
}, },
created: { //分享时间 thread_type: { //文章类型:1.话题 2.文章 3.照片墙 4.子文章
type: Date, type: Number,
index: true
},
share_type:{ //分享类型:1.微信朋友圈 2.微信单人 3.微信组
type: Number,
index: true,
require: true
},
p_thread: { //父文章ID
type: String,
index: true,
ref: 'forumThread'
},
source: { //分享者
type: String,
require: true, require: true,
index: true,
ref: 'ForumUser'
},
ip: { //IP地址
type: String
},
userAgent: {
type: String
},
created: {
type: Date,
required: true,
default: Date.now default: Date.now
} }
}, { }, {
......
...@@ -16,8 +16,9 @@ var mongoose = require('mongoose'), ...@@ -16,8 +16,9 @@ var mongoose = require('mongoose'),
moment = require('moment'), moment = require('moment'),
then = require('thenjs'); then = require('thenjs');
var ForumPVLog = mongoose.model('ForumPVLog'); var ForumPVLog = mongoose.model('ForumPVLog'),
var ForumUVLog = mongoose.model('ForumUVLog'); ForumUVLog = mongoose.model('ForumUVLog'),
ForumShareLog = mongoose.model('ForumShareLog');
exports.sendRequest = function(ent_code, mid, action, messageid, integral, exp) { exports.sendRequest = function(ent_code, mid, action, messageid, integral, exp) {
var obj = { var obj = {
tag: 'member', tag: 'member',
...@@ -51,7 +52,6 @@ var getClientIP = function(req) { ...@@ -51,7 +52,6 @@ var getClientIP = function(req) {
}; };
exports.createLog = function(req,source,info,type,thread_type,thread,p_thread) { exports.createLog = function(req,source,info,type,thread_type,thread,p_thread) {
var logObj = { var logObj = {
ent_code: req.session.user.ent_code, ent_code: req.session.user.ent_code,
source:source, source:source,
...@@ -97,3 +97,17 @@ exports.createLog = function(req,source,info,type,thread_type,thread,p_thread) { ...@@ -97,3 +97,17 @@ exports.createLog = function(req,source,info,type,thread_type,thread,p_thread) {
console.error('没有日志信息'); console.error('没有日志信息');
} }
}; };
exports.createShareLog = function(req) {
var logObj = req.session.mobileForumUser.share_log;
logObj.source = req.session.mobileForumUser.source;
if(logObj){
then(function(cont){
var share_log_model = new ForumShareLog(logObj);
share_log_model.save(cont);
}).fail(function(cont,err){
console.error(err);
});
}
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment