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

分享 - 功能添加

parent a1b4cb97
'use strict';
var express = require('express'),
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 forumShareConfigService = require('../../service/forumShareConfigService');
var forumShareService = require('../../service/forumShareService');
var config = require('../../../config/config');
var httpService=require('../../service/httpService');
var then = require('thenjs');
var forumInfoService = require('../../service/forumInfoService');
var forumThreadService = require('../../service/forumThreadService'),
returnCode = require('../../utils/returnCode'),
forumShareConfigService = require('../../service/forumShareConfigService'),
forumShareService = require('../../service/forumShareService'),
config = require('../../../config/config'),
httpService=require('../../service/httpService'),
forumInfoService = require('../../service/forumInfoService');
module.exports = function(app) {
app.use('/v1/forum', router);
......@@ -24,19 +24,17 @@ function write(res, title, desc, link, imgUrl, mid, ent_code) {
'Cache-Control': 'no-cache',
'Pragma': 'no-cache'
});
// result.debug = 'true' === isDebug ? true : false;
// result.jsApiList = jsApiList;
res.write('$(function(){');
// var title = type+'类型分享',
// link = 'http://sandbox.wxpai.cn/app/aubywx/100041',
// imgUrl = 'http://dev.fs.wxpai.cn/upload/100001_2015410_1428671539587_40.jpg',
// desc = '测试描述';
// var string = '';
var success = '$.ajax({'+
'type:\'GET\','+
'url: \'/v1/forum/'+ent_code+'/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 success_singlemessage = '$.ajax({'+
'type:\'GET\','+
'url: \'/v1/forum/'+ent_code+'/share/singlemessage\''+
'});';
var success_timeline = '$.ajax({'+
'type:\'GET\','+
'url: \'/v1/forum/'+ent_code+'/share/timeline\''+
'});';
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.error(function(res){console.log(res);});');
......@@ -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) {
var rs = {};
var type = req.query.type;
var id = req.query.id;
var uId= req.session.mobileForumUser.userId;
var mid = 0;
req.session.mobileForumUser = {
userId :123,
openId:'123123'
};
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) {
mid = req.session.openUser.mid;
}
var ent_code = req.params.ent_code;
var link = '/app/forum/' + ent_code + '/index?pageUrl=';
if (type) {
//板块微信接口
if (type === 'Info') {
then(function(cont) {
forumShareConfigService.getByKey(ent_code, 'share', function(err, result) {
var title = '',
desc = '',
imgUrl = '';
if (result) {
result = result.meta_value;
for (var i = 0; i < result.length; i++) {
if (result[i].type === 'info') {
title = result[i].title;
desc = result[i].desc;
imgUrl = result[i].imgUrl;
}
}
if (id) {
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);
} else {
cont(err);
}
});
}).fail(function(cont, err) {
delete req.session.mobileForumUser.share_log;
//板块微信接口
if(!type || !id){
console.error('请求参数错误')
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) {
if(err){
console.error(err);
res.json({
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) {
//获得文章
forumThreadService.getById(id, function(err, result) {
if (result) {
cont(null, result);
} else {
cont(err);
}
});
}).then(function(cont, result) {
//如果没有share值则用默认值
if (!result.share||result.share_type==2) {
res.json(_.assign({result:false},returnCode.BUSY));
}else{
var title = '',desc = '',imgUrl = '';
if (result) {
//匹配板块默认分享设置
result = result.meta_value;
_.forEach(result,function(e){
if (e.type === 'info') {
title = e.title;
desc = e.desc;
imgUrl = e.imgUrl;
return;
}
});
link = link + 'index&infoId='+ id+'&uId='+uId;
getWX(res, title, desc, link, imgUrl, mid,ent_code, id,type,null);
}else{
//没找到板块默认分享设置
console.error('info默认分享设置不存在');
res.json(_.assign({result:false},returnCode.DATA_NOTEXITS));
}
}
});
}else if (type === 'Thread') {
share_log.type = 3;
share_log.thread = id;
then(function(cont) {
//获得文章
forumThreadService.getById(id,cont);
}).then(function(cont, thread) {
if(!thread){
//文章个性化分享设置不存在
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;
//获取分享设置
if (!thread.share || thread.share_type==2) {
//获取默认文章分享设置
forumShareConfigService.getByKey(ent_code, 'share', function(err, sc) {
var title = '',
desc = '',
imgUrl = '';
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、照片墙
var title = '',desc = '',imgUrl = '';
if(err || !sc){
cont(err || share_type+'默认分享设置不存在');
}else{
sc = sc.meta_value;
for (var i = 0; i < sc.length; i++) {
if (result.type === 1 || (result.type === 2 && result.level === 2)) {
if (sc[i].type === 'article') {
title = sc[i].title;
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;
}
//设置分享信息
_.forEach(sc,function(e){
if(share_type === e.type){
title = e.title;
desc = e.desc;
imgUrl = e.imgUrl;
return;
}
}
link = link +'&uId='+uId;
getWX(res, title, desc, link, imgUrl, mid,ent_code,result._id,type,result);
} else {
cont(err);
});
getWX(res, title, desc, link, imgUrl, mid,ent_code,thread._id,type,thread);
}
});
} else {
//如果有分享设置,则返回分享设置
forumShareService.getById(result.share, function(err, forumShare) {
//1、文章 2、话题 3、照片墙
if (result.type === 1 || (result.type === 2 && result.level === 2)) {
link = link + 'thread&infoId=' + result.info + '&ent_code=' + ent_code + '&id=' + result._id;
//key = 'article';
} 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';
//获取个性化文章分享设置
forumShareService.getById(thread.share, function(err, forumShare) {
if(err || !forumShare){
//文章个性化分享设置不存在
console.error(err || '文章个性化分享设置不存在');
res.json(_.assign({result:false},returnCode.DATA_NOTEXITS));
}else{
getWX(res, forumShare.title, forumShare.description, link, forumShare.icon, mid,ent_code,thread._id,type,thread);
}
link = link +'&uId='+uId;
getWX(res, forumShare.title, forumShare.description, link, forumShare.icon, mid,ent_code,result._id,type,result);
});
}
}).fail(function(cont, err) {
console.error(err);
res.json({
result: false,
code: '10002'
});
});
}
} else {
res.json({
result: false,
code: '10002'
}
}).fail(function(cont, err) {
console.error(err);
res.json(_.assign({result:false},returnCode.WRONG_PARAM));
});
}
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 {
ipAddress = null;
}
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,
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);
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();
});
......@@ -5,36 +5,72 @@ var mongoose = require('mongoose'),
//分享日志
var ForumShareLogSchema = new Schema({
ent_code: {
type: Number,
require: true,
index: true
},
user: { //分享者
type: Schema.Types.ObjectId,
require: true,
index: true,
ref: 'ForumUser'
},
thread: { //分享内容
type: Schema.Types.ObjectId,
require: true,
index: true,
ref: 'ForumThread'
},
destination: { //分享的目标,微信好友,微信朋友圈,QQ空间,微博等等
type: String,
require: true,
index: true
},
ip: { //IP地址
type: String,
require: true
},
created: { //分享时间
type: Date,
require: true,
default: Date.now
}
type: Number,
require: true,
index: true
},
user: { //访问者
type: String,
require: true,
index: true,
ref: 'ForumUser'
},
mid: { //会员ID
type: Number,
require: true,
index: true
},
open_id: { //微信公众号唯一识别
type: String,
require: true,
index: true
},
info: { //板块ID
type: String,
index: true,
ref: 'ForumInfo'
},
thread: { //话题、文章、照片墙的ID
type: String,
index: true,
ref: 'forumThread'
},
type: { //页面:1.主页 2.板块 3.文章
type: Number,
index: true,
require: true
},
thread_type: { //文章类型:1.话题 2.文章 3.照片墙 4.子文章
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,
index: true,
ref: 'ForumUser'
},
ip: { //IP地址
type: String
},
userAgent: {
type: String
},
created: {
type: Date,
required: true,
default: Date.now
}
}, {
'collection': 'pisns_forum_share_log'
});
......
......@@ -16,8 +16,9 @@ var mongoose = require('mongoose'),
moment = require('moment'),
then = require('thenjs');
var ForumPVLog = mongoose.model('ForumPVLog');
var ForumUVLog = mongoose.model('ForumUVLog');
var ForumPVLog = mongoose.model('ForumPVLog'),
ForumUVLog = mongoose.model('ForumUVLog'),
ForumShareLog = mongoose.model('ForumShareLog');
exports.sendRequest = function(ent_code, mid, action, messageid, integral, exp) {
var obj = {
tag: 'member',
......@@ -51,7 +52,6 @@ var getClientIP = function(req) {
};
exports.createLog = function(req,source,info,type,thread_type,thread,p_thread) {
var logObj = {
ent_code: req.session.user.ent_code,
source:source,
......@@ -97,3 +97,17 @@ exports.createLog = function(req,source,info,type,thread_type,thread,p_thread) {
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