course/plugin/admin/app/controller/StudentParentController.php
2025-01-18 15:04:13 +08:00

354 lines
12 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
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;
use plugin\admin\app\controller\Crud;
use support\exception\BusinessException;
use think\Exception;
/**
* 家长管理
*/
class StudentParentController extends Crud
{
/**
* @var StudentParent
*/
protected $model = null;
/**
* 构造函数
* @return void
*/
public function __construct()
{
$this->model = new StudentParent;
}
/**
* 浏览
* @return Response
*/
public function index(): Response
{
// 导入模板文件
$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
{
try {
$parent = \app\common\model\StudentParent::order('id desc');
$data = $request->get();
if (isset($data['parent_name']) && !empty($data['parent_name'])) {
$parent->where('parent_name', 'like', '%' . $data['parent_name'] . '%');
}
if (isset($data['account']) && !empty($data['account'])) {
$parent->where('account', 'like', '%' . $data['account'] . '%');
}
$limit = (int)$request->get('limit', 10);
$limit = $limit <= 0 ? 10 : $limit;
$page = (int)$request->get('page');
$page = $page > 0 ? $page : 1;
$total = $parent->count();
$list = $parent->page($page, $limit)->select()->toArray();
return json([
'code' => ResponseCode::WEB_API_SUCCESS,
'data' => $list,
'count' => $total,
'msg' => 'success'
]);
} catch (Exception $e) {
return json([
'code' => ResponseCode::WEB_API_FAIL,
'msg' => $e->getMessage()
]);
}
}
/**
* 插入
* @param Request $request
* @return Response
* @throws BusinessException
*/
public function insert(Request $request): Response
{
if ($request->method() === 'POST') {
$request_data = $request->post();
$salt = random_str(16);
if (empty($request_data['password'])) {
$password = trim(mb_substr($request_data['account'], 0, -2)) . '001';
$password = md5($password . $salt);
} else {
$password = md5($request_data['password'] . $salt);
}
$res = \app\common\model\StudentParent::create([
'parent_name' => $request_data['parent_name'],
'account' => $request_data['account'],
'password' => $password,
'salt' => $salt,
'avatar' => $request_data['avatar'],
]);
return json([
'code' => ResponseCode::WEB_API_SUCCESS,
'msg' => '添加成功'
]);
// return parent::insert($request);
}
return view('student-parent/insert');
}
/**
* 更新
* @param Request $request
* @return Response
* @throws BusinessException
*/
public function update(Request $request): Response
{
if ($request->method() === 'POST') {
try {
$request_data = $request->post();
$student = \app\common\model\StudentParent::where(['id' => $request_data['id']])->findOrEmpty();
if ($student->isEmpty()) {
throw new Exception('未找到家长信息,操作失败');
}
if (empty($request_data['password'])) {
$update = [
'parent_name' => $request_data['parent_name'],
'account' => $request_data['account'],
'avatar' => $request_data['avatar'],
];
} else {
$salt = random_str(16);
$password = md5($request_data['password'] . $salt);
$update = [
'parent_name' => $request_data['parent_name'],
'account' => $request_data['account'],
'password' => $password,
'salt' => $salt,
'avatar' => $request_data['avatar'],
];
}
$student->save($update);
return json([
'code' => ResponseCode::WEB_API_SUCCESS,
'msg' => 'success'
]);
} catch (Exception $e) {
return json([
'code' => ResponseCode::WEB_API_FAIL,
'msg' => $e->getMessage()
]);
}
return parent::update($request);
}
return view('student-parent/update');
}
/**
* @desc 重置openid
* @param Request $request
* @return Response
*/
public function resetOpenid(Request $request)
{
try {
$request_data = $request->post();
$parent = \app\common\model\StudentParent::where(['id' => $request_data['id']])->findOrEmpty();
if($parent->isEmpty()){
throw new Exception('未找到此账号信息');
}
$res = $parent->save([
'openid' => '',
'avatar' => '',
]);
if (!$res) {
throw new Exception('重置失败');
}
return json([
'code' => ResponseCode::WEB_API_SUCCESS,
'msg' => '重置成功'
]);
} catch (Exception $e) {
return json([
'code' => ResponseCode::WEB_API_FAIL,
'msg' => $e->getMessage()
]);
}
}
/**
* @desc 重置密码
* @param Request $request
* @return Response
*/
public function resetPassword(Request $request)
{
try {
$request_data = $request->post();
$parent = \app\common\model\StudentParent::where(['id' => $request_data['id']])->findOrEmpty();
$new_password = trim(mb_substr($parent->account, 0, -2)) . '001';
$salt = random_str(16);
$password = md5($new_password . $salt);
$res = $parent->save([
'salt' => $salt,
'password' => $password
]);
if (!$res) {
throw new Exception('重置失败');
}
return json([
'code' => ResponseCode::WEB_API_SUCCESS,
'msg' => '重置成功'
]);
} catch (Exception $e) {
return json([
'code' => ResponseCode::WEB_API_FAIL,
'msg' => $e->getMessage()
]);
}
}
/**
* @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 = '';
$check_msg = '';
// 遍历每一行
for ($row = 2; $row <= $rowCnt; $row++) {
// 遍历每一列
$account = trim($currSheet->getCell('A' . $row)->getValue() ?: '');
$parent_name = trim($currSheet->getCell('B' . $row)->getValue() ?: '');
// if(check_chinese_chars($parent_name)){
// $check_msg .= '【'+ $parent_name +'】';
// }
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 . '已存在';
}
if($check_msg){
$return_msg .= ',包含中文字符数据' . $check_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()
]);
}
}
}