magento2 后台数据展示+csv导出

PHP技术
354
0
0
2022-04-11
   在 magento2 后台展示 3D 图片点击数统计,且数据可以导出为 csv 文件。大概效果如下:

magento2 后台数据展示+csv导出

magento2 后台数据展示+csv导出

一、在 Applet\etc\adminhtml\menu.xml 中增加 menu 菜单。

<!-- product click list -->
    <add id="May_Applet::product" title="Product Click" translate="title" module="May_Applet" sortOrder="25" parent="May" resource="May_Applet::product"/>
    <add id="May_Applet::click_list" title="Product Click List" translate="title" module="May_Applet" sortOrder="50" parent="May_Applet::product" action="applet/click/lists" resource="May_Applet::click_list"/>

二、在 Applet\etc\acl.xml 中增加下面代码:

  <resource id="May_Applet::click_list" title="Product Click List" sortOrder="20"/>

三、增加文件 Applet\view\adminhtml\templates\click\lists.phtml ,代码如下:


<form class="form" action="<?php echo $block->getPostUrl() ?>" method="post" enctype="multipart/form-data">
<?php
$resultData = $block->getContent();
$exportUrl = $block->getExportUrl();
?>
<input name="form_key" type="hidden" value="<?php echo $block->getFormKey() ?>" />

<div class="page-main-actions">
  <div class="page-actions-placeholder"></div>
  <div class="page-actions" data-ui-id="page-actions-toolbar-content-header">
    <div class="page-actions-inner" data-title="<?php echo __('Product Click List');?>">
      <div class="page-actions-buttons">
        <button id="check-again" title="check again" type="button"
            onclick="window.location.href='<?php echo $exportUrl;?>';"
            class="action- scalable save primary ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"
            data-form-role="save" data-ui-id="save-button" role="button" aria-disabled="false"><span
            class="ui-button-text"><?php echo __('Export Product Click List');?></span></button>
      </div>
    </div>
  </div>
</div>

<table class="data-grid">
  <thead>
  <tr>
    <th class="data-grid-th" style="width:20px;"><?php echo __('ID');?></th>
    <th class="data-grid-th" style="width:50px;"><?php echo __('Customer Id');?></th>
    <th class="data-grid-th" style="width:200px;"><?php echo __('Openid');?></th>
    <th class="data-grid-th" style="width:20px;"><?php echo __('Sku');?></th>
    <th class="data-grid-th" style="width:50px;"><?php echo __('Click Time');?></th>
    <th class="data-grid-th" style="width:20px;"><?php echo __('Created At');?></th>
    <th class="data-grid-th" style="width:20px;"><?php echo __('Updated At');?></th>
  </tr>
  </thead>
  <tbody>
  <?php
  if ($resultData) :
    foreach($resultData as $item):
      $id = $item['id'];
      $customerId = $item['customer_id'] ? $item['customer_id'] : '';
      $Openid = $item['open_id'] ? $item['open_id'] : '';
      $sku = $item['sku'] ? $item['sku'] : '';
      $clickTime = !empty($item['click_time']) ? $item['click_time'] : '';
      $createdAt = !empty($item['created_at']) ? $item['created_at'] : '';
      $updatedAt = !empty($item['updated_at']) ? $item['updated_at'] : '';
      ?>
      <tr>
        <td><div><?php echo $id;?></div></td>
        <td><div><?php echo $customerId;?></div></td>
        <td><div><?php echo $Openid;?></div></td>
        <td><div><?php echo $sku;?></div></td>
        <td><div><?php echo $clickTime;?></div></td>
        <td><div><?php echo $createdAt;?></div></td>
        <td><div><?php echo $updatedAt;?></div></td>
      </tr>
    <?php endforeach;
  else:?>
    <tr><td colspan="8"><?php echo $block->getError();?></td></tr>
  <?php
  endif;
  ?>
  </tbody>
</table>

</form>

四、增加文件 Applet\view\adminhtml\layout\applet_click_lists.xml ,代码如下:

    <block class="May\Applet\Block\Adminhtml\Click\Lists" name="click.lists" template="May_Applet::click/lists.phtml" />

五、增加文件 Applet\Block\Adminhtml\Click\Lists.php ,代码如下:

...

  public function getBackUrl()
  {
    return $this->_urlBuilder->getUrl('*/*/');
  }

  /**
   * @return string
   */ 
  public function getExportUrl()
  {
    return $this->_urlBuilder->getUrl('applet/click/lists/', ['export' => 1]);
  }

  /**
   * get product click content
   */ 
  public function getContent()
  {
    $items = $this->productClickCollection
      ->setOrder('id', 'desc')
      ->getItems();
    $data = [];
    foreach ($items as $item) {
      $data[] = $item->getData();
    }
    return $data;
  }

  /**
   * @return |null
   */ 
  public function getError()
  {
    return $this->_error;
  }

...

六、增加文件 Applet\Controller\Adminhtml\Click\Lists.php ,代码如下:

  protected function _isAllowed()
  {
    return $this->_authorization->isAllowed('May_Applet::click_list');
  }

  public function execute()
  {
    $params = $this->getRequest()->getParams();
    if (!empty($params['export']) && $params['export'] == 1) {
      $this->exportWechatMessageLogList();
    } else {
      /** @var \Magento\Backend\Model\View\Result\Page $resultPage */ 
      $resultPage = $this->resultPageFactory->create();
      $resultPage->setActiveMenu('May_Applet::click_list');
      $resultPage->addBreadcrumb(__('Product Click List'), __('Product Click List'));
      $resultPage->getConfig()->getTitle()->prepend(__('Product Click List'));
      return $resultPage;
    }
  }

  /**
   * export reserve CSV file
   */ 
  public function exportWechatMessageLogList()
  {
    try {
      $result = $this->productClickCollection->getData();
      $header = [
        'id',
        'customer_id',
        'open_id',
        'sku',
        'click_time',
        'created_at',
        'updated_at',
      ];
      $title = 'product_click_list_' . date('YmdHi') . '.csv';
      $this->appletHelper->exportToCSV($result, $header, $title);
    } catch (\Exception $e) {
      $this->messageManager->addErrorMessage($e->getMessage());
      return $this->redirectUrl();
    }
  }

  /**
   * return url
   * @return \Magento\Framework\Controller\Result\Redirect
   */ 
  protected function redirectUrl()
  {
    $redirectUrl = '*/*/lists';
    $resultRedirect = $this->resultRedirectFactory->create();
    return $resultRedirect->setPath($redirectUrl);
  }


}

以上为主要代码,仅在此作为笔记记录。