course/plugin/admin/app/controller/StudentController.php
2024-07-16 23:51:41 +08:00

208 lines
6.1 KiB
PHP

<?php
namespace plugin\admin\app\controller;
use app\common\model\StudentParent;
use app\constant\ResponseCode;
use support\Request;
use support\Response;
use plugin\admin\app\model\Student;
use plugin\admin\app\controller\Crud;
use support\exception\BusinessException;
use think\Exception;
/**
* 学生管理
*/
class StudentController extends Crud
{
/**
* @var Student
*/
protected $model = null;
/**
* 构造函数
* @return void
*/
public function __construct()
{
$this->model = new Student;
}
/**
* 浏览
* @return Response
*/
public function index(): Response
{
return view('student/index');
}
public function select(Request $request): Response
{
try {
$data = $request->get();
$student = \app\common\model\Student::order('id asc');
if (isset($data['id']) && $data['id']) {
$student->where(['id' => $data['id']]);
}
$limit = (int)$request->get('limit', 10);
$limit = $limit <= 0 ? 10 : $limit;
$page = (int)$request->get('page');
$page = $page > 0 ? $page : 1;
$total = $student->count();
$list = $student->page($page, $limit)->with(['parent'])->select();
return json([
'code' => ResponseCode::WEB_API_SUCCESS,
'count' => $total,
'data' => $list,
'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(explode(' ', $request_data['account'])[0]) . '001';
$password = md5($password . $salt);
} else {
$password = md5($request_data['password'] . $salt);
}
$res = \app\common\model\Student::create([
'student_name' => $request_data['student_name'],
'account' => $request_data['account'],
'password' => $password,
'salt' => $salt,
'avatar' => $request_data['avatar'],
'parent_id' => $request_data['parent_id'],
]);
return json([
'code' => ResponseCode::WEB_API_SUCCESS,
'msg' => '添加成功'
]);
// return parent::insert($request);
}
$parent = StudentParent::order('id asc')->field('id,parent_name,account')->select()->toArray();
return view('student/insert', ['parent' => $parent]);
}
/**
* 更新
* @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\Student::where(['id' => $request_data['id']])->findOrEmpty();
if ($student->isEmpty()) {
throw new Exception('未找到学生信息,操作失败');
}
if (empty($request_data['password'])) {
$update = [
'student_name' => $request_data['student_name'],
'account' => $request_data['account'],
'avatar' => $request_data['avatar'],
'parent_id' => $request_data['parent_id'],
];
} else {
$salt = random_str(16);
$password = md5($request_data['password'] . $salt);
$update = [
'student_name' => $request_data['student_name'],
'account' => $request_data['account'],
'password' => $password,
'salt' => $salt,
'avatar' => $request_data['avatar'],
'parent_id' => $request_data['parent_id'],
];
}
$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);
}
$parent = StudentParent::order('id asc')->field('id,parent_name,account')->select()->toArray();
return view('student/update', ['parent' => $parent]);
}
/**
* @desc 重置密码
* @param Request $request
* @return Response
*/
public function resetPassword(Request $request)
{
try {
$request_data = $request->post();
$student = \app\common\model\Student::where(['id' => $request_data['id']])->findOrEmpty();
$new_password = trim(explode(' ', $student->account)[0]) . '001';
$salt = random_str(16);
$password = md5($new_password . $salt);
$res = $student->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()
]);
}
}
}