ExportDataController.class.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace Admin\Controller;
  3. use Think\Model;
  4. /**
  5. * 数据导出模块
  6. * @author xusong
  7. * @version 0.0.1
  8. * @datetime 2016-12-01T21:51:08+0800
  9. */
  10. class ExportDataController extends CommonController {
  11. /**
  12. * [_initialize 前置操作-继承公共前置方法]
  13. * @author Devil
  14. * @blog http://gong.gg/
  15. * @version 0.0.1
  16. * @datetime 2016-12-03T12:39:08+0800
  17. */
  18. private $actions;
  19. public function _initialize() {
  20. // 调用父类前置方法
  21. parent::_initialize();
  22. $this->actions = $this->getRegisterAction();
  23. }
  24. public function index()
  25. {
  26. $this->display();
  27. }
  28. private function getRegisterAction()
  29. {
  30. return $actions = array(
  31. 'user_record'=>'用户数据',
  32. );
  33. }
  34. public function doAction()
  35. {
  36. $action = I('action');
  37. if($this->actions[$action]){
  38. $this->$action();
  39. }else{
  40. echo '请注册操作';
  41. }
  42. }
  43. /**
  44. * 导出片单
  45. * @param string start_date '2019-01-18'
  46. * @param string end_date '2019-02-22'
  47. */
  48. public function user_record()
  49. {
  50. Vendor('PHPExcel.PHPExcel');
  51. $obpe = new \PHPExcel();
  52. if(!$uid = trim(I('uid'))){
  53. exit('用户不存在');
  54. }
  55. //登录历史
  56. $data['login'] = M('iptv_user_login_log')->field('uid,created_at')->where(['uid'=>$uid])->order('created_at asc')->select();
  57. $login_title = ['账号','登录时间'];
  58. array_unshift($data['login'], $login_title);
  59. //订购历史
  60. $data['order'] = M('iptv_order')->field('uid,created_at')->where(['uid'=>$uid])->order('created_at asc')->select();
  61. $order_title = ['账号','订购时间'];
  62. array_unshift($data['order'], $order_title);
  63. //观看历史
  64. $data['watch'] = M('iptv_watch_log wl')
  65. ->field('wl.uid,wl.created_at,s.name as source_name')
  66. ->join('__SOURCE__ s on wl.source_id = s.id','left')
  67. ->where(['wl.uid'=>$uid])
  68. ->order('wl.created_at asc')
  69. ->select();
  70. $watch_title = ['账号','观看时间','节目名称'];
  71. array_unshift($data['watch'], $watch_title);
  72. /* @func 设置文档基本属性 */
  73. // $obpe_pro = $obpe->getProperties();
  74. // $obpe_pro->setCreator('midoks')//设置创建者
  75. // ->setLastModifiedBy('2013/2/16 15:00')//设置时间
  76. // ->setTitle('data')//设置标题
  77. // ->setSubject('beizhu')//设置备注
  78. // ->setDescription('miaoshu')//设置描述
  79. // ->setKeywords('keyword')//设置关键字 | 标记
  80. // ->setCategory('catagory');//设置类别
  81. $index = 0;
  82. foreach ($data as $type => $value) {
  83. if($index != 0){
  84. $obpe->createSheet();
  85. }
  86. $obpe->setactivesheetindex($index);
  87. if($type == 'watch'){
  88. $obpe->getActiveSheet()->setTitle('观看记录');
  89. }
  90. if($type == 'login'){
  91. $obpe->getActiveSheet()->setTitle('登录记录');
  92. }
  93. if($type == 'order'){
  94. $obpe->getActiveSheet()->setTitle('订购记录');
  95. }
  96. $number = 0;
  97. foreach($value as $key2=>$value2){
  98. /* @func 设置列 */
  99. $key = 0;
  100. $number = $number + 1;
  101. foreach ($value2 as $value3) {
  102. $code = strtoupper(chr(65 + $key));//输出大写字母
  103. $obpe->getactivesheet()->setcellvalue($code.$number, $value3);
  104. $key = $key+1;
  105. }
  106. }
  107. $index ++;
  108. }
  109. //写入类容
  110. $obwrite = \PHPExcel_IOFactory::createWriter($obpe, 'Excel5');
  111. //保存文件
  112. $fileName = '投诉'.$uid.date('YmdHis').'.xls';
  113. $obwrite->save($fileName);
  114. header( 'Content-Description: File Transfer' );
  115. header( 'Content-Type: application/octet-stream' );
  116. header( 'Content-Disposition: attachment;filename = ' . $fileName);
  117. header( 'Content-Transfer-Encoding: binary' );
  118. header( 'Expires: 0' );
  119. header( 'Cache-Control: must-revalidate, post-check = 0, pre-check = 0' );
  120. header( 'Pragma: public' );
  121. header( 'Content-Length: ' . filesize( $fileName ) );
  122. ob_clean();
  123. flush();
  124. readfile( $fileName );
  125. unlink($fileName);
  126. }
  127. }