Skip to content

Commit

Permalink
站内信功能
Browse files Browse the repository at this point in the history
  • Loading branch information
shirne committed Jul 28, 2024
1 parent 9e46eef commit ae90d1a
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/application/index/controller/member/MessageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@
*/
class MessageController extends BaseController
{
public function count()
{
session_write_close();
$receive = Db::name('MemberMessage')->where('member_id', $this->user['id'])->where('is_delete', 0)->where('read_time', 0)->where(function ($query) {
$query->where('reply_id', 0)->whereOr(Db::raw('reply_id in (select message_id from sa_member_message where member_id=' . $this->user['id'] . ')'));
})->count();

$send = Db::name('MemberMessage')->where('member_id', $this->user['id'])->where('is_delete', 0)->where('read_time', 0)->where(Db::raw('reply_id in (select message_id from sa_member_message where from_member_id=' . $this->user['id'] . ')'))->count();

$this->success('ok', '', [
'total' => $receive + $send,
'receive' => $receive,
'send' => $send
]);
}

public function index($page = 1, $pagesize = 15)
{
$paged = Db::view('MemberMessage', '*')
Expand All @@ -27,6 +43,8 @@ public function index($page = 1, $pagesize = 15)
if (empty($item['from_nickname'])) {
$item['from_nickname'] = $item['from_username'];
}
$newcount = Db::name('MemberMessage')->where('group_id', $item['message_id'])->where('member_id', $this->user['id'])->where('is_delete', 0)->where('read_time', 0)->count();
$item['newcount'] = $newcount;
}
$ids = array_column($lists, 'message_id');
if (!empty($ids)) {
Expand Down Expand Up @@ -75,7 +93,8 @@ public function list($id, $page = 1, $pagesize = 100)

$ids = array_column($lists, 'message_id');
if (!empty($ids)) {
Db::name('MemberMessage')->where('member_id', $this->userid)->where('show_time', 0)->whereIn('message_id', $ids)->update(['show_time' => time(), 'read_time' => time()]);
Db::name('MemberMessage')->where('member_id', $this->userid)->where('show_time', 0)->whereIn('message_id', $ids)->update(['show_time' => time()]);
Db::name('MemberMessage')->where('member_id', $this->userid)->where('read_time', 0)->whereIn('message_id', $ids)->update(['read_time' => time()]);
}

$this->assign('active', $message['member_id'] == $this->user['id'] ? 0 : 1);
Expand All @@ -98,6 +117,8 @@ public function sendlist($page = 1, $pagesize = 15)
if (empty($item['to_nickname'])) {
$item['to_nickname'] = $item['to_username'];
}
$newcount = Db::name('MemberMessage')->where('group_id', $item['message_id'])->where('member_id', $this->user['id'])->where('is_delete', 0)->where('read_time', 0)->count();
$item['newcount'] = $newcount;
}
$this->assign('lists', $lists);
$this->assign('page', $paged->render());
Expand Down
151 changes: 151 additions & 0 deletions src/template/default/public/base.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,157 @@
<script type="text/javascript">
jQuery(function ($) {
setNav('{$navmodel}');
$('.list-group-item').click(function (e) {
var target = $(e.target);
if (target.is('a')) return;
if (target.parents('a').length > 0) return;
var link = $(this).find('a[href]')
if (link.length > 0) {
if (link.is('.btn-confirm')) {
return;
}
var anchor = target.data('anchor')
if (anchor) {
link.attr('href', link.attr('href') + '#' + anchor)
}
link[0].click();
}
});
$('.gotop-btn').click(function () {
$("html,body").animate({
scrollTop: 0
}, 500);
})
$('.need-login').click(function () {
dialog.confirm("{:lang( 'Please login first!' )}", function () {
location.href = "{:url('index/login/index')}"
})
})
$('.favate-btn').click(function (e) {
e.preventDefault();
if (!isLogin) {
dialog.alert('请先登录')
return
}
var type = $(this).data('type')
var id = $(this).data('id')
$.ajax({
url: "{:aurl('index/member.favourite/add')}",
type: 'POST',
dataType: 'json',
data: {
type: type,
id: id,
},
success: function (json) {
if (json.code == 1) {
dialog.success(json.msg)
setTimeout(function () {
location.reload()
}, 500)
} else {
dialog.alert(json.msg)
}
}
})
})
$('.cancel-favate-btn').click(function (e) {
e.preventDefault();
if (!isLogin) {
dialog.alert('请先登录')
return
}
var type = $(this).data('type')
var id = $(this).data('id')
dialog.confirm('取消收藏?', function () {
$.ajax({
url: "{:aurl('index/member.favourite/remove')}",
type: 'POST',
dataType: 'json',
data: {
type: type,
id: id,
},
success: function (json) {
if (json.code == 1) {
dialog.success(json.msg)
setTimeout(function () {
location.reload()
}, 500)
} else {
dialog.alert(json.msg)
}
}
})
})
})
var ajaxes = $('.ajax-notice')
var timer
function startAjax() {
clearTimeout(timer)
var modules = []
for (var i = 0; i < ajaxes.length; i++) {
var module = ajaxes.eq(i).data('module')
if (modules.indexOf(module) > -1) continue;
modules.push(module)
switch (module) {
default:
getMessageCount()
}
}
timer = setTimeout(startAjax, 5000);
}
if (isLogin) {
startAjax();
}
var isMessageSync = false
function getMessageCount() {
if (isMessageSync) return
isMessageSync = true
try {
$.ajax({
url: "{:aurl('index/member.message/count')}",
type: 'POST',
dataType: 'json',
success: function (json) {
isMessageSync = false
if (json.code == 1) {
$('.ajax-notice-message').each(function () {
var badge = $(this).find('.badge')
var count = json.data.total
if ($(this).data('type') == 'receive') {
count = json.data.receive
} else if ($(this).data('type') == 'send') {
count = json.data.send
}
if (count > 0) {
if (badge.length < 1) {
$(this).append('<span class="badge badge-light">' + count + '</span>')
} else {
badge.text(count)
}
} else {
badge.remove()
}
})
}
},
error: function () {
isMessageSync = false
}
})
} catch (err) {
isMessageSync = false
}
}
})
</script>
{block name="script" }{/block}
Expand Down

0 comments on commit ae90d1a

Please sign in to comment.