course/app/api/controller/StudentController.php

226 lines
10 KiB
PHP
Raw Normal View History

<?php
namespace app\api\controller;
use app\BaseController;
2024-07-24 22:37:22 +08:00
use app\common\model\StudentSchedule;
use app\common\service\StudentService;
2024-07-23 22:18:17 +08:00
use app\constant\ResponseCode;
use support\Request;
class StudentController extends BaseController
{
2024-07-23 21:59:15 +08:00
protected $noNeedLogin = ['login', 'getCode'];
/**
* @desc 登录
* @param Request $request
* @return \support\Response
*/
public function login(Request $request)
{
$service = new StudentService();
$res = $service->login($request->post());
return $this->json($res);
}
2024-08-09 18:03:45 +08:00
/**
* @desc 更改密码
* @param Request $request
* @return \support\Response
*/
public function resetPassword(Request $request)
{
$service = new StudentService();
2024-08-10 09:03:07 +08:00
$res = $service->resetPassword($request);
2024-08-09 18:03:45 +08:00
return $this->json($res);
}
2024-07-23 21:59:15 +08:00
/**
* @desc 获取code
* @param Request $request
* @return \support\Response
*/
public function getCode(Request $request)
{
2024-07-23 22:16:04 +08:00
$appid = getenv('APP_ID');
2024-07-24 14:26:26 +08:00
$redirect_uri = urlencode(getenv('SERVER_DOMAIN') . "/h5#/pages/login/login");//重定向地址
2024-07-23 22:16:04 +08:00
// $redirect_uri = getenv('SERVER_DOMAIN') . "/h5#/pages/index/index";//重定向地址
2024-07-24 14:31:09 +08:00
2024-07-23 21:59:15 +08:00
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_userinfo&state=STATE&connect_redirect=1#wechat_redirect";
2024-07-23 22:16:04 +08:00
// $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
2024-07-23 22:18:17 +08:00
return $this->json([
'code' => ResponseCode::SUCCESS,
'data' => $url
]);
// return redirect($url);
2024-07-23 21:59:15 +08:00
}
/**
* @desc 学生信息
* @param Request $request
* @return \support\Response
*/
public function userInfo(Request $request)
{
2024-07-27 10:35:57 +08:00
$service = new StudentService();
$res = $service->userInfo($request);
return $this->json($res);
}
2024-07-25 16:41:39 +08:00
/**
* @desc 更新学生openid
* @param Request $request
* @return \support\Response
* @throws \GuzzleHttp\Exception\GuzzleException
*/
2024-07-23 21:59:15 +08:00
public function updateOpenid(Request $request)
{
$service = new StudentService();
2024-07-24 22:37:22 +08:00
$res = $service->updateOpenid($request);
return $this->json($res);
}
2024-07-24 23:44:07 +08:00
/**
* @desc 学生课表
* @param Request $request
* @return void
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
2024-07-25 22:43:43 +08:00
public function downloadSchedule(Request $request)
2024-07-24 22:37:22 +08:00
{
2024-07-24 23:44:07 +08:00
$request_data = $request->post();
2024-07-25 22:43:43 +08:00
$month = date('Y-m', strtotime($request_data['date']));
$student_schedule = StudentSchedule::where(['student_id' => $request->student->id, 'is_publish' => 1])
2024-07-25 22:43:43 +08:00
->where(['month' => $month])
2024-07-24 23:44:07 +08:00
->with(['teacher', 'subject'])
->order('start_time asc')
->select()->toArray();
2024-07-24 22:37:22 +08:00
2024-07-24 23:44:07 +08:00
$array = [];
2024-07-24 22:37:22 +08:00
$stu_nmae = '';
2024-07-24 23:44:07 +08:00
foreach ($student_schedule as $k => &$v) {
2024-07-24 22:37:22 +08:00
$array[] = [
2024-07-24 23:44:07 +08:00
'week' => $this->getWeek(strtotime($v['date'])),
'date' => $v['date'],
'time_on' => $v['time'],
'subject' => $v['subject_name'],
'teacher_name' => $v['teacher_name'],
2024-07-25 22:46:01 +08:00
'time_num' => $v['hour'] . '/h',
2024-07-24 22:37:22 +08:00
];
2024-07-24 23:44:07 +08:00
$stu_nmae = $v['student_name'];
2024-07-24 22:37:22 +08:00
}
$params = [
2024-07-24 23:44:07 +08:00
'row' => count($student_schedule) + 1,//数据的行数
'file_name' => time() . '.png',
2024-07-24 22:37:22 +08:00
'title' => $stu_nmae,
2024-07-25 22:43:43 +08:00
'table_time' => $month,
2024-07-24 22:37:22 +08:00
'data' => $array,
];
2024-07-25 22:43:43 +08:00
$file_path = '/files/images/student_schedule/' . $month . "/{$request->student->id}/";
2024-07-24 22:37:22 +08:00
$base = [
'border' => 10,//图片外边框
2024-07-25 22:43:43 +08:00
'file_path' => public_path($file_path),//图片保存路径
2024-07-24 22:37:22 +08:00
'title_height' => 30,//报表名称高度
2024-07-24 23:44:07 +08:00
'title_font_size' => 16,//报表名称字体大小
2024-11-13 16:01:00 +08:00
'font_ulr' => public_path('/Alibaba-PuHuiTi-Light.ttf'),//字体文件路径
2024-07-24 22:37:22 +08:00
'text_size' => 12,//正文字体大小
'row_hight' => 30,//每行数据行高
'filed_id_width' => 60,//序号列的宽度
'filed_name_width' => 120,//玩家名称的宽度
2024-07-25 22:43:43 +08:00
'filed_data_width' => 120,//数据列的宽度
2024-07-24 23:44:07 +08:00
'table_header' => ['', '星期', '日期', '上课时间', '学科', '教师', '课时'],//表头文字
2024-07-25 22:43:43 +08:00
'column_text_offset_arr' => [45, 70, 70, 100, 55, 65, 65],//表头文字左偏移量
'row_text_offset_arr' => [50, 110, 90, 110, 90, 90, 90],//数据列文字左偏移量
2024-07-24 22:37:22 +08:00
];
$base['img_width'] = $base['filed_id_width'] + $base['filed_name_width'] + $base['filed_data_width'] * 5 + $base['border'] * 2;//图片宽度
$base['img_height'] = $params['row'] * $base['row_hight'] + $base['border'] * 2 + $base['title_height'];//图片高度
$border_top = $base['border'] + $base['title_height'];//表格顶部高度
$border_bottom = $base['img_height'] - $base['border'];//表格底部高度
$base['column_x_arr'] = [
$base['border'] + $base['filed_id_width'],//第一列边框线x轴像素 70/2+10
$base['border'] + $base['filed_id_width'] + $base['filed_name_width'],//第二列边框线x轴像素 190
$base['border'] + $base['filed_id_width'] + $base['filed_name_width'] + $base['filed_data_width'] * 1,//第三列边框线x轴像素
$base['border'] + $base['filed_id_width'] + $base['filed_name_width'] + $base['filed_data_width'] * 2,//第四列边框线x轴像素
$base['border'] + $base['filed_id_width'] + $base['filed_name_width'] + $base['filed_data_width'] * 3,//第五列边框线x轴像素
$base['border'] + $base['filed_id_width'] + $base['filed_name_width'] + $base['filed_data_width'] * 4,//第五列边框线x轴像素
$base['border'] + $base['filed_id_width'] + $base['filed_name_width'] + $base['filed_data_width'] * 5,//第五列边框线x轴像素
];
// var_dump( $base['column_x_arr']);
$img = imagecreatetruecolor($base['img_width'], $base['img_height']);//创建指定尺寸图片
$bg_color = imagecolorallocate($img, 255, 255, 255);//设定图片背景色
$text_coler = imagecolorallocate($img, 0, 0, 0);//设定文字颜色
$border_coler = imagecolorallocate($img, 0, 0, 0);//设定边框颜色
// $white_coler = imagecolorallocate($img, 255, 255, 255);//设定边框颜色
imagefill($img, 0, 0, $bg_color);//填充图片背景色
//先填充一个黑色的大块背景
imagefilledrectangle($img, $base['border'], $base['border'] + $base['title_height'], $base['img_width'] - $base['border'], $base['img_height'] - $base['border'], $border_coler);//画矩形
//再填充一个小两个像素的 背景色区域,形成一个两个像素的外边框
imagefilledrectangle($img, $base['border'] + 2, $base['border'] + $base['title_height'] + 2, $base['img_width'] - $base['border'] - 2, $base['img_height'] - $base['border'] - 2, $bg_color);//画矩形
//画表格纵线 及 写入表头文字
foreach ($base['column_x_arr'] as $key => $x) {
imageline($img, $x, $border_top, $x, $border_bottom, $border_coler);//画纵线
2024-07-24 23:44:07 +08:00
$x = $x - $base['column_text_offset_arr'][$key] + 1;
2024-07-24 22:37:22 +08:00
// echo $x;echo "<br>";
$y = $border_top + $base['row_hight'] - 8;
$text = $base['table_header'][$key];
2024-07-24 23:44:07 +08:00
imagettftext($img, $base['text_size'], 0, $x, $y, $text_coler, $base['font_ulr'], $text);//写入表头文字
2024-07-24 22:37:22 +08:00
}
//画表格横线
foreach ($params['data'] as $key => $item) {
$border_top += $base['row_hight'];
imageline($img, $base['border'], $border_top, $base['img_width'] - $base['border'], $border_top, $border_coler);
2024-07-24 23:44:07 +08:00
$x = $base['column_x_arr'][0] - $base['row_text_offset_arr'][0];
2024-07-24 22:37:22 +08:00
$y = $border_top + $base['row_hight'] - 10;
2024-07-24 23:44:07 +08:00
$text = $key + 1;
2024-07-24 22:37:22 +08:00
imagettftext($img, $base['text_size'], 0, $x, $y, $text_coler, $base['font_ulr'], $text);//写入序号
$sub = 0;
foreach ($item as $value) {
$sub++;
imagettftext($img, $base['text_size'], 0, $base['column_x_arr'][$sub] - $base['row_text_offset_arr'][$sub], $border_top + $base['row_hight'] - 10, $text_coler, $base['font_ulr'], $value);//写入data数据
}
}
//计算标题写入起始位置
$title_fout_box = imagettfbbox($base['title_font_size'], 0, $base['font_ulr'], $params['title']);//imagettfbbox() 返回一个含有 8 个单元的数组表示了文本外框的四个角:
2024-07-23 21:59:15 +08:00
2024-07-24 22:37:22 +08:00
$title_fout_width = $title_fout_box[2] - $title_fout_box[0];//右下角 X 位置 - 左下角 X 位置 为文字宽度
$title_fout_height = $title_fout_box[1] - $title_fout_box[7];//左下角 Y 位置- 左上角 Y 位置 为文字高度
//居中写入标题
imagettftext($img, $base['title_font_size'], 0, ($base['img_width'] - $title_fout_width) / 2, $base['title_height'], $text_coler, $base['font_ulr'], $params['title']);
//写入制表时间
imagettftext($img, $base['text_size'], 0, $base['border'], $base['title_height'], $text_coler, $base['font_ulr'], '时间:' . $params['table_time']);
$save_path = $base['file_path'] . $params['file_name'];
if (!is_dir($base['file_path']))//判断存储路径是否存在,不存在则创建
{
mkdir($base['file_path'], 0777, true);//可创建多级目录
}
2024-10-13 21:35:59 +08:00
imagepng($img, $save_path, 0);//输出图片输出png使用imagepng方法输出gif使用imagegif方法
2024-07-25 22:43:43 +08:00
$url = getenv('SERVER_DOMAIN') . $file_path . $params['file_name'];
return $this->json([
'code' => ResponseCode::SUCCESS,
'data' => $url
]);
2024-07-23 21:59:15 +08:00
}
2024-07-24 23:44:07 +08:00
public function getWeek($date)
{
$week_array = array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
$week = date("w", $date);
return $week_array[$week];
}
2024-08-09 18:03:45 +08:00
}