123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use think\Validate;
- use think\Db;
- /**
- * 学生信息接口
- */
- class Student extends Api
- {
- protected $noNeedLogin = ['getArea','getSchoolByArea','getListByPid'];
- protected $noNeedRight = '*';
- public function _initialize()
- {
- parent::_initialize();
- $this->model = new \app\common\model\Student;
- }
- /**
- * 新增学生
- *
- */
- public function add()
- {
- $area_id = $this->request->request('area_id');
- $school_id = $this->request->request('school_id');
- $grade_id = $this->request->request('grade_id');
- $class_id = $this->request->request('class_id');
- $student_name = $this->request->request('student_name');
- $parent_name = $this->request->request('parent_name');
- $parent_tel = $this->request->request('parent_tel');
- if (!$area_id || !$school_id || !$grade_id || !$class_id || !$student_name || !$parent_name) {
- $this->error(__('Invalid parameters'));
- }
- if (!Validate::regex($parent_tel, "^1\d{10}$")) {
- $this->error(__('Mobile is incorrect'));
- }
- $userInfo = $this->auth->getUserinfo();
- $studentInfo = $this->model->get(["user_id"=>$userInfo["id"]]);
- $data = [];
- $data['city_id'] = 1479;
- $data['area_id'] = $area_id;
- $data['school_id'] = $school_id;
- $data['grade_id'] = $grade_id;
- $data['class_id'] = $class_id;
- $data['student_name'] = $student_name;
- $data['parent_name'] = $parent_name;
- $data['parent_tel'] = $parent_tel;
- if($userInfo) {
- $data['user_id'] = $userInfo["id"];
- } else {
- $this->error(__('未检测到用户信息,请重新授权!'));
- }
- if($studentInfo) {
- $data['is_main'] = 0;
- } else {
- $data['is_main'] = 1;
- }
- $data['parent_tel'] = $parent_tel;
- $data['create_time'] = time();
- $id = $this->model->insertGetId($data);
- if($id) {
- $this->success(__('添加成功!'), ["id"=>$id]);
- } else {
- $this->error(__('添加失败!请重新获取!'));
- }
- }
- /**
- * 获取学生列表信息
- */
- public function getstudentlist() {
- $userInfo = $this->auth->getUserinfo();
- $studentInfo = $this->model->alias("a")
- ->field("a.id,s.name as school,g.name as grade,c.name as class,a.student_name,a.is_main")
- ->join('fa_school s',"a.school_id = s.id")
- ->join('fa_grade g',"a.grade_id = g.id")
- ->join('fa_classes c',"a.class_id = c.id")
- ->where(["user_id"=>$userInfo["id"]])->select();
- if($studentInfo) {
- $this->success(__('获取成功!'), $studentInfo);
- } else {
- $this->error(__('数据为空!'));
- }
- }
- /**
- * 获取临沂市所有区县
- */
- public function getArea() {
- $areaModel = new \app\admin\model\unishop\Area();
- $area = $areaModel->field("id,name")->where(['pid' => '1479'])->select();
- if($area) {
- $this->success(__('获取成功!'), $area);
- } else {
- $this->error(__('数据为空!'));
- }
- }
- // /**
- // * 根据区县ID获取学校列表
- // */
- // public function getSchoolByArea() {
- // $area_id = $this->request->request('area_id');
- // if(!$area_id) {
- // $this->error(__('Invalid parameters'));
- // }
- // $schoolModel = new \app\admin\model\unishop\School();
- // $schoollist = $schoolModel->where(["area_id"=>$area_id])->select();
- // if($schoollist) {
- // $this->success(__('获取成功!'), $schoollist);
- // } else {
- // $this->error(__('数据为空!'));
- // }
- // }
- /**
- * 根据上级ID获取列表
- */
- public function getListByPid() {
- $p_id = $this->request->request('p_id');
- $model = $this->request->request('model'); // school grade class
- if(!$p_id || !$model) {
- $this->error(__('Invalid parameters'));
- }
- switch ($model) {
- case "school":
- $schoolModel = new \app\admin\model\unishop\School();
- $data = $schoolModel->where(["area_id"=>$p_id])->select();
- break;
- case "grade":
- $gradeModel = new \app\admin\model\unishop\Grade();
- $data = $gradeModel->where(["school_id"=>$p_id])->select();
- break;
- case "class":
- $classModel = new \app\admin\model\unishop\Classes();
- $data = $classModel->where(["grade_id"=>$p_id])->select();
- break;
- default:
- $data = [];
- }
- if($data) {
- $this->success(__('获取成功!'), $data);
- } else {
- $this->error(__('数据为空!'));
- }
- }
- /**
- * 切换子账号
- */
- public function switchAcount() {
- $s_id = $this->request->request('s_id');//student 表的ID
- $userinfo = $this->auth->getUserinfo();
- $user_id = $userinfo["id"];
- // 查询所有的当前用户下的 子账号 学生信息列表
- $studentModel = new \app\admin\model\unishop\Student();
- //加个事物吧
- Db::startTrans();
- try {
- // 先更新所有子账号为非主账号
- $res1 = $studentModel->updateIsOk(["is_main"=>0],["user_id"=>$user_id]);
- // 再设置指定账号为主账号
- $res2 = $studentModel->updateIsOk(["is_main"=>1],["id"=>$s_id]);
- if($res1 && $res2) {
- Db::commit();
- $this->success(__('设置成功!'));
- } else {
- $this->error(__('设置失败!'));
- }
- } catch (Exception $e) {
- $this->setError($e->getMessage());
- Db::rollback();
- $this->error(__('设置失败!'));
- }
- }
- /**
- * 获取主账号信息
- */
- public function getStudent() {
- $userinfo = $this->auth->getUserinfo();
- $user_id = $userinfo["id"];
- $studentModel = new \app\admin\model\unishop\Student();
- $data = $studentModel->alias("a")
- ->field("a.id,ua.name as area,s.name as school,g.name as grade,c.name as class,a.student_name,a.parent_name,a.parent_tel")
- ->join('fa_unishop_area ua',"a.area_id = ua.id")
- ->join('fa_school s',"a.school_id = s.id")
- ->join('fa_grade g',"a.grade_id = g.id")
- ->join('fa_classes c',"a.class_id = c.id")
- ->where(["is_main"=>1,"user_id"=>$user_id])->find();
- if($data) {
- $this->success(__('获取成功!'), $data);
- } else {
- $this->error(__('数据为空!'));
- }
- }
- /**
- * 获取用户信息
- */
- public function getUserInfo() {
- $userinfo = $this->auth->getUserinfo();
- $user_id = $userinfo["id"];
- $studentModel = new \app\admin\model\unishop\Student();
- $data = $studentModel->alias("a")
- ->field("a.id,ua.name as area,s.name as school,g.name as grade,c.name as class,a.student_name,a.parent_name,a.parent_tel")
- ->join('fa_unishop_area ua',"a.area_id = ua.id")
- ->join('fa_school s',"a.school_id = s.id")
- ->join('fa_grade g',"a.grade_id = g.id")
- ->join('fa_classes c',"a.class_id = c.id")
- ->where(["is_main"=>1,"user_id"=>$user_id])->find();
- if($data) {
- $res = [];
- $res["atavar"] = $userinfo["avatar"];
- $res["nickname"] = $userinfo["nickname"];
- $res["address"] = $data["school"]."-".$data["grade"]."-".$data["class"]."-".$data["student_name"];
- $this->success(__('获取成功!'), $res);
- } else {
- $this->error(__('数据为空!'));
- }
- }
- /**
- * 解绑用户
- */
- public function unbind() {
- $s_id = $this->request->request('s_id');//student 表的ID
- $studentModel = new \app\admin\model\unishop\Student();
- $user_id = $this->auth->id;
- $studentinfo = $studentModel->where(["id"=>$s_id,"user_id"=>$user_id])->find();
- if(!$studentinfo) {
- $this->error(__('未查询到用户信息!'));
- }
- $is_main = $studentinfo["is_main"];
- $res = 0;
- if($is_main === 1) {
- $this->error(__('默认绑定用户不可解绑!'));
- } elseif($is_main === 0) {
- $res = $studentModel->where(["id"=>$s_id,"user_id"=>$user_id])->delete();
- }
- if($res) {
- $this->success(__('解绑成功!'));
- } else {
- $this->error(__('解绑失败!'));
- }
- }
- }
|