|
|
|
|
@ -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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|