ApiController.php 22 KB

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