Commit 6c5dd3f6 authored by wangtao's avatar wangtao

业务板块api接口

parent 833afb02
<?php
namespace app\api\controller\manage;
use app\api\middleware\Auth;
use app\api\service\UtilService;
use app\api\validate\StudentValidate;
use app\BaseController;
use app\model\Student as StudentModel;
use app\Request;
use think\facade\Db;
use app\model\project\User;
use app\event\PhpOffice;
class Student extends BaseController
{
protected $middleware = [
Auth::class,
];
//添加学生
public function createStudent(Request $request)
{
$vo = (new StudentValidate())->goCheck(['realname', 'sex', 'age', 'birthday', 'idcard', 'mobile', 'xq', 'nj', 'bj', 'xh']);
if ($vo !== true) {
return $vo;
}
$parm = $request->param();
Db::startTrans();
try {
$user_id = $this->createuser($parm);
$parm['birthday'] = strtotime($parm['birthday']);
$parm['school_user_id'] = $request->userId;
$parm['user_id'] = $user_id;
$result = StudentModel::create($parm);
Db::commit();
return $this->returnMsg('操作成功', 1, $result);
} catch (\Exception $e) {
Db::rollback();
return $this->returnMsg($e->getMessage());
}
}
//创建用户
public function createuser($parm)
{
$userinfo = User::where('mobile', $parm['mobile'])->where('is_del', 0)->field('id')->find();
if ($userinfo) {
//学生已注册进行绑定
$user_id = $userinfo->id;
$userinfo->school_user_id = \request()->userId;
$userinfo->save();
} else {
//学生未注册添加一条用户记录
$salt = random(4);
$password = md5(substr($parm['mobile'], 5, 6) . $salt);
$datauser = [
'realname' => $parm['realname'],
'username' => $parm['realname'],
'mobile' => $parm['mobile'],
'sex' => $parm['sex'],
'reg_time' => time(),
'password' => $password,
'salt' => $salt,
'school_user_id' => \request()->userId,
];
$userinfo = User::create($datauser);
$user_id = $userinfo->id;
}
return $user_id;
}
//编辑学生
public function updateStudent(Request $request)
{
$vo = (new StudentValidate())->goCheck(['student_id', 'realname', 'sex', 'age', 'birthday', 'idcard', 'xq', 'nj', 'bj', 'xh']);
if ($vo !== true) {
return $vo;
}
$parm = $request->param();
$updatedata = $parm;
unset($updatedata['student_id']);
if (isset($updatedata['mobile'])) unset($updatedata['mobile']);
$result = StudentModel::update($updatedata, ['id' => $parm['student_id']]);
return $this->returnMsg('操作成功', 1, $result);
}
//学生列表
public function studentList(Request $request)
{
$parm = $request->param();
$where = ['school_user_id' => $request->userId];
$map = [];
if (isset($parm['searchrealname']) && $parm['searchrealname']) {
$map[] = ['realname|mobile', 'like', '%' . $parm['searchrealname'] . '%'];
}
$page = $request->param('page', 1);
$pageSize = $request->param('pageSize', 10);
$list = StudentModel::where($where)->where($map)
->order('createtime desc')
->append(['sex_text', 'birthday_text'])
->paginate([
'page' => $page,
'list_rows' => $pageSize
]);
return $this->returnMsg('success', 1, $list);
}
//学生详情
public function studentDetail(Request $request)
{
$vo = (new StudentValidate())->goCheck(['student_id']);
if ($vo !== true) {
return $vo;
}
$parm = $request->param();
$info = StudentModel::with(['userprofile'])->find($parm['student_id'])->append(['sex_text', 'birthday_text']);
return $this->returnMsg('success', 1, $info);
}
//删除学生
public function StudentDelete(Request $request)
{
$vo = (new StudentValidate())->goCheck(['student_id']);
if ($vo !== true) {
return $vo;
}
$parm = $request->param();
$result = StudentModel::destroy([$parm['student_id']]);
return $this->returnMsg('success', 1, $result);
}
//导入学生
public function StudentExcel(Request $request)
{
$vo = (new StudentValidate())->goCheck(['file_id']);
if ($vo !== true) {
return $vo;
}
$parm = $request->param();
$checkfiled = ['realname', 'sex', 'age', 'birthday', 'idcard', 'mobile', 'xq', 'nj', 'bj', 'xh'];
try {
$fileinfo = get_upload_file($parm['file_id'], 'info');
$filePath = $fileinfo['fileurl'];
$filepath = '.' . strstr($filePath, '/static/');
if (!file_exists($filepath)) {
throw new \Exception("文件不存在: " . $filePath);
}
$list = PhpOffice::importexcel($filepath);
if (empty($list)) {
throw new \Exception("文件数据空 ");
}
$datalin = [];
foreach ($list[0] as $k => $item) {
$thisitem = trim($item);
$datalin = StudentModel::excelfiled($thisitem, $k, $datalin);
}
unset($list[0]);
$createdata = [];
$sv = new StudentValidate();
foreach ($list as $vo) {
$savedata = array();
foreach ($datalin as $ks => $linval) {
if ($ks == 'birthday') {
$savedata[$ks] = strtotime(trim($vo[$linval]));
} else {
$savedata[$ks] = trim($vo[$linval]);
}
}
//验证数据
$vo = $sv->goCheck($checkfiled, $savedata);
if ($vo !== true) {
throw new \Exception($sv->getError());
}
$user_id = $this->createuser($savedata);
$savedata['school_user_id'] = $request->userId;
$savedata['user_id'] = $user_id;
$createdata[] = $savedata;
}
StudentModel::saveAll($createdata);
return $this->returnMsg('导入成功', 1);
} catch (\Exception $e) {
return $this->returnMsg($e->getMessage());
}
}
}
\ No newline at end of file
<?php
namespace app\api\validate;
use app\model\Student;
use app\Request;
use think\Validate;
use think\model\concern\SoftDelete;
class StudentValidate extends BaseValidate
{
protected $rule = [
'realname' => 'require',
'sex' => 'require',
'age' => 'require|between:5,80',
'birthday' => 'require',
'idcard' => 'require|idCard',
'mobile' => 'require|mobile|checkmobile',
'xq' => 'require',
'nj' => 'require',
'bj' => 'require',
'xh' => 'require',
'student_id' => 'require',
];
protected $message = [
'realname.require' => '姓名不能为空',
'sex.require' => '性别不能为空',
'age.require' => '年龄不能为空',
'birthday.require' => '出生年月不能为空',
'idcard.require' => '身份证不能为空',
'idcard.idCard' => '身份证格式不对',
'mobile.require' => '手机号不能为空',
'mobile.mobile' => '手机号格式不对',
'xq.require' => '校区不能为空',
'nj.require' => '年纪不能为空',
'bj.require' => '班级不能为空',
'xh.require' => '学号不能为空',
'mobile.checkmobile' => '手机号已存在',
'student_id.require' => '学生id不能为空'
];
protected function checkmobile($value, $rule, $data = [])
{
$result = Student::where(['mobile' => $value])->count();
if ($result) return false;
return true;
}
}
\ No newline at end of file
<?php
/**
* ===========================================================================
* Veitool 快捷开发框架系统
* Author: Niaho 26843818@qq.com
* Copyright (c)2019-2025 www.veitool.com All rights reserved.
* Licensed: 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
* ---------------------------------------------------------------------------
*/
namespace app\event;
use PhpOffice\PhpSpreadsheet\IOFactory;
/**
* phpoffice excel导入导出类
*/
class PhpOffice
{
public static function importexcel($filePath)
{
// 加载 Excel 文件
$spreadsheet = IOFactory::load($filePath);
$sheet = $spreadsheet->getActiveSheet();
return $sheet->toArray();
// // 获取最高行和列
// $highestRow = $sheet->getHighestRow();
// $highestColumn = $sheet->getHighestColumn();
// $data = [];
// // 读取表头 (第一行)
// $headers = [];
// for ($col = 'A'; $col <= $highestColumn; $col++) {
// $headers[] = $sheet->getCell($col . '1')->getValue();
// }
// // 读取数据 (从第二行开始)
// for ($row = 2; $row <= $highestRow; $row++) {
// $rowData = [];
// $colIndex = 0;
// for ($col = 'A'; $col <= $highestColumn; $col++) {
// $rowData[$headers[$colIndex]] = $sheet->getCell($col . $row)->getValue();
// $colIndex++;
// }
// $data[] = $rowData;
// }
//
// return $data;
}
}
......@@ -26,7 +26,6 @@ class Project extends Model
use SoftDelete;
protected $deleteTime = 'deletetime';
protected $autoWriteTimestamp = true;
......
<?php
/**
* ===========================================================================
* Veitool 快捷开发框架系统
* Author: Niaho 26843818@qq.com
* Copyright (c)2019-2025 www.veitool.com All rights reserved.
* Licensed: 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
* ---------------------------------------------------------------------------
*/
namespace app\model;
use think\facade\Db;
use think\Model;
use think\model\concern\SoftDelete;
/**
* 模型公用类
*/
class Student extends Model
{
protected $autoWriteTimestamp = true;
protected $createTime = 'createtime';
use SoftDelete;
protected $deleteTime = 'deletetime';
public function userprofile()
{
return $this->belongsTo(\app\model\project\User::class, 'user_id')
->field('id,mobile,username');
}
public function getBirthdayTextAttr($value, $data)
{
return date('Y-m-d', $data['birthday']);
}
public function getSexTextAttr($value, $data)
{
switch ($data['sex']) {
case 1:
$text = '男';
break;
case 2:
$text = '女';
break;
default:
$text = '未知';
break;
}
return $text;
}
public static function excelfiled($thisitem, $k, $datalin)
{
if ($thisitem == '姓名') {
$datalin['realname'] = $k;
} elseif ($thisitem == '联系方式') {
$datalin['mobile'] = $k;
} elseif ($thisitem == '性别') {
$datalin['sex'] = $k;
} elseif ($thisitem == '年龄') {
$datalin['age'] = $k;
} elseif ($thisitem == '证件号码') {
$datalin['idcard'] = $k;
} elseif ($thisitem == '出生年月') {
$datalin['birthday'] = $k;
} elseif ($thisitem == '校区') {
$datalin['xq'] = $k;
} elseif ($thisitem == '年纪') {
$datalin['nj'] = $k;
} elseif ($thisitem == '班级') {
$datalin['bj'] = $k;
} elseif ($thisitem == '学号') {
$datalin['xh'] = $k;
}
return $datalin;
}
public function setSexAttr($value, $data)
{
$text = $value;
switch ($data['sex']) {
case '男':
$text = 1;
break;
case '女':
$text = 2;
break;
case '未知':
$text = 0;
break;
}
return $text;
}
}
\ No newline at end of file
......@@ -87,5 +87,11 @@ class User extends Model
return $txt;
}
public function student()
{
return $this->belongsTo(\app\model\project\User::class, 'id', 'user_id')
->field('id,title');
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment