QrcodeStatController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace addons\Wechat\merchant\controllers;
  3. use Yii;
  4. use yii\data\Pagination;
  5. use common\traits\MerchantCurd;
  6. use common\helpers\ExcelHelper;
  7. use addons\Wechat\common\models\QrcodeStat;
  8. use addons\Wechat\common\enums\QrcodeStatTypeEnum;
  9. /**
  10. * 微信二维码统计
  11. *
  12. * Class QrcodeStatController
  13. * @package addons\Wechat\merchant\controllers
  14. * @author jianyan74 <751393839@qq.com>
  15. */
  16. class QrcodeStatController extends BaseController
  17. {
  18. use MerchantCurd;
  19. /**
  20. * @var QrcodeStat
  21. */
  22. public $modelClass = QrcodeStat::class;
  23. /**
  24. * 首页
  25. */
  26. public function actionIndex()
  27. {
  28. $request = Yii::$app->request;
  29. $type = $request->get('type', '');
  30. $keyword = $request->get('keyword', '');
  31. $from_date = $request->get('from_date', date('Y-m-d', strtotime("-60 day")));
  32. $to_date = $request->get('to_date', date('Y-m-d', strtotime("+1 day")));
  33. $data = QrcodeStat::find()
  34. ->where(['merchant_id' => $this->getMerchantId()])
  35. ->andFilterWhere(['like', 'name', $keyword])
  36. ->andFilterWhere(['type' => $type])
  37. ->andFilterWhere(['between', 'created_at', strtotime($from_date), strtotime($to_date)]);
  38. $attention_data = clone $data;
  39. $scan_data = clone $data;
  40. $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $this->pageSize]);
  41. $models = $data->offset($pages->offset)
  42. ->orderBy('id desc')
  43. ->limit($pages->limit)
  44. ->all();
  45. // 关注统计
  46. $attention_count = $attention_data
  47. ->andWhere(['type' => QrcodeStatTypeEnum::ATTENTION])
  48. ->count();
  49. // 扫描统计
  50. $scan_count = $scan_data
  51. ->andWhere(['type' => QrcodeStatTypeEnum::SCAN])
  52. ->count();
  53. return $this->render('index', [
  54. 'models' => $models,
  55. 'pages' => $pages,
  56. 'type' => $type,
  57. 'attention_count' => $attention_count,
  58. 'scan_count' => $scan_count,
  59. 'keyword' => $keyword,
  60. 'from_date' => $from_date,
  61. 'to_date' => $to_date,
  62. ]);
  63. }
  64. /**
  65. * 导出
  66. *
  67. * @throws \PhpOffice\PhpSpreadsheet\Exception
  68. * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
  69. */
  70. public function actionExport()
  71. {
  72. $request = Yii::$app->request;
  73. $from_date = $request->get('from_date');
  74. $to_date = $request->get('to_date');
  75. $dataList = QrcodeStat::find()
  76. ->where(['between', 'created_at', strtotime($from_date), strtotime($to_date)])
  77. ->andWhere(['merchant_id' => $this->getMerchantId()])
  78. ->andFilterWhere(['type' => $request->get('type')])
  79. ->andFilterWhere(['like', 'name', $request->get('keyword')])
  80. ->orderBy('created_at desc')
  81. ->with('fans')
  82. ->asArray()
  83. ->all();
  84. $header = [
  85. ['ID', 'id'],
  86. ['场景名称', 'name'],
  87. ['openid', 'fans.openid'],
  88. ['昵称', 'fans.nickname'],
  89. ['场景值', 'scene_str'],
  90. ['场景ID', 'scene_id'],
  91. ['关注/扫描', 'type', 'selectd', ['' => '全部', '1' => '关注', '2' => '扫描']],
  92. ['创建时间', 'created_at', 'date', 'Y-m-d H:i:s'],
  93. ];
  94. // 导出Excel
  95. return ExcelHelper::exportData($dataList, $header, '扫描统计_' . time());
  96. }
  97. }
粤ICP备19079148号