zm
2021-03-24 879b860eb7213b182106416aad8f501f898e8831
兑换码导入功用开发,未完
4 files modified
211 ■■■■■ changed files
plugins/exchange/controllers/mall/LibraryController.php 11 ●●●●● patch | view | raw | blame | history
plugins/exchange/forms/mall/CodeEditForm.php 95 ●●●●● patch | view | raw | blame | history
plugins/exchange/models/ExchangeCode.php 4 ●●● patch | view | raw | blame | history
plugins/exchange/views/library/edit.php 101 ●●●●● patch | view | raw | blame | history
plugins/exchange/controllers/mall/LibraryController.php
@@ -12,6 +12,7 @@
use app\plugins\exchange\forms\mall\LibraryEditForm;
use app\plugins\exchange\forms\mall\LibraryForm;
use app\plugins\exchange\forms\mall\RecordLogForm;
use app\plugins\exchange\forms\mall\ImportForm;
class LibraryController extends Controller
{
@@ -85,4 +86,14 @@
            return $this->asJson($form->getCode());
        }
    }
    // 导入卡号和密码数据
    public function actionImport()
    {
        if (\Yii::$app->request->isAjax) {
            $form = new ImportForm();
            $form->attributes = \Yii::$app->request->post();
            return $this->asJson($form->import());
        }
    }
}
plugins/exchange/forms/mall/CodeEditForm.php
@@ -10,6 +10,7 @@
use app\core\response\ApiCode;
use app\models\Model;
use yii\helpers\Json;
use app\plugins\exchange\forms\common\CommonModel;
use app\plugins\exchange\forms\common\CreateCode;
use app\plugins\exchange\models\ExchangeCode;
@@ -17,6 +18,7 @@
class CodeEditForm extends Model
{
    public $library_id;
    public $list;
    public $num;
    public $id;
    public function rules()
@@ -25,6 +27,7 @@
            [['library_id', 'num'], 'required'],
            [['library_id', 'num', 'id'], 'integer'],
            [['num'], 'integer', 'max' => 1000],
            [['list'], 'safe'],
        ];
    }
@@ -92,4 +95,96 @@
            ];
        }
    }
    public function batch()
    {
        // if (!$this->validate()) {
        //     return $this->getErrorResponse();
        // }
        $transaction = \Yii::$app->db->beginTransaction();
        $list = Json::decode($this->list, true);
        try {
            if (!$list || empty($list)) {
                throw new \Exception('卡号密码数据不能为空');
            }
            $form = new CodeForm();
            $form->library_id = $this->library_id;
            $codeList = $form->getlist()['data']['list'];
            $error = array();
            foreach ($codeList as $key=>$value){
                foreach ($list as $k=>$v){
                      if($value['card_no']==$v['key1']){
                            $error[] = $v['key1'];
                            unset($list[$key]);
                      }
                }
            }
            if (!empty($error)) {
                throw new \Exception('数据列表存在重复的数据');
            }
            $data = [];
            $url = '';
            $model = new ExchangeCode();
            $dataList = [];
            foreach ($list as $k => $newItem) {
                // 拼接需要保存的卡密数据
                $model->attributes = [
                    'mall_id' => \Yii::$app->mall->id,
                    'library_id' => $this->library_id,
                    'type' => 0,
                    'code' => $newItem['key2'],
                    'status' => 1,
                    'validity_type' => 'all',
                    'valid_end_time' => $newItem['key4'],
                    'valid_start_time' => $newItem['key3'],
                    'created_at' => date("Y-m-d H:i:s"),
                    'r_user_id' => 0,
                    'card_no'=> $newItem['key1']
                ];
                $model->validate();
                $dataList[] = $model->attributes;
            }
            // 保存卡密数据
            \Yii::$app->db->createCommand()->batchInsert(
                ExchangeCode::tableName(),
                array_keys($model->attributes),
                $data
            )->execute();
            return $this->success(['msg' => '保存成功', 'data' => [
                'success' => count($data),
                'fail' => count($list) - count($data),
                'url' => $url
            ]]);
        } catch (\Exception $exception) {
            $transaction->rollBack();
            return $this->fail(['msg' => $exception->getMessage(), 'data' => $list]);
        }
    }
    public function success($data)
    {
        $res = $this->handle($data);
        $res['code'] = ApiCode::CODE_SUCCESS;
        return $res;
    }
    public function fail($data)
    {
        $res = $this->handle($data);
        $res['code'] = ApiCode::CODE_ERROR;
        return $res;
    }
    private function handle($data)
    {
        $msg = $data['msg'] ?? '';
        unset($data['msg']);
        return [
            'msg' => $msg,
            'data' => $data
        ];
    }
}
plugins/exchange/models/ExchangeCode.php
@@ -24,6 +24,7 @@
 * @property string $r_rewards
 * @property string $name
 * @property string $mobile
 * @property string $card_no
 */
class ExchangeCode extends ModelActiveRecord
{
@@ -49,7 +50,7 @@
            [['mall_id', 'library_id', 'type', 'status', 'r_user_id'], 'integer'],
            [['valid_end_time', 'valid_start_time', 'created_at', 'r_raffled_at'], 'safe'],
            [['r_rewards'], 'string'],
            [['code', 'validity_type', 'name', 'mobile', 'r_origin'], 'string', 'max' => 100],
            [['code', 'validity_type', 'name', 'mobile', 'r_origin','card_no'], 'string', 'max' => 100],
        ];
    }
@@ -75,6 +76,7 @@
            'r_origin' => '兑换来源',
            'name' => '后台联系人',
            'mobile' => '后台手机号码',
            'card_no'=>'卡号'
        ];
    }
plugins/exchange/views/library/edit.php
@@ -583,6 +583,7 @@
                    <div>
                        <el-button @click="getActivity" size="small" type="primary">生成动态活码</el-button>
                        <el-button @click="appendDialog=true;num=''" size="small" type="primary">批量生成兑换码</el-button>
                        <el-button type="primary" @click="uploadVisible = true" size="small">批量导入卡号</el-button>
                    </div>
                </div>
                <div class="rules use-rules">
@@ -719,6 +720,66 @@
    <div class="bottom-div" flex="cross:center" v-if="id == 0">
        <el-button :loading="saveLoading" type="primary" size="small" class="save"  @click="saveEdit('form')">保存</el-button>
    </div>
    <el-dialog
            title="批量导入卡密数据"
            :visible.sync="updateDialogVisible"
            width="20%"
    >
        <div>
            <div style="margin-bottom: 15px;">文件大小:{{sizeM}}</div>
            <div flex="">
                <div style="display: inline-block">
                    导入进度:
                </div>
                <div style="width: 80%">
                    <el-progress :stroke-width="10" :percentage="progress"></el-progress>
                </div>
            </div>
            <div v-if="updateData">
                <div flex="cross:center main:center" style="margin-top: 15px;">
                    <image style="width: 29px;height: 29px;margin-right:5px" src="statics/img/mall/pass.png"></image>
                    <span>成功导入{{updateData.success}}条数据</span>
                </div>
                <div flex="cross:center main:center" style="margin-top: 15px;" >
                    <span style="margin-right: 5px;">
                        未导入
                        <span style="color: #ff5d5d">{{updateData.fail}}</span>
                        条数据
                    </span>
                    <div>
                        <el-button @click="updateNo" style="color: #3398ff;cursor: pointer;border: none;padding: 0;margin-left: 5px;">
                            下载未导入数据
                        </el-button>
                    </div>
                </div>
            </div>
        </div>
        <div slot="footer" class="dialog-footer" flex="main:center">
            <el-button type="primary" @click="updateDialogVisible = false">我知道了</el-button>
        </div>
    </el-dialog>
    <el-dialog
        title="提示"
        :visible.sync="uploadVisible"
        width="30%">
        <p flex="cross:center">
            <image src="statics/img/plugins/sigh.png" style="margin-right: 10px;width:30px;height: 30px;"></image>
            <span>请导入不大于2M的Excel文件</span>
        </p>
        <span slot="footer" class="dialog-footer">
             <el-upload
                 style="margin-right: 10px;"
                 action=""
                 :multiple="false"
                 :http-request="handleFile"
                 accept=".xls,.csv"
                 :on-change="excelChange"
                 :show-file-list="false">
                <el-button type="primary" >点击导入</el-button>
            </el-upload>
        </span>
    </el-dialog>
    <!-- 选择奖励品 -->
    <el-dialog title="选择奖励品类型" :visible.sync="chooseDialog" width="590px">
        <div flex="dir:left cross:center" style="margin-bottom: 24px;" v-for="(item,index) in reward_type" :key="index">
@@ -1146,6 +1207,10 @@
                        {id: 'balance', img: './../plugins/exchange/assets/img/balance.png'},
                    ]
                ],
                uploadVisible: false,
                updateDialogVisible: false,
                updateData: null,
                file: '',
                getActivityCode: false,
                codeShow: true,
                qrVisible: false,
@@ -1973,6 +2038,42 @@
                    }
                })
            },
            excelChange(file, fileList) {
                this.file = file.raw;
            },
            async handleFile() {
                let formData = new FormData();
                let size = this.file.size;
                let sizeM = (size / Math.pow(1024, 2)).toFixed(2) + "M";
                this.sizeM = sizeM;
                formData.append('file', this.file);
                this.uploadVisible = false;
                this.updateDialogVisible = true;
                formData.append('library_id', this.id);
                let that = this;
                const e = await request({
                    params: {
                        r: `plugin/exchange/mall/library/import`,
                    },
                    method: 'post',
                    data: formData,
                    onUploadProgress: function(progressEvent) {
                        if (progressEvent.lengthComputable) {
                            that.progress = Math.round((progressEvent.loaded / progressEvent.total) * 100);
                        }
                    }
                });
                if (e.data.code === 0) {
                    this.updateData = e.data.data.data;
                    this.getInform();
                } else {
                    that.$message({
                        type: 'warning',
                        message: e.data.msg
                    });
                }
            },
        },
        mounted: function () {
            this.getSvip();