|
@@ -0,0 +1,237 @@
|
|
|
+<?php namespace App\Http\Controllers\Admin;
|
|
|
+
|
|
|
+// use App\Http\Requests\Admin\Article as Request;
|
|
|
+// use App\Models\Article as Model;
|
|
|
+use App\Models\ArticleComment as ArticleCommentModel;
|
|
|
+
|
|
|
+use PhpOffice\PhpSpreadsheet\Cell\DataType;
|
|
|
+use PhpOffice\PhpSpreadsheet\IOFactory;
|
|
|
+use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
|
+use PhpOffice\PhpSpreadsheet\Style\Alignment;
|
|
|
+use PhpOffice\PhpSpreadsheet\Style\Fill;
|
|
|
+// use App\Models\Custom as CustomModel;
|
|
|
+// use Illuminate\Support\Facades\DB;
|
|
|
+// use App\Models\City;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 分享设置
|
|
|
+ *
|
|
|
+ * @author huanglei
|
|
|
+ *
|
|
|
+ */
|
|
|
+class ArticleComment extends Auth{
|
|
|
+ const EVENT_TYPE = [
|
|
|
+ '1' => '阅读',
|
|
|
+ '2' => '点赞',
|
|
|
+ '3' => '分享',
|
|
|
+ '4' => '推荐',
|
|
|
+ '5' => '取消推荐',
|
|
|
+ '6' => '取消点赞'
|
|
|
+ ];
|
|
|
+ protected function _initialize(){
|
|
|
+ parent::_initialize();
|
|
|
+
|
|
|
+ $this->assign('breadcrumb1','分享设置');
|
|
|
+ $this->assign('breadcrumb2','内容分享>>数据');
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 数据列表
|
|
|
+ *
|
|
|
+ * */
|
|
|
+ public function index(ArticleCommentModel $ArticleCommentModel){
|
|
|
+
|
|
|
+ // 接受参数
|
|
|
+ $tid = request('tid','');
|
|
|
+ $c_type = request('c_type');
|
|
|
+ $title = request('title');
|
|
|
+ $startTime = request('start_time','');
|
|
|
+ $endTime = request('end_time','');
|
|
|
+
|
|
|
+ // 编码转ID
|
|
|
+ // $id = $code ? $ArticleCommentModel->codeToId($code) : 0;
|
|
|
+ // 查询条件
|
|
|
+ $map = [];
|
|
|
+ // 编码ID
|
|
|
+ if( $tid ) $map[] = ['article_comment.article_id','=',$tid];
|
|
|
+
|
|
|
+ if( $title ) $map[] = ['article.title','LIKE','%'.$title.'%'];
|
|
|
+ if( $startTime ) $map[] = ['article_comment.start_time','>=',strtotime($startTime)];
|
|
|
+ if( $endTime ) $map[] = ['article_comment.end_time','<=',strtotime($endTime)];
|
|
|
+ if( !is_null($c_type) ) $map[] = ['article_comment.event_type','=',$c_type];
|
|
|
+
|
|
|
+ // 查询数据
|
|
|
+ $list = $ArticleCommentModel->query()
|
|
|
+ ->join('custom','article_comment.uid','=','custom.uid')
|
|
|
+ ->leftJoin('article','article_comment.article_id','=','article.id');
|
|
|
+
|
|
|
+ $list = $list->where($map)
|
|
|
+ ->orderByDesc('id')
|
|
|
+ ->select([
|
|
|
+ 'article_comment.*','custom.username as custom_name',
|
|
|
+ 'article.title as title'
|
|
|
+ ])
|
|
|
+ ->paginate(request('limit',config('page_num',10)))->appends(request()->all());
|
|
|
+
|
|
|
+ // 循环处理数据
|
|
|
+ foreach ($list as $key => $value) {
|
|
|
+
|
|
|
+ // id转编号
|
|
|
+ $value['article_code'] = $ArticleCommentModel->idToCode($value['article_id']);
|
|
|
+ // 事件类型:1阅读;2点赞;3分享;4推荐;5取消点赞;6取消推荐
|
|
|
+ $value['event'] = self::EVENT_TYPE[$value['event_type']];
|
|
|
+
|
|
|
+ // 重组
|
|
|
+ $list[$key] = $value;
|
|
|
+ }
|
|
|
+ // 分配数据
|
|
|
+ $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
|
|
|
+ $this->assign('list', $list);
|
|
|
+ $this->assign('tid', $tid);
|
|
|
+ // 加载模板
|
|
|
+ return $this->fetch();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 导出表格导入
|
|
|
+ *
|
|
|
+ * */
|
|
|
+ public function down_excel(ArticleCommentModel $ArticleCommentModel){
|
|
|
+
|
|
|
+ // 接受参数
|
|
|
+ $tid = request('tid','');
|
|
|
+ $c_type = request('c_type');
|
|
|
+ $title = request('title');
|
|
|
+ $startTime = request('start_time','');
|
|
|
+ $endTime = request('end_time','');
|
|
|
+ $map = [];
|
|
|
+ // 编码ID
|
|
|
+ if( $tid ) $map[] = ['article_comment.article_id','=',$tid];
|
|
|
+
|
|
|
+ if( $title ) $map[] = ['article.title','LIKE','%'.$title.'%'];
|
|
|
+ if( $startTime ) $map[] = ['article_comment.start_time','>=',strtotime($startTime)];
|
|
|
+ if( $endTime ) $map[] = ['article_comment.end_time','<=',strtotime($endTime)];
|
|
|
+ if( !is_null($c_type) ) $map[] = ['article_comment.event_type','=',$c_type];
|
|
|
+
|
|
|
+ // 查询数据
|
|
|
+ $list = $ArticleCommentModel->query()
|
|
|
+ ->join('custom','article_comment.uid','=','custom.uid')
|
|
|
+ ->leftJoin('article','article_comment.article_id','=','article.id');
|
|
|
+
|
|
|
+ $list = $list->where($map)
|
|
|
+ ->orderByDesc('id')
|
|
|
+ ->select([
|
|
|
+ 'article_comment.*',
|
|
|
+ 'custom.username as custom_name',
|
|
|
+ 'article.title as title',
|
|
|
+ 'article.content as content',
|
|
|
+
|
|
|
+ ])
|
|
|
+ ->paginate(request('limit',config('page_num',10)))->appends(request()->all());
|
|
|
+
|
|
|
+ // 返回结果
|
|
|
+ $data = [];
|
|
|
+ // 循环处理数据
|
|
|
+ foreach ($list as $value) {
|
|
|
+ // id转编号
|
|
|
+ $value['article_id'] = $ArticleCommentModel->idToCode($value['article_id']);
|
|
|
+ $value['content'] = $value['content']? $value['content'] : '';
|
|
|
+ $value['comment'] = $value['comment']? $value['comment'] : '';
|
|
|
+ $value['event'] = $value['event_type']? self::EVENT_TYPE[$value['event_type']]: '';
|
|
|
+ $value['insert_time'] = $value['insert_time']? date('Y-m-d H:i:s',$value['insert_time']) : '';
|
|
|
+ $value['update_time'] = $value['update_time']? date('Y-m-d H:i:s',$value['update_time']) : '';
|
|
|
+ // 重组
|
|
|
+ $data[] = $value;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 去下载
|
|
|
+
|
|
|
+ $this->toDown($data);
|
|
|
+
|
|
|
+ } catch (\Throwable $th) {
|
|
|
+ echo $th->getMessage();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 去下载
|
|
|
+ */
|
|
|
+ private function toDown($data){
|
|
|
+ // 创建新的电子表格对象
|
|
|
+
|
|
|
+ $spreadsheet = new Spreadsheet();
|
|
|
+ // 设置合并单元格的行和列,例如合并A1到B2的单元格
|
|
|
+
|
|
|
+ $sheet = $this->setStyle($spreadsheet);
|
|
|
+
|
|
|
+ // 从第二行写入
|
|
|
+ $row = 2;
|
|
|
+ // 循环写入
|
|
|
+
|
|
|
+ foreach ($data as $value) {
|
|
|
+
|
|
|
+ // 单元格内容写入
|
|
|
+ $sheet->setCellValue('A'.$row, $value['article_id']);
|
|
|
+ $sheet->setCellValue('B'.$row, $value['comment']);
|
|
|
+ $sheet->setCellValue('C'.$row, $value['custom_name']);
|
|
|
+ $sheet->setCellValueExplicit('D'.$row, $value['event'],DataType::TYPE_STRING);
|
|
|
+ $sheet->setCellValueExplicit('E'.$row, $value['content'],DataType::TYPE_STRING);
|
|
|
+ // $sheet->setCellValueExplicit('C'.$row, $value['custom_name'],DataType::TYPE_STRING);
|
|
|
+ $sheet->setCellValue('F'.$row, $value['insert_time']);
|
|
|
+ $sheet->setCellValue('G'.$row, $value['update_time']);
|
|
|
+
|
|
|
+
|
|
|
+ // 函数自增
|
|
|
+ $row++;
|
|
|
+
|
|
|
+ }
|
|
|
+ // 创建内容
|
|
|
+ $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
|
|
|
+ header('Pragma: public');
|
|
|
+ header('Content-type:application/vnd.ms-excel');
|
|
|
+ header('Content-Disposition: inline;filename=内容分享数据.xlsx');
|
|
|
+ // 输出数据流
|
|
|
+ return $writer->save('php://output');
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 设置表格样式
|
|
|
+ *
|
|
|
+ */
|
|
|
+ private function setStyle(Spreadsheet $spreadsheet){
|
|
|
+ // 选择当前活动的工作表
|
|
|
+ $sheet = $spreadsheet->getActiveSheet();
|
|
|
+ // 宽
|
|
|
+ $sheet->getColumnDimension('A')->setWidth(20);
|
|
|
+ $sheet->getColumnDimension('B')->setWidth(30);
|
|
|
+ $sheet->getColumnDimension('C')->setWidth(15);
|
|
|
+ $sheet->getColumnDimension('D')->setWidth(15);
|
|
|
+ $sheet->getColumnDimension('E')->setWidth(50);
|
|
|
+ $sheet->getColumnDimension('F')->setWidth(25);
|
|
|
+ $sheet->getColumnDimension('G')->setWidth(25);
|
|
|
+
|
|
|
+ // 默认高度
|
|
|
+ $sheet->getDefaultRowDimension()->setRowHeight(18);
|
|
|
+ // 加粗第一行
|
|
|
+ $sheet->getStyle('A:U')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER)->setVertical(Alignment::VERTICAL_CENTER);
|
|
|
+ $sheet->getStyle('A1:U1')->getFont()->setBold(true);
|
|
|
+ $sheet->getStyle('A1:U1')->getFill()->setFillType(Fill::FILL_SOLID)->getStartColor()->setARGB('FF00FF00'); // ARGB颜色代码,例如绿色
|
|
|
+ // 设置表格标题
|
|
|
+ $sheet
|
|
|
+ ->setCellValue('A1', '文章ID')
|
|
|
+ ->setCellValue('B1', '评论内容')
|
|
|
+ ->setCellValue('C1', '用户名称')
|
|
|
+ ->setCellValue('D1', '事件类型')
|
|
|
+ ->setCellValue('E1', '文章内容')
|
|
|
+ ->setCellValue('F1', '文章发布时间')
|
|
|
+ ->setCellValue('G1', '操作时间');
|
|
|
+
|
|
|
+ // 返回结果
|
|
|
+ return $sheet;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|