ArticleEvent.php 6.0 KB

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