diff --git a/plugin/admin/app/controller/StudentController.php b/plugin/admin/app/controller/StudentController.php index 1225479..ef7c920 100644 --- a/plugin/admin/app/controller/StudentController.php +++ b/plugin/admin/app/controller/StudentController.php @@ -5,6 +5,8 @@ namespace plugin\admin\app\controller; use app\common\model\StudentParent; use app\common\model\StudentSchedule; use app\constant\ResponseCode; +use PhpOffice\PhpSpreadsheet\Cell\Coordinate; +use PhpOffice\PhpSpreadsheet\IOFactory; use support\Request; use support\Response; use plugin\admin\app\model\Student; @@ -38,7 +40,10 @@ class StudentController extends Crud */ public function index(): Response { - return view('student/index'); + // 导入模板文件 + $import_example = 'https://lxpk.lingruikj.com/files/xlsx/202408/2024080322474292.xlsx'; + $example_name = '学生家长导入模板数据.xlsx'; + return view('student/index', ['import_example' => $import_example, 'example_name' => $example_name]); } public function select(Request $request): Response @@ -47,10 +52,16 @@ class StudentController extends Crud $data = $request->get(); - $student = \app\common\model\Student::order('id asc'); + $student = \app\common\model\Student::order('id desc'); if (isset($data['id']) && $data['id']) { $student->where(['id' => $data['id']]); } + if(isset($data['account']) && $data['account']){ + $student->where('account' ,'like', '%' . $data['account'] . '%'); + } + if(isset($data['student_name']) && $data['student_name']){ + $student->where('student_name' ,'like', '%' . $data['student_name'] . '%'); + } $limit = (int)$request->get('limit', 10); $limit = $limit <= 0 ? 10 : $limit; @@ -264,4 +275,109 @@ class StudentController extends Crud } } + /** + * @desc 导入教师数据 + * @param Request $request + * @return Response + */ + public function importStudentAccount(Request $request) + { + try { + ini_set('memory_limit', '256M'); + $file = $request->file('file'); + if ($file && $file->isValid()) { + $ext = $file->getUploadExtension(); + if (!in_array($ext, ['xlsx', 'xls', 'csv'])) { + throw new Exception('上传文件格式错误'); + } + $uploadDir = '/files/xlsx/' . date('Ym') . '/'; + $filename = date('YmdHis') . rand(10, 99); + $uploadPath = $uploadDir . $filename . '.' . $ext; + $rootUploadDir = public_path($uploadPath); + $file->move($rootUploadDir); + + //读取表格数据,插入数据库 + $objRead = IOFactory::createReader('Xlsx'); + + if (!$objRead->canRead($rootUploadDir)) { + /** @var Xls $objRead */ + $objRead = IOFactory::createReader('Xls'); + + if (!$objRead->canRead($rootUploadDir)) { + throw new Exception('只支持导入Excel文件!'); + } + } + /* 如果不需要获取特殊操作,则只读内容,可以大幅度提升读取Excel效率 */ + $objRead->setReadDataOnly(true); + + /* 建立excel对象 */ + $obj = $objRead->load($rootUploadDir); + /* 获取指定的sheet表 */ + $currSheet = $obj->getSheet(0); + /* 取得最大的列号 */ + $columnH = $currSheet->getHighestColumn(); + /* 兼容原逻辑,循环时使用的是小于等于 */ + $columnCnt = Coordinate::columnIndexFromString($columnH); + + /* 获取总行数 */ + $rowCnt = $currSheet->getHighestRow(); + + if ($rowCnt <= 1) { + throw new Exception('不能上传空数据'); + } + + $success_count = 0; + $error_count = 0; + $msg = ''; + // 遍历每一行 + + for ($row = 2; $row <= $rowCnt; $row++) { + // 遍历每一列 + $account = trim($currSheet->getCell('A' . $row)->getValue() ?: ''); + $student_name = trim($currSheet->getCell('B' . $row)->getValue() ?: ''); + if($account && $student_name){ + $password = trim($currSheet->getCell('C' . $row)->getValue() ?: ''); + $salt = random_str(16); + if (empty($password)) { + $password = trim(explode(' ', $account)[0]) . '001'; + } + //查找教师数据是否存在 + $parent = \app\common\model\Student::where(['account' => $account, 'student_name' => $student_name])->findOrEmpty(); + if ($parent->isEmpty()) { + $password = md5($password . $salt); + \app\common\model\Student::create([ + 'account' => $account, + 'student_name' => $student_name, + 'password' => $password, + 'salt' => $salt, + ]); + $success_count++; + } else { + $error_count++; + $msg .= '【' . $student_name . '】'; + } + } + + + } + } + $return_msg = '成功【' . $success_count . '】条,' . '失败【' . $error_count . '】条'; + if ($error_count) { + $return_msg .= ',失败数据' . $msg . '已存在'; + } + + return json([ + 'code' => ResponseCode::WEB_API_SUCCESS, + 'data' => [], + 'msg' => $return_msg + ]); + } catch (Exception $e) { + return json([ + 'code' => ResponseCode::WEB_API_FAIL, + 'msg' => $e->getMessage() + ]); + } + } + + } diff --git a/plugin/admin/app/controller/StudentParentController.php b/plugin/admin/app/controller/StudentParentController.php index bf12c54..0720dc5 100644 --- a/plugin/admin/app/controller/StudentParentController.php +++ b/plugin/admin/app/controller/StudentParentController.php @@ -3,6 +3,8 @@ namespace plugin\admin\app\controller; use app\constant\ResponseCode; +use PhpOffice\PhpSpreadsheet\Cell\Coordinate; +use PhpOffice\PhpSpreadsheet\IOFactory; use support\Request; use support\Response; use plugin\admin\app\model\StudentParent; @@ -11,11 +13,11 @@ use support\exception\BusinessException; use think\Exception; /** - * 家长管理 + * 家长管理 */ class StudentParentController extends Crud { - + /** * @var StudentParent */ @@ -29,14 +31,17 @@ class StudentParentController extends Crud { $this->model = new StudentParent; } - + /** * 浏览 * @return Response */ public function index(): Response { - return view('student-parent/index'); + // 导入模板文件 + $import_example = 'https://lxpk.lingruikj.com/files/xlsx/202408/2024080322474292.xlsx'; + $example_name = '学生家长导入模板数据.xlsx'; + return view('student-parent/index', ['import_example' => $import_example, 'example_name' => $example_name]); } public function select(Request $request): Response @@ -113,7 +118,7 @@ class StudentParentController extends Crud * @param Request $request * @return Response * @throws BusinessException - */ + */ public function update(Request $request): Response { if ($request->method() === 'POST') { @@ -180,7 +185,7 @@ class StudentParentController extends Crud 'salt' => $salt, 'password' => $password ]); - if(!$res){ + if (!$res) { throw new Exception('重置失败'); } @@ -196,4 +201,110 @@ class StudentParentController extends Crud } } + + /** + * @desc 导入教师数据 + * @param Request $request + * @return Response + */ + public function importParentAccount(Request $request) + { + try { + ini_set('memory_limit', '256M'); + $file = $request->file('file'); + if ($file && $file->isValid()) { + $ext = $file->getUploadExtension(); + if (!in_array($ext, ['xlsx', 'xls', 'csv'])) { + throw new Exception('上传文件格式错误'); + } + $uploadDir = '/files/xlsx/' . date('Ym') . '/'; + $filename = date('YmdHis') . rand(10, 99); + $uploadPath = $uploadDir . $filename . '.' . $ext; + $rootUploadDir = public_path($uploadPath); + $file->move($rootUploadDir); + + + //读取表格数据,插入数据库 + $objRead = IOFactory::createReader('Xlsx'); + + if (!$objRead->canRead($rootUploadDir)) { + /** @var Xls $objRead */ + $objRead = IOFactory::createReader('Xls'); + + if (!$objRead->canRead($rootUploadDir)) { + throw new Exception('只支持导入Excel文件!'); + } + } + /* 如果不需要获取特殊操作,则只读内容,可以大幅度提升读取Excel效率 */ + $objRead->setReadDataOnly(true); + + /* 建立excel对象 */ + $obj = $objRead->load($rootUploadDir); + /* 获取指定的sheet表 */ + $currSheet = $obj->getSheet(0); + /* 取得最大的列号 */ + $columnH = $currSheet->getHighestColumn(); + /* 兼容原逻辑,循环时使用的是小于等于 */ + $columnCnt = Coordinate::columnIndexFromString($columnH); + + /* 获取总行数 */ + $rowCnt = $currSheet->getHighestRow(); + + if ($rowCnt <= 1) { + throw new Exception('不能上传空数据'); + } + + $success_count = 0; + $error_count = 0; + $msg = ''; + // 遍历每一行 + + for ($row = 2; $row <= $rowCnt; $row++) { + // 遍历每一列 + $account = trim($currSheet->getCell('A' . $row)->getValue() ?: ''); + $parent_name = trim($currSheet->getCell('B' . $row)->getValue() ?: ''); + if($account && $parent_name){ + $password = trim($currSheet->getCell('C' . $row)->getValue() ?: ''); + $salt = random_str(16); + if (empty($password)) { + $password = trim(mb_substr($account, 0, -2)) . '001'; + } + //查找教师数据是否存在 + $parent = \app\common\model\StudentParent::where(['account' => $account, 'parent_name' => $parent_name])->findOrEmpty(); + if ($parent->isEmpty()) { + $password = md5($password . $salt); + \app\common\model\StudentParent::create([ + 'account' => $account, + 'parent_name' => $parent_name, + 'password' => $password, + 'salt' => $salt, + ]); + $success_count++; + } else { + $error_count++; + $msg .= '【' . $parent_name . '】'; + } + } + + + } + } + $return_msg = '成功【' . $success_count . '】条,' . '失败【' . $error_count . '】条'; + if ($error_count) { + $return_msg .= ',失败数据' . $msg . '已存在'; + } + + return json([ + 'code' => ResponseCode::WEB_API_SUCCESS, + 'data' => [], + 'msg' => $return_msg + ]); + } catch (Exception $e) { + return json([ + 'code' => ResponseCode::WEB_API_FAIL, + 'msg' => $e->getMessage() + ]); + } + } + } diff --git a/plugin/admin/app/controller/TeacherController.php b/plugin/admin/app/controller/TeacherController.php index 40888b5..26498b1 100644 --- a/plugin/admin/app/controller/TeacherController.php +++ b/plugin/admin/app/controller/TeacherController.php @@ -8,6 +8,8 @@ use app\common\model\Subject; use app\common\model\TeacherScheduleTime; use app\common\model\TimeZone; use app\constant\ResponseCode; +use PhpOffice\PhpSpreadsheet\Cell\Coordinate; +use PhpOffice\PhpSpreadsheet\IOFactory; use support\Request; use support\Response; use plugin\admin\app\model\Teacher; @@ -41,7 +43,10 @@ class TeacherController extends Crud */ public function index(): Response { - return view('teacher/index'); + // 导入模板文件 + $import_example = 'https://lxpk.lingruikj.com/files/xlsx/202408/2024080322192257.xlsx'; + $example_name = '教师导入模板数据.xlsx'; + return view('teacher/index', ['import_example' => $import_example, 'example_name' => $example_name]); } /** @@ -331,4 +336,111 @@ class TeacherController extends Crud } } + + /** + * @desc 导入教师数据 + * @param Request $request + * @return Response + */ + public function importTeacherAccount(Request $request) + { + try { + ini_set('memory_limit', '256M'); + $file = $request->file('file'); + if ($file && $file->isValid()) { + $ext = $file->getUploadExtension(); + if (!in_array($ext, ['xlsx', 'xls', 'csv'])) { + throw new Exception('上传文件格式错误'); + } + $uploadDir = '/files/xlsx/' . date('Ym') . '/'; + $filename = date('YmdHis') . rand(10, 99); + $uploadPath = $uploadDir . $filename . '.' . $ext; + $rootUploadDir = public_path($uploadPath); + $file->move($rootUploadDir); + + + //读取表格数据,插入数据库 + $objRead = IOFactory::createReader('Xlsx'); + + if (!$objRead->canRead($rootUploadDir)) { + /** @var Xls $objRead */ + $objRead = IOFactory::createReader('Xls'); + + if (!$objRead->canRead($rootUploadDir)) { + throw new Exception('只支持导入Excel文件!'); + } + } + /* 如果不需要获取特殊操作,则只读内容,可以大幅度提升读取Excel效率 */ + $objRead->setReadDataOnly(true); + + /* 建立excel对象 */ + $obj = $objRead->load($rootUploadDir); + /* 获取指定的sheet表 */ + $currSheet = $obj->getSheet(0); + /* 取得最大的列号 */ + $columnH = $currSheet->getHighestColumn(); + /* 兼容原逻辑,循环时使用的是小于等于 */ + $columnCnt = Coordinate::columnIndexFromString($columnH); + + /* 获取总行数 */ + $rowCnt = $currSheet->getHighestRow(); + + if ($rowCnt <= 1) { + throw new Exception('不能上传空数据'); + } + + $success_count = 0; + $error_count = 0; + $msg = ''; + // 遍历每一行 + for ($row = 2; $row <= $rowCnt; $row++) { + // 遍历每一列 + + $account = trim($currSheet->getCell('A' . $row)->getValue() ?: ''); + $teacher_name = trim($currSheet->getCell('B' . $row)->getValue() ?: ''); + + if($account && $teacher_name){ + + $password = trim($currSheet->getCell('C' . $row)->getValue() ?: ''); + $salt = random_str(16); + if (empty($password)) { + $password = 'YD' . $account . '123'; + } + //查找教师数据是否存在 + $teacher = \app\common\model\Teacher::where(['account' => $account, 'teacher_name' => $teacher_name])->findOrEmpty(); + if ($teacher->isEmpty()) { + $password = md5($password . $salt); + \app\common\model\Teacher::create([ + 'account' => $account, + 'teacher_name' => $teacher_name, + 'password' => $password, + 'salt' => $salt, + ]); + $success_count++; + } else { + $error_count++; + $msg .= '【' . $teacher_name . '】'; + } + } + + } + } + $return_msg = '成功【' . $success_count . '】条,' . '失败【' . $error_count . '】条'; + if ($error_count) { + $return_msg .= ',失败数据' . $msg . '已存在'; + } + + return json([ + 'code' => ResponseCode::WEB_API_SUCCESS, + 'data' => [], + 'msg' => $return_msg + ]); + } catch (Exception $e) { + return json([ + 'code' => ResponseCode::WEB_API_FAIL, + 'msg' => $e->getMessage() + ]); + } + } + } diff --git a/plugin/admin/app/view/student-feedback/index.html b/plugin/admin/app/view/student-feedback/index.html index 341a72a..66a5abf 100644 --- a/plugin/admin/app/view/student-feedback/index.html +++ b/plugin/admin/app/view/student-feedback/index.html @@ -107,8 +107,8 @@ diff --git a/plugin/admin/app/view/student-homework/index.html b/plugin/admin/app/view/student-homework/index.html index da915b3..ebe34fc 100644 --- a/plugin/admin/app/view/student-homework/index.html +++ b/plugin/admin/app/view/student-homework/index.html @@ -98,8 +98,8 @@ diff --git a/plugin/admin/app/view/student-parent/index.html b/plugin/admin/app/view/student-parent/index.html index 8846377..24a7710 100644 --- a/plugin/admin/app/view/student-parent/index.html +++ b/plugin/admin/app/view/student-parent/index.html @@ -56,6 +56,9 @@ + + @@ -82,12 +92,30 @@ // 相关常量 const PRIMARY_KEY = "id"; - const SELECT_API = "/app/admin/student-parent/select"; + const SELECT_API = "/app/admin/student-parent/select?field=id&order=desc"; const UPDATE_API = "/app/admin/student-parent/update"; const DELETE_API = "/app/admin/student-parent/delete"; const INSERT_URL = "/app/admin/student-parent/insert"; const UPDATE_URL = "/app/admin/student-parent/update"; - + + + + // 字段 头像 avatar + layui.use(["upload", "layer"], function() { + layui.upload.render({ + elem: "#upload_parent_info", + accept: 'file', //普通文件 + acceptMime: ".xls,.xlsx", + url: "/app/admin/studentParent/importParentAccount", + field: "file", + done: function (res) { + layer.alert(res.msg, {icon: 1}, function(index){ + layer.close(index); + }); + } + }); + }); + // 表格渲染 layui.use(["table", "form", "common", "popup", "util"], function() { let table = layui.table; @@ -195,7 +223,20 @@ table.on("toolbar(data-table)", function(obj) { if (obj.event === "add") { add(); - } else if (obj.event === "refresh") { + }else if (obj.event === "import") { + import_file(); + } else if (obj.event === "download_example") { + const link = document.createElement('a'); + link.style.display = 'none'; + // 设置下载地址 + link.setAttribute('href', '{$import_example}'); + // 设置文件名 + link.setAttribute('download', '{$example_name}'); + document.body.appendChild(link); + link.click(); + document.body.removeChild(link); + // window.location.href = '{$import_example}'; + } else if (obj.event === "refresh") { refreshTable(); } else if (obj.event === "batchRemove") { batchRemove(obj); @@ -242,6 +283,10 @@ }); }); + let import_file = function() { + $('#upload_parent_info').click(); + } + // 表格新增数据 let add = function() { layer.open({ diff --git a/plugin/admin/app/view/student-schedule/index.html b/plugin/admin/app/view/student-schedule/index.html index 7e58be7..1430f6e 100644 --- a/plugin/admin/app/view/student-schedule/index.html +++ b/plugin/admin/app/view/student-schedule/index.html @@ -126,7 +126,7 @@ diff --git a/plugin/admin/app/view/student/index.html b/plugin/admin/app/view/student/index.html index caf7402..2338f13 100644 --- a/plugin/admin/app/view/student/index.html +++ b/plugin/admin/app/view/student/index.html @@ -9,6 +9,45 @@
+