Student.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Validate;
  5. use think\Db;
  6. /**
  7. * 学生信息接口
  8. */
  9. class Student extends Api
  10. {
  11. protected $noNeedLogin = ['getArea','getSchoolByArea','getListByPid'];
  12. protected $noNeedRight = '*';
  13. public function _initialize()
  14. {
  15. parent::_initialize();
  16. $this->model = new \app\common\model\Student;
  17. }
  18. /**
  19. * 新增学生
  20. *
  21. */
  22. public function add()
  23. {
  24. $area_id = $this->request->request('area_id');
  25. $school_id = $this->request->request('school_id');
  26. $grade_id = $this->request->request('grade_id');
  27. $class_id = $this->request->request('class_id');
  28. $student_name = $this->request->request('student_name');
  29. $parent_name = $this->request->request('parent_name');
  30. $parent_tel = $this->request->request('parent_tel');
  31. if (!$area_id || !$school_id || !$grade_id || !$class_id || !$student_name || !$parent_name) {
  32. $this->error(__('Invalid parameters'));
  33. }
  34. if (!Validate::regex($parent_tel, "^1\d{10}$")) {
  35. $this->error(__('Mobile is incorrect'));
  36. }
  37. $userInfo = $this->auth->getUserinfo();
  38. $studentInfo = $this->model->get(["user_id"=>$userInfo["id"]]);
  39. $data = [];
  40. $data['city_id'] = 1479;
  41. $data['area_id'] = $area_id;
  42. $data['school_id'] = $school_id;
  43. $data['grade_id'] = $grade_id;
  44. $data['class_id'] = $class_id;
  45. $data['student_name'] = $student_name;
  46. $data['parent_name'] = $parent_name;
  47. $data['parent_tel'] = $parent_tel;
  48. if($userInfo) {
  49. $data['user_id'] = $userInfo["id"];
  50. } else {
  51. $this->error(__('未检测到用户信息,请重新授权!'));
  52. }
  53. if($studentInfo) {
  54. $data['is_main'] = 0;
  55. } else {
  56. $data['is_main'] = 1;
  57. }
  58. $data['parent_tel'] = $parent_tel;
  59. $data['create_time'] = time();
  60. $id = $this->model->insertGetId($data);
  61. if($id) {
  62. $this->success(__('添加成功!'), ["id"=>$id]);
  63. } else {
  64. $this->error(__('添加失败!请重新获取!'));
  65. }
  66. }
  67. /**
  68. * 获取学生列表信息
  69. */
  70. public function getstudentlist() {
  71. $userInfo = $this->auth->getUserinfo();
  72. $studentInfo = $this->model->alias("a")
  73. ->field("a.id,s.name as school,g.name as grade,c.name as class,a.student_name,a.is_main")
  74. ->join('fa_school s',"a.school_id = s.id")
  75. ->join('fa_grade g',"a.grade_id = g.id")
  76. ->join('fa_classes c',"a.class_id = c.id")
  77. ->where(["user_id"=>$userInfo["id"]])->select();
  78. if($studentInfo) {
  79. $this->success(__('获取成功!'), $studentInfo);
  80. } else {
  81. $this->error(__('数据为空!'));
  82. }
  83. }
  84. /**
  85. * 获取临沂市所有区县
  86. */
  87. public function getArea() {
  88. $areaModel = new \app\admin\model\unishop\Area();
  89. $area = $areaModel->field("id,name")->where(['pid' => '1479'])->select();
  90. if($area) {
  91. $this->success(__('获取成功!'), $area);
  92. } else {
  93. $this->error(__('数据为空!'));
  94. }
  95. }
  96. // /**
  97. // * 根据区县ID获取学校列表
  98. // */
  99. // public function getSchoolByArea() {
  100. // $area_id = $this->request->request('area_id');
  101. // if(!$area_id) {
  102. // $this->error(__('Invalid parameters'));
  103. // }
  104. // $schoolModel = new \app\admin\model\unishop\School();
  105. // $schoollist = $schoolModel->where(["area_id"=>$area_id])->select();
  106. // if($schoollist) {
  107. // $this->success(__('获取成功!'), $schoollist);
  108. // } else {
  109. // $this->error(__('数据为空!'));
  110. // }
  111. // }
  112. /**
  113. * 根据上级ID获取列表
  114. */
  115. public function getListByPid() {
  116. $p_id = $this->request->request('p_id');
  117. $model = $this->request->request('model'); // school grade class
  118. if(!$p_id || !$model) {
  119. $this->error(__('Invalid parameters'));
  120. }
  121. switch ($model) {
  122. case "school":
  123. $schoolModel = new \app\admin\model\unishop\School();
  124. $data = $schoolModel->where(["area_id"=>$p_id])->select();
  125. break;
  126. case "grade":
  127. $gradeModel = new \app\admin\model\unishop\Grade();
  128. $data = $gradeModel->where(["school_id"=>$p_id])->select();
  129. break;
  130. case "class":
  131. $classModel = new \app\admin\model\unishop\Classes();
  132. $data = $classModel->where(["grade_id"=>$p_id])->select();
  133. break;
  134. default:
  135. $data = [];
  136. }
  137. if($data) {
  138. $this->success(__('获取成功!'), $data);
  139. } else {
  140. $this->error(__('数据为空!'));
  141. }
  142. }
  143. /**
  144. * 切换子账号
  145. */
  146. public function switchAcount() {
  147. $s_id = $this->request->request('s_id');//student 表的ID
  148. $userinfo = $this->auth->getUserinfo();
  149. $user_id = $userinfo["id"];
  150. // 查询所有的当前用户下的 子账号 学生信息列表
  151. $studentModel = new \app\admin\model\unishop\Student();
  152. //加个事物吧
  153. Db::startTrans();
  154. try {
  155. // 先更新所有子账号为非主账号
  156. $res1 = $studentModel->updateIsOk(["is_main"=>0],["user_id"=>$user_id]);
  157. // 再设置指定账号为主账号
  158. $res2 = $studentModel->updateIsOk(["is_main"=>1],["id"=>$s_id]);
  159. if($res1 && $res2) {
  160. Db::commit();
  161. $this->success(__('设置成功!'));
  162. } else {
  163. $this->error(__('设置失败!'));
  164. }
  165. } catch (Exception $e) {
  166. $this->setError($e->getMessage());
  167. Db::rollback();
  168. $this->error(__('设置失败!'));
  169. }
  170. }
  171. /**
  172. * 获取主账号信息
  173. */
  174. public function getStudent() {
  175. $userinfo = $this->auth->getUserinfo();
  176. $user_id = $userinfo["id"];
  177. $studentModel = new \app\admin\model\unishop\Student();
  178. $data = $studentModel->alias("a")
  179. ->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")
  180. ->join('fa_unishop_area ua',"a.area_id = ua.id")
  181. ->join('fa_school s',"a.school_id = s.id")
  182. ->join('fa_grade g',"a.grade_id = g.id")
  183. ->join('fa_classes c',"a.class_id = c.id")
  184. ->where(["is_main"=>1,"user_id"=>$user_id])->find();
  185. if($data) {
  186. $this->success(__('获取成功!'), $data);
  187. } else {
  188. $this->error(__('数据为空!'));
  189. }
  190. }
  191. /**
  192. * 获取用户信息
  193. */
  194. public function getUserInfo() {
  195. $userinfo = $this->auth->getUserinfo();
  196. $user_id = $userinfo["id"];
  197. $studentModel = new \app\admin\model\unishop\Student();
  198. $data = $studentModel->alias("a")
  199. ->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")
  200. ->join('fa_unishop_area ua',"a.area_id = ua.id")
  201. ->join('fa_school s',"a.school_id = s.id")
  202. ->join('fa_grade g',"a.grade_id = g.id")
  203. ->join('fa_classes c',"a.class_id = c.id")
  204. ->where(["is_main"=>1,"user_id"=>$user_id])->find();
  205. if($data) {
  206. $res = [];
  207. $res["atavar"] = $userinfo["avatar"];
  208. $res["nickname"] = $userinfo["nickname"];
  209. $res["address"] = $data["school"]."-".$data["grade"]."-".$data["class"]."-".$data["student_name"];
  210. $this->success(__('获取成功!'), $res);
  211. } else {
  212. $this->error(__('数据为空!'));
  213. }
  214. }
  215. /**
  216. * 解绑用户
  217. */
  218. public function unbind() {
  219. $s_id = $this->request->request('s_id');//student 表的ID
  220. $studentModel = new \app\admin\model\unishop\Student();
  221. $user_id = $this->auth->id;
  222. $studentinfo = $studentModel->where(["id"=>$s_id,"user_id"=>$user_id])->find();
  223. if(!$studentinfo) {
  224. $this->error(__('未查询到用户信息!'));
  225. }
  226. $is_main = $studentinfo["is_main"];
  227. $res = 0;
  228. if($is_main === 1) {
  229. $this->error(__('默认绑定用户不可解绑!'));
  230. } elseif($is_main === 0) {
  231. $res = $studentModel->where(["id"=>$s_id,"user_id"=>$user_id])->delete();
  232. }
  233. if($res) {
  234. $this->success(__('解绑成功!'));
  235. } else {
  236. $this->error(__('解绑失败!'));
  237. }
  238. }
  239. }