input('code')) || empty($request->input('iv')) || empty($request->input('encryptedData'))) { return response()->json(['status' => 'error', 'info' => '参数错误']); } $code = $request->input('code'); $iv = $request->input('iv'); $encryptedData = $request->input('encryptedData'); $app = app('wechat.mini_program'); $res = $app->auth->session($code); if(!isset($res['session_key'])) { return response()->json(['status' => 'error', 'info' => '接口错误']); } $info = $app->encryptor->decryptData($res['session_key'], $iv, $encryptedData); if(!isset($info['openId'])) { return response()->json(['status' => 'error', 'info' => '接口错误']); } $student = Student::firstOrCreate([ 'open_id' => $info['openId'] ], [ 'nickname' => $info['nickName'], 'gender' => $info['gender'], 'city' => $info['city'], 'province' => $info['province'], 'country' => $info['country'], 'avatar_url' => $info['avatarUrl'], 'name' => $info['nickName'], 'short_leave_times' => 0, 'long_leave_times' => 0, ]); $data = ['id' => $student->id, 'nickname' => $student->nickname, 'avatar_url' => $student->avatar_url]; return response()->json(['status' => 'success', 'info' => '操作成功', 'data' => $data]); } public function checkPosition(Request $request) { if(empty($request->input('latitude')) || empty($request->input('longitude'))) { return response()->json(['status' => 'fail', 'info' => '参数错误']); } $center_position = Setting::where('key', 'check_card_location')->first(); if(empty($center_position) || empty($center_position->value) || count($tmp = explode(',', $center_position->value)) < 2) { $tmp = ['39.916527', '116.397128']; } $client = new Client(); $from = $tmp[0] . ',' . $tmp[1]; $to = $request->input('latitude') . ',' . $request->input('longitude'); $url = 'https://apis.map.qq.com/ws/distance/v1/?from=' . $from . '&to=' . $to . '&key=' . env('TECENT_POSITION_KEY'); $res = $client->get($url); $res = json_decode((string)$res->getBody()); $result = 'no'; if($res->status == 0) { $radius = Setting::where('key', 'check_card_radius')->first(); $radius = empty($radius) ? 1000 : $radius->value; $distance = $res->result->elements[0]->distance; if($distance <= $radius) { $result = 'ok'; } } return response()->json(['status' => 'success', 'result' => $result]); } public function startCheckCard(Request $request) { if(empty($request->input('student_id')) || empty($student = Student::find($request->input('student_id')))) { return response()->json(['status' => 'fail', 'info' => '找不到学员']); } $student_course = StudentCourse::where('student_id', $student->id)->first(); if(empty($student_course)) { return response()->json(['status' => 'fail', 'info' => '暂无课程']); } $res = CheckCard::create([ 'student_id' => $student_course->student_id, 'course_id' => $student_course->course_id, 'student_course_id' => $student_course->id, 'begin_date_time' => Carbon::now()->toDateTimeString() ]); if(empty($res)) { return response()->json(['status' => 'fail', 'info' => '创建失败']); } return response()->json(['status' => 'success', 'check_card_id' => $res->id, 'info' => '操作成功']); } public function endCheckCard(Request $request) { if(empty($request->input('check_card_id')) || empty($item = CheckCard::find($request->input('check_card_id')))) { return response()->json(['status' => 'fail', 'info' => '找不到打卡记录']); } $item->end_date_time = Carbon::now()->toDateTimeString(); if(!$item->save()) { return response()->json(['status' => 'fail', 'info' => '打卡失败']); } return response()->json(['status' => 'success', 'info' => '打卡成功']); } public function getShareInfo(Request $request) { $share_image = Setting::where('key', 'share_image')->first(); if(empty($share_image) || empty($share_image->value) || !Storage::disk('upload')->exists($share_image->value)) { return response()->json(['status' => 'fail', 'info' => '没有分享图片的信息!']); } if(empty($request->input('student_id')) || empty($student = Student::find($request->input('student_id')))) { return response()->json(['status' => 'fail', 'info' => '找不到用户信息!']); } $image_url = url($share_image->value); $image = Image::make($image_url); $items = $student->getCheckCardDates(); $share_text = Setting::where('key', 'share_text')->first(); if(empty($share_text) || empty($share_text->value)) { return response()->json(['status' => 'fail', 'info' => '找不到分享的文字!']); } $text = str_replace_array('{days}', [$items->count() + 1], $share_text->value); $share_text_pos = Setting::where('key', 'share_text_pos')->first(); if(empty($share_text_pos) || empty($share_text_pos->value) || count($pos = explode(',', $share_text_pos->value)) < 2) { return response()->json(['status' => 'fail', 'info' => '分享文字位置错误或未设置!']); } return response()->json(['status' => 'success', 'width' => $image->width(), 'height' => $image->height(), 'shareImage' => $image_url, 'shareText' => $text, 'shareTextPosX' => $pos[0], 'shareTextPosY' => $pos[1]]); } public function getMoreVideos(Request $request) { $offset = $request->input('offset', 0); $list = Content::where('type', 3)->orderBy('sort')->offset($offset)->limit(15)->get(); foreach($list as $item) { $item->pic_url = 'https://u5.9026.com/addons/swdz_mall/core/web/uploads/image/f0/f0734ad93d46497483344846864596a4.jpg'; $item->url = url($item->content); } return response()->json(['status' => 'success', 'list' => $list]); } public function getCourseInfo(Request $request) { if(empty($request->input('student_id')) || empty($student = Student::find($request->input('student_id')))) { return response()->json(['status' => 'fail', 'info' => '找不到用户信息!']); } $student_course = StudentCourse::where('student_id', $student->id)->first(); if(empty($student_course)) { return response()->json(['status' => 'fail', 'info' => '找不到课程!']); } $student_course->course_name = $student_course->course->name; $student_course->teacher_names = $student_course->getTeacherNames(); $student_course->end_date = Carbon::createFromTimestamp(strtotime($student_course->apply_date))->addDays($student_course->duration)->toDateString(); $student_course->short_leave_times = $student->short_leave_times; $student_course->long_leave_times = $student->long_leave_times; return response()->json(['status' => 'success', 'courseInfo' => $student_course]); } public function getMyLearnInfo(Request $request) { if(empty($request->input('student_id')) || empty($student = Student::find($request->input('student_id')))) { return response()->json(['status' => 'fail', 'info' => '找不到用户信息!']); } $now = Carbon::now(); $begin_date_time = Carbon::create($now->year, $now->month, 1)->toDateTimeString(); $thisMonthLearnTime = 0; $totalLearnTime = 0; $checkCardList = collect(); $items = CheckCard::where('student_id', $this['id'])->whereNotNull('begin_date_time')->whereNotNull('end_date_time')->get(); foreach($items as $item) { $duration = strtotime($item->end_date_time) - strtotime($item->begin_date_time); $totalLearnTime += $duration; if($item->begin_date_time >= $begin_date_time) { $thisMonthLearnTime += $duration; $day = Carbon::createFromTimestamp(strtotime($item->end_date_time))->day; $checkCardList->push(collect(['month' => 'current', 'day' => $day, 'color' => '#77b9b9'])); } } $today = $now->day; for($i = 1; $i <= $today; ++$i) { $tmp = $checkCardList->where('day', $i); if(empty($tmp)) { $checkCardList->push(collect(['month' => 'current', 'day' => $i, 'color' => '#f65556'])); } } $thisMonthLearnTime = $this->getHumanTime($thisMonthLearnTime); $totalLearnTime = $this->getHumanTime($totalLearnTime); $checkCardDays = $student->getCheckCardDates()->count(); return response()->json(['status' => 'success', 'checkCardList' => $checkCardList, 'thisMonthLearnTime' => $thisMonthLearnTime, 'totalLearnTime' => $totalLearnTime, 'checkCardDays' => $checkCardDays]) } public function getHumanTime($seconds) { $res = ''; $tmp = floor($seconds / 3600); $diff_time = $seconds % 3600; if(!empty($tmp)) { $res .= $tmp . '小时'; } $tmp = floor($diff_time / 3600); $diff_time = $diff_time % 60; if(!empty($tmp)) { $res .= $tmp . '分钟'; } if(!empty($diff_time)) { $res .= $diff_time . '秒'; } return $res; } }