MemberSearch.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace backend\models\searches;
  3. use backend\models\MemberApi;
  4. use common\helpers\ExcelHelper;
  5. use common\helpers\UserAgentHelper;
  6. use Yii;
  7. use yii\data\ArrayDataProvider;
  8. use yii\data\Pagination;
  9. use yii\helpers\Url;
  10. class MemberSearch extends DataTable
  11. {
  12. /**
  13. * @param array $params
  14. * @param int $type
  15. * @return ArrayDataProvider
  16. */
  17. public function search($params, $type)
  18. {
  19. $this->setAttributes($params);
  20. // 初始化DataTable查询参数
  21. $this->initSearchParams();
  22. $pagination = new Pagination();
  23. $pagination->setPageSize($this->pageSize);
  24. $data['pageSize'] = $pagination->getPageSize();
  25. $data['page'] = $this->page;
  26. if ($this->orderBy) {
  27. $data['orderBy'] = $this->orderBy;
  28. }
  29. if (!empty($this->search['value']) && trim($this->search['value']) !== '') {
  30. $data['search'] = trim($this->search['value']);
  31. }
  32. $data['type'] = $type;
  33. if ($this->pageSize > 5000) {
  34. if (Yii::$app->getRequest()->getIsAjax()) {
  35. Yii::$app->getResponse()->redirect(Url::current(), UserAgentHelper::isIE() ? 200 : 302);
  36. } else {
  37. $this->exportXls($data);
  38. }
  39. Yii::$app->end();
  40. }
  41. $api = new MemberApi();
  42. $result = $api->getList($data);
  43. $models = [];
  44. if ($result['code'] == 1) {
  45. $models = (array)$result['data']['dataList'];
  46. $pagination->totalCount = $result['data']['totalCount'];
  47. }
  48. $dataProvider = new ArrayDataProvider([
  49. 'models' => $models,
  50. 'pagination' => $pagination,
  51. 'totalCount' => $pagination->totalCount,
  52. ]);
  53. return $dataProvider;
  54. }
  55. /**
  56. * @param array $data
  57. */
  58. private function exportXls($data)
  59. {
  60. ExcelHelper::init();
  61. $attachmentName = "用户管理_" . date('Y-m-d') . '.xlsx';
  62. $header = ['账户', '邮箱', '姓名', '手机', '状态', '注册时间'];
  63. $source = [];
  64. $api = new MemberApi();
  65. $data['pageSize'] = 5000;
  66. $data['page'] = 1;
  67. while (true) {
  68. $result = $api->getList($data);
  69. if ($result['code'] == 1 && !empty($result['data']['dataList'])) {
  70. foreach ((array)$result['data']['dataList'] as $key => $row) {
  71. $arr = [];
  72. $arr[] = $row['logins'];
  73. $arr[] = $row['username'];
  74. $arr[] = $row['name'];
  75. $arr[] = $row['mobile'];
  76. if ($row['is_enable'] == 1) {
  77. $arr[] = "启用";
  78. } else {
  79. $arr[] = "禁用";
  80. }
  81. $arr[] = date('Y-m-d H:i:s', intval($row['in_time'] / 1000));
  82. $source[] = $arr;
  83. }
  84. } else {
  85. break;
  86. }
  87. if ($data['page'] * $data['pageSize'] >= $result['data']['totalCount']) {
  88. break;
  89. }
  90. $data['page']++;
  91. }
  92. ExcelHelper::output($source, $header, $attachmentName);
  93. }
  94. }