2022 EduSoho开发手册 日常技巧之增加数据导出功能

注册服务

services.yml

  keshifens_export_map:
    class: KeshifenPlugin\Component\Export\Factory
    arguments: ['@service_container']
    public: true

注册Factory

Factory.php

<?php
/*
 * 作者:少宇 - 赛驰网络
 * 联系:170 33333 111
 * 时间:2022/3/10 上午2:56
 * 网址:https://www.saichinet.com
 *
 * 山东赛驰网络科技有限公司
 * Shandong Search Network Technology Co., Ltd.
 * Copyright © 2012-2020 Search Network, All Rights Reserved
 */

namespace KeshifenPlugin\Component\Export;

class Factory
{
    public function getMap()
    {
        return [
            'keshifens:count' => 'KeshifenPlugin\Component\Export\Keshifen\CountExporter',
        ];
    }
}

创建导出文件

CountExporter

<?php

namespace KeshifenPlugin\Component\Export\Keshifen;

use AppBundle\Common\ArrayToolkit;
use AppBundle\Component\Export\Exporter;
use Biz\User\Service\UserService;

class CountExporter extends Exporter
{
    protected $conditions;

    public function getTitles()
    {
        return array(
            '用户Id',
            '用户名',
            '手机号码',
        );
    }

    public function canExport()
    {
        $user = $this->getUser();

        if ($user->hasPermission('admin_operation_invite_record') || $user->hasPermission('admin_v2_operation_invite_record')) {
            return true;
        }

        return false;
    }

    public function getCount()
    {
        return $this->getUserUidService()->countUserUids($this->conditions);
    }

    public function getContent($start, $limit)
    {
        return [];
    }

    public function buildCondition($conditions)
    {
        $conditions = ArrayToolkit::parts($conditions, array('keyword'));
        if (!empty($conditions['keyword'])) {
            $user = $this->getUserService()->getUserByLoginField($conditions['keyword']);

            $conditions['user_id'] = empty($user) ? '0' : $user['id'];
        }

        return $conditions;
    }

    protected function exportDataByUserUid($user_uid, $users)
    {
        $content   = [];
        return $content;
    }

    /**
     * @return UserService
     */
    protected function getUserService()
    {
        return $this->getBiz()->service('User:UserService');
    }

    /**
     * @return UserUidService
     */
    protected function getUserUidService()
    {
        return $this->getBiz()->service('UserUidPlugin:UserUid:UserUidService');
    }

    protected function getSettingService()
    {
        return $this->getBiz()->service('System:SettingService');
    }
}

前台导出文件

index.html.twig

  <form id="user-search-form" class="form-inline well well-sm" action="" method="get" novalidate>
    <div class="form-group">
      <input type="text" name="nickname" class="form-control" value="{{ app.request.query.get('nickname') }}"
             placeholder="{{ 'admin.user.teacher.fields.nickname.input_placeholder'|trans }}">
    </div>
    
    <button class="btn btn-primary">{{ 'admin.user.form.btn.search_submit_btn'|trans }}</button>
  
    {% include 'export/export-btn.html.twig' with
      {
        'exportFileName' : 'keshifens:count',
        'targetFormId' : 'user-keshifen-form'
      } %}
  </form>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值