Commit d046aa8e authored by 刘文胜's avatar 刘文胜

社区分享

parent ab6de49e
......@@ -3,6 +3,7 @@ var express = require('express'),
router = express.Router(),
_ = require('lodash'),
then = require('thenjs'),
request=require('request'),
mongoose = require('mongoose');
var forumThreadService = require('../../service/forumThreadService'),
......@@ -236,11 +237,15 @@ router.get('/:ent_code/share/singlemessage', function(req, res, next) {
}
});
});
function getWXV2(res, title, desc, link, imgUrl, mid,ent_code, id,type,forumThread) {
function getWXV2(res, title, desc, link, imgUrl, mid,ent_code, id,type,forumThread,fullurl) {
//['{板块名称}','{文章标题}','{话题名称}','{照片墙标题}'];
//share_type:是否使用默认设置:1使用默认分享设置 2.使用自己的分享设置
link = config.service + link + "&mid=" + mid;
if(fullurl){
link = link + "&mid=" + mid;
}else{
link = config.service + link + "&mid=" + mid;
}
if(type==="Info") {
forumInfoService.getInfoById(id, function(err, forumInfo) {
if (err || forumInfo < 1) {
......@@ -298,106 +303,143 @@ router.get('/sharev2.js', function(req, res, next) {
var title = '',desc = '',imgUrl = '';
//板块微信接口
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;
//替换分享log内容
req.session.shareLog = share_log;
forumInfoService.getInfoById(id, function(err, forumInfo) {
if (err) {
console.error(err);
res.json(_.assign({result:false},returnCode.BUSY));
}else{
if(forumInfo.share_type && forumInfo.share_type == 1){
var shareInfo = forumInfo.share || {};
title = shareInfo.title,desc = shareInfo.description,imgUrl = shareInfo.icon;
}else{
title = forumInfo.name,desc = forumInfo.description,imgUrl = forumInfo.icon;
}
link = link + 'index&infoId='+ id+'&uId='+uId;
getWXV2(res, title, desc, link, imgUrl, mid,ent_code, id,type,null);
if(type && type === 'index'){
then.parallel([
function(cont){
//type为index时的,默认link
link = config.service + '/app/forum/' + ent_code + '/mediashequn'+'?uId='+uId;
request.get({url:config.rest_api+'/v1.0/internal/forum/config/enter/byentcode?entCode='+ent_code,body:{},json:true}, function (e, r, result) {
if(result.data){
if(result.data.indexOf('?') === -1){
link = result.data + '?uId='+ uId;
}else{
link = result.data + '&uId='+ uId;
}
}
cont();
});
},function(cont){
request.get({url:config.rest_api+'/v1.0/internal/forum/config/share/byentcode?entCode='+ent_code,body:{},json:true}, function (e, r, result) {
if(result.data){
try{
var obj = JSON.parse(result.data);
title = obj.shareTitle || '';
desc = obj.shareDescription || '';
imgUrl = obj.sharePictureUrl || '';
}catch(err){
console.log(err);
}
}
cont();
});
}
]).then(function(cont, datas) {
getWXV2(res, title, desc, link, imgUrl, mid,ent_code, id,type,null,true);
}).fail(function(cont, err) {
res.json(_.assign({result:false},returnCode.WRONG_PARAM));
});
}else if (type === 'Thread') {
then(function(cont) {
//获得文章
forumThreadService.getThreadByIdSimple({_id:id},cont);
}).then(function(cont, thread) {
if(!thread){
//文章不存在
cont('分享文章不存在');
}else{
share_log.type = 3;
share_log.thread = id;
share_log.p_thread = thread.pid;
share_log.info = thread.info._id || thread.info;
share_log.thread_type = thread.pid?4:thread.type;
//替换分享log内容
req.session.shareLog = share_log;
}else{
//板块微信接口
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;
//替换分享log内容
req.session.shareLog = share_log;
//组装分享路径与判断分享类型
var baseParamsUrl = '&infoId=' + thread.info._id + '&ent_code=' + ent_code + '&tId=' + thread._id +'&uId='+uId;
var share_type = '';
switch(thread.type){
case 1://文章
link = link + 'detail';
share_type = 'article';
title = thread.title == '发话题'?"":thread.title;
break;
case 2://话题
if(thread.level === 2){
link += 'detail';
share_type = 'article';
title = thread.pid.title;
}else if(thread.level === 1){
link += 'topic';
share_type = 'topic';
title = thread.title;
}
break;
case 3://照片墙
if(thread.level === 2){
link += 'photoDetail';
share_type = 'photo';
title = thread.pid.title;
}else if(thread.level === 1){
link += 'photoWall';
share_type = 'photo';
title = thread.title;
}
break;
}
desc = thread.content?thread.content.replace(/<\/?.+?>/g,"").replace(/&nbsp;/g,"").replace(/\t/g,"").replace(/\n/g,""):'';
// 分享图标:帖子带图片则使用第一张图片,否则使用发帖者的头像
if(thread.images && thread.images[0]){
imgUrl = thread.images[0].urlFileName;
forumInfoService.getInfoById(id, function(err, forumInfo) {
if (err) {
console.error(err);
res.json(_.assign({result:false},returnCode.BUSY));
}else{
imgUrl = thread.from.displayIcon || thread.from.icon || thread.info.icon;
if(forumInfo.share_type && forumInfo.share_type == 1){
var shareInfo = forumInfo.share || {};
title = shareInfo.title,desc = shareInfo.description,imgUrl = shareInfo.icon;
}else{
title = forumInfo.name,desc = forumInfo.description,imgUrl = forumInfo.icon;
}
link = link + 'index&infoId='+ id+'&uId='+uId;
getWXV2(res, title, desc, link, imgUrl, mid,ent_code, id,type,null);
}
});
link += baseParamsUrl;
//获取分享设置
if (thread.share_type!==2) {
//文章默认分享
getWXV2(res, title, desc, link, imgUrl, mid,ent_code,thread._id,type,thread);
}else if(thread.share){
getWXV2(res, thread.share.title, thread.share.description, link, thread.share.icon, mid,ent_code,thread._id,type,thread);
} else {
getWXV2(res, title, desc, link, imgUrl, mid,ent_code,thread._id,type,thread);
}else if (type === 'Thread') {
then(function(cont) {
//获得文章
forumThreadService.getThreadByIdSimple({_id:id},cont);
}).then(function(cont, thread) {
if(!thread){
//文章不存在
cont('分享文章不存在');
}else{
share_log.type = 3;
share_log.thread = id;
share_log.p_thread = thread.pid;
share_log.info = thread.info._id || thread.info;
share_log.thread_type = thread.pid?4:thread.type;
//替换分享log内容
req.session.shareLog = share_log;
//组装分享路径与判断分享类型
var baseParamsUrl = '&infoId=' + thread.info._id + '&ent_code=' + ent_code + '&tId=' + thread._id +'&uId='+uId;
var share_type = '';
switch(thread.type){
case 1://文章
link = link + 'detail';
share_type = 'article';
title = thread.title == '发话题'?"":thread.title;
break;
case 2://话题
if(thread.level === 2){
link += 'detail';
share_type = 'article';
title = thread.pid.title;
}else if(thread.level === 1){
link += 'topic';
share_type = 'topic';
title = thread.title;
}
break;
case 3://照片墙
if(thread.level === 2){
link += 'photoDetail';
share_type = 'photo';
title = thread.pid.title;
}else if(thread.level === 1){
link += 'photoWall';
share_type = 'photo';
title = thread.title;
}
break;
}
desc = thread.content?thread.content.replace(/<\/?.+?>/g,"").replace(/&nbsp;/g,"").replace(/\t/g,"").replace(/\n/g,""):'';
// 分享图标:帖子带图片则使用第一张图片,否则使用发帖者的头像
if(thread.images && thread.images[0]){
imgUrl = thread.images[0].urlFileName;
}else{
imgUrl = thread.from.displayIcon || thread.from.icon || thread.info.icon;
}
link += baseParamsUrl;
//获取分享设置
if (thread.share_type!==2) {
//文章默认分享
getWXV2(res, title, desc, link, imgUrl, mid,ent_code,thread._id,type,thread);
}else if(thread.share){
getWXV2(res, thread.share.title, thread.share.description, link, thread.share.icon, mid,ent_code,thread._id,type,thread);
} else {
getWXV2(res, title, desc, link, imgUrl, mid,ent_code,thread._id,type,thread);
}
}
}
}).fail(function(cont, err) {
console.error(err);
res.json(_.assign({result:false},returnCode.WRONG_PARAM));
});
}).fail(function(cont, err) {
console.error(err);
res.json(_.assign({result:false},returnCode.WRONG_PARAM));
});
}
}
});
\ No newline at end of file
......@@ -9,6 +9,7 @@ var config = {
app: {
name: 'pisns-forum-api'
},
rest_api:'http://localhost:8080',
service:'http://localhost',
port: 3011,
mongodb: {
......@@ -38,6 +39,7 @@ var config = {
app: {
name: 'pisns-forum-api'
},
rest_api:'http://rest.wxpai.cn',
service:'http://sandbox.wxpai.cn',
port: 3011,
mongodb: {
......@@ -67,6 +69,7 @@ var config = {
app: {
name: 'pisns-forum-api'
},
rest_api:'https://rest.wxpai.cn',
service:'http://piplus.wxpai.cn',
port: 3011,
......
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