123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- <?php namespace App\Http\Controllers\Admin;
- use App\Models\ArticleEvent as Model;
- use App\Models\Custom;
- /**
- * 分享设置
- *
- * @author huanglei
- *
- */
- class ArticleEvent extends Auth{
- const EVENT_TYPE = [
- '1' => '阅读',
- '2' => '点赞',
- '3' => '分享',
- '4' => '推荐',
- ];
- protected function _initialize(){
- parent::_initialize();
- $this->assign('breadcrumb1','资讯管理');
- $this->assign('breadcrumb2','行为记录');
- }
- /**
- * 数据列表
- *
- * */
- public function index(Model $Model){
- // 接受参数
- $articleId = request('article_id','');
- $typeId = request('type_id');
- $title = request('title');
- $startTime = request('start_time','');
- $endTime = request('end_time','');
- // 查询条件
- $map = [];
- // 编码ID
- if( $articleId ) $map[] = ['article_event.article_id','=',$articleId];
- if( $title ) $map[] = ['article.title','LIKE','%'.$title.'%'];
- if( $startTime ) $map[] = ['article_event.insert_time','>=',strtotime($startTime)];
- if( $endTime ) $map[] = ['article_event.insert_time','<=',strtotime($endTime)];
- if( $typeId ) $map[] = ['article_event.type_id','=',$typeId];
- // 查询数据
- $list = $Model->query()
- ->join('custom','article_event.custom_uid','=','custom.uid')
- ->join('article','article_event.article_id','=','article.id')->where($map)
- ->orderByDesc('article_event.id')
- ->select([
- 'article_event.id',
- 'article_event.status',
- 'article.title as title',
- 'article_event.type_id',
- 'article_event.article_id',
- 'custom.username as custom_name',
- 'custom.uid as custom_uid',
- 'custom.weiban_extid as weiban_extid',
- 'article_event.update_time',
- ])
- ->paginate(request('limit',config('page_num',10)))->appends(request()->all());
- // 循环处理数据
- foreach ($list as $key => $value) {
- // 事件类型:1阅读;2点赞;3分享;4推荐
- $value['event'] = isset(self::EVENT_TYPE[$value['type_id']]) ? self::EVENT_TYPE[$value['type_id']] : '';
- $value['event'] = $value['event'] ? ($value['status']?'取消':'').$value['event'] : '';
- // 重组
- $list[$key] = $value;
- }
- // 分配数据
- $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
- $this->assign('list', $list);
- // 加载模板
- return $this->fetch();
- }
- /**
- * 导出表格导入
- *
- * */
- public function down_excel(Model $Model,Custom $Custom){
- // 接受参数
- $title = request('title');
- $typeId = request('type_id');
- $startTime = request('start_time','');
- $endTime = request('end_time','');
- // 查询条件
- $map = [];
- // 编码ID
- if( $title ) $map[] = ['article.title','LIKE','%'.$title.'%'];
- if( $startTime ) $map[] = ['article_event.insert_time','>=',strtotime($startTime)];
- if( $endTime ) $map[] = ['article_event.insert_time','<=',strtotime($endTime)];
- if( $typeId ) $map[] = ['article_event.type_id','=',$typeId];
-
- // 查询数据
- $list = $Model->query()
- ->join('custom','article_event.custom_uid','=','custom.uid')
- ->join('article','article_event.article_id','=','article.id')->where($map)
- ->orderByDesc('article_event.id')
- ->select([
- 'custom.uid as custom_uid',
- 'custom.username as custom_name',
- 'article_event.article_id',
- 'article.title as title',
- 'article_event.status',
- 'article_event.type_id',
- 'custom.weiban_extid as weiban_extid',
- 'article_event.update_time',
- ])->get()->toArray();
- // 返回结果
- $data = [];
- // 循环处理数据
- foreach ($list as $value) {
- // id转编号
- $value['custom_uid'] = $Custom->idToCode($value['custom_uid']);
- // 事件类型:1阅读;2点赞;3分享;4推荐
- $value['type_id'] = isset(self::EVENT_TYPE[$value['type_id']]) ? self::EVENT_TYPE[$value['type_id']] : '';
- $value['type_id'] = $value['type_id'] ? ($value['status']?'取消':'').$value['type_id'] : '';
- $value['update_time'] = $value['update_time']? date('Y-m-d H:i:s',$value['update_time']) : '';
- // 删除数据
- unset($value['status']);
- // 重组
- $data[] = $value;
- }
-
- try {
- // 去下载
-
- $this->toDown($data);
-
- } catch (\Throwable $th) {
- echo $th->getMessage();
- }
-
- }
- /**
- * 去下载
- */
- private function toDown($data){
- try {
- $config = [
- 'path' => public_path().'/uploads/' // xlsx文件保存路径
- ];
- $excel = new \Vtiful\Kernel\Excel($config);
- $header = [
- '客户编码',
- '客户昵称',
- '资讯ID',
- '资讯标题',
- '操作类型',
- '微伴ID',
- '操作时间',
- ];
- $filePath = $excel->fileName( uniqid().'.xlsx', 'sheet1')
- ->header($header)
- ->data($data)
- ->output();
- $filename = '资讯用户轨迹下载.xlsx';
- header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
- header('Content-Disposition: attachment;filename="' . $filename . '"');
- header('Content-Length: ' . filesize($filePath));
- header('Content-Transfer-Encoding: binary');
- header('Cache-Control: must-revalidate');
- header('Cache-Control: max-age=0');
- header('Pragma: public');
- ob_clean();
- flush();
- if (copy($filePath, 'php://output') === false) {
- dd('下载出错');
- }
- @unlink($filePath);
- exit();
- }catch (\Throwable $th) {
- return $th->getMessage();
- }
- }
- }
|