mirror of
https://github.com/kuaifan/dootask.git
synced 2026-03-03 16:02:08 +00:00
perf: 优化签到通知
This commit is contained in:
parent
fd22cd3265
commit
92314495d2
@ -8,6 +8,8 @@ use App\Models\UserCheckinRecord;
|
||||
use App\Models\WebSocketDialog;
|
||||
use App\Models\WebSocketDialogMsg;
|
||||
use App\Module\Base;
|
||||
use Cache;
|
||||
use Carbon\Carbon;
|
||||
use Request;
|
||||
|
||||
/**
|
||||
@ -90,15 +92,19 @@ class PublicController extends AbstractController
|
||||
if ($key != $setting['key']) {
|
||||
return 'key error';
|
||||
}
|
||||
$setting['time'] = $setting['time'] ? Base::json2array($setting['time']) : ['00:00', '23:59'];
|
||||
$times = $setting['time'] ? Base::json2array($setting['time']) : ['00:00', '23:59'];
|
||||
$advance = (intval($setting['advance']) ?: 120) * 60;
|
||||
$delay = (intval($setting['delay']) ?: 120) * 60;
|
||||
//
|
||||
$nowDate = date("Y-m-d");
|
||||
$nowTime = date("H:i:s");
|
||||
//
|
||||
$timeStart = strtotime(date("{$nowDate} {$setting['time'][0]}"));
|
||||
$timeEnd = strtotime(date("{$nowDate} {$setting['time'][1]}"));
|
||||
if (Base::time() < $timeStart || Base::time() > $timeEnd) {
|
||||
return "not in valid time, valid time is {$setting['time'][0]}-{$setting['time'][1]}";
|
||||
$timeStart = strtotime("{$nowDate} {$times[0]}");
|
||||
$timeEnd = strtotime("{$nowDate} {$times[1]}");
|
||||
$timeAdvance = max($timeStart - $advance, strtotime($nowDate));
|
||||
$timeDelay = min($timeEnd + $delay, strtotime("{$nowDate} 23:59:59"));
|
||||
if (Base::time() < $timeAdvance || $timeDelay < Base::time()) {
|
||||
return "not in valid time, valid time is " . date("H:i", $timeAdvance) . "-" . date("H:i", $timeDelay);
|
||||
}
|
||||
//
|
||||
$macs = explode(",", $mac);
|
||||
@ -106,6 +112,7 @@ class PublicController extends AbstractController
|
||||
foreach ($macs as $mac) {
|
||||
$mac = strtoupper($mac);
|
||||
if (Base::isMac($mac) && $UserCheckinMac = UserCheckinMac::whereMac($mac)->first()) {
|
||||
$checkins[] = $UserCheckinMac;
|
||||
$array = [
|
||||
'userid' => $UserCheckinMac->userid,
|
||||
'mac' => $UserCheckinMac->mac,
|
||||
@ -114,26 +121,40 @@ class PublicController extends AbstractController
|
||||
$record = UserCheckinRecord::where($array)->first();
|
||||
if (empty($record)) {
|
||||
$record = UserCheckinRecord::createInstance($array);
|
||||
$checkins[] = $UserCheckinMac;
|
||||
}
|
||||
$record->times = Base::array2json(array_merge($record->times, [$nowTime]));
|
||||
$record->report_time = $time;
|
||||
$record->save();
|
||||
|
||||
}
|
||||
}
|
||||
//
|
||||
if ($checkins && $setting['notice'] === 'open') {
|
||||
$botUser = User::botGetOrCreate('check-in');
|
||||
if ($botUser) {
|
||||
if ($checkins && $botUser = User::botGetOrCreate('check-in')) {
|
||||
$sendMsg = function($type, UserCheckinMac $checkin) use ($botUser, $nowDate) {
|
||||
$cacheKey = "Checkin::sendMsg-{$nowDate}-{$type}:" . $checkin->userid;
|
||||
if (Cache::get($cacheKey) === "yes") {
|
||||
return;
|
||||
}
|
||||
Cache::put($cacheKey, "yes", Carbon::now()->addDay());
|
||||
//
|
||||
$dialog = WebSocketDialog::checkUserDialog($botUser->userid, $checkin->userid);
|
||||
if ($dialog) {
|
||||
$hi = date("H:i");
|
||||
$pre = $type == "up" ? "上班" : "下班";
|
||||
$remark = $checkin->remark ?: $checkin->mac;
|
||||
$text = "{$pre}签到成功,签到时间: {$hi} ({$remark})";
|
||||
WebSocketDialogMsg::sendMsg(null, $dialog->id, 'text', ['text' => $text], $botUser->userid);
|
||||
}
|
||||
};
|
||||
if ($timeAdvance <= Base::time() && Base::time() <= $timeStart + 3600) {
|
||||
// 上班签到(迟到1小时内仍提醒)
|
||||
foreach ($checkins as $checkin) {
|
||||
$dialog = WebSocketDialog::checkUserDialog($botUser->userid, $checkin->userid);
|
||||
if ($dialog) {
|
||||
$hi = date("H:i");
|
||||
$remark = $checkin->remark ?: $checkin->mac;
|
||||
$text = "签到成功,签到时间: {$hi} ({$remark})";
|
||||
WebSocketDialogMsg::sendMsg(null, $dialog->id, 'text', ['text' => $text], $botUser->userid); // todo 未能在任务end事件来发送任务
|
||||
}
|
||||
$sendMsg('up', $checkin);
|
||||
}
|
||||
}
|
||||
if ($timeEnd <= Base::time() && Base::time() <= $timeDelay) {
|
||||
// 下班签到
|
||||
foreach ($checkins as $checkin) {
|
||||
$sendMsg('down', $checkin);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -216,7 +216,7 @@ class SystemController extends AbstractController
|
||||
*
|
||||
* @apiParam {String} type
|
||||
* - get: 获取(默认)
|
||||
* - save: 保存设置(参数:['open', 'time', 'notice', 'edit', 'key'])
|
||||
* - save: 保存设置(参数:['open', 'time', 'advance', 'delay', 'edit', 'key'])
|
||||
* @apiSuccess {Number} ret 返回状态码(1正确、0错误)
|
||||
* @apiSuccess {String} msg 返回信息(错误描述)
|
||||
* @apiSuccess {Object} data 返回数据
|
||||
@ -235,7 +235,8 @@ class SystemController extends AbstractController
|
||||
if (!in_array($key, [
|
||||
'open',
|
||||
'time',
|
||||
'notice',
|
||||
'advance',
|
||||
'delay',
|
||||
'edit',
|
||||
'key',
|
||||
])) {
|
||||
@ -257,7 +258,8 @@ class SystemController extends AbstractController
|
||||
//
|
||||
$setting['open'] = $setting['open'] ?: 'close';
|
||||
$setting['time'] = $setting['time'] ? Base::json2array($setting['time']) : ['00:00', '23:59'];
|
||||
$setting['notice'] = $setting['notice'] ?: 'close';
|
||||
$setting['advance'] = intval($setting['advance']) ?: 120;
|
||||
$setting['delay'] = intval($setting['delay']) ?: 120;
|
||||
$setting['edit'] = $setting['edit'] ?: 'close';
|
||||
$setting['cmd'] = "curl -sSL '" . Base::fillUrl("api/public/checkin/install?key={$setting['key']}") . "' | sh";
|
||||
//
|
||||
|
||||
@ -20,14 +20,21 @@
|
||||
type="timerange"
|
||||
format="HH:mm"
|
||||
:placeholder="$L('请选择签到时间')"/>
|
||||
<div class="form-tip">{{$L('每日首次签到成功消息通知')}}</div>
|
||||
</FormItem>
|
||||
<FormItem :label="$L('签到通知')" prop="notice">
|
||||
<RadioGroup v-model="formData.notice">
|
||||
<Radio label="open">{{ $L('开启') }}</Radio>
|
||||
<Radio label="close">{{ $L('关闭') }}</Radio>
|
||||
</RadioGroup>
|
||||
<div class="form-tip">{{$L('每日首次签到成功消息通知')}}</div>
|
||||
<Form @submit.native.prevent>
|
||||
<FormItem :label="$L('最早可提前')" prop="advance">
|
||||
<div class="input-number-box">
|
||||
<InputNumber v-model="formData.advance" :min="0" :step="1"/>
|
||||
<label>{{ $L('分钟') }}</label>
|
||||
</div>
|
||||
</FormItem>
|
||||
<FormItem :label="$L('最晚可延后')" prop="delay">
|
||||
<div class="input-number-box">
|
||||
<InputNumber v-model="formData.delay" :min="0" :step="1"/>
|
||||
<label>{{ $L('分钟') }}</label>
|
||||
</div>
|
||||
</FormItem>
|
||||
</Form>
|
||||
<div class="form-tip">{{$L('签到前后时间收到消息通知')}}</div>
|
||||
</FormItem>
|
||||
<FormItem :label="$L('允许修改')" prop="edit">
|
||||
<RadioGroup v-model="formData.edit">
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user