Files
Background-panel/application/admin/controller/general/Student.php
2022-09-28 18:05:37 +08:00

268 lines
8.6 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 app\admin\controller\general;
use app\common\controller\Backend;
use Exception;
use fast\Random;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;
use app\common\model\Area;
/**
* 学生管理
*
* @icon fa fa-circle-o
*/
class Student extends Backend
{
private $IdentifierModel;
/**
* Student模型对象
* @var \app\admin\model\Student
*/
protected $model = null;
public function _initialize()
{
parent::_initialize();
$this->model = new \app\admin\model\Student;
$this->IdentifierModel = new \app\admin\model\Identifier;
$this->view->assign("genderList", $this->model->getGenderList());
$this->view->assign("typeList", $this->model->getTypeList());
$this->view->assign("statusList", $this->model->getStatusList());
$this->view->assign("isSuperAdmin", $this->auth->isSuperAdmin());
$this->view->assign("group_id", $this->auth->getGroupIds()[0]);
}
/**
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
* 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
* 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
*/
/**
* 查看
*/
public function index()
{
//当前是否为关联查询
$this->relationSearch = true;
//设置过滤方法
$this->request->filter(['strip_tags', 'trim']);
if ($this->request->isAjax()) {
//如果发送的来源是Selectpage则转发到Selectpage
if ($this->request->request('keyField')) {
return $this->selectpage();
}
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$wheres = [];
if(!$this->auth->isSuperAdmin()){
$groupIds = $this->auth->getGroupIds();
$wheres = ['student.group_id'=>['in',$groupIds]];
}
$list = $this->model
->with(['groups','grade'])
->where($where)
->where($wheres)
->order($sort, $order)
->paginate($limit);
foreach ($list as $row) {
$row->getRelation('groups')->visible(['name']);
$row->getRelation('grade')->visible(['grade','class']);
}
$result = array("total" => $list->total(), "rows" => $list->items());
return json($result);
}
$this->assignconfig("isSuperAdmin", $this->auth->isSuperAdmin());
return $this->view->fetch();
}
/**
* 添加
*
* @return string
* @throws \think\Exception
*/
public function add()
{
if (false === $this->request->isPost()) {
return $this->view->fetch();
}
$params = $this->request->post('row/a');
if (empty($params)) {
$this->error(__('Parameter %s can not be empty', ''));
}
$params = $this->preExcludeFields($params);
if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
$params[$this->dataLimitField] = $this->auth->id;
}
$result = false;
Db::startTrans();
try {
//编号状态
/*$identifier = $this->IdentifierModel->where('identifier', $params['identifier'])->find();
if(empty($identifier)){
$this->error("编号不存在");
}
if($identifier['status'] != 'normal'){
$this->error("编号已分配");
}
//修改编号状态
$this->IdentifierModel->where('identifier', $params['identifier'])->update(['status'=>'hidden']);*/
$params['salt'] = Random::alnum();
$params['password'] = md5($params['password']);
$school = db('auth_group')->where('id',$params['group_id'])->find();
$params['school'] = $school['name'];
$params['avatar'] = $this->getPicUrl($params['avatar']);
$info = db('auth_group')->where('id', $params['group_id'])->find();
while ($info['level'] > 2){
$info = db('auth_group')->where('id', $info['pid'])->find();
}
//地区编号
$area_encoded = db('area')->where('id', $info['city'])->find();
$params['city'] = $area_encoded['name'];
$result = $this->model->validateFailException()->validate('Student.add')->allowField(true)->save($params);
Db::commit();
} catch (ValidateException|PDOException|Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if ($result === false) {
$this->error(__('No rows were inserted'));
}
$this->success();
}
function getPicUrl($url)
{
if($url==''||$url==null){
return '';
}
if(substr($url,0,4)=='http'){
return $url;
}else{
return 'https://hphb-storage.oss-cn-beijing.aliyuncs.com'.$url;
}
}
/**
* 编辑
*
* @param $ids
* @return string
* @throws DbException
* @throws \think\Exception
*/
public function edit($ids = null)
{
$row = $this->model->get($ids);
if (!$row) {
$this->error(__('No Results were found'));
}
$adminIds = $this->getDataLimitAdminIds();
if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
$this->error(__('You have no permission'));
}
if (false === $this->request->isPost()) {
$this->view->assign('row', $row);
return $this->view->fetch();
}
$params = $this->request->post('row/a');
if (empty($params)) {
$this->error(__('Parameter %s can not be empty', ''));
}
$params = $this->preExcludeFields($params);
$result = false;
Db::startTrans();
try {
if ($params['password']) {
$params['salt'] = Random::alnum();
$params['password'] = md5($params['password']);
} else {
unset($params['password'], $params['salt']);
}
//这里需要针对mobile做唯一验证1111
$adminValidate = \think\Loader::validate('Student');
$adminValidate->rule([
'mobile' => 'regex:1[3-9]\d{9}|unique:student,mobile,' . $row->id,
]);
$params['avatar'] = $this->getPicUrl($params['avatar']);
$result = $row->validateFailException()->validate('Student.edit')->allowField(true)->save($params);
Db::commit();
} catch (ValidateException|PDOException|Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if (false === $result) {
$this->error(__('No rows were updated'));
}
$da = db('student')->where('id',$ids)->find();
$avatar = $this->getPicUrl($da['avatar']);
db('student')->where('id',$da['id'])->update(array('avatar'=>$avatar));
$this->success();
}
/**
* 删除
*
* @param $ids
* @return void
* @throws DbException
* @throws DataNotFoundException
* @throws ModelNotFoundException
*/
/*public function del($ids = null)
{
if (false === $this->request->isPost()) {
$this->error(__("Invalid parameters"));
}
$ids = $ids ?: $this->request->post("ids");
if (empty($ids)) {
$this->error(__('Parameter %s can not be empty', 'ids'));
}
$pk = $this->model->getPk();
$adminIds = $this->getDataLimitAdminIds();
if (is_array($adminIds)) {
$this->model->where($this->dataLimitField, 'in', $adminIds);
}
$list = $this->model->where($pk, 'in', $ids)->select();
$count = 0;
Db::startTrans();
try {
//释放编号
$identifiers = array_column($list, 'identifier');
$this->IdentifierModel->where('identifier', 'in', $identifiers)->update(['status'=>'normal']);
foreach ($list as $item) {
$count += $item->delete();
}
Db::commit();
} catch (PDOException|Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if ($count) {
$this->success();
}
$this->error(__('No rows were deleted'));
}*/
}