Skip to content

Commit d6980b4

Browse files
committed
评论功能完成开发
1 parent df32a9b commit d6980b4

File tree

8 files changed

+162
-88
lines changed

8 files changed

+162
-88
lines changed

server/apps/classes/Api.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,25 @@ static function feed($type, $uid, $tid=0, $event_id=0)
1818
{
1919
return \Swoole::$php->model->Feeds->send($type, $uid, $tid, $event_id);
2020
}
21+
22+
static function userInfoSafe(&$user)
23+
{
24+
unset($user['password'], $user['username'], $user['reg_ip'], $user['reg_time'], $user['lastip'], $user['lastlogin']);
25+
}
26+
27+
static function updateAvatarUrl(&$user, $https = false)
28+
{
29+
if (empty($user['avatar']))
30+
{
31+
$user['avatar'] = '/static/images/default.png';
32+
}
33+
if (substr($user['avatar'], 0, 4) != 'http')
34+
{
35+
$user['avatar'] = WEBROOT . $user['avatar'];
36+
}
37+
if ($https and substr($user['avatar'], 0, 5) != 'https')
38+
{
39+
$user['avatar'] = 'https'.substr($user['avatar'], 4);
40+
}
41+
}
2142
}

server/apps/classes/Content.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,24 +147,29 @@ static private function getTreeChilds($id)
147147
}
148148
}
149149

150+
static function parseMarkdown($text)
151+
{
152+
$text = str_replace('```', '~~~', $text);
153+
$parser = new Michelf\MarkdownExtra;
154+
$parser->fn_id_prefix = "post22-";
155+
$parser->code_attr_on_pre = false;
156+
$parser->tab_width = 4;
157+
return $parser->transform($text);
158+
}
159+
150160
/**
151161
* @param $wiki_id
152162
* @param $text
153163
* @return string
154164
*/
155-
static function md2html($wiki_id, $text)
165+
static function getWikiHtml($wiki_id, $text)
156166
{
157167
$key = 'wiki_page_'.$wiki_id;
158168
$html = \Swoole::$php->cache->get($key);
159169
if (!$html)
160170
{
161171
//GitHub Code Parse
162-
$text = str_replace('```', '~~~', $text);
163-
$parser = new Michelf\MarkdownExtra;
164-
$parser->fn_id_prefix = "post22-";
165-
$parser->code_attr_on_pre = false;
166-
$parser->tab_width = 4;
167-
$html = $parser->transform($text);
172+
$html = self::parseMarkdown($text);
168173
\Swoole::$php->cache->set($key, $html, 0);
169174
}
170175
else

server/apps/controllers/Api.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,58 @@ class Api extends Swoole\Controller
1313
const AVATAR_URL = 'http://182.254.148.72:9502/uploads/avatar/';
1414
const NO_AVATAR = 'http://182.254.148.72:9502/static/common/';
1515

16+
function getLoginInfo()
17+
{
18+
if (empty($_COOKIE['PHPSESSID']))
19+
{
20+
not_found:
21+
return $this->json([], 404);
22+
}
23+
$this->session->start();
24+
if (!empty($_SESSION['user']))
25+
{
26+
$user = $_SESSION['user'];
27+
App\Api::userInfoSafe($user);
28+
return $this->json($user);
29+
}
30+
goto not_found;
31+
}
32+
33+
function postComment()
34+
{
35+
if (empty($_COOKIE['PHPSESSID']))
36+
{
37+
return $this->json([], 403);
38+
}
39+
$this->session->start();
40+
if (empty($_SESSION['user']))
41+
{
42+
return $this->json(['login' => $this->config['user']['login_url']], 403);
43+
}
44+
if (empty($_POST['content']) or empty($_POST['app']) or empty($_POST['id']))
45+
{
46+
return $this->json(null, 1001);
47+
}
48+
$table = table('duoshuo_posts');
49+
Swoole\Filter::safe($_POST['content']);
50+
Swoole\Loader::addNameSpace('Stauros', Swoole::$app_path.'/include/Stauros/lib/Stauros');
51+
$clean = strip_tags($_POST['content']);
52+
$ret = $table->put(array(
53+
'uid' => $_SESSION['user']['id'],
54+
'created_at' => Swoole\Tool::now(),
55+
'thread_id' => intval($_POST['id']),
56+
'thread_key' => $_POST['app'] . '-' . intval($_POST['id']),
57+
'message' => $clean,
58+
));
59+
if ($ret)
60+
{
61+
return $this->json(['id' => $ret]);
62+
}
63+
else
64+
{
65+
return $this->json(null, 500);
66+
}
67+
}
1668

1769
static function parseMarkdown($text)
1870
{

server/apps/controllers/Page.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,8 @@ protected function loginSucess()
9393
{
9494
$token = Swoole\RandomKey::string(32);
9595
$user = $_SESSION['user'];
96-
unset($user['password'], $user['username'], $user['reg_ip'], $user['reg_time'], $user['lastip'], $user['lastlogin']);
97-
if (empty($user['avatar']))
98-
{
99-
$user['avatar'] = '/static/images/default.png';
100-
}
101-
if (substr($user['avatar'], 0, 4) != 'http')
102-
{
103-
$user['avatar'] = WEBROOT . $user['avatar'];
104-
}
96+
App\Api::userInfoSafe($user);
97+
App\Api::updateAvatarUrl($user);
10598
$this->cache->set('login_token_' . $token, $user, 86400);
10699
$refer = Swoole\Tool::urlAppend($refer, array('token' => $token));
107100
}

server/apps/controllers/Wiki.php

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,35 @@ protected function getComments()
115115
$thread_key = 'wiki-'.$this->tpl->_tpl_vars['id'];
116116
$t = table('duoshuo_posts');
117117
$list = $t->gets(array('thread_key' => $thread_key, 'order' => 'id asc'));
118+
$uids = [];
119+
foreach($list as $li)
120+
{
121+
if ($li['uid'])
122+
{
123+
$uids[$li['uid']] = 1;
124+
}
125+
}
126+
if (count($uids) > 0)
127+
{
128+
$users = model('UserInfo')->getMap(array('in' => ['id', $uids], 'select' => 'id, nickname, avatar'));
129+
}
130+
else
131+
{
132+
$users = [];
133+
}
134+
foreach($list as &$li)
135+
{
136+
$li['message'] = App\Content::parseMarkdown($li['message']);
137+
if ($li['uid'])
138+
{
139+
$user = $users[$li['uid']];
140+
App\Api::updateAvatarUrl($user, true);
141+
$li['author_name'] = $user['nickname'];
142+
$li['avatar'] = $user['avatar'];
143+
$li['author_url'] = "http://www.swoole.com/page/user/uid-" . $li['uid'];
144+
}
145+
}
118146
$this->tpl->assign('comments', $list);
119-
120-
$t2 = table('duoshuo_thread');
121-
$list2 = $t2->gets(array('thread_key' => $thread_key, 'limit' => 1, 'order' => ''));
122-
$this->tpl->assign('thread', $list2[0]);
123147
}
124148

125149

@@ -220,7 +244,7 @@ private function getPageInfo()
220244
$this->swoole->tpl->assign("wiki_page", $this->pageInfo);
221245

222246
markdown:
223-
$html = App\Content::md2html($wiki_id, $text);
247+
$html = App\Content::getWikiHtml($wiki_id, $text);
224248
$this->swoole->tpl->assign("content", $html);
225249
}
226250

server/apps/controllers/Wiki_admin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ private function getMainData()
359359
$this->assign("wiki_page", $wiki_page);
360360

361361
markdown:
362-
$html = App\Content::md2html($wiki_id, $text);
362+
$html = App\Content::getWikiHtml($wiki_id, $text);
363363
$this->assign("content", $html);
364364
}
365365

server/apps/templates/wiki/noframe/index.html

Lines changed: 42 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -91,107 +91,72 @@ <h1 id="h_title">{{$wiki_page.title}}
9191
{{/if}}
9292
{{$content}}
9393
</article>
94-
<hr />
95-
<!-- Duoshuo Comment BEGIN -->
94+
<hr/>
9695
{{if $wiki_page.close_comment == 0}}
97-
<div class="ds-thread" data-thread-key="wiki-106" data-title="Swoole扩展"
98-
data-url="http://wiki.swoole.com/wiki/page/106.html" id="ds-thread">
96+
<div class="ds-thread" id="ds-thread">
9997
<div id="ds-reset">
100-
<div class="ds-meta"><a href="javascript:void(0)"
101-
class="ds-like-thread-button ds-rounded ds-thread-liked"><span
102-
class="ds-icon ds-icon-heart"></span> <span class="ds-thread-like-text">已喜欢</span><span
103-
class="ds-thread-cancel-like">取消喜欢</span></a><span class="ds-like-panel"><span
104-
class="ds-highlight">{{$thread.likes}}</span> 人喜欢</span></div>
10598
<div class="ds-comments-info">
106-
<div class="ds-sort"><a class="ds-order-desc">最新</a><a class="ds-order-asc ds-current">最早</a><a
107-
class="ds-order-hot">最热</a></div>
99+
<div class="ds-sort"><a class="ds-order-desc" target="_blank">最新</a><a class="ds-order-asc ds-current" target="_blank">最早</a><a class="ds-order-hot" target="_blank">最热</a></div>
108100
<ul class="ds-comments-tabs">
109-
<li class="ds-tab"><a class="ds-comments-tab-duoshuo ds-current"
110-
href="javascript:void(0);"><span class="ds-highlight">{{$comments|@count}}</span>条评论</a>
101+
<li class="ds-tab"><a class="ds-comments-tab-duoshuo ds-current" href="javascript:void(0);" target="_blank"><span class="ds-highlight">{{$comments|@count}}</span>条评论</a>
111102
</li>
112103
</ul>
113104
</div>
114-
115105
<ul class="ds-comments">
116106
{{foreach item=v from=$comments}}
117-
<li class="ds-post" >
107+
<li class="ds-post">
118108
<div class="ds-post-self">
119109
<div class="ds-avatar" data-user-id="8435783"><a rel="nofollow author" target="_blank"
120110
href="{{$v.author_url}}"
121111
title="{{$v.author_name}}"><img
122-
src="https://wiki.swoole.com/static/images/default.png"
112+
src="{{if $v.avatar}}{{$v.avatar}}{{else}}/static/images/default.png{{/if}}"
123113
alt="{{$v.author_name}}"></a></div>
124114
<div class="ds-comment-body">
125115
<div class="ds-comment-header"><a class="ds-user-name ds-highlight" data-qqt-account=""
126116
href="{{$v.author_url}}"
127117
rel="nofollow" target="_blank" data-user-id="8435783">{{$v.author_name}}</a>
128118
</div>
129119
<p>{{$v.message}}</p>
130-
131-
<div class="ds-comment-footer ds-comment-actions"><span class="ds-time">{{$v.created_at|substr:0:10}}</span><a
132-
class="ds-post-reply" href="javascript:void(0);"><span
133-
class="ds-icon ds-icon-reply"></span>回复</a><a class="ds-post-likes"
134-
href="javascript:void(0);"><span
135-
class="ds-icon ds-icon-like"></span></a><a class="ds-post-repost"
136-
href="javascript:void(0);">
137-
</div>
138120
</div>
121+
</div>
139122
</li>
140123
{{/foreach}}
141124
</ul>
142-
<div class="ds-paginator" style="display: none;">
143-
<div class="ds-border"></div>
144-
<a data-page="1" href="javascript:void(0);" class="ds-current">1</a></div>
145-
<div class="ds-toolbar">
146-
<div class="ds-account-control"><span class="ds-icon ds-icon-settings"></span> <span>帐号管理</span>
147-
<ul>
148-
<li><a class="ds-bind-more" href="javascript:void(0);" style="border-top: none">绑定更多</a>
149-
</li>
150-
<li><a target="_blank" href="http://duoshuo.com/settings/">设置</a></li>
151-
<li><a rel="nofollow" href="http://www4swoole.duoshuo.com/logout/"
152-
style="border-bottom: none">登出</a></li>
153-
</ul>
154-
</div>
155-
<div class="ds-visitor"><a class="ds-visitor-name" href="http://weibo.com/hantianfeng"
156-
target="_blank">Rango-韩天峰</a><a class="ds-unread-comments-count"
157-
href="javascript:void(0);" title="你没有新回复"
158-
style="display: none;">0</a></div>
159-
</div>
160-
<div class="ds-replybox"><a class="ds-avatar" href="http://duoshuo.com/settings/avatar/" target="_blank"
161-
title="设置头像"><img
162-
src="http://tva4.sinaimg.cn/crop.1.1.561.561.50/3fecf63djw8f7t533npdhj20g00i5dgz.jpg"
163-
alt="Rango-韩天峰"></a>
164125

165-
<form method="post"><input type="hidden" name="thread_id" value="1163167853466092117">
166-
<input type="hidden" name="parent_id" value="">
167-
<input type="hidden" name="nonce" value="573edf51578e7">
168-
169-
<div class="ds-textarea-wrapper ds-rounded-top"><textarea name="message" title="Ctrl+Enter快捷提交"
170-
placeholder="说点什么吧…"></textarea>
126+
<div class="ds-replybox" id="post_comment_div" style="display: none;">
127+
<div class="ds-avatar"><img src="" id="login_user_avatar"></div>
128+
<form method="post" onsubmit="return postComment(this);">
129+
<input type="hidden" name="wiki_id" value="{{$wiki_page.id}}">
130+
<div class="ds-textarea-wrapper ds-rounded-top">
131+
<textarea name="message" title="Ctrl+Enter快捷提交" placeholder="说点什么吧(支持Markdown语法)"></textarea>
171132
<pre class="ds-hidden-text"></pre>
172133
</div>
173134
<div class="ds-post-toolbar">
174-
<div class="ds-post-options ds-gradient-bg"><span class="ds-sync"><input
175-
id="ds-sync-checkbox" type="checkbox" name="repost" checked="checked" value="weibo"> <label
176-
for="ds-sync-checkbox">分享到:</label><a href="javascript:void(0)"
177-
class="ds-service-icon ds-weibo"
178-
data-service="weibo" title="新浪微博"></a></span>
135+
<div class="ds-post-options ds-gradient-bg">
179136
</div>
180137
<button class="ds-post-button" type="submit">发布</button>
181-
<div class="ds-toolbar-buttons"><a class="ds-toolbar-button ds-add-emote" title="插入表情"></a>
182-
</div>
183138
</div>
184139
</form>
185140
</div>
186141
</div>
142+
</div>
187143
</div>
188144
<link href="/static/css/duoshuo.css" rel="stylesheet">
189145
{{/if}}
190146
<script type="text/javascript">
147+
function postComment(o) {
148+
if ($.trim(o.message.value) == '') {
149+
return false;
150+
}
151+
$.post('/api/postComment/', {'content': o.message.value, 'app' : 'wiki', 'id':o.wiki_id.value}, function(data){
152+
if (data.code == 0) {
153+
location.reload();
154+
}
155+
});
156+
return false;
157+
}
191158
var _bdhmProtocol = (("https:" == document.location.protocol) ? "https://" : "http://");
192-
193159
$(document).ready(function () {
194-
var resize_count = 0;
195160
var timer = setInterval(function () {
196161
var a = $("#sidebar")[0];
197162
var b = $("div.wiki_content")[0];
@@ -202,15 +167,28 @@ <h1 id="h_title">{{$wiki_page.title}}
202167
}
203168
window.clearInterval(timer);
204169
}, 300);
205-
});
206-
207-
$(document).ready(function() {
208170
$('a').each(function(e){
209171
//外链
210172
if (this.href.substring(_bdhmProtocol.length, location.host.length + _bdhmProtocol.length) != location.host) {
211173
this.target = "_blank";
212174
}
213175
});
176+
$.getJSON('/api/getLoginInfo', function (data) {
177+
$('#post_comment_div').show();
178+
if (data.code == 0) {
179+
if (data.data.avatar.substring(0, 5) != 'https') {
180+
if (data.data.avatar.substring(0, 4) == 'http') {
181+
data.data.avatar = 'https' + data.data.avatar.substring(4);
182+
} else {
183+
data.data.avatar = 'https://' + location.host + data.data.avatar;
184+
}
185+
186+
}
187+
$('#login_user_avatar').attr('src', data.data.avatar).attr('alt', data.data.nickname).attr('title', data.data.nickname);
188+
} else {
189+
$('#post_comment_div').html('<p><br/><a href="http://www.swoole.com/page/login/">[登录后发表评论]</a></p>');
190+
}
191+
});
214192
});
215193
</script>
216194
</div>

web/static/css/duoshuo.css

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@
414414
clear: both;
415415
position: relative;
416416
overflow: visible;
417-
_zoom: 1
417+
margin-top: 15px;
418418
}
419419

420420
#ds-thread #ds-reset a {
@@ -715,7 +715,7 @@
715715
width: auto;
716716
font-size: 12px;
717717
z-index: 3;
718-
margin: 8px 0;
718+
margin: 15px 0;
719719
padding: 0 0 0 60px;
720720
position: relative;
721721
_zoom: 1
@@ -1064,6 +1064,7 @@
10641064
margin: 8px 0;
10651065
border-radius: 3px;
10661066
border: 1px solid #ddd;
1067+
line-height: 1.5;
10671068
color: #666
10681069
}
10691070

0 commit comments

Comments
 (0)