Skip to content

Commit

Permalink
修改 展示签到历史功能
Browse files Browse the repository at this point in the history
  • Loading branch information
liufei-ereach committed Oct 20, 2023
1 parent f0ffacc commit 8d166fd
Show file tree
Hide file tree
Showing 8 changed files with 283 additions and 4 deletions.
3 changes: 3 additions & 0 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@
(new Extend\Middleware("api"))->add(UserAuthMiddleware::class),

(new Extend\Event())->listen(checkinUpdated::class, [DoCheckinHistory::class, 'checkinHistory']),

(new Extend\Routes('api'))
->get('/checkin/history', 'checkin.history', Mattoid\CheckinHistory\Api\Controller\ListCheckinHistoryController::class)
];
3 changes: 2 additions & 1 deletion js/dist/forum.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/dist/forum.js.map

Large diffs are not rendered by default.

41 changes: 39 additions & 2 deletions js/src/forum/pages/CheckinHistoryPage.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import UserPage from 'flarum/forum/components/UserPage';
import dynamicallyLoadLib from "../utils/dynamicallyLoadLib";

export default class CheckinHistoryPage extends UserPage {

Expand All @@ -13,14 +14,15 @@ export default class CheckinHistoryPage extends UserPage {
if (app.session.user) {
return (
<div className="CheckinHistoryUserPage">
<div id="calendar" />
</div>
);
}
}

// 创建成功,此时可以拿到真实 DOM 了。
oncreate() {

oncreate(vnode) {
this.renderCalendar(vnode);
}

// DOM 渲染刷新后。业务有刷新变动数据时候使用。
Expand All @@ -42,4 +44,39 @@ export default class CheckinHistoryPage extends UserPage {
onbeforeupdate() {

}

async getData(info, successCb, failureCb) {
const results = await app.store.find('checkin/history', {
start: info.start.toISOString(),
end: info.end.toISOString()
});

return results.payload.data.map((item) => {
return item.attributes;
});
}

async renderCalendar(vnode) {
await dynamicallyLoadLib('fullcalendarCore');
await dynamicallyLoadLib(['fullcalendarLocales', 'fullcalendarDayGrid', 'fullcalendarInteraction', 'fullcalendarList']);

const calendarEl = document.getElementById('calendar');
const openModal = this.openCreateModal.bind(this);

const calendar = new FullCalendar.Calendar(calendarEl, {
locale: app.translator.getLocale(),
allDayText: "今天",
initialView: 'dayGridMonth',
dateClick: function (info) {
openModal(info);
},
events: (info, successCb, failureCb) => this.getData(info, successCb, failureCb)
});
calendar.render();
}

openCreateModal(info) {
console.log(info)
}

}
107 changes: 107 additions & 0 deletions js/src/forum/utils/dynamicallyLoadLib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
const libs = {
fullcalendarCore: {
js: 'https://cdn.jsdelivr.net/npm/@fullcalendar/core@6.1.8/index.global.min.js',
loaded: () => typeof FullCalendar !== 'undefined',
},
fullcalendarLocales: {
js: 'https://cdn.jsdelivr.net/npm/@fullcalendar/core@6.1.8/locales-all.global.min.js',
loaded: () => typeof FullCalendar !== 'undefined' && FullCalendar.globalLocales.length > 2,
},
fullcalendarDayGrid: {
js: 'https://cdn.jsdelivr.net/npm/@fullcalendar/daygrid@6.1.8/index.global.min.js',
loaded: () => typeof FullCalendar !== 'undefined' && FullCalendar.globalPlugins.find((p) => p.name === '@fullcalendar/daygrid'),
},
fullcalendarInteraction: {
js: 'https://cdn.jsdelivr.net/npm/@fullcalendar/interaction@6.1.8/index.global.min.js',
loaded: () => typeof FullCalendar !== 'undefined' && FullCalendar.globalPlugins.find((p) => p.name === '@fullcalendar/interaction'),
},
fullcalendarList: {
js: 'https://cdn.jsdelivr.net/npm/@fullcalendar/list@6.1.8/index.global.min.js',
loaded: () => typeof FullCalendar !== 'undefined' && FullCalendar.globalPlugins.find((p) => p.name === '@fullcalendar/list'),
},

flatpickr: {
css: 'https://cdn.jsdelivr.net/npm/flatpickr/dist/flatpickr.min.css',
js: 'https://cdn.jsdelivr.net/npm/flatpickr',
loaded: () => typeof window.flatpickr !== 'undefined',
},
flatpickrLocale: {
js: (locale) => `https://cdn.jsdelivr.net/npm/flatpickr/dist/l10n/${locale}.js`,
loaded: (locale) => typeof window.flatpickr.l10ns[locale] !== 'undefined',
},
};

export default function dynamicallyLoadLib(lib, ...moreArgs) {
if (Array.isArray(lib)) {
return Promise.all(lib.map((l) => dynamicallyLoadLib(l)));
}

let libConf = {};

// If the lib is an object
if (typeof lib === 'object') {
libConf = { ...lib };

if (!libConf.loaded) {
console.warn('dynamicallyLoadLib: No loaded function defined for lib', lib);
return Promise.resolve();
}
}

if (typeof lib === 'string') {
// If lib is not in the libs object log a warning and return a resolved promise
if (!libs[lib]) {
console.warn('dynamicallyLoadLib: lib not found', lib);
return Promise.resolve();
}

libConf = { ...libs[lib] };
}

if (Object.keys(libConf).length === 0) {
console.warn('dynamicallyLoadLib: lib is not a string nor an valid object', lib);
return Promise.resolve();
}

let loadPromises = [];

if (libConf.loaded(...moreArgs)) {
loadPromises.push(Promise.resolve());
} else {
if (libConf.css) {
const css = Array.isArray(libConf.css) ? libConf.css : [libConf.css];
css.forEach((href) => {
const css = document.createElement('link');
css.rel = 'stylesheet';
css.href = href;
document.head.appendChild(css);
});
}

if (libConf.js) {
if (typeof libConf.js === 'function') {
libConf.js = libConf.js(...moreArgs);
}

const js = Array.isArray(libConf.js) ? libConf.js : [libConf.js];
js.forEach((src) => {
const script = document.createElement('script');
script.src = src;
document.head.appendChild(script);
});
}

loadPromises.push(
new Promise((resolve, reject) => {
const interval = setInterval(() => {
if (libConf.loaded(...moreArgs)) {
clearInterval(interval);
resolve();
}
}, 5);
})
);
}

return Promise.all(loadPromises);
}
53 changes: 53 additions & 0 deletions src/Api/Controller/ListCheckinHistoryController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/*
* This file is part of askvortsov/flarum-moderator-warnings
*
* Copyright (c) 2021 Alexander Skvortsov.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/

namespace Mattoid\CheckinHistory\Api\Controller;

use Flarum\Api\Controller\AbstractListController;
use Flarum\Http\RequestUtil;
use Flarum\User\Exception\PermissionDeniedException;
use Mattoid\CheckinHistory\Api\Serializer\CheckinHistorySerializer;
use Mattoid\CheckinHistory\Model\UserCheckinHistory;
use Psr\Http\Message\ServerRequestInterface;
use Tobscure\JsonApi\Document;

class ListCheckinHistoryController extends AbstractListController
{
public $serializer = CheckinHistorySerializer::class;

public $include = ['warnedUser', 'addedByUser', 'hiddenByUser', 'post', 'post.discussion', 'post.user'];

/**
* Get the data to be serialized and assigned to the response document.
*
* @param ServerRequestInterface $request
* @param Document $document
*
* @throws PermissionDeniedException
*
* @return mixed
*/
protected function data(ServerRequestInterface $request, Document $document)
{

$actor = RequestUtil::getActor($request);

if (!$actor->can('event.view')) {
return array();
}

$userId = $request->getAttributes()['actor']['id'];

return UserCheckinHistory::where('user_id', $userId)->where('last_checkin_date', ">=", $request->getQueryParams()['start'])
->where('last_checkin_date', "<=", $request->getQueryParams()['end'])->get();

}
}
50 changes: 50 additions & 0 deletions src/Api/Serializer/CheckinHistorySerializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
* This file is part of askvortsov/flarum-moderator-warnings
*
* Copyright (c) 2021 Alexander Skvortsov.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/

namespace Mattoid\CheckinHistory\Api\Serializer;

use Mattoid\CheckinHistory\Model\UserCheckinHistory;
use Flarum\Api\Serializer\AbstractSerializer;
use Flarum\Api\Serializer\PostSerializer;
use Flarum\Post\Post;

class CheckinHistorySerializer extends AbstractSerializer
{
protected $type = 'checkin.history';

/**
* {@inheritdoc}
*/
protected function getDefaultAttributes($history)
{
$attributes = [
'id' => $history->id,
'userId' => $history->user_id,
'name' => $history->type ? '签到' : '补签',
'totalCheckinCount' => $history->total_checkin_count,
'totalContinuousCheckinCount' => $history->total_continuous_checkin_count,
'start' => $history->last_checkin_date,
'time' => $history->last_checkin_time,
];

return $attributes;
}

protected function format($text)
{
return UserCheckinHistory::getFormatter()->render($text, new Post());
}

protected function post($history)
{
return $this->hasOne($history, PostSerializer::class);
}
}
28 changes: 28 additions & 0 deletions src/Model/UserCheckinHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,36 @@
namespace Mattoid\CheckinHistory\Model;

use Flarum\Database\AbstractModel;
use Flarum\Formatter\Formatter;

class UserCheckinHistory extends AbstractModel
{
protected $table = "user_checkin_history";

/**
* The text formatter instance.
*
* @var \Flarum\Formatter\Formatter
*/
protected static $formatter;

/**
* Get the text formatter instance.
*
* @return \Flarum\Formatter\Formatter
*/
public static function getFormatter()
{
return static::$formatter;
}

/**
* Set the text formatter instance.
*
* @param \Flarum\Formatter\Formatter $formatter
*/
public static function setFormatter(Formatter $formatter)
{
static::$formatter = $formatter;
}
}

0 comments on commit 8d166fd

Please sign in to comment.