ApiController.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. <?php
  2. namespace App\Http\Controllers\WeChat;
  3. use App\Models\CheckCard;
  4. use App\Models\Content;
  5. use App\Models\Leave;
  6. use App\Models\Remark;
  7. use App\Models\RemarkDetail;
  8. use App\Models\RemarkTitle;
  9. use App\Models\Setting;
  10. use App\Models\Student;
  11. use App\Models\StudentCourse;
  12. use App\Models\StudentCourseTeacher;
  13. use App\Models\WeChatUser;
  14. use Carbon\Carbon;
  15. use GuzzleHttp\Client;
  16. use Illuminate\Http\Request;
  17. use App\Http\Controllers\Controller;
  18. use Illuminate\Support\Facades\Log;
  19. use Illuminate\Support\Facades\Storage;
  20. use Intervention\Image\Facades\Image;
  21. class ApiController extends Controller
  22. {
  23. public function login(Request $request)
  24. {
  25. if(empty($request->input('code'))) {
  26. return response()->json(['status' => 'error', 'info' => '参数错误']);
  27. }
  28. $app = app('wechat.mini_program');
  29. $res = $app->auth->session($request->input('code'));
  30. if(!isset($res['session_key'])) {
  31. return response()->json(['status' => 'error', 'info' => '接口错误']);
  32. }
  33. $res = WeChatUser::updateOrCreate([
  34. 'open_id' => $res['openid'],
  35. ], [
  36. 'code' => $request->input('code'),
  37. 'session_key' => $res['session_key'],
  38. ]);
  39. if(empty($res)) {
  40. return response()->json(['status' => 'error', 'info' => '数据库错误']);
  41. }
  42. return response()->json(['status' => 'success', 'id' => $res->id]);
  43. // if(empty($request->input('code')) || empty($request->input('iv')) || empty($request->input('encryptedData'))) {
  44. // return response()->json(['status' => 'error', 'info' => '参数错误']);
  45. // }
  46. // $code = $request->input('code');
  47. // $iv = $request->input('iv');
  48. // $encryptedData = $request->input('encryptedData');
  49. // $app = app('wechat.mini_program');
  50. // $res = $app->auth->session($code);
  51. //
  52. // if(!isset($res['session_key'])) {
  53. // return response()->json(['status' => 'error', 'info' => '接口错误']);
  54. // }
  55. //
  56. // $info = $app->encryptor->decryptData($res['session_key'], $iv, $encryptedData);
  57. //
  58. // if(!isset($info['openId'])) {
  59. // return response()->json(['status' => 'error', 'info' => '接口错误']);
  60. // }
  61. //
  62. // $student = Student::firstOrCreate([
  63. // 'open_id' => $info['openId']
  64. // ], [
  65. // 'nickname' => $info['nickName'],
  66. // 'gender' => $info['gender'],
  67. // 'city' => $info['city'],
  68. // 'province' => $info['province'],
  69. // 'country' => $info['country'],
  70. // 'avatar_url' => $info['avatarUrl'],
  71. // 'name' => $info['nickName'],
  72. // 'short_leave_times' => 0,
  73. // 'long_leave_times' => 0,
  74. // ]);
  75. //
  76. // $data = ['id' => $student->id, 'nickname' => $student->nickname, 'avatar_url' => $student->avatar_url];
  77. // return response()->json(['status' => 'success', 'info' => '操作成功', 'data' => $data]);
  78. }
  79. public function updateUserInfo(Request $request)
  80. {
  81. if(empty($request->input('id')) || empty($we_chat_user = WeChatUser::find($request->input('id'))) || empty($we_chat_user->session_key)) {
  82. return response()->json(['status' => 'error', 'info' => '参数错误']);
  83. }
  84. if(empty($request->input('iv')) || empty($request->input('encryptedData'))) {
  85. return response()->json(['status' => 'error', 'info' => '参数错误']);
  86. }
  87. $iv = $request->input('iv');
  88. $encryptedData = $request->input('encryptedData');
  89. $session_key = $we_chat_user->session_key;
  90. $app = app('wechat.mini_program');
  91. $info = $app->encryptor->decryptData($session_key, $iv, $encryptedData);
  92. if(!isset($info['openId'])) {
  93. return response()->json(['status' => 'error', 'info' => '接口错误']);
  94. }
  95. $res = $we_chat_user->update([
  96. 'nickname' => $info['nickName'],
  97. 'gender' => $info['gender'],
  98. 'city' => $info['city'],
  99. 'province' => $info['province'],
  100. 'country' => $info['country'],
  101. 'avatar_url' => $info['avatarUrl']
  102. ]);
  103. if(empty($res)) {
  104. return response()->json(['status' => 'error', 'info' => '更新错误']);
  105. }
  106. // $student = Student::firstOrCreate([
  107. // 'open_id' => $info['openId']
  108. // ], [
  109. // 'nickname' => $info['nickName'],
  110. // 'gender' => $info['gender'],
  111. // 'city' => $info['city'],
  112. // 'province' => $info['province'],
  113. // 'country' => $info['country'],
  114. // 'avatar_url' => $info['avatarUrl'],
  115. // 'name' => $info['nickName'],
  116. // 'short_leave_times' => 0,
  117. // 'long_leave_times' => 0,
  118. // ]);
  119. $data = ['id' => $we_chat_user->id, 'nickname' => $we_chat_user->nickname, 'avatar_url' => $we_chat_user->avatar_url];
  120. return response()->json(['status' => 'success', 'info' => '操作成功', 'data' => $data]);
  121. }
  122. public function getPhone(Request $request)
  123. {
  124. if(empty($request->input('id')) || empty($we_chat_user = WeChatUser::find($request->input('id')))) {
  125. return response()->json(['status' => 'fail', 'info' => '找不到用户']);
  126. }
  127. if(empty($request->input('iv')) || empty($request->input('encryptedData'))) {
  128. return response()->json(['status' => 'error', 'info' => '参数错误']);
  129. }
  130. $iv = $request->input('iv');
  131. $encryptedData = $request->input('encryptedData');
  132. $session_key = $we_chat_user->session_key;
  133. $app = app('wechat.mini_program');
  134. $info = $app->encryptor->decryptData($session_key, $iv, $encryptedData);
  135. if(isset($info['purePhoneNumber'])) {
  136. return response()->json(['status' => 'success', 'info' => '操作成功', 'phone' => $info['purePhoneNumber']]);
  137. }
  138. return response()->json(['status' => 'fail', 'info' => '没有绑定手机']);
  139. }
  140. public function bindPhone(Request $request)
  141. {
  142. if(empty($request->input('id')) || empty($we_chat_user = WeChatUser::find($request->input('id')))) {
  143. return response()->json(['status' => 'fail', 'info' => '找不到用户']);
  144. }
  145. if(empty($request->input('iv')) || empty($request->input('encryptedData'))) {
  146. return response()->json(['status' => 'error', 'info' => '参数错误']);
  147. }
  148. $iv = $request->input('iv');
  149. $encryptedData = $request->input('encryptedData');
  150. $session_key = $we_chat_user->session_key;
  151. $app = app('wechat.mini_program');
  152. $info = $app->encryptor->decryptData($session_key, $iv, $encryptedData);
  153. if(isset($info['purePhoneNumber'])) {
  154. $student = Student::where('phone', $info['purePhoneNumber'])->first();
  155. if(empty($student)) {
  156. return response()->json(['status' => 'fail', 'info' => '找不到学员']);
  157. }
  158. $res = $student->update([
  159. 'open_id' => $we_chat_user->open_id,
  160. 'nickname' => $we_chat_user->nickname,
  161. 'gender' => $we_chat_user->gender,
  162. 'city' => $we_chat_user->city,
  163. 'province' => $we_chat_user->province,
  164. 'country' => $we_chat_user->country,
  165. 'avatar_url' => $we_chat_user->avatar_url
  166. ]);
  167. if(empty($res)) {
  168. return response()->json(['status' => 'fail', 'info' => '数据更新失败']);
  169. }
  170. $data = ['id' => $student->id, 'nickname' => $student->nickname, 'avatar_url' => $student->avatar_url];
  171. return response()->json(['status' => 'success', 'info' => '操作成功', 'data' => $data]);
  172. }
  173. return response()->json(['status' => 'fail', 'info' => '没有绑定手机']);
  174. }
  175. public function checkPosition(Request $request)
  176. {
  177. if(empty($request->input('latitude')) || empty($request->input('longitude'))) {
  178. return response()->json(['status' => 'fail', 'info' => '参数错误']);
  179. }
  180. $center_position = Setting::where('key', 'check_card_location')->first();
  181. if(empty($center_position) || empty($center_position->value) || count($tmp = explode(',', $center_position->value)) < 2) {
  182. $tmp = ['39.916527', '116.397128'];
  183. }
  184. $client = new Client();
  185. $from = $tmp[0] . ',' . $tmp[1];
  186. $to = $request->input('latitude') . ',' . $request->input('longitude');
  187. $url = 'https://apis.map.qq.com/ws/distance/v1/?from=' . $from . '&to=' . $to . '&key=' . env('TECENT_POSITION_KEY');
  188. $res = $client->get($url);
  189. $res = json_decode((string)$res->getBody());
  190. $result = 'no';
  191. if($res->status == 0) {
  192. $radius = Setting::where('key', 'check_card_radius')->first();
  193. $radius = empty($radius) ? 1000 : $radius->value;
  194. $distance = $res->result->elements[0]->distance;
  195. if($distance <= $radius) {
  196. $result = 'ok';
  197. }
  198. }
  199. return response()->json(['status' => 'success', 'result' => $result]);
  200. }
  201. public function startCheckCard(Request $request)
  202. {
  203. if(empty($request->input('student_id')) || empty($student = Student::find($request->input('student_id')))) {
  204. return response()->json(['status' => 'fail', 'info' => '找不到学员']);
  205. }
  206. $student_course = StudentCourse::where('student_id', $student->id)->first();
  207. if(empty($student_course)) {
  208. return response()->json(['status' => 'fail', 'info' => '暂无课程']);
  209. }
  210. $res = CheckCard::create([
  211. 'student_id' => $student_course->student_id,
  212. 'course_id' => $student_course->course_id,
  213. 'student_course_id' => $student_course->id,
  214. 'begin_date_time' => Carbon::now()->toDateTimeString()
  215. ]);
  216. if(empty($res)) {
  217. return response()->json(['status' => 'fail', 'info' => '创建失败']);
  218. }
  219. return response()->json(['status' => 'success', 'check_card_id' => $res->id, 'info' => '操作成功']);
  220. }
  221. public function endCheckCard(Request $request)
  222. {
  223. if(empty($request->input('check_card_id')) || empty($item = CheckCard::find($request->input('check_card_id')))) {
  224. return response()->json(['status' => 'fail', 'info' => '找不到打卡记录']);
  225. }
  226. $item->end_date_time = Carbon::now()->toDateTimeString();
  227. if(!$item->save()) {
  228. return response()->json(['status' => 'fail', 'info' => '打卡失败']);
  229. }
  230. return response()->json(['status' => 'success', 'info' => '打卡成功']);
  231. }
  232. public function getShareInfo(Request $request)
  233. {
  234. $share_image = Setting::where('key', 'share_image')->first();
  235. if(empty($share_image) || empty($share_image->value) || !Storage::disk('upload')->exists($share_image->value)) {
  236. return response()->json(['status' => 'fail', 'info' => '没有分享图片的信息!']);
  237. }
  238. if(empty($request->input('student_id')) || empty($student = Student::find($request->input('student_id')))) {
  239. return response()->json(['status' => 'fail', 'info' => '找不到用户信息!']);
  240. }
  241. $image_url = url($share_image->value);
  242. $image = Image::make($image_url);
  243. $items = $student->getCheckCardDates();
  244. $share_text = Setting::where('key', 'share_text')->first();
  245. if(empty($share_text) || empty($share_text->value)) {
  246. return response()->json(['status' => 'fail', 'info' => '找不到分享的文字!']);
  247. }
  248. $text = str_replace_array('{days}', [$items->count() + 1], $share_text->value);
  249. $share_text_pos = Setting::where('key', 'share_text_pos')->first();
  250. if(empty($share_text_pos) || empty($share_text_pos->value) || count($pos = explode(',', $share_text_pos->value)) < 2) {
  251. return response()->json(['status' => 'fail', 'info' => '分享文字位置错误或未设置!']);
  252. }
  253. return response()->json(['status' => 'success', 'width' => $image->width(), 'height' => $image->height(), 'shareImage' => $image_url, 'shareText' => $text, 'shareTextPosX' => $pos[0], 'shareTextPosY' => $pos[1]]);
  254. }
  255. public function getMoreVideosAndArticles(Request $request)
  256. {
  257. $video_offset = $request->input('video_offset', 0);
  258. $article_offset = $request->input('article_offset', 0);
  259. if(empty($request->input('type')) || !in_array($request->input('type'), ['both', 'video', 'article'])) {
  260. return response()->json(['status' => 'fail', 'info' => '参数错误']);
  261. }
  262. if($request->input('type') == 'both') {
  263. $video_list = Content::where('type', 3)->orderBy('sort')->offset($video_offset)->limit(15)->get();
  264. foreach($video_list as $item) {
  265. if(empty($item->pic_url)) {
  266. $item->pic_url = 'https://u5.9026.com/addons/swdz_mall/core/web/uploads/image/f0/f0734ad93d46497483344846864596a4.jpg';
  267. } else {
  268. $item->pic_url = url($item->pic_url);
  269. }
  270. $item->url = url($item->content);
  271. }
  272. $article_list = Content::where('type', 4)->orderBy('sort')->offset($article_offset)->limit(15)->get();
  273. return response()->json(['status' => 'success', 'video_list' => $video_list, 'article_list' => $article_list, 'type' => $request->input('type')]);
  274. } else if($request->input('type') == 'video') {
  275. $list = Content::where('type', 3)->orderBy('sort')->offset($video_offset)->limit(15)->get();
  276. foreach($list as $item) {
  277. if(empty($item->pic_url)) {
  278. $item->pic_url = 'https://u5.9026.com/addons/swdz_mall/core/web/uploads/image/f0/f0734ad93d46497483344846864596a4.jpg';
  279. } else {
  280. $item->pic_url = url($item->pic_url);
  281. }
  282. $item->url = url($item->content);
  283. }
  284. } else {
  285. $list = Content::where('type', 4)->orderBy('sort')->offset($article_offset)->limit(15)->get();
  286. }
  287. return response()->json(['status' => 'success', 'list' => $list, 'type' => $request->input('type')]);
  288. }
  289. public function getAnnounces(Request $request)
  290. {
  291. $offset = $request->input('offset', 0);
  292. $list = Content::whereIn('type', [1])->orderBy('sort')->offset($offset)->limit(15)->get();
  293. foreach($list as $item) {
  294. $item->publish_date = substr($item->created_at, 0, 10);
  295. }
  296. return response()->json(['status' => 'success', 'list' => $list]);
  297. }
  298. public function getCourseInfo(Request $request)
  299. {
  300. if(empty($request->input('student_id')) || empty($student = Student::find($request->input('student_id')))) {
  301. return response()->json(['status' => 'fail', 'info' => '找不到用户信息!']);
  302. }
  303. $student_course = StudentCourse::where('student_id', $student->id)->first();
  304. if(empty($student_course)) {
  305. return response()->json(['status' => 'fail', 'info' => '找不到课程!']);
  306. }
  307. $student_course->course_name = $student_course->course->name;
  308. $student_course->teacher_names = $student_course->getTeacherNames();
  309. $student_course->end_date = Carbon::createFromTimestamp(strtotime($student_course->apply_date))->addDays($student_course->duration)->toDateString();
  310. $student_course->short_leave_times = $student->short_leave_times;
  311. $student_course->long_leave_times = $student->long_leave_times;
  312. return response()->json(['status' => 'success', 'courseInfo' => $student_course]);
  313. }
  314. public function getMyLearnInfo(Request $request)
  315. {
  316. if(empty($request->input('student_id')) || empty($student = Student::find($request->input('student_id')))) {
  317. return response()->json(['status' => 'fail', 'info' => '找不到用户信息!']);
  318. }
  319. $now = Carbon::now();
  320. $begin_date_time = Carbon::create($now->year, $now->month, 1)->toDateTimeString();
  321. $thisMonthLearnTime = 0;
  322. $totalLearnTime = 0;
  323. $checkCardList = collect();
  324. $items = CheckCard::where('student_id', $student->id)->whereNotNull('begin_date_time')->whereNotNull('end_date_time')->get();
  325. foreach($items as $item) {
  326. $duration = strtotime($item->end_date_time) - strtotime($item->begin_date_time);
  327. $totalLearnTime += $duration;
  328. if($item->begin_date_time >= $begin_date_time) {
  329. $thisMonthLearnTime += $duration;
  330. $day = Carbon::createFromTimestamp(strtotime($item->end_date_time))->day;
  331. $tmp = $checkCardList->where('day', $day);
  332. if($tmp->count() == 0) {
  333. $checkCardList->push(collect(['month' => 'current', 'day' => $day, 'color' => '#77b9b9']));
  334. }
  335. }
  336. }
  337. $today = $now->day;
  338. for($i = 1; $i <= $today; ++$i) {
  339. $tmp = $checkCardList->where('day', $i);
  340. if($tmp->count() == 0) {
  341. $checkCardList->push(collect(['month' => 'current', 'day' => $i, 'color' => '#f65556']));
  342. }
  343. }
  344. $thisMonthLearnTime = $this->getHumanTime($thisMonthLearnTime);
  345. $totalLearnTime = $this->getHumanTime($totalLearnTime);
  346. $checkCardDays = $student->getCheckCardDates()->count();
  347. return response()->json(['status' => 'success', 'checkCardList' => $checkCardList, 'thisMonthLearnTime' => $thisMonthLearnTime, 'totalLearnTime' => $totalLearnTime, 'checkCardDays' => $checkCardDays]);
  348. }
  349. public function getHumanTime($seconds)
  350. {
  351. $res = '';
  352. $tmp = floor($seconds / 3600);
  353. $diff_time = $seconds % 3600;
  354. if(!empty($tmp)) {
  355. $res .= $tmp . '小时';
  356. }
  357. $tmp = floor($diff_time / 3600);
  358. $diff_time = $diff_time % 60;
  359. if(!empty($tmp)) {
  360. $res .= $tmp . '分钟';
  361. }
  362. if(!empty($diff_time)) {
  363. $res .= $diff_time . '秒';
  364. }
  365. return $res;
  366. }
  367. public function applyLeave(Request $request)
  368. {
  369. if(empty($request->input('student_id')) || empty($student = Student::find($request->input('student_id')))) {
  370. return response()->json(['status' => 'fail', 'info' => '找不到学员']);
  371. }
  372. $student_course = StudentCourse::where('student_id', $student->id)->first();
  373. if(empty($student_course)) {
  374. return response()->json(['status' => 'fail', 'info' => '暂无课程']);
  375. }
  376. $res = Leave::create([
  377. 'student_id' => $student->id,
  378. 'course_id' => $student_course->course_id,
  379. 'student_course_id' => $student_course->id,
  380. 'date' => $request->input('date'),
  381. 'days' => $request->input('days'),
  382. 'remark' => $request->input('remark')
  383. ]);
  384. if(!$res) {
  385. return response()->json(['status' => 'fail', 'info' => '保存失败']);
  386. }
  387. return response()->json(['status' => 'success', 'info' => '请假成功']);
  388. }
  389. public function getRemarkTitles(Request $request)
  390. {
  391. $titles = RemarkTitle::where('status', 2)->get();
  392. return response()->json(['status' => 'success', 'titles' => $titles]);
  393. }
  394. public function remarkTeacher(Request $request)
  395. {
  396. if(empty($request->input('student_id')) || empty($student = Student::find($request->input('student_id')))) {
  397. return response()->json(['status' => 'fail', 'info' => '找不到学员']);
  398. }
  399. $student_course = $student->getStudentCourse();
  400. if(empty($student_course)) {
  401. return response()->json(['status' => 'fail', 'info' => '找不到课程']);
  402. }
  403. $student_course_teacher = StudentCourseTeacher::where('student_id', $student->id)->first();
  404. if(empty($student_course_teacher)) {
  405. return response()->json(['status' => 'fail', 'info' => '找不到讲师']);
  406. }
  407. $remarks = $request->except(['student_id']);
  408. foreach($remarks as $key => $value) {
  409. $remark_title = RemarkTitle::find($key);
  410. if(!empty($remark_title) && !empty($value) && $value != 'null') {
  411. $res = Remark::create([
  412. 'course_id' => $student_course->course_id,
  413. 'teacher_id' => $student_course_teacher->teacher_id,
  414. 'student_id' => $student->id,
  415. ]);
  416. RemarkDetail::create([
  417. 'remark_id' => $res->id,
  418. 'question' => $remark_title->name,
  419. 'score' => $value,
  420. ]);
  421. } else {
  422. return response()->json(['status' => 'fail', 'info' => '评价不能为空']);
  423. }
  424. }
  425. return response()->json(['status' => 'success']);
  426. }
  427. public function getArticleContent(Request $request)
  428. {
  429. if(empty($request->input('id')) || empty($item = Content::find($request->input('id')))) {
  430. return response()->json(['status' => 'fail', 'info' => '找不到文章']);
  431. }
  432. $item->content = $this->replaceImageSrc($item->content);
  433. $item->publish_date = substr($item->created_at, 0, 10);
  434. return response()->json(['status' => 'success', 'article' => $item]);
  435. }
  436. public function replaceImageSrc($img_tag)
  437. {
  438. $doc = new \DOMDocument();
  439. $img_tag = '<meta http-equiv="Content-Type" content="text/html;charset=utf-8">' . $img_tag;
  440. $doc->loadHTML($img_tag);
  441. $tags = $doc->getElementsByTagName('img');
  442. foreach ($tags as $tag) {
  443. $old_src = $tag->getAttribute('src');
  444. $new_src_url = url($old_src);
  445. $tag->setAttribute('src', $new_src_url);
  446. }
  447. return $doc->saveHTML();
  448. }
  449. }