StudentController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Jobs\Student\UniversityClassTimeMap;
  4. use App\Models\User\WxUser;
  5. use App\Models\User\WxUserStudentCertify;
  6. use App\Models\User\WxUserUniversityCourse;
  7. use App\Models\WxUniversity;
  8. use App\Wen\Utils\StrUtils;
  9. use App\Wen\Utils\UserUtils;
  10. use App\Wen\Utils\Utils;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Support\Carbon;
  13. use Illuminate\Support\Facades\Cache;
  14. use Illuminate\Support\Facades\DB;
  15. class StudentController extends BaseController
  16. {
  17. public function process(Request $request){
  18. $type = _empty_default_($request->type, '');
  19. if ($type == 'university_detail') {
  20. return $this->university_detail($request);
  21. }else if($type == 'university_list'){
  22. return $this->university_list($request);
  23. }else if($type == 'upload_course'){
  24. return $this->upload_course($request);
  25. }
  26. }
  27. private function upload_course(Request &$request){
  28. $uid = _empty_default_($request->uid, 0);
  29. if(_empty_($uid)){
  30. return $this->fail(200000);
  31. }
  32. $courses = _empty_default_($request->courses, []);
  33. if(_empty_($courses)){
  34. return $this->fail(200001);
  35. }
  36. if(is_string($courses)){
  37. $courses = json_decode($courses, true);
  38. }
  39. if(_empty_($courses)){
  40. return $this->fail(200001);
  41. }
  42. $studentCer = WxUserStudentCertify::find($uid);
  43. if(_empty_($studentCer)){
  44. return $this->fail(200003);
  45. }
  46. $university = WxUniversity::find($studentCer->sid);
  47. if(_empty_($university)){
  48. return $this->fail(200003);
  49. }
  50. $course_ = [];
  51. $currentYear = date('Y');
  52. $currentTerm = 0;
  53. $currentDate = Carbon::now();
  54. $firstTermStart = $university->sem_start_date_1 ? Carbon::parse($university->sem_start_date_1) : null;
  55. $firstTermEnd = $university->sem_end_date_1 ? Carbon::parse($university->sem_end_date_1) : null;
  56. $secondTermStart = $university->sem_start_date_2 ? Carbon::parse($university->sem_start_date_2) : null;
  57. $secondTermEnd = $university->sem_end_date_2 ? Carbon::parse($university->sem_end_date_2) : null;
  58. // 判断当前时间属于哪个学期
  59. if ($firstTermStart && $firstTermEnd && $currentDate->between($firstTermStart, $firstTermEnd)) {
  60. $currentTerm = 1;
  61. } elseif ($secondTermStart && $secondTermEnd && $currentDate->between($secondTermStart, $secondTermEnd)) {
  62. $currentTerm = 2;
  63. } else {
  64. $hasFirstTermData = $firstTermStart && $firstTermEnd;
  65. $hasSecondTermData = $secondTermStart && $secondTermEnd;
  66. if (!$hasFirstTermData && !$hasSecondTermData) {
  67. return $this->fail(200008, [], '管理员未配置该大学的校历');
  68. } else if ($hasFirstTermData || $hasSecondTermData) {
  69. }else{
  70. }
  71. }
  72. foreach ($courses as $c){
  73. foreach (['name', 'week', 'section', 'sectionCount', 'weeks'] as $key){
  74. if(_empty_(_array_key($c, $key, ''))){
  75. _logger_(__file__, __line__, $courses);
  76. return $this->fail(200001, [], '缺失课程'.$key);
  77. }
  78. }
  79. if(!is_array($c['weeks'])){
  80. return $this->fail(200001, [], '课程周数必须为数组');
  81. }
  82. $course_[] = [
  83. 'user_id' => $studentCer->user_id,
  84. 'sid' => $university->sid,
  85. 'year' => $currentYear,
  86. 'term' => $currentTerm,
  87. 'name' => $c['name'],
  88. 'num' => _array_key($c, 'num', null),
  89. 'credit' => _array_key($c, 'credit', null),
  90. 'total_hours' => _array_key($c, 'totalHours', null),
  91. 'lecture_hours' => _array_key($c, 'lectureHours', null),
  92. 'category' => _array_key($c, 'category', null),
  93. 'teach_method' => _array_key($c, 'teachMethod', null),
  94. 'teacher' => _array_key($c, 'teacher', null),
  95. 'address' => _array_key($c, 'address', null),
  96. 'raw_weeks' => _array_key($c, 'rawWeeks', null),
  97. 'raw_section' => _array_key($c, 'rawSection', null),
  98. 'week' => $c['week'],
  99. 'section' => $c['section'],
  100. 'section_count' => $c['sectionCount'],
  101. 'weeks' => $c['weeks']
  102. ];
  103. }
  104. if($course_){
  105. DB::beginTransaction();
  106. try {
  107. // todo:
  108. WxUserUniversityCourse::where('user_id', $studentCer->user_id)->delete();
  109. foreach ($course_ as $c_){
  110. $model = new WxUserUniversityCourse();
  111. $model->user_id = $c_['user_id'];
  112. $model->sid = $c_['sid'];
  113. $model->year = $c_['year'];
  114. $model->term = $c_['term'];
  115. $model->name = $c_['name'];
  116. $model->num = $c_['num'];
  117. $model->credit = $c_['credit'];
  118. $model->total_hours = $c_['total_hours'];
  119. $model->lecture_hours = $c_['lecture_hours'];
  120. $model->category = $c_['category'];
  121. $model->teach_method = $c_['teach_method'];
  122. $model->teacher = $c_['teacher'];
  123. $model->address = $c_['address'];
  124. $model->raw_weeks = $c_['raw_weeks'];
  125. $model->raw_section = $c_['raw_section'];
  126. $model->week = $c_['week'];
  127. $model->section = $c_['section'];
  128. $model->section_count = $c_['section_count'];
  129. $model->weeks = $c_['weeks'];
  130. // 保存模型到数据库
  131. $model->save();
  132. }
  133. WxUniversity::where('sid', $university->sid)->increment('course_user_count');
  134. if(!Cache::has($university->sid.':ctmd')){
  135. UniversityClassTimeMap::dispatch();
  136. }
  137. DB::commit();
  138. return $this->success();
  139. } catch (\Exception $e) {
  140. DB::rollBack();
  141. _logger_(__file__, __line__, $e->getMessage());
  142. return $this->fail(200006);
  143. }
  144. }
  145. }
  146. private function university_list(Request &$request){
  147. $keyword = _empty_default_($request->keyword, '');
  148. $scene = _empty_default_($request->scene, '');
  149. $sid = _empty_default_($request->sid, null);
  150. $user_id = _empty_default_($request->user_id, 0);
  151. if($scene == 'certify'){
  152. $query = WxUniversity::where('is_certify', 1);
  153. }else{
  154. $query = WxUniversity::whereNotNull('sid');
  155. }
  156. if($user_id > 0){
  157. $sid = WxUserStudentCertify::where('user_id', $user_id)->value('sid');
  158. if(_empty_($sid)){
  159. return $this->fail(200003, [], '查询不到该用户的学生认证信息');
  160. }
  161. }
  162. if($sid){
  163. $query = $query->where('sid', $sid);
  164. }
  165. if($keyword){
  166. $query = $query->where(function ($query) use ($keyword) {
  167. if(is_numeric($keyword)){
  168. $query->orWhere('name', 'like', '%' . $keyword . '%')
  169. ->orWhere('sid', $keyword);
  170. }else{
  171. $query->orWhere('name', 'like', '%' . $keyword . '%')->orWhere('charge', 'like', '%' . $keyword . '%')->orWhere('location', 'like', '%' . $keyword . '%');
  172. }
  173. });
  174. }
  175. $data = $query->orderBy('order', 'asc')->simplePaginate(20);
  176. if($data){
  177. $data->map(function ($v){
  178. $this->university_item_process($v);
  179. return $v;
  180. });
  181. return $this->success($data);
  182. }
  183. return $this->fail(200003);
  184. }
  185. public function university_item_process(WxUniversity &$university){
  186. $currentDate = Carbon::now();
  187. $firstTermStart = $university->sem_start_date_1 ? Carbon::parse($university->sem_start_date_1) : null;
  188. $firstTermEnd = $university->sem_end_date_1 ? Carbon::parse($university->sem_end_date_1) : null;
  189. $secondTermStart = $university->sem_start_date_2 ? Carbon::parse($university->sem_start_date_2) : null;
  190. $secondTermEnd = $university->sem_end_date_2 ? Carbon::parse($university->sem_end_date_2) : null;
  191. // 判断当前时间属于哪个学期
  192. if ($firstTermStart && $firstTermEnd && $currentDate->between($firstTermStart, $firstTermEnd)) {
  193. $university->sem_start_date = $firstTermStart->format('Y/m/d');
  194. } elseif ($secondTermStart && $secondTermEnd && $currentDate->between($secondTermStart, $secondTermEnd)) {
  195. $university->sem_start_date = $secondTermStart->format('Y/m/d');
  196. } else {
  197. $hasFirstTermData = $firstTermStart && $firstTermEnd;
  198. $hasSecondTermData = $secondTermStart && $secondTermEnd;
  199. if (!$hasFirstTermData && !$hasSecondTermData) {
  200. $university->sem_start_date = 'null';
  201. } else if ($hasFirstTermData || $hasSecondTermData) {
  202. $university->sem_start_date = '';
  203. }
  204. }
  205. }
  206. private function university_detail(Request &$request){
  207. $uname = _empty_default_($request->uname, '');
  208. if(_empty_($uname)){
  209. return $this->fail(200001);
  210. }
  211. $university = WxUniversity::where('name', $uname)->first();
  212. if($university){
  213. $this->university_item_process($university);
  214. return $this->success($university);
  215. }else{
  216. return $this->fail(200003);
  217. }
  218. }
  219. public function university(Request $request){
  220. $keyword = _empty_default_($request->keyword, '');
  221. $scene = _empty_default_($request->scene, '');
  222. $sid = _empty_default_($request->sid, null);
  223. $user_id = _empty_default_($request->user_id, 0);
  224. if($scene == 'certify'){
  225. $query = WxUniversity::where('is_certify', 1);
  226. }else{
  227. $query = WxUniversity::whereNotNull('sid');
  228. }
  229. if($user_id > 0){
  230. $sid = WxUserStudentCertify::where('user_id', $user_id)->value('sid');
  231. if(_empty_($sid)){
  232. return $this->fail(200003, [], '查询不到该用户的学生认证信息');
  233. }
  234. }
  235. if($sid){
  236. $query = $query->where('sid', $sid);
  237. }
  238. if($keyword){
  239. $query = $query->where(function ($query) use ($keyword) {
  240. if(is_numeric($keyword)){
  241. $query->orWhere('name', 'like', '%' . $keyword . '%')
  242. ->orWhere('sid', $keyword);
  243. }else{
  244. $query->orWhere('name', 'like', '%' . $keyword . '%')->orWhere('charge', 'like', '%' . $keyword . '%')->orWhere('location', 'like', '%' . $keyword . '%');
  245. }
  246. });
  247. }
  248. $data = $query->orderBy('order', 'asc')->simplePaginate(20);
  249. if($data){
  250. return $this->success($data);
  251. }
  252. return $this->fail(200003);
  253. }
  254. public function student_certify(Request $request){
  255. $way = _empty_default_($request->way, '');
  256. $uid = $request->uid;
  257. if(_empty_($way)){
  258. return $this->fail(200001);
  259. }
  260. $sid = _empty_default_($request->sid, '');
  261. $role = _empty_default_($request->role, '');
  262. $term = (int)(_empty_default_($request->term, ''));
  263. if(_empty_($sid) || _empty_($role) || _empty_($term)){
  264. return $this->fail(200001);
  265. }
  266. if($term < 2015){
  267. return $this->fail(200004, [], '仅2015届以上可认证');
  268. }
  269. if($term > 2024){
  270. return $this->fail(200004, [], '入学年份不合法');
  271. }
  272. if(!in_array($role, ['student'])){
  273. return $this->fail(200004, [], '学校身份参数不合法');
  274. }
  275. $university = WxUniversity::where('sid', $sid)->first();
  276. if($way == 'email'){
  277. if(strpos($university->email_suffix, '@') !== 0){
  278. return $this->fail(200003, [], '该学校还没有正确的邮箱后缀');
  279. }
  280. $code = _empty_default_($request->code, '');
  281. $email = _empty_default_($request->email, '');
  282. if(_empty_($code) || _empty_($email)){
  283. return $this->fail(200001);
  284. }
  285. if(_empty_($university->email_suffix)){
  286. return $this->fail(200003, [], '该学校不允许邮箱认证');
  287. }
  288. if(!StrUtils::endsWith($email, $university->email_suffix)){
  289. return $this->fail(200004, [], '邮箱后缀和学校邮箱不匹配');
  290. }
  291. $cache_key = md5($uid.':code:60:'.$email);
  292. if(!Cache::has($cache_key)){
  293. return $this->fail(200044);
  294. }
  295. if(Cache::get($cache_key) != $code){
  296. return $this->fail(200004, [], '验证码错误');
  297. }
  298. $cetify = WxUserStudentCertify::where('user_id', $uid)->first();
  299. if($cetify){
  300. if($cetify->status == 1){
  301. return $this->fail(200010, [], '您已经完成学生认证,无需再次认证');
  302. }else{
  303. DB::beginTransaction();
  304. try {
  305. // todo:
  306. $cetify->status = 1;
  307. $cetify->save();
  308. WxUser::where('id', $uid)->update(['university' => $university->name]);
  309. Cache::forget('get:cached:user:'.$uid);
  310. DB::commit();
  311. return $this->success($cetify);
  312. } catch (\Exception $e) {
  313. DB::rollBack();
  314. _logger_(__file__, __line__, $e->getMessage());
  315. return $this->fail(200002);
  316. }
  317. }
  318. }
  319. DB::beginTransaction();
  320. try {
  321. $cetify = new WxUserStudentCertify();
  322. $cetify->user_id = $uid;
  323. $cetify->sid = $university->sid;
  324. $cetify->school = $university->name;
  325. $cetify->role = $role;
  326. $cetify->term = $term;
  327. $cetify->email = $email;
  328. $cetify->status = 1;
  329. $r = $cetify->save();
  330. if($r){
  331. WxUser::where('id', $uid)->update(['university' => $university->name]);
  332. Cache::forget('get:cached:user:'.$uid);
  333. DB::commit();
  334. return $this->success($cetify);
  335. }else{
  336. DB::rollBack();
  337. return $this->fail(200002);
  338. }
  339. } catch (\Exception $e) {
  340. DB::rollBack();
  341. _logger_(__file__, __line__, $e->getMessage());
  342. return $this->fail(200002);
  343. }
  344. }else if($way == 'school-card'){
  345. $picture = _empty_default_($request->picture, '');
  346. $picture2 = _empty_default_($request->picture2, '');
  347. $introduce = _empty_default_($request->introduce, '');
  348. if(_empty_($picture)){
  349. return $this->fail(200001, [], '至少上传一张图片');
  350. }
  351. $cetify = WxUserStudentCertify::where('user_id', $uid)->first();
  352. if($cetify) {
  353. if ($cetify->status == 1) {
  354. return $this->fail(200010, [], '您已经完成学生认证,无需再次认证');
  355. }else{
  356. // return $this->fail(200010, [], '您的申请正在审核中,无需重复提交');
  357. }
  358. }
  359. $img_state_change = [];
  360. if($picture){
  361. $img_state_change[] = $picture;
  362. }
  363. if($picture2){
  364. $img_state_change[] = $picture2;
  365. }
  366. DB::beginTransaction();
  367. try {
  368. if($cetify){
  369. }else{
  370. $cetify = new WxUserStudentCertify();
  371. }
  372. $cetify->user_id = $uid;
  373. $cetify->sid = $university->sid;
  374. $cetify->school = $university->name;
  375. $cetify->role = $role;
  376. $cetify->term = $term;
  377. $cetify->picture = $picture;
  378. $cetify->picture2 = $picture2;
  379. $cetify->introduce = $introduce;
  380. $cetify->status = 1;
  381. $r = $cetify->save();
  382. if($r){
  383. Utils::image_state_change($img_state_change, 1);
  384. UserUtils::assistant_notice_review(108, $cetify->user_id);
  385. DB::commit();
  386. return $this->success(['flag'=>1], 200, '已预通过');
  387. }else{
  388. DB::rollBack();
  389. return $this->fail(200002);
  390. }
  391. } catch (\Exception $e) {
  392. DB::rollBack();
  393. _logger_(__file__, __line__, $e->getMessage());
  394. return $this->fail(200002);
  395. }
  396. }
  397. }
  398. // [
  399. // "name"=> "思想政治实践课2",
  400. // "num"=> "U1300008",
  401. // "credit"=> "1.0",
  402. // "totalHours"=> "16.0",
  403. // "lectureHours"=> "0.0",
  404. // "computeHours"=> "0.0",
  405. // "category"=> "专业主干课/必修课",
  406. // "teachMethod"=> "讲授",
  407. // "method"=> "非统考",
  408. // "teacher"=> "黄鹏振",
  409. // "weeks"=> [
  410. // 3,
  411. // 5,
  412. // 7,
  413. // 9,
  414. // 11,
  415. // 13,
  416. // 15,
  417. // 17
  418. // ],
  419. // "section"=> 11,
  420. // "address"=> "线上上课(北1)",
  421. // "rawWeeks"=> "2-17",
  422. // "rawSection"=> "五[11-12节]单",
  423. // "week"=> 5,
  424. // "sectionCount"=> 2
  425. // ]
  426. }