zm
2021-03-25 d84ff6053b22269a6c59dc005e9efb8de6595988
提货卡导出功能开发完成
1 files added
3 files modified
181 ■■■■ changed files
plugins/exchange/controllers/mall/LibraryController.php 4 ●●●● patch | view | raw | blame | history
plugins/exchange/forms/mall/CodeForm.php 54 ●●●●● patch | view | raw | blame | history
plugins/exchange/forms/mall/ExportForm.php 92 ●●●●● patch | view | raw | blame | history
plugins/exchange/views/library/edit.php 31 ●●●● patch | view | raw | blame | history
plugins/exchange/controllers/mall/LibraryController.php
@@ -13,6 +13,7 @@
use app\plugins\exchange\forms\mall\LibraryForm;
use app\plugins\exchange\forms\mall\RecordLogForm;
use app\plugins\exchange\forms\mall\ImportForm;
use app\plugins\exchange\forms\mall\CodeForm;
class LibraryController extends Controller
{
@@ -96,13 +97,12 @@
            return $this->asJson($form->import());
        }
    }
    
    // 模板下载和导出数据
    public function actionExport()
    {
        if (\Yii::$app->request->isPost) {
            $form = new ListForm();
            $form = new CodeForm();
            $form->attributes = \Yii::$app->request->post();
            return $this->asJson($form->export());
        }
plugins/exchange/forms/mall/CodeForm.php
@@ -22,6 +22,8 @@
    public $library_id;
    public $status; //-1 过期 0禁用 1 启用 2兑换
    public $code;
    public $card_no;
    public $keyword;
    public $created_at;
    public $type; //0 后台 礼品
    public $flag;
@@ -32,12 +34,56 @@
        return [
            [['library_id'], 'required'],
            [['library_id', 'type', 'status', 'page'], 'integer'],
            [['code', 'flag'], 'string', 'max' => 100],
            [['code', 'flag','card_no','keyword'], 'string', 'max' => 100],
            [['created_at'], 'trim'],
        ];
    }
    public function getList()
    public function export()
    {
        if (!$this->validate()) {
            return $this->getErrorResponse();
        }
        try {
            $model = new ExportForm();
            $newList = [];
            if(!empty($this->created_at)){
                $this->created_at = explode(",",$this->created_at);
            }
            if(!empty($this->keyword)){
                $this->code = $this->keyword;
            }
            $list = $this->getList(10000)['data']['list'];
            foreach ($list as $item) {
                $newItem['card_no'] = $item['card_no'];
                $newItem['code'] = $item['code'];
                $newItem['created_at'] = $item['created_at'];
                switch($item['status']){
                    case 0:
                        $newItem['status'] = '禁用';
                        break;
                    case 1:
                        $newItem['status'] = '启用';
                        break;
                    case 2:
                        $newItem['status'] = '兑换';
                        break;
                    case 3:
                        $newItem['status'] = '结束';
                        break;
                    default:
                        $newItem['status'] = '';
                }
                $newList[$item['card_no']] = $newItem;
            }
            $model->codeList = array_values($newList);
            $model->exportData();
        } catch (\Exception $exception) {
            return $this->fail(['msg' => $exception]);
        }
    }
    public function getList($limit = 20)
    {
        if (!$this->validate()) {
            return $this->getErrorResponse();
@@ -79,7 +125,7 @@
            if (!is_null($this->type) && $this->type !== '' && $this->type != -1) {
                array_push($where, ['type' => $this->type]);
            }
            empty($this->code) || array_push($where, ['like', 'code', $this->code]);
            empty($this->code) || array_push($where, ['like', 'card_no', $this->code]);
            empty($this->created_at) || array_push(
                $where,
                ['>=', 'created_at', current($this->created_at)],
@@ -95,7 +141,7 @@
                return $exp->export($query);
            }
            $list = $query->page($pagination)->asArray()->all();
            $list = $query->page($pagination,$limit)->asArray()->all();
            $qrcode = new QrcodeForm();
            $qrcode->setTempList();
plugins/exchange/forms/mall/ExportForm.php
New file
@@ -0,0 +1,92 @@
<?php
/**
 * Created by PhpStorm.
 * User: 风哀伤
 * Date: 2020/3/13
 * Time: 14:06
 * @copyright: ©2019 浙江禾匠信息科技
 * @link: http://www.zjhejiang.com
 */
namespace app\plugins\exchange\forms\mall;
use app\core\CsvExport;
use app\plugins\exchange\models\ExchangeCode;
use yii\helpers\Json;
/**
 * Class ExportForm
 * @package app\plugins\ecard\forms\mall
 * @property ExchangeCode $ecard
 */
class ExportForm extends CsvExport
{
    public $codeList = [];
    public function getFileName()
    {
        return '卡号密码--' . mysql_timestamp();
    }
    public function exportData()
    {
        $headList = ['卡号','密码','生成时间','状态'];
        $this->export($this->codeList, $headList, $this->getFileName());
    }
    public function getErrorFileName()
    {
        return '卡密模板--未导入数据' . mysql_timestamp();
    }
    public function exportError()
    {
        $headList = ['卡号','密码','生成时间','状态'];
        $fileName = urlencode($this->getErrorFileName());
        $fileName = $fileName . '.csv';
        $newFilePath = \Yii::$app->basePath . '/web/temp/';
        if (!is_dir($newFilePath)) {
            mkdir($newFilePath, 0777, true);
        }
        $fp = fopen($newFilePath . $fileName, 'a+');
        try {
            //输出Excel列名信息
            foreach ($headList as $key => $value) {
                //CSV的Excel支持GBK编码,一定要转换,否则乱码
                $headList[$key] = iconv('utf-8', 'gbk', $value);
            }
            //将数据通过fputcsv写到文件句柄
            fputcsv($fp, $headList);
            //计数器
            $num = 0;
            //每隔$limit行,刷新一下输出buffer,不要太大,也不要太小
            $limit = 100000;
            $i = 0;
            while ($i < count($this->codeList)) {
                $num++;
                //刷新一下输出buffer,防止由于数据过多造成问题
                if ($limit == $num) {
                    ob_flush();
                    flush();
                    $num = 0;
                }
                $data = array_reduce($this->codeList[$i], function ($v1, $v2) {
                    $v1[] = $v2['value'];
                    return $v1;
                }, []);
                $row = $this->handleRowData($data);
                fputcsv($fp, $row);
                $i++;
            }
            fclose($fp);
        } catch (\Exception $exception) {
            fclose($fp);
            throw $exception;
        }
        return \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/temp/' . $fileName;
    }
}
plugins/exchange/views/library/edit.php
@@ -189,7 +189,7 @@
    }
    .el-message-box__status+.el-message-box__message::after {
        content: '这将导致该码无法使用,并且不可恢复';
        /*content: '这将导致该码无法使用,并且不可恢复';*/
        color: #ff4544;
        margin-top: 10px;
    }
@@ -596,7 +596,7 @@
                    <div flex="wrap:wrap cross:center" style="margin-bottom: 15px;">
                        <div>搜索</div>
                        <div class="input-item">
                            <el-input @keyup.enter.native="search" size="small" placeholder="请输入兑换码" v-model="codeSearch.keyword" clearable @clear="search">
                            <el-input @keyup.enter.native="search" size="small" placeholder="请输入卡号" v-model="codeSearch.keyword" clearable @clear="search">
                                <el-button slot="append" icon="el-icon-search" @click="search"></el-button>
                            </el-input>
                        </div>
@@ -630,6 +630,7 @@
                                start-placeholder="开始日期"
                                end-placeholder="结束日期">
                        </el-date-picker>
                        <!--
                        <app-new-export-dialog
                                text="导出表格"
                                title="表格导出"
@@ -638,6 +639,7 @@
                                :directly="true"
                                :params="codeSearch">
                        </app-new-export-dialog>
                        -->
                    </div>
                    <div flex="cross:center" class="download">
                        <el-button @click="allDownload" size="mini">下载二维码</el-button>
@@ -645,7 +647,7 @@
                    <el-table class="table-info" :data="list" border style="width: 100%" v-loading="listLoading && !exchangeVisible" @selection-change="codeChange">
                        <el-table-column type="selection" width="55"></el-table-column>
                        <el-table-column prop="card_no" label="卡号" width="220"></el-table-column>
                        <el-table-column prop="code" label="兑换码" width="220">
                        <el-table-column prop="code" label="密码" width="220">
                            <template slot-scope="scope">
                                <span :id="'a'+scope.row.code">{{scope.row.code}}</span>
                                <img class="hidden-code" :id="'id'+scope.row.id">
@@ -804,11 +806,11 @@
                        <input name="_csrf" type="hidden" id="_csrf"
                               value="<?= Yii::$app->request->csrfToken ?>">
                        <input name="s-keyword" :value="codeSearch.keyword" type="hidden">
                        <input name="s-type" :value="codeSearch.type" type="hidden">
                        <input name="s_created_at_s" :value="codeSearch.created_at[0]" type="hidden">
                        <input name="s_created_at_e" :value="codeSearch.created_at[1]" type="hidden">
                        <input name="s-status" :value="codeSearch.status" type="hidden">
                        <input name="library_id" :value="id" type="hidden">
                        <input name="keyword" :value="codeSearch.keyword" type="hidden">
                        <input name="type" :value="codeSearch.type" type="hidden">
                        <input name="created_at" :value="codeSearch.created_at" type="hidden">
                        <input name="status" :value="codeSearch.status" type="hidden">
                        <input name="export" :value="exportParams.export" type="hidden">
                    </div>
                    <div flex="dir:right" style="margin-top: 20px;">
@@ -1253,7 +1255,7 @@
                progress: 0,
                file: '',
                exportParams: {
                    action_url: `<?= Yii::$app->urlManager->createUrl('/plugin/exchange/mall/index/export') ?>`,
                    action_url: `<?= Yii::$app->urlManager->createUrl('/plugin/exchange/mall/library/export') ?>`,
                    export: 'export'
                },
                exportDialogVisible: false,
@@ -2087,7 +2089,10 @@
            excelChange(file, fileList) {
                this.file = file.raw;
            },
            exportData() {
                this.exportDialogVisible = true;
                this.exportParams.export = 'export';
            },
            updateNo() {
                    const elt = document.createElement('a');
                    elt.setAttribute('href', this.updateData.url);
@@ -2128,12 +2133,6 @@
                    });
                }
            },
        },
        // 导出数据
        exportData() {
            this.exportDialogVisible = true;
            this.exportParams.export = 'export';
        },
        mounted: function () {