perf: 完善签到功能

This commit is contained in:
kuaifan 2022-12-12 08:27:51 +08:00
parent 6937bbace8
commit b2bb2ddb1f
9 changed files with 102 additions and 94 deletions

View File

@ -2,12 +2,9 @@
namespace App\Http\Controllers\Api;
use App\Exceptions\ApiException;
use App\Models\User;
use App\Models\UserCheckin;
use App\Models\UserCheckinMac;
use App\Models\UserCheckinRecord;
use App\Module\Base;
use Carbon\Carbon;
use Request;
/**
@ -94,10 +91,10 @@ class PublicController extends AbstractController
$macs = explode(",", $mac);
foreach ($macs as $item) {
$item = strtoupper($item);
if (Base::isMac($item) && $userCheckin = UserCheckin::whereMac($item)->first()) {
if (Base::isMac($item) && $UserCheckinMac = UserCheckinMac::whereMac($item)->first()) {
UserCheckinRecord::createInstance([
'userid' => $userCheckin->userid,
'mac' => $userCheckin->mac,
'userid' => $UserCheckinMac->userid,
'mac' => $UserCheckinMac->mac,
'time' => $time,
])->save();
}

View File

@ -848,53 +848,66 @@ class SystemController extends AbstractController
$headings[] = '成员名称';
$headings[] = '成员邮箱';
$headings[] = '签到日期';
$headings[] = '签到时间1';
$headings[] = '签到时间2';
$headings[] = '签到班次';
$headings[] = '首次签到时间';
$headings[] = '首次签到结果';
$headings[] = '最后签到时间';
$headings[] = '最后签到结果';
//
$sheets = [];
$start = Carbon::parse($date[0])->startOfDay();
$end = Carbon::parse($date[1])->endOfDay();
$startD = Carbon::parse($date[0])->startOfDay();
$endD = Carbon::parse($date[1])->endOfDay();
$users = User::whereIn('userid', $userid)->take(20)->get();
/** @var User $user */
foreach ($users as $user) {
$records = UserCheckinRecord::whereUserid($user->userid)->whereBetween("created_at", [$start, $end])->orderBy('id')->get();
$records = UserCheckinRecord::whereUserid($user->userid)->whereBetween("created_at", [$startD, $endD])->orderBy('id')->get();
//
$styles = ["A1:I1" => ["font" => ["bold" => true]]];
$datas = [];
$styles = [];
$startT = $start->timestamp;
$endT = $end->timestamp;
$startT = $startD->timestamp;
$endT = $endD->timestamp;
$index = 1;
while ($startT < $endT) {
$index++;
$first = $records->whereBetween("created_at", [Carbon::parse($startT), Carbon::parse($startT + $secondStart)])->first();
$last = $records->whereBetween("created_at", [Carbon::parse($startT + $secondEnd), Carbon::parse($startT + 86400)])->last();
$first = $first ? Carbon::parse($first->created_at)->timestamp : 0;
$last = $last ? Carbon::parse($last->created_at)->timestamp : 0;
if (empty($first) || $first > $startT + $secondStart) {
$styles["E{$index}"] = [
'font' => [
'color' => [
'rgb' => 'ff0000'
]
],
];
}
if (empty($last) || $last < $startT + $secondEnd) {
$styles["F{$index}"] = [
'font' => [
'color' => [
'rgb' => 'ff0000'
]
],
];
$firstBetween = [Carbon::createFromTimestamp($startT), Carbon::createFromTimestamp($startT + $secondEnd - 1)];
$lastBetween = [Carbon::createFromTimestamp($startT + $secondStart + 1), Carbon::createFromTimestamp($startT + 86400)];
$firstRecord = $records->whereBetween("created_at", $firstBetween)->first();
$lastRecord = $records->whereBetween("created_at", $lastBetween)->last();
$firstTimestamp = $firstRecord ? Carbon::parse($firstRecord->created_at)->timestamp : 0;
$lastTimestamp = $lastRecord ? Carbon::parse($lastRecord->created_at)->timestamp : 0;
if (Base::time() < $startT) {
$firstResult = "-";
$lastResult = "-";
} else {
$firstResult = "正常";
$lastResult = "正常";
if (empty($firstTimestamp)) {
$firstResult = "缺卡";
$styles["G{$index}"] = ["font" => ["color" => ["rgb" => "ff0000"]]];
} elseif ($firstTimestamp > $startT + $secondStart) {
$firstResult = "迟到";
$styles["G{$index}"] = ["font" => ["color" => ["rgb" => "436FF6"]]];
}
if (empty($lastTimestamp)) {
$lastResult = "缺卡";
$styles["I{$index}"] = ["font" => ["color" => ["rgb" => "ff0000"]]];
} elseif ($lastTimestamp < $startT + $secondEnd) {
$lastResult = "早退";
$styles["I{$index}"] = ["font" => ["color" => ["rgb" => "436FF6"]]];
}
}
$firstTimestamp = $firstTimestamp ? date("H:i", $firstTimestamp) : "-";
$lastTimestamp = $lastTimestamp ? date("H:i", $lastTimestamp) : "-";
$datas[] = [
$user->userid,
$user->nickname,
$user->email,
date("Y-m-d", $startT),
$first ? date("H:i", $first) : '-',
$last ? date("H:i", $last) : '-',
implode("-", $time),
$firstTimestamp,
$firstResult,
$lastTimestamp,
$lastResult,
];
$startT += 86400;
}

View File

@ -7,12 +7,11 @@ use App\Models\Meeting;
use App\Models\Project;
use App\Models\UmengAlias;
use App\Models\User;
use App\Models\UserCheckin;
use App\Models\UserCheckinMac;
use App\Models\UserDelete;
use App\Models\UserDepartment;
use App\Models\UserEmailVerification;
use App\Models\UserTransfer;
use App\Models\WebSocket;
use App\Models\WebSocketDialog;
use App\Models\WebSocketDialogMsg;
use App\Module\AgoraIO\AgoraTokenGenerator;
@ -506,13 +505,13 @@ class UsersController extends AbstractController
* - no: 未认证
* - 其他值: 全部(默认)
* - keys.department 部门ID0表示默认部门不赋值获取所有部门
* - keys.checkin_mac 签到mac地址
* - keys.checkin_mac 签到mac地址get_checkin_mac=1时有效)
*
* @apiParam {Number} [checkin_mac] 获取签到mac地址
* @apiParam {Number} [get_checkin_mac] 获取签到mac地址
* - 0: 不获取(默认)
* - 1: 获取
* @apiParam {Number} [page] 当前页,默认:1
* @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:50
* @apiParam {Number} [page] 当前页,默认:1
* @apiParam {Number} [pagesize] 每页显示数量,默认:20,最大:50
*
* @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述)
@ -525,6 +524,7 @@ class UsersController extends AbstractController
$builder = User::select(['*', 'nickname as nickname_original']);
//
$keys = Request::input('keys');
$getCheckinMac = intval(Request::input('get_checkin_mac')) === 1;
if (is_array($keys)) {
if ($keys['key']) {
if (str_contains($keys['key'], "@")) {
@ -577,9 +577,9 @@ class UsersController extends AbstractController
$builder->where("department", "like", "%,{$keys['department']},%");
}
}
if (isset($keys['checkin_mac'])) {
if ($getCheckinMac && isset($keys['checkin_mac'])) {
$builder->whereIn('userid', function ($query) use ($keys) {
$query->select('userid')->from('user_checkins')->where("mac", "like", "%{$keys['checkin_mac']}%");
$query->select('userid')->from('user_checkin_macs')->where("mac", "like", "%{$keys['checkin_mac']}%");
});
}
} else {
@ -587,9 +587,9 @@ class UsersController extends AbstractController
}
$list = $builder->orderByDesc('userid')->paginate(Base::getPaginate(50, 20));
//
if (intval(Request::input('checkin_mac')) === 1) {
if ($getCheckinMac) {
$list->transform(function (User $user) {
$user->checkin_macs = UserCheckin::whereUserid($user->userid)->orderBy('id')->pluck('mac');
$user->checkin_macs = UserCheckinMac::whereUserid($user->userid)->orderBy('id')->pluck('mac');
return $user;
});
}
@ -666,7 +666,7 @@ class UsersController extends AbstractController
];
}
}
return UserCheckin::saveMac($userInfo->userid, $array);
return UserCheckinMac::saveMac($userInfo->userid, $array);
case 'department':
if (!is_array($data['department'])) {
@ -1336,7 +1336,7 @@ class UsersController extends AbstractController
{
$user = User::auth();
//
$list = UserCheckin::whereUserid($user->userid)->orderBy('id')->get();
$list = UserCheckinMac::whereUserid($user->userid)->orderBy('id')->get();
//
return Base::retSuccess('success', $list);
}
@ -1386,6 +1386,6 @@ class UsersController extends AbstractController
return Base::retError('最多只能添加3个MAC地址');
}
//
return UserCheckin::saveMac($user->userid, $array);
return UserCheckinMac::saveMac($user->userid, $array);
}
}

View File

@ -6,7 +6,7 @@ namespace App\Models;
use App\Module\Base;
/**
* App\Models\UserCheckin
* App\Models\UserCheckinMac
*
* @property int $id
* @property int|null $userid 会员id
@ -14,18 +14,18 @@ use App\Module\Base;
* @property string|null $remark 备注
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @method static \Illuminate\Database\Eloquent\Builder|UserCheckin newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|UserCheckin newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|UserCheckin query()
* @method static \Illuminate\Database\Eloquent\Builder|UserCheckin whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserCheckin whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserCheckin whereMac($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserCheckin whereRemark($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserCheckin whereUpdatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserCheckin whereUserid($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserCheckinMac newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|UserCheckinMac newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|UserCheckinMac query()
* @method static \Illuminate\Database\Eloquent\Builder|UserCheckinMac whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserCheckinMac whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserCheckinMac whereMac($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserCheckinMac whereRemark($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserCheckinMac whereUpdatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserCheckinMac whereUserid($value)
* @mixin \Eloquent
*/
class UserCheckin extends AbstractModel
class UserCheckinMac extends AbstractModel
{
/**
* 保存mac地址
@ -45,7 +45,7 @@ class UserCheckin extends AbstractModel
'remark' => $item['remark']
];
}
$row = UserCheckin::updateInsert([
$row = self::updateInsert([
'userid' => $userid,
'mac' => $item['mac']
], $update);
@ -54,7 +54,7 @@ class UserCheckin extends AbstractModel
$list[] = $row;
}
}
UserCheckin::whereUserid($userid)->whereNotIn('id', $ids)->delete();
self::whereUserid($userid)->whereNotIn('id', $ids)->delete();
//
return Base::retSuccess('修改成功', $list);
});

View File

@ -4,7 +4,7 @@ use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUserCheckinsTable extends Migration
class CreateUserCheckinMacsTable extends Migration
{
/**
* Run the migrations.
@ -13,7 +13,7 @@ class CreateUserCheckinsTable extends Migration
*/
public function up()
{
Schema::create('user_checkins', function (Blueprint $table) {
Schema::create('user_checkin_macs', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('userid')->nullable()->default(0)->comment('会员id');
$table->string('mac', 100)->nullable()->default('')->comment('MAC地址');
@ -29,6 +29,6 @@ class CreateUserCheckinsTable extends Migration
*/
public function down()
{
Schema::dropIfExists('user_checkins');
Schema::dropIfExists('user_checkin_macs');
}
}

View File

@ -91,7 +91,7 @@
</Select>
</div>
</li>
<li v-if="mode==='checkin_mac'">
<li v-if="checkinMac">
<div class="search-label">
{{$L("MAC地址")}}
</div>
@ -284,9 +284,9 @@ export default {
name: "TeamManagement",
components: {UserInput},
props: {
mode: {
type: String,
default: 'user'
checkinMac: {
type: Boolean,
default: false
},
},
data() {
@ -496,7 +496,7 @@ export default {
},
}, [h('div', this.$L('修改密码'))]))
if (this.mode === 'checkin_mac') {
if (this.checkinMac) {
dropdownItems.push(h('EDropdownItem', {
props: {
command: 'checkin_mac',
@ -657,7 +657,7 @@ export default {
}
},
created() {
if (this.mode === 'checkin_mac') {
if (this.checkinMac) {
this.columns.splice(5, 0, {
title: this.$L('MAC地址'),
key: 'checkin_mac',
@ -734,7 +734,7 @@ export default {
url: 'users/lists',
data: {
keys,
checkin_mac: this.mode === 'checkin_mac' ? 1 : 0,
get_checkin_mac: this.checkinMac ? 1 : 0,
page: Math.max(this.page, 1),
pagesize: Math.max($A.runNum(this.pageSize), 10),
},

View File

@ -1,11 +1,11 @@
<template>
<div class="setting-item submit">
<Form ref="formData" label-width="auto" @submit.native.prevent>
<Alert show-icon style="margin-bottom:18px">
{{$L('手机连接上指定WIFI后自动签到。')}}
<Alert style="margin-bottom:18px">
{{$L('设备连接上指定路由器WiFi后自动签到。')}}
</Alert>
<Row class="setting-template">
<Col span="12">{{$L('MAC地址')}}</Col>
<Col span="12">{{$L('设备MAC地址')}}</Col>
<Col span="12">{{$L('备注')}}</Col>
</Row>
<Row v-for="(item, key) in formData" :key="key" class="setting-template">
@ -13,7 +13,7 @@
<Input
v-model="item.mac"
:maxlength="20"
:placeholder="$L('请输入MAC地址')"
:placeholder="$L('请输入设备MAC地址')"
clearable
@on-clear="delDatum(key)"/>
</Col>
@ -21,7 +21,7 @@
<Input v-model="item.remark" :maxlength="100" :placeholder="$L('备注')"/>
</Col>
</Row>
<Button type="default" icon="md-add" @click="addDatum">{{$L('添加地址')}}</Button>
<Button type="default" icon="md-add" @click="addDatum">{{$L('添加设备')}}</Button>
</Form>
<div class="setting-footer">
<Button :loading="loadIng > 0" type="primary" @click="submitForm">{{$L('提交')}}</Button>

View File

@ -2,7 +2,7 @@
<div class="setting-component-item">
<Form ref="formData" :model="formData" :rules="ruleData" label-width="auto" @submit.native.prevent>
<div class="block-setting-box">
<h3>{{ $L('WIFI自动签到') }}</h3>
<h3>{{ $L('自动签到') }}</h3>
<FormItem :label="$L('功能开启')" prop="open">
<RadioGroup v-model="formData.open">
<Radio label="open">{{ $L('开启') }}</Radio>
@ -21,15 +21,12 @@
</RadioGroup>
<div class="form-tip">{{$L('允许成员自己修改MAC地址')}}</div>
</FormItem>
<FormItem :label="$L('功能说明')" prop="explain">
<p>1. {{$L('此功能仅支持手机客户端使用。')}}</p>
<p>2. {{$L('手机连接上指定路由器WIFI后自动签到。')}}{{$L('(注:不限制连接方式)')}}</p>
<p>3. {{$L('签到延迟时长为±1分钟。')}}</p>
</FormItem>
<FormItem :label="$L('安装说明')" prop="install">
<p>1. {{$L('此功能仅支持Openwrt系统的路由器。')}}</p>
<p>2. {{$L('关闭签到功能再开启需要重新安装。')}}</p>
<p>3. {{$L('进入路由器终端执行以下命令即可完成安装:')}}</p>
<FormItem :label="$L('安装说明')" prop="explain">
<p>1. {{$L('签到延迟时长为±1分钟。')}}</p>
<p>2. {{$L('设备连接上指定路由器WiFi后自动签到。')}}</p>
<p>3. {{$L('仅支持Openwrt系统的路由器。')}}</p>
<p>4. {{$L('关闭签到功能再开启需要重新安装。')}}</p>
<p>5. {{$L('进入路由器终端执行以下命令即可完成安装:')}}</p>
<Input ref="cmd" @on-focus="clickCmd" style="margin-top:6px" type="textarea" readonly :value="formData.cmd"/>
</FormItem>
</template>
@ -59,17 +56,17 @@
:placeholder="$L('请选择签到日期')"/>
<div class="form-tip page-setting-checkin-export-common">
{{$L('快捷选择')}}:
<em @click="exportData.date=dateShortcuts('prev')">上个月</em>
<em @click="exportData.date=dateShortcuts('this')">这个月</em>
<em @click="exportData.date=dateShortcuts('prev')">{{$L('上个月')}}</em>
<em @click="exportData.date=dateShortcuts('this')">{{$L('这个月')}}</em>
</div>
</FormItem>
<FormItem :label="$L('签到时间')">
<FormItem :label="$L('签到班次')">
<TimePicker
v-model="exportData.time"
type="timerange"
format="HH:mm"
style="width:100%"
:placeholder="$L('请选择签到时间')"/>
:placeholder="$L('请选择签到班次')"/>
<div class="form-tip page-setting-checkin-export-common">
{{$L('快捷选择')}}:
<em @click="exportData.time=['8:30', '18:00']">8:30-18:00</em>
@ -89,7 +86,7 @@
v-model="allUserShow"
placement="right"
:size="1380">
<TeamManagement v-if="allUserShow" mode="checkin_mac"/>
<TeamManagement v-if="allUserShow" checkin-mac/>
</DrawerOverlay>
</div>
</template>

View File

@ -96,6 +96,7 @@
}
.management-user {
flex: 1;
width: 0;
display: flex;
flex-direction: column;
padding-left: 20px;