MailSearch.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2017/12/1/001
  6. * Time: 15:23
  7. */
  8. namespace backend\models\searches;
  9. use backend\models\MailApi;
  10. use yii\data\ArrayDataProvider;
  11. use yii\data\Pagination;
  12. use yii\helpers\Url;
  13. class MailSearch extends DataTable
  14. {
  15. public function search($params)
  16. {
  17. $this->setAttributes($params);
  18. $this->initSearchParams();
  19. $pagination = new Pagination();
  20. $pagination->pageSize = $this->pageSize;
  21. $data['page'] = $this->page;
  22. $data['pageSize'] = $pagination->getPageSize();
  23. if ($this->orderBy) {
  24. $data['orderBy'] = $this->orderBy;
  25. }
  26. if (!empty($this->search['value']) && trim($this->search['value']) !== '') {
  27. $data['search'] = trim($this->search['value']);
  28. }
  29. if ($this->pageSize > 1000) {
  30. if (\Yii::$app->getRequest()->isAjax) {
  31. \Yii::$app->getResponse()->redirect(Url::current());
  32. } else {
  33. $this->exportXls($data);
  34. }
  35. \Yii::$app->end();
  36. }
  37. $mailApi = new MailApi();
  38. $result = $mailApi->getMailList($data);
  39. $models = [];
  40. if ($result['code'] == 1) {
  41. $models = (array)$result['data']['dataList'];
  42. $pagination->totalCount = (int)$result['data']['totalCount'];
  43. }
  44. $dataProvider = new ArrayDataProvider([
  45. 'models' => $models,
  46. 'pagination' => $pagination,
  47. 'totalCount' => $pagination->totalCount,
  48. ]);
  49. return $dataProvider;
  50. }
  51. /**
  52. * @param $data
  53. */
  54. private function exportXls($data)
  55. {
  56. \Yii::$app->getResponse()->setDownloadHeaders("邮件记录_" . date('Y-m-d') . '.xls')->send();
  57. if (!ob_get_level()) {
  58. ob_start();
  59. }
  60. echo '<style>.num{mso-number-format:General;} .text{mso-number-format:"\@";/*force text*/}</style>';
  61. echo '<table border="1" cellpadding="0" cellspacing="0">';
  62. echo '<tr><th>ID</th><th>类型</th><th>状态</th><th>标题</th><th>发布时间</th></tr>';
  63. $api = new MailApi();
  64. $leftSize = $this->pageSize;
  65. while ($leftSize > 0) {
  66. // 重新计算页数和分页大小 start
  67. $pageSize = $leftSize > 1000 ? 1000 : $leftSize;
  68. $data['pageSize'] = $pageSize;
  69. $data['page'] = ceil($this->start / $pageSize) + 1;
  70. // 重新计算页数和分页大小 end
  71. $result = $api->getMailList($data);
  72. if ($result['code'] == 1 && $result['data']['dataList']) {
  73. foreach ((array)$result['data']['dataList'] as $key => $row) {
  74. $str = '<tr>';
  75. $str .= "<td class='text'>{$row['id']}</td>";
  76. if ((int)$row['type'] == 0) {
  77. $str .= "<td class='text'>会员</td>";
  78. } elseif ((int)$row['type'] == 1) {
  79. $str .= "<td class='text'>代理商</td>";
  80. } elseif ((int)$row['type'] == 2) {
  81. $str .= "<td class='text'>会员+代理商</td>";
  82. } else {
  83. $str .= "<td class='text'>自定义</td>";
  84. }
  85. if ((int)$row['state'] == 1) {
  86. $str .= "<td class='text'>已发送</td>";
  87. } else {
  88. $str .= "<td class='text'>待发送</td>";
  89. }
  90. $str .= "<td class='text'>{$row['subject']}</td>";
  91. $str .= "<td class='text'>".date('Y-m-d H:i:s', intval($row['in_time'] / 1000))."</td>";
  92. $str .= "</tr>";
  93. echo $str;
  94. }
  95. ob_flush();
  96. flush();
  97. } else {
  98. break;
  99. }
  100. $this->start += $pageSize;
  101. $leftSize -= $pageSize;
  102. if ($this->start >= $result['data']['totalCount']) {
  103. break;
  104. }
  105. }
  106. echo "</table>";
  107. if (ob_get_level()) {
  108. ob_end_flush();
  109. }
  110. }
  111. }