ArticleEvent.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php namespace App\Http\Controllers\Admin;
  2. use App\Models\ArticleEvent as Model;
  3. use App\Models\Custom;
  4. /**
  5. * 分享设置
  6. *
  7. * @author huanglei
  8. *
  9. */
  10. class ArticleEvent extends Auth{
  11. const EVENT_TYPE = [
  12. '1' => '阅读',
  13. '2' => '点赞',
  14. '3' => '分享',
  15. '4' => '推荐',
  16. ];
  17. protected function _initialize(){
  18. parent::_initialize();
  19. $this->assign('breadcrumb1','资讯管理');
  20. $this->assign('breadcrumb2','事件记录');
  21. }
  22. /**
  23. * 数据列表
  24. *
  25. * */
  26. public function index(Model $Model){
  27. // 接受参数
  28. $articleId = request('article_id','');
  29. $typeId = request('type_id');
  30. $title = request('title');
  31. $startTime = request('start_time','');
  32. $endTime = request('end_time','');
  33. // 查询条件
  34. $map = [];
  35. // 编码ID
  36. if( $articleId ) $map[] = ['article_event.article_id','=',$articleId];
  37. if( $title ) $map[] = ['article.title','LIKE','%'.$title.'%'];
  38. if( $startTime ) $map[] = ['article_event.insert_time','>=',strtotime($startTime)];
  39. if( $endTime ) $map[] = ['article_event.insert_time','<=',strtotime($endTime)];
  40. if( $typeId ) $map[] = ['article_event.type_id','=',$typeId];
  41. // 查询数据
  42. $list = $Model->query()
  43. ->join('custom','article_event.custom_uid','=','custom.uid')
  44. ->join('article','article_event.article_id','=','article.id')->where($map)
  45. ->orderByDesc('article_event.id')
  46. ->select([
  47. 'article_event.id',
  48. 'article_event.status',
  49. 'article.title as title',
  50. 'article_event.type_id',
  51. 'article_event.article_id',
  52. 'custom.username as custom_name',
  53. 'custom.uid as custom_uid',
  54. 'custom.weiban_extid as weiban_extid',
  55. 'article_event.update_time',
  56. ])
  57. ->paginate(request('limit',config('page_num',10)))->appends(request()->all());
  58. // 循环处理数据
  59. foreach ($list as $key => $value) {
  60. // 事件类型:1阅读;2点赞;3分享;4推荐
  61. $value['event'] = isset(self::EVENT_TYPE[$value['type_id']]) ? self::EVENT_TYPE[$value['type_id']] : '';
  62. $value['event'] = $value['event'] ? ($value['status']?'取消':'').$value['event'] : '';
  63. // 重组
  64. $list[$key] = $value;
  65. }
  66. // 分配数据
  67. $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
  68. $this->assign('list', $list);
  69. // 加载模板
  70. return $this->fetch();
  71. }
  72. /**
  73. * 导出表格导入
  74. *
  75. * */
  76. public function down_excel(Model $Model,Custom $Custom){
  77. // 接受参数
  78. $title = request('title');
  79. $typeId = request('type_id');
  80. $startTime = request('start_time','');
  81. $endTime = request('end_time','');
  82. // 查询条件
  83. $map = [];
  84. // 编码ID
  85. if( $title ) $map[] = ['article.title','LIKE','%'.$title.'%'];
  86. if( $startTime ) $map[] = ['article_event.insert_time','>=',strtotime($startTime)];
  87. if( $endTime ) $map[] = ['article_event.insert_time','<=',strtotime($endTime)];
  88. if( $typeId ) $map[] = ['article_event.type_id','=',$typeId];
  89. // 查询数据
  90. $list = $Model->query()
  91. ->join('custom','article_event.custom_uid','=','custom.uid')
  92. ->join('article','article_event.article_id','=','article.id')->where($map)
  93. ->orderByDesc('article_event.id')
  94. ->select([
  95. 'custom.uid as custom_uid',
  96. 'custom.username as custom_name',
  97. 'article_event.article_id',
  98. 'article.title as title',
  99. 'article_event.status',
  100. 'article_event.type_id',
  101. 'custom.weiban_extid as weiban_extid',
  102. 'article_event.update_time',
  103. ])->get()->toArray();
  104. // 返回结果
  105. $data = [];
  106. // 循环处理数据
  107. foreach ($list as $value) {
  108. // id转编号
  109. $value['custom_uid'] = $Custom->idToCode($value['custom_uid']);
  110. // 事件类型:1阅读;2点赞;3分享;4推荐
  111. $value['type_id'] = isset(self::EVENT_TYPE[$value['type_id']]) ? self::EVENT_TYPE[$value['type_id']] : '';
  112. $value['type_id'] = $value['type_id'] ? ($value['status']?'取消':'').$value['type_id'] : '';
  113. $value['update_time'] = $value['update_time']? date('Y-m-d H:i:s',$value['update_time']) : '';
  114. // 删除数据
  115. unset($value['status']);
  116. // 重组
  117. $data[] = $value;
  118. }
  119. try {
  120. // 去下载
  121. $this->toDown($data);
  122. } catch (\Throwable $th) {
  123. echo $th->getMessage();
  124. }
  125. }
  126. /**
  127. * 去下载
  128. */
  129. private function toDown($data){
  130. try {
  131. $config = [
  132. 'path' => public_path().'/uploads/' // xlsx文件保存路径
  133. ];
  134. $excel = new \Vtiful\Kernel\Excel($config);
  135. $header = [
  136. '客户编码',
  137. '客户昵称',
  138. '资讯ID',
  139. '资讯标题',
  140. '操作类型',
  141. '微伴ID',
  142. '操作时间',
  143. ];
  144. $filePath = $excel->fileName( uniqid().'.xlsx', 'sheet1')
  145. ->header($header)
  146. ->data($data)
  147. ->output();
  148. $filename = '资讯用户轨迹下载.xlsx';
  149. header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
  150. header('Content-Disposition: attachment;filename="' . $filename . '"');
  151. header('Content-Length: ' . filesize($filePath));
  152. header('Content-Transfer-Encoding: binary');
  153. header('Cache-Control: must-revalidate');
  154. header('Cache-Control: max-age=0');
  155. header('Pragma: public');
  156. ob_clean();
  157. flush();
  158. if (copy($filePath, 'php://output') === false) {
  159. dd('下载出错');
  160. }
  161. @unlink($filePath);
  162. exit();
  163. }catch (\Throwable $th) {
  164. return $th->getMessage();
  165. }
  166. }
  167. }