Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
pisns-forum-api
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
scrmGroup
pisns-forum-api
Commits
134b62e3
Commit
134b62e3
authored
Apr 14, 2015
by
邓军
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
微信分享接口
parent
a04eda39
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
712 additions
and
399 deletions
+712
-399
forumShare.js
app/controllers/mobile/forumShare.js
+171
-0
forumShareConfig.js
app/models/forumShareConfig.js
+26
-0
forumShareConfigService.js
app/service/forumShareConfigService.js
+30
-0
forumShareService.js
app/service/forumShareService.js
+17
-0
forumThreadService.js
app/service/forumThreadService.js
+468
-399
No files found.
app/controllers/mobile/forumShare.js
0 → 100644
View file @
134b62e3
'use strict'
;
var
express
=
require
(
'express'
),
router
=
express
.
Router
(),
returnCode
=
require
(
'../../utils/returnCode'
),
_
=
require
(
'lodash'
);
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
then
=
require
(
'thenjs'
);
//var forumInfoService = require('../../service/forumInfoService');
module
.
exports
=
function
(
app
)
{
app
.
use
(
'/v1/forum'
,
router
);
};
function
getWX
(
res
,
title
,
desc
,
link
,
imgUrl
)
{
link
=
config
.
service
+
link
;
res
.
writeHead
(
200
,
{
'Content-Type'
:
'text/javascript'
,
'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
string
=
'wx.onMenuShareTimeline({'
+
'
\
"title
\
":
\
"'
+
title
+
'
\
",'
+
'
\
"link
\
":
\
"'
+
link
+
'
\
",'
+
'
\
"imgUrl
\
":
\
"'
+
imgUrl
+
'
\
", '
+
'success: function(){},'
+
'cancel: function () {}'
+
' });'
+
' wx.onMenuShareAppMessage({'
+
'
\
"title
\
":
\
"'
+
title
+
'
\
", '
+
'
\
"desc
\
":
\
"'
+
desc
+
'
\
", '
+
'
\
"link
\
":
\
"'
+
link
+
'
\
", '
+
'
\
"imgUrl
\
":
\
"'
+
imgUrl
+
'
\
", '
+
'
\
"type
\
":
\'\'
,'
+
'
\
"dataUrl
\
":
\'\'
, '
+
'success: function(){},'
+
'cancel: function () { '
+
'}'
+
' });'
res
.
write
(
'wx.ready(function(){'
+
string
+
'});'
);
res
.
write
(
'wx.error(function(res){console.log(res);});'
);
res
.
write
(
'});'
);
res
.
end
();
}
//微信分享接口
router
.
get
(
'/:ent_code/share.js'
,
function
(
req
,
res
,
next
)
{
var
rs
=
{};
var
type
=
req
.
query
.
type
;
var
id
=
req
.
query
.
id
;
var
ent_code
=
req
.
params
.
ent_code
;
var
link
=
'/app/forum/'
+
ent_code
+
'/index/'
;
if
(
type
)
{
//板块微信接口
if
(
type
===
'Info'
)
{
then
(
function
(
cont
)
{
forumShareConfigService
.
getByEntCode
(
ent_code
,
function
(
err
,
result
)
{
var
title
=
''
,
desc
=
''
,
imgUrl
=
''
;
if
(
result
)
{
for
(
var
i
=
0
;
i
<
result
.
length
;
i
++
)
{
if
(
result
[
i
].
meta_key
===
'share_info_title'
)
{
title
=
result
[
i
].
meta_value
;
}
else
if
(
result
[
i
].
meta_key
===
'share_info_description'
)
{
desc
=
result
[
i
].
meta_value
;
}
else
if
(
result
[
i
].
meta_key
===
'share_info_icon'
)
{
imgUrl
=
result
[
i
].
meta_value
;
}
}
link
=
link
+
'index.html?infoId='
+
id
;
getWX
(
res
,
desc
,
title
,
link
,
imgUrl
);
}
else
{
cont
(
err
);
}
});
}).
fail
(
function
(
cont
,
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
)
{
forumShareConfigService
.
getByEntCode
(
ent_code
,
function
(
err
,
sc
)
{
var
title
=
''
,
desc
=
''
,
imgUrl
=
''
;
if
(
sc
)
{
var
key
=
''
;
//1、文章 2、话题 3、照片墙
if
(
result
.
type
===
1
||
(
result
.
type
===
2
&&
result
.
level
===
2
))
{
link
=
link
+
'main.html?infoId='
+
result
.
info
+
'&ent_code='
+
ent_code
+
'&id='
+
result
.
_id
;
key
=
'article'
;
}
else
if
(
result
.
type
===
2
&&
result
.
level
===
1
)
{
link
=
link
+
'hot_topic.html?infoId='
+
result
.
info
+
'&ent_code='
+
ent_code
+
'&id='
+
result
.
_id
;
key
=
'topic'
;
}
else
if
(
result
.
type
===
3
)
{
link
=
link
+
'photo.html?infoId='
+
result
.
info
+
'&ent_code='
+
ent_code
+
'&id='
+
result
.
_id
;
key
=
'photo'
;
}
for
(
var
i
=
0
;
i
<
sc
.
length
;
i
++
)
{
if
(
sc
[
i
].
meta_key
===
'share_'
+
key
+
'_title'
)
{
title
=
sc
[
i
].
meta_value
;
}
else
if
(
sc
[
i
].
meta_key
===
'share_'
+
key
+
'_description'
)
{
desc
=
sc
[
i
].
meta_value
;
}
else
if
(
sc
[
i
].
meta_key
===
'share_'
+
key
+
'_icon'
)
{
imgUrl
=
sc
[
i
].
meta_value
;
}
}
getWX
(
res
,
title
,
desc
,
link
,
imgUrl
);
}
else
{
cont
(
err
);
}
});
}
else
{
//如果有分享设置,则返回分享设置
forumShareService
.
getById
(
result
.
share
,
function
(
err
,
forumShare
)
{
//1、文章 2、话题 3、照片墙
if
(
result
.
type
===
1
||
(
result
.
type
===
2
&&
result
.
level
===
2
))
{
link
=
link
+
'main.html?infoId='
+
result
.
info
+
'&ent_code='
+
ent_code
+
'&id='
+
result
.
_id
;
// key = 'article';
}
else
if
(
result
.
type
===
2
&&
result
.
level
===
1
)
{
link
=
link
+
'hot_topic.html?infoId='
+
result
.
info
+
'&ent_code='
+
ent_code
+
'&id='
+
result
.
_id
;
//key = 'topic';
}
else
if
(
result
.
type
===
3
)
{
link
=
link
+
'photo.html?infoId='
+
result
.
info
+
'&ent_code='
+
ent_code
+
'&id='
+
result
.
_id
;
//key = 'photo';
}
getWX
(
res
,
forumShare
.
title
,
forumShare
.
description
,
link
,
forumShare
.
icon
);
});
}
}).
fail
(
function
(
cont
,
err
)
{
console
.
error
(
err
);
res
.
json
({
result
:
false
,
code
:
'10002'
});
});
}
}
else
{
res
.
json
({
result
:
false
,
code
:
'10002'
});
}
});
app/models/forumShareConfig.js
0 → 100644
View file @
134b62e3
'use strict'
;
var
mongoose
=
require
(
'mongoose'
),
Schema
=
mongoose
.
Schema
;
//分享内容
var
ForumShareConfigSchema
=
new
Schema
({
ent_code
:
{
type
:
Number
,
require
:
true
,
index
:
true
},
meta_key
:
{
type
:
String
,
index
:
true
},
meta_value
:
{
type
:
Object
,
require
:
true
}
},
{
'collection'
:
'pisns_forum_share_config'
});
module
.
exports
=
mongoose
.
model
(
'ForumShareConfig'
,
ForumShareConfigSchema
);
app/service/forumShareConfigService.js
0 → 100644
View file @
134b62e3
'use strict'
;
var
mongoose
=
require
(
'mongoose'
);
var
ForumShareConfig
=
mongoose
.
model
(
'ForumShareConfig'
);
var
async
=
require
(
'async'
);
//创建配置信息
exports
.
createForumShareConfig
=
function
(
entity
,
callback
){
var
forumShareConfig
=
new
ForumShareConfig
(
entity
);
forumShareConfig
.
save
(
function
(
err
,
forumShareConfig
)
{
if
(
err
)
{
console
.
error
(
err
);
callback
(
err
,
null
);
}
else
{
callback
(
null
,
forumShareConfig
);
}
});
};
//根据ent_code获取配置信息
exports
.
getByEntCode
=
function
(
ent_code
,
callback
){
ForumShareConfig
.
find
({
ent_code
:
ent_code
},
function
(
err
,
result
){
if
(
err
)
{
console
.
error
(
err
);
callback
(
err
,
null
);
}
else
{
callback
(
null
,
result
);
}
});
};
\ No newline at end of file
app/service/forumShareService.js
0 → 100644
View file @
134b62e3
'use strict'
;
var
mongoose
=
require
(
'mongoose'
);
var
ForumShare
=
mongoose
.
model
(
'ForumShare'
);
//根据ID获取分享设置
exports
.
getById
=
function
(
id
,
callback
)
{
ForumShare
.
findOne
({
_id
:
id
},
function
(
err
,
result
)
{
if
(
err
)
{
console
.
error
(
err
);
callback
(
err
,
null
);
}
else
{
callback
(
null
,
result
);
}
});
}
\ No newline at end of file
app/service/forumThreadService.js
View file @
134b62e3
...
...
@@ -8,457 +8,526 @@ var forumCommentService = require('./forumCommentService');
var
async
=
require
(
'async'
);
//创建文章
exports
.
createThread
=
function
(
entity
,
callback
)
{
var
forum
=
new
ForumThread
(
entity
);
forum
.
save
(
function
(
err
,
forum
)
{
if
(
err
)
{
console
.
error
(
err
);
callback
(
err
,
null
);
}
else
{
callback
(
null
,
forum
);
}
});
exports
.
createThread
=
function
(
entity
,
callback
)
{
var
forum
=
new
ForumThread
(
entity
);
forum
.
save
(
function
(
err
,
forum
)
{
if
(
err
)
{
console
.
error
(
err
);
callback
(
err
,
null
);
}
else
{
callback
(
null
,
forum
);
}
});
};
//根据ID获取文章记录,不查评论,子话题
exports
.
getById
=
function
(
id
,
callback
)
{
ForumThread
.
findOne
({
_id
:
id
},
function
(
err
,
result
)
{
if
(
err
)
{
console
.
error
(
err
);
callback
(
err
,
null
);
}
else
{
callback
(
null
,
result
);
}
});
}
//根据ID获取文章
exports
.
getThreadById
=
function
(
tid
,
callback
)
{
async
.
parallel
([
function
(
cb
)
{
var
conditions
=
{
_id
:
tid
};
ForumThread
.
find
(
conditions
).
populate
(
'from'
).
populate
(
'info'
).
exec
(
function
(
err
,
docs
)
{
if
(
err
)
{
console
.
error
(
err
);
callback
(
err
,
null
);
}
else
{
var
thread
=
{};
if
(
docs
&&
docs
.
length
>
0
)
{
thread
=
docs
[
0
];
cb
(
null
,
thread
);
}
else
{
cb
(
null
,
thread
);
}
}
});
},
function
(
cb
)
{
var
conditions
=
{
thread
:
tid
,
level
:
'1'
};
//获取最新10条评论
forumCommentService
.
getAllComment
(
conditions
,
1
,
10
,
function
(
err
,
results
)
{
if
(
err
)
{
console
.
log
(
err
);
cb
(
err
,
null
);
}
else
{
cb
(
null
,
results
);
}
});
},
function
(
cb
)
{
//获取话题子文章列表
var
conditions
=
{
pid
:
tid
,
type
:
2
};
getAllThreadByFidHelp
(
conditions
,
1
,
10
,
null
,
function
(
err
,
threads
)
{
if
(
err
)
{
console
.
error
(
err
);
cb
(
err
,
null
);
}
else
{
// console.log(threads);
cb
(
null
,
threads
);
}
});
},
function
(
cb
)
{
//获取照片墙最新文章列表
var
conditions
=
{
pid
:
tid
,
type
:
3
};
getAllThreadByFidHelp
(
conditions
,
1
,
10
,
'-created'
,
function
(
err
,
threads
)
{
if
(
err
)
{
console
.
error
(
err
);
cb
(
err
,
null
);
}
else
{
cb
(
null
,
threads
);
}
});
},
function
(
cb
)
{
//获取照片墙最热文章列表
var
conditions
=
{
pid
:
tid
,
type
:
3
};
getAllThreadByFidHelp
(
conditions
,
1
,
10
,
'-praise_count'
,
function
(
err
,
threads
)
{
if
(
err
)
{
console
.
error
(
err
);
cb
(
err
,
null
);
}
else
{
cb
(
null
,
threads
);
}
});
},
function
(
cb
)
{
//获取发帖人数
ForumThread
.
aggregate
(
{
$match
:
{
pid
:
mongoose
.
Types
.
ObjectId
(
tid
)
}
},
{
$group
:
{
_id
:
{
from
:
'$from'
},
count
:
{
$sum
:
1
}
}
},
function
(
err
,
data
){
if
(
err
)
{
cb
(
err
,
null
);
}
else
{
cb
(
null
,
data
.
length
);
}
});
}
],
function
(
err
,
results
)
{
if
(
err
)
{
callback
(
err
,
null
);
}
else
{
var
thread
=
results
[
0
];
var
comments
=
results
[
1
];
var
subThreads
=
results
[
2
]
||
[];
//话题子文章列表
var
latestPhotos
=
results
[
3
]
||
[];
//最新照片墙列表
var
hotPhotos
=
results
[
4
]
||
[];
//最热照片墙列表
var
canyuPeopleCount
=
results
[
5
]
||
0
;
//参与人数
var
threadObj
=
thread
.
toObject
();
threadObj
.
comments
=
comments
;
threadObj
.
subThreads
=
subThreads
;
threadObj
.
latestPhotos
=
latestPhotos
;
threadObj
.
hotPhotos
=
hotPhotos
;
threadObj
.
canyuPeopleCount
=
canyuPeopleCount
;
callback
(
null
,
threadObj
);
}
});
exports
.
getThreadById
=
function
(
tid
,
callback
)
{
async
.
parallel
([
function
(
cb
)
{
var
conditions
=
{
_id
:
tid
};
ForumThread
.
find
(
conditions
).
populate
(
'from'
).
populate
(
'info'
).
exec
(
function
(
err
,
docs
)
{
if
(
err
)
{
console
.
error
(
err
);
callback
(
err
,
null
);
}
else
{
var
thread
=
{};
if
(
docs
&&
docs
.
length
>
0
)
{
thread
=
docs
[
0
];
cb
(
null
,
thread
);
}
else
{
cb
(
null
,
thread
);
}
}
});
},
function
(
cb
)
{
var
conditions
=
{
thread
:
tid
,
level
:
'1'
};
//获取最新10条评论
forumCommentService
.
getAllComment
(
conditions
,
1
,
10
,
function
(
err
,
results
)
{
if
(
err
)
{
console
.
log
(
err
);
cb
(
err
,
null
);
}
else
{
cb
(
null
,
results
);
}
});
},
function
(
cb
)
{
//获取话题子文章列表
var
conditions
=
{
pid
:
tid
,
type
:
2
};
getAllThreadByFidHelp
(
conditions
,
1
,
10
,
null
,
function
(
err
,
threads
)
{
if
(
err
)
{
console
.
error
(
err
);
cb
(
err
,
null
);
}
else
{
// console.log(threads);
cb
(
null
,
threads
);
}
});
},
function
(
cb
)
{
//获取照片墙最新文章列表
var
conditions
=
{
pid
:
tid
,
type
:
3
};
getAllThreadByFidHelp
(
conditions
,
1
,
10
,
'-created'
,
function
(
err
,
threads
)
{
if
(
err
)
{
console
.
error
(
err
);
cb
(
err
,
null
);
}
else
{
cb
(
null
,
threads
);
}
});
},
function
(
cb
)
{
//获取照片墙最热文章列表
var
conditions
=
{
pid
:
tid
,
type
:
3
};
getAllThreadByFidHelp
(
conditions
,
1
,
10
,
'-praise_count'
,
function
(
err
,
threads
)
{
if
(
err
)
{
console
.
error
(
err
);
cb
(
err
,
null
);
}
else
{
cb
(
null
,
threads
);
}
});
},
function
(
cb
)
{
//获取发帖人数
ForumThread
.
aggregate
({
$match
:
{
pid
:
mongoose
.
Types
.
ObjectId
(
tid
)
}
},
{
$group
:
{
_id
:
{
from
:
'$from'
},
count
:
{
$sum
:
1
}
}
},
function
(
err
,
data
)
{
if
(
err
)
{
cb
(
err
,
null
);
}
else
{
cb
(
null
,
data
.
length
);
}
});
}
],
function
(
err
,
results
)
{
if
(
err
)
{
callback
(
err
,
null
);
}
else
{
var
thread
=
results
[
0
];
var
comments
=
results
[
1
];
var
subThreads
=
results
[
2
]
||
[];
//话题子文章列表
var
latestPhotos
=
results
[
3
]
||
[];
//最新照片墙列表
var
hotPhotos
=
results
[
4
]
||
[];
//最热照片墙列表
var
canyuPeopleCount
=
results
[
5
]
||
0
;
//参与人数
var
threadObj
=
thread
.
toObject
();
threadObj
.
comments
=
comments
;
threadObj
.
subThreads
=
subThreads
;
threadObj
.
latestPhotos
=
latestPhotos
;
threadObj
.
hotPhotos
=
hotPhotos
;
threadObj
.
canyuPeopleCount
=
canyuPeopleCount
;
callback
(
null
,
threadObj
);
}
});
};
//根据ID更新文章
exports
.
updateThreadById
=
function
(
tid
,
entity
,
callback
){
ForumThread
.
update
({
_id
:
tid
},
entity
,
null
,
function
(
err
,
result
){
if
(
err
)
{
console
.
error
(
err
);
callback
(
err
,
null
);
}
else
{
callback
(
null
,
null
);
}
});
exports
.
updateThreadById
=
function
(
tid
,
entity
,
callback
)
{
ForumThread
.
update
({
_id
:
tid
},
entity
,
null
,
function
(
err
,
result
)
{
if
(
err
)
{
console
.
error
(
err
);
callback
(
err
,
null
);
}
else
{
callback
(
null
,
null
);
}
});
};
//根据ID更新文章
exports
.
deleteThreadById
=
function
(
tid
,
callback
){
ForumThread
.
remove
({
_id
:
tid
},
function
(
err
,
result
){
if
(
err
)
{
console
.
error
(
err
);
callback
(
err
,
null
);
}
else
{
callback
(
null
,
null
);
}
});
exports
.
deleteThreadById
=
function
(
tid
,
callback
)
{
ForumThread
.
remove
({
_id
:
tid
},
function
(
err
,
result
)
{
if
(
err
)
{
console
.
error
(
err
);
callback
(
err
,
null
);
}
else
{
callback
(
null
,
null
);
}
});
};
//一级评论
function
populateComment
(
doc
,
callback
){
if
(
doc
&&
doc
.
comments
.
length
>
0
)
{
var
asyncTasks
=
[];
doc
.
comments
.
forEach
(
function
(
comment
){
asyncTasks
.
push
(
function
(
callback
)
{
ForumComment
.
populate
(
comment
,
{
path
:
'from to'
,
select
:
'uid nickName icon displayName displayIcon comments'
}
,
function
(
err
,
c
){
if
(
err
)
{
console
.
error
(
err
);
callback
(
null
,
null
);
}
else
{
// console.log(comment);
if
(
comment
.
comments
){
populateSubComment
(
comment
,
function
(
err
,
results
){
if
(
err
){
console
.
error
(
err
);
callback
(
null
,
null
);
}
else
{
var
newobj
=
comment
.
toObject
();
newobj
.
comments
=
results
;
callback
(
null
,
newobj
);
}
});
}
else
{
callback
(
null
,
doc
);
}
}
function
populateComment
(
doc
,
callback
)
{
if
(
doc
&&
doc
.
comments
.
length
>
0
)
{
var
asyncTasks
=
[];
doc
.
comments
.
forEach
(
function
(
comment
)
{
asyncTasks
.
push
(
function
(
callback
)
{
ForumComment
.
populate
(
comment
,
{
path
:
'from to'
,
select
:
'uid nickName icon displayName displayIcon comments'
},
function
(
err
,
c
)
{
if
(
err
)
{
console
.
error
(
err
);
callback
(
null
,
null
);
}
else
{
// console.log(comment);
if
(
comment
.
comments
)
{
populateSubComment
(
comment
,
function
(
err
,
results
)
{
if
(
err
)
{
console
.
error
(
err
);
callback
(
null
,
null
);
}
else
{
var
newobj
=
comment
.
toObject
();
newobj
.
comments
=
results
;
callback
(
null
,
newobj
);
}
});
}
else
{
callback
(
null
,
doc
);
}
}
});
});
});
async
.
parallel
(
asyncTasks
,
function
(
err
,
results
)
{
if
(
err
)
{
console
.
log
(
err
);
callback
(
null
,
null
);
}
else
{
if
(
results
&&
results
.
length
>
0
)
{
callback
(
null
,
results
[
0
]);
}
else
{
callback
(
null
,
{});
}
// callback(null, results);
}
});
}
else
{
callback
(
null
,
doc
);
}
});
async
.
parallel
(
asyncTasks
,
function
(
err
,
results
)
{
if
(
err
)
{
console
.
log
(
err
);
callback
(
null
,
null
);
}
else
{
if
(
results
&&
results
.
length
>
0
)
{
callback
(
null
,
results
[
0
]);
}
else
{
callback
(
null
,
{});
}
// callback(null, results);
}
});
}
else
{
callback
(
null
,
doc
);
}
}
//二级评论
function
populateSubComment
(
subComments
,
callback
){
if
(
subComments
.
comments
&&
subComments
.
comments
.
length
>
0
)
{
var
asyncTasks
=
[];
subComments
.
comments
.
forEach
(
function
(
comment
){
asyncTasks
.
push
(
function
(
callback
)
{
ForumComment
.
findOne
({
_id
:
comment
}).
populate
({
path
:
'from to'
,
select
:
'uid nickName icon'
}).
exec
(
function
(
err
,
c
){
if
(
err
)
{
console
.
error
(
err
);
callback
(
null
,
null
);
}
else
{
callback
(
null
,
c
);
}
function
populateSubComment
(
subComments
,
callback
)
{
if
(
subComments
.
comments
&&
subComments
.
comments
.
length
>
0
)
{
var
asyncTasks
=
[];
subComments
.
comments
.
forEach
(
function
(
comment
)
{
asyncTasks
.
push
(
function
(
callback
)
{
ForumComment
.
findOne
({
_id
:
comment
}).
populate
({
path
:
'from to'
,
select
:
'uid nickName icon'
}).
exec
(
function
(
err
,
c
)
{
if
(
err
)
{
console
.
error
(
err
);
callback
(
null
,
null
);
}
else
{
callback
(
null
,
c
);
}
});
});
});
async
.
parallel
(
asyncTasks
,
function
(
err
,
results
)
{
if
(
err
)
{
console
.
log
(
err
);
callback
(
null
,
null
);
}
else
{
callback
(
null
,
results
);
}
});
});
async
.
parallel
(
asyncTasks
,
function
(
err
,
results
)
{
if
(
err
)
{
console
.
log
(
err
);
callback
(
null
,
null
);
}
else
{
callback
(
null
,
results
);
}
});
}
else
{
callback
(
null
,
null
);
}
}
else
{
callback
(
null
,
null
);
}
}
//获取数量
function
countAllByFid
(
conditions
,
callback
)
{
ForumThread
.
count
(
conditions
,
function
(
err
,
count
)
{
if
(
err
)
{
console
.
error
(
err
);
callback
(
err
,
null
);
}
else
{
callback
(
null
,
count
);
}
});
function
countAllByFid
(
conditions
,
callback
)
{
ForumThread
.
count
(
conditions
,
function
(
err
,
count
)
{
if
(
err
)
{
console
.
error
(
err
);
callback
(
err
,
null
);
}
else
{
callback
(
null
,
count
);
}
});
}
//导出获取数量函数
exports
.
getAllCountByFid
=
function
(
conditions
,
callback
)
{
countAllByFid
(
conditions
,
callback
);
exports
.
getAllCountByFid
=
function
(
conditions
,
callback
)
{
countAllByFid
(
conditions
,
callback
);
}
//获取话题、照片墙子文章数据
function
getSubThreads
(
doc
,
sort
,
callback
)
{
var
conditions
=
{
pid
:
doc
.
_id
};
countAllByFid
(
conditions
,
function
(
err
,
count
)
{
if
(
err
)
{
console
.
error
(
err
);
callback
(
err
,
null
);
}
else
{
var
sortBy
=
'-top -created'
;
if
(
sort
)
{
sortBy
=
sort
;
}
ForumThread
.
find
(
conditions
).
populate
(
'from'
).
sort
(
sortBy
).
exec
(
function
(
err
,
docs
)
{
if
(
err
)
{
console
.
error
(
err
);
callback
(
err
,
null
);
}
else
{
var
obj
=
{};
obj
.
total
=
count
;
obj
.
items
=
docs
;
var
newDoc
=
doc
.
toObject
();
newDoc
.
subThreads
=
obj
;
callback
(
null
,
newDoc
);
}
});
}
});
function
getSubThreads
(
doc
,
sort
,
callback
)
{
var
conditions
=
{
pid
:
doc
.
_id
};
countAllByFid
(
conditions
,
function
(
err
,
count
)
{
if
(
err
)
{
console
.
error
(
err
);
callback
(
err
,
null
);
}
else
{
var
sortBy
=
'-top -created'
;
if
(
sort
)
{
sortBy
=
sort
;
}
ForumThread
.
find
(
conditions
).
populate
(
'from'
).
sort
(
sortBy
).
exec
(
function
(
err
,
docs
)
{
if
(
err
)
{
console
.
error
(
err
);
callback
(
err
,
null
);
}
else
{
var
obj
=
{};
obj
.
total
=
count
;
obj
.
items
=
docs
;
var
newDoc
=
doc
.
toObject
();
newDoc
.
subThreads
=
obj
;
callback
(
null
,
newDoc
);
}
});
}
});
}
function
getAllThreadByFidHelp
(
conditions
,
pageNo
,
pageSize
,
sort
,
callback
){
countAllByFid
(
conditions
,
function
(
err
,
count
){
if
(
err
){
console
.
error
(
err
);
callback
(
err
,
null
);
}
else
{
var
skip
=
(
pageNo
-
1
)
*
pageSize
;
var
limit
=
count
-
skip
>
pageSize
?
pageSize
:
(
count
-
skip
);
var
sortBy
=
'-topTime -created'
;
if
(
sort
){
sortBy
=
sort
;
}
ForumThread
.
find
(
conditions
).
populate
(
'from'
).
populate
(
'info'
).
populate
({
path
:
'tag'
}).
populate
({
path
:
'comments'
,
options
:{
limit
:
5
,
sort
:
'-created'
},
select
:
'from to created content'
}).
limit
(
limit
).
skip
(
skip
).
sort
(
sortBy
).
exec
(
function
(
err
,
docs
)
{
if
(
err
)
{
console
.
error
(
err
);
callback
(
err
,
null
);
}
else
{
var
obj
=
{};
obj
.
total
=
count
;
obj
.
pageNo
=
pageNo
;
obj
.
pageSize
=
pageSize
;
obj
.
items
=
docs
;
function
getAllThreadByFidHelp
(
conditions
,
pageNo
,
pageSize
,
sort
,
callback
)
{
countAllByFid
(
conditions
,
function
(
err
,
count
)
{
if
(
err
)
{
console
.
error
(
err
);
callback
(
err
,
null
);
}
else
{
var
skip
=
(
pageNo
-
1
)
*
pageSize
;
var
limit
=
count
-
skip
>
pageSize
?
pageSize
:
(
count
-
skip
);
var
sortBy
=
'-topTime -created'
;
if
(
sort
)
{
sortBy
=
sort
;
}
ForumThread
.
find
(
conditions
).
populate
(
'from'
).
populate
(
'info'
).
populate
({
path
:
'tag'
}).
populate
({
path
:
'comments'
,
options
:
{
limit
:
5
,
sort
:
'-created'
},
select
:
'from to created content'
}).
limit
(
limit
).
skip
(
skip
).
sort
(
sortBy
).
exec
(
function
(
err
,
docs
)
{
if
(
err
)
{
console
.
error
(
err
);
callback
(
err
,
null
);
}
else
{
var
obj
=
{};
obj
.
total
=
count
;
obj
.
pageNo
=
pageNo
;
obj
.
pageSize
=
pageSize
;
obj
.
items
=
docs
;
if
(
docs
&&
docs
.
length
>
0
)
{
var
asyncTasks
=
[];
if
(
docs
&&
docs
.
length
>
0
)
{
var
asyncTasks
=
[];
docs
.
forEach
(
function
(
doc
)
{
// console.log(doc);
if
(
doc
.
type
===
1
||
doc
.
level
!==
1
)
{
//非照片墙或文章时获取评论
asyncTasks
.
push
(
function
(
callback
)
{
populateComment
(
doc
,
callback
);
});
}
else
{
asyncTasks
.
push
(
function
(
callback
)
{
getSubThreads
(
doc
,
null
,
callback
);
});
}
docs
.
forEach
(
function
(
doc
){
// console.log(doc);
if
(
doc
.
type
===
1
||
doc
.
level
!==
1
){
//非照片墙或文章时获取评论
asyncTasks
.
push
(
function
(
callback
)
{
populateComment
(
doc
,
callback
);
});
}
else
{
asyncTasks
.
push
(
function
(
callback
)
{
getSubThreads
(
doc
,
null
,
callback
);
});
}
});
async
.
parallel
(
asyncTasks
,
function
(
err
,
results
)
{
if
(
err
)
{
console
.
error
(
err
);
callback
(
null
,
null
);
}
else
{
obj
.
items
=
results
;
callback
(
null
,
obj
);
}
});
}
else
{
callback
(
null
,
obj
);
}
}
});
}
});
});
async
.
parallel
(
asyncTasks
,
function
(
err
,
results
)
{
if
(
err
)
{
console
.
error
(
err
);
callback
(
null
,
null
);
}
else
{
obj
.
items
=
results
;
callback
(
null
,
obj
);
}
});
}
else
{
callback
(
null
,
obj
);
}
}
});
}
});
}
//获取全部列表数据
exports
.
getAllThreadByFid
=
function
(
conditions
,
pageNo
,
pageSize
,
sort
,
callback
)
{
getAllThreadByFidHelp
(
conditions
,
pageNo
,
pageSize
,
sort
,
callback
);
exports
.
getAllThreadByFid
=
function
(
conditions
,
pageNo
,
pageSize
,
sort
,
callback
)
{
getAllThreadByFidHelp
(
conditions
,
pageNo
,
pageSize
,
sort
,
callback
);
};
//根据板块ID更新板块下的top为0,并把当前文章的top设为1(置顶)
exports
.
updateTopByThreadId
=
function
(
infoId
,
threadId
,
callback
){
// ForumThread.update({info:infoId}, {top:0},{multi:true}, function(err, doc) {
// if(err){
// callback(err,null);
// }else{
// ForumThread.findOneAndUpdate({_id:threadId}, {top:1}, function(err, doc) {
// if(err){
// callback(err,null);
// }else{
// callback(null,null);
// }
// });
// }
// });
ForumThread
.
findOneAndUpdate
({
_id
:
threadId
},
{
top
:
1
,
topTime
:
new
Date
()},
function
(
err
,
doc
)
{
if
(
err
){
callback
(
err
,
null
);
}
else
{
callback
(
null
,
null
);
}
});
exports
.
updateTopByThreadId
=
function
(
infoId
,
threadId
,
callback
)
{
// ForumThread.update({info:infoId}, {top:0},{multi:true}, function(err, doc) {
// if(err){
// callback(err,null);
// }else{
// ForumThread.findOneAndUpdate({_id:threadId}, {top:1}, function(err, doc) {
// if(err){
// callback(err,null);
// }else{
// callback(null,null);
// }
// });
// }
// });
ForumThread
.
findOneAndUpdate
({
_id
:
threadId
},
{
top
:
1
,
topTime
:
new
Date
()
},
function
(
err
,
doc
)
{
if
(
err
)
{
callback
(
err
,
null
);
}
else
{
callback
(
null
,
null
);
}
});
};
//根据板块ID更新板块下的top为0,并把当前文章的top设为1(置顶)
exports
.
updateUnTopByThreadId
=
function
(
infoId
,
threadId
,
callback
){
ForumThread
.
findOneAndUpdate
({
_id
:
threadId
},
{
top
:
0
,
topTime
:
null
},
function
(
err
,
doc
)
{
if
(
err
){
callback
(
err
,
null
);
}
else
{
callback
(
null
,
null
);
}
});
exports
.
updateUnTopByThreadId
=
function
(
infoId
,
threadId
,
callback
)
{
ForumThread
.
findOneAndUpdate
({
_id
:
threadId
},
{
top
:
0
,
topTime
:
null
},
function
(
err
,
doc
)
{
if
(
err
)
{
callback
(
err
,
null
);
}
else
{
callback
(
null
,
null
);
}
});
};
//更新文章点赞数
exports
.
updateThreadRaiseCount
=
function
(
threadId
,
callback
){
ForumThread
.
update
(
{
_id
:
threadId
},
{
$inc
:
{
praise_count
:
1
}},
{
w
:
1
,
safe
:
true
},
function
(
err
,
result
){
if
(
err
){
console
.
error
(
err
);
callback
(
err
,
null
);
}
else
{
callback
(
null
,
null
);
}
});
exports
.
updateThreadRaiseCount
=
function
(
threadId
,
callback
)
{
ForumThread
.
update
({
_id
:
threadId
},
{
$inc
:
{
praise_count
:
1
}
},
{
w
:
1
,
safe
:
true
},
function
(
err
,
result
)
{
if
(
err
)
{
console
.
error
(
err
);
callback
(
err
,
null
);
}
else
{
callback
(
null
,
null
);
}
});
};
//更新文章分享数
exports
.
updateThreadShareCount
=
function
(
threadId
,
callback
){
ForumThread
.
update
(
{
_id
:
threadId
},
{
$inc
:
{
share_count
:
1
}},
{
w
:
1
,
safe
:
true
},
function
(
err
,
result
){
if
(
err
){
console
.
error
(
err
);
callback
(
err
,
null
);
}
else
{
callback
(
null
,
null
);
}
});
exports
.
updateThreadShareCount
=
function
(
threadId
,
callback
)
{
ForumThread
.
update
({
_id
:
threadId
},
{
$inc
:
{
share_count
:
1
}
},
{
w
:
1
,
safe
:
true
},
function
(
err
,
result
)
{
if
(
err
)
{
console
.
error
(
err
);
callback
(
err
,
null
);
}
else
{
callback
(
null
,
null
);
}
});
};
//更新文章评论数
exports
.
updateThreadCommentCount
=
function
(
threadId
,
callback
){
ForumThread
.
update
(
{
_id
:
threadId
},
{
$inc
:
{
comment_count
:
1
}},
{
w
:
1
,
safe
:
true
},
function
(
err
,
result
){
if
(
err
){
console
.
error
(
err
);
callback
(
err
,
null
);
}
else
{
callback
(
null
,
null
);
}
});
exports
.
updateThreadCommentCount
=
function
(
threadId
,
callback
)
{
ForumThread
.
update
({
_id
:
threadId
},
{
$inc
:
{
comment_count
:
1
}
},
{
w
:
1
,
safe
:
true
},
function
(
err
,
result
)
{
if
(
err
)
{
console
.
error
(
err
);
callback
(
err
,
null
);
}
else
{
callback
(
null
,
null
);
}
});
};
//更新文章浏览数
exports
.
updateThreadPvCount
=
function
(
threadId
,
callback
){
ForumThread
.
update
(
{
_id
:
threadId
},
{
$inc
:
{
pv_count
:
1
}},
{
w
:
1
,
safe
:
true
},
function
(
err
,
result
){
if
(
err
){
console
.
error
(
err
);
callback
(
err
,
null
);
}
else
{
callback
(
null
,
null
);
}
});
exports
.
updateThreadPvCount
=
function
(
threadId
,
callback
)
{
ForumThread
.
update
({
_id
:
threadId
},
{
$inc
:
{
pv_count
:
1
}
},
{
w
:
1
,
safe
:
true
},
function
(
err
,
result
)
{
if
(
err
)
{
console
.
error
(
err
);
callback
(
err
,
null
);
}
else
{
callback
(
null
,
null
);
}
});
};
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment