liyinglin 3 years ago
parent 0e670705d2
commit 47eca696bd

@ -2,10 +2,13 @@
namespace App\Http\Controllers\Admin;
use App\Helpers\ResponseCode;
use App\Models\Admin;
use App\Models\Department;
use App\Models\OperateLog;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Rap2hpoutre\FastExcel\FastExcel;
use Spatie\Permission\Models\Role;
class AdminController extends CommonController
@ -240,4 +243,65 @@ class AdminController extends CommonController
}
}
/**
* @OA\Post (
* path="/api/admin/import",
* tags={"后台管理"},
* summary="导入数据",
* description="",
* @OA\Parameter(name="file", in="query", @OA\Schema(type="object"), required=true, description="文件"),
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Response(
* response="200",
* description="导入用户"
* )
* )
*/
public function import(Request $request)
{
$file = $request->file('file');
//判断文件是否有效
if (!($request->hasFile('file') && $file->isValid())) {
return $this->fail([ResponseCode::ERROR_BUSINESS, '文件不存在或无效']);
}
//获取文件大小
$img_size = floor($file->getSize() / 1024);
if ($img_size >= 5 * 1024) {
return $this->fail([ResponseCode::ERROR_BUSINESS, '文件必须小于5M']);
}
//过滤文件后缀
$ext = $file->getClientOriginalExtension();
if (!in_array($ext, ['xls', 'xlsx', 'csv'])) {
return $this->fail([ResponseCode::ERROR_BUSINESS, '仅支持xls/xlsx/csv格式']);
}
$tempFile = $file->getRealPath();
$dataArray = (new FastExcel)->import($tempFile)->toArray();
// 获取所有key
$keyList = array_keys($dataArray[0]);
if (!in_array('GID登录用户名', $keyList)) {
return $this->fail([ResponseCode::ERROR_BUSINESS, 'GID登录用户名字段不存在']);
}
if (!in_array('部门', $keyList)) {
return $this->fail([ResponseCode::ERROR_BUSINESS, '部门字段不存在']);
}
if (!in_array('姓名', $keyList)) {
return $this->fail([ResponseCode::ERROR_BUSINESS, '姓名字段不存在']);
}
if (!in_array('手机号码', $keyList)) {
return $this->fail([ResponseCode::ERROR_BUSINESS, '手机号码字段不存在']);
}
$list = [];
foreach ($dataArray as $key => $value) {
$departmentId = Department::where('name', $value['部门'])->value('id');
$whereArray = ['name' => $value['姓名']];
$updateDataArray = ['name' => $value['姓名'], 'username' => $value['GID登录用户名'],
'department_id' => $departmentId,
'mobile' => $value['手机号码'],
'password'=> \Illuminate\Support\Facades\Hash::make("Admin" . date("Y"))
];
Admin::updateOrCreate($whereArray, $updateDataArray);
}
return $this->success($list);
}
}

@ -23,6 +23,7 @@ Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
Route::get("admin/gate/user-list", [\App\Http\Controllers\Admin\GateController::class, "uerList"]);
Route::get("admin/gate/visit-list", [\App\Http\Controllers\Admin\GateController::class, "visitList"]);
Route::get("admin/gate/use-code", [\App\Http\Controllers\Admin\GateController::class, "useCode"]);
Route::get("admin/import", [\App\Http\Controllers\Admin\AdminController::class, "import"]);
// 后台
Route::group(["namespace" => "Admin", "prefix" => "admin", "middleware" => "sanctum.jwt:admin,rbac"], function () {

Loading…
Cancel
Save