GoverningDialog.vue 10.3 KB
<!-- 批量积分调整 -->
<template>
    <d2-dialog btn-ok-label="提交审核"
               class="adjustDialog"
               :is-visible.sync="visible"
               title="批量调整积分"
               width="640px"
               @closeDialog="handleClose"
               @confirmDialog="handleConfirmImport">
        <div class="tip-text">
            提交审核通过后,积分将自动更新
        </div>
        <glsx-combi-form ref="editFormRef"
                         class="edit-form-container"
                         :default-width="defaultWidth"
                         :form-data="editFormData"
                         :form-obj="editData"
                         :form-ref="editChildRef"
                         :form-rules="formRules"
                         glsx-combi-form
                         :is-block="isBlock"
                         :label-width="labelWidth"
                         :nested-modal-append-to-body="true">
            <template #fileHtml>
                <el-row>
                    <el-col :span="19">
                        <el-input v-model="editData.fileName"
                                  readonly
                                  style="width: 338px;" />
                    </el-col>
                    <el-col class="m-l-md"
                            :span="2">
                        <el-button class="import-btn">
                            导入文件
                            <input id="fileUpload"
                                   accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel"
                                   class="import-input"
                                   type="file"
                                   @change="importFile($event)">
                        </el-button>
                    </el-col>
                </el-row>
                <el-link class="import-template"
                         icon="el-icon-link"
                         type="primary"
                         @click="exportTemplate">
                    模版下载
                </el-link>
            </template>
            <template #captcha>
                <div class="captcha-wrap">
                    <el-input v-model="editData.captcha"
                              placeholder="请输入"

                              style="width: 338px;">
                    </el-input>
                    <div class="captcha-text" @click.prevent="getCode">
                        {{ !isCodeDisabled ? '获取验证码' : `${count}s` }}
                    </div>
                </div>
            </template>

        </glsx-combi-form>
    </d2-dialog>
</template>

<script>
import { mapActions, mapMutations, mapState } from "vuex";
import { getAdmin, getSms, governingImport } from "@/api/point";
import { deepClone, Util } from "@/common/util";

export default {
	name: "AdjustDialog",

	props: {
		visible: {
			type: Boolean,
			default: false,
		},
	},
	data() {
		return {
			labelWidth: "110px",
			editChildRef: "editChildRef",
			isBlock: true,
			defaultWidth: "338px",
			isCodeDisabled: false,
			count: "",
			timer: null,
			editData: {
				captcha: "",
				fileName: "",
				phone: "",
				admin: "",
			},
			editFormData: [
				{
					label: "导入文件",
					name: "fileName",
					addHtml: "fileHtml",
					width: "500px",
				},
				{
					label: "管理员",
					name: "admin",
					type: "select",
					optionsName: "adminList",
					optionId: "adminId",
					optionLabel: "adminName",
					changeAttr: (val) => {
						this.editData.phone = val;
						this.isCodeDisabled = false;
						clearInterval(this.timer);
						this.timer = null;
						this.editData.captcha = "";
					},
				},
				{
					label: "验证码",
					addHtml: "captcha",
					name: "captcha",
				},
			],
			formRules: {
				fileName: [{ required: true, message: "请选择导入文件", trigger: "change" }],
				captcha: [{ required: true, message: "请输入验证码", trigger: "blur" }, {
					validator(rule, value, callback) {
						const str = String(value || "").trim();
						if (str === "") {
							// 空值交给 required 规则处理
							callback();
							return;
						}
						if (!/^\d+$/.test(str)) {
							callback(new Error("验证码只能输入数字"));
							return;
						}
						callback();
					},
					trigger: ["blur", "change"],
				}],
				admin: [{ required: true, message: "请选择管理员", trigger: "change" }],
			},
			baseData: {
				adminList: [],
			},
		};
	},

	created() {
		this.getAdminData();
	},
	computed: {
		...mapState({
			dowloadList: state => state.d2admin.user.dowloadList,
			updateList: state => state.d2admin.user.updateList,
			updateRefesh: state => state.d2admin.user.updateRefesh,
		}),
	},
	methods: {
		...mapActions("d2admin/user", ["actionSetDownloadList"]),
		// 模板下载
		exportTemplate() {
			let params = {};
			params.method = "dj.api.smartcarlife.staff.score.manual.import.template";
			const list = [
				...this.dowloadList,
				{
					name: "积分调整导入模板",
					exportParam: JSON.stringify(params),
					status: "waiting",
					timeid: new Date().getTime(),
				},
			];
			this.actionSetDownloadList({ list, type: "dowloadList" });
		},
		handleBatchImport() {
			this.$emit("handleConfirm");
			this.$refs.editFormRef.$refs[this.editChildRef] && this.$refs.editFormRef.$refs[this.editChildRef].resetFields();
		},
		// 上传文件,转码为base64
		importFile(e) {
			const size = e.target.files[0].size / 1024 / 1024;
			if (size < 9) {
				this.$set(this.editData, "fileName", e.target.files[0].name);
				// // 转码base64
				const reader = new FileReader();
				reader.readAsDataURL(e.target.files[0]);
				reader.onload = (event) => {
					const imgFile = event.target.result;
					const arrBase64 = imgFile.split(",");
					this.fileInfo = e.target.files[0].name.split(".")[e.target.files[0].name.split(".")?.length - 1] + "@" + arrBase64[1];
					document.getElementById("fileUpload").value = "";
				};
			} else {
				this.notice.errorTips("上传文件不可超过9M");
				document.getElementById("fileUpload").value = "";
			}
		},
		handleConfirmImport() {
			this.$refs.editFormRef.$refs[this.editChildRef].validate((valid1) => {
				if (valid1) {
					let params = {
						file: this.fileInfo,
						phone: this.editData.phone,
						captcha: this.editData.captcha,

					};
					governingImport(params).then((res) => {
						if (res) {
							// 异步处理文件上传 把id传入处理轮询
							const list = [...this.updateList, {
								name: this.editData.fileName,
								ifSuccess: false,
								uuid: res,
								type: "upload",
							}];

							this.actionSetDownloadList({ list, type: "updateList" });
							this.notice.okTips("上传成功");
							this.handleBatchImport();
						}
					});
				}
			});
		},
		getAdminData() {
			getAdmin().then((res) => {
				if (res) {
					this.baseData.adminList = res.map((item) => {
						return {
							adminId: `${item.phone}`,
							adminName: `${item.name} ${item.phone}`,
							name: item.name,
							phone: item.phone,
						};
					});
					this.getOptionsList(this, "editFormData", false);
					if (this.baseData.adminList.length == 0) {
						this.editData.admin = "";
						this.editData.phone = "";
					} else {
						this.editData.admin = this.baseData.adminList[0].adminId;
						this.editData.phone = this.baseData.adminList[0].phone;
					}
				}
			});
		},
		getCode() {
			if (!this.editData.phone) {
				this.$message.error("请选择管理员");
				return;
			}
			if (this.isCodeDisabled)
				return;
			if (!this.timer) {
				this.count = 60;
				getSms({
					phone: this.editData.phone,
				})
					.then((res) => {
						if (res) {
							this.$message.success("短信验证码发送成功");
							this.isCodeDisabled = true;
							this.timer = setInterval(() => {
								if (this.count > 0 && this.count <= 60) {
									this.count--;
								} else {
									this.isCodeDisabled = false;
									clearInterval(this.timer);
									this.timer = null;
								}
							}, 1000);
						} else {
							this.$message.error(res.message);
						}
					});
			}
		},
		handleClose() {
			this.$emit("handleClose");
		},
		getOptionsList(that, formName, hasAll = true, refreshSigle) {
			if (refreshSigle) {
				let item = that[formName].find(k => k.name == refreshSigle);
				let index = that[formName].findIndex(k => k.name == refreshSigle);
				that.$set(
					that[formName][index],
					"optionList",
					deepClone(that.baseData[item.optionsName]),
				);
				if (
					hasAll
					&& that.baseData[item.optionsName].length
					&& that.baseData[item.optionsName][0][
						that[formName][index].optionLabel
					] !== "全部"
				) {
					const temp = {};
					temp[that[formName][index].optionId] = "";
					temp[that[formName][index].optionLabel] = "全部";
					that[formName][index].optionList.unshift(temp);
				}
			} else {
				that[formName].forEach((item, index) => {
					if (
						that.baseData[item.optionsName]?.length
					) {
						that.$set(
							that[formName][index],
							"optionList",
							deepClone(that.baseData[item.optionsName]),
						);
						if (
							hasAll
							&& that.baseData[item.optionsName].length
							&& that.baseData[item.optionsName][0][
								that[formName][index].optionLabel
							] !== "全部"
						) {
							const temp = {};
							temp[that[formName][index].optionId] = "";
							temp[that[formName][index].optionLabel] = "全部";
							that[formName][index].optionList.unshift(temp);
						}
					}
				});
			}
		},
	},
};
</script>

<style scoped lang="scss">
.adjustDialog {
	::v-deep {
		.el-dialog__header {
			border-bottom: 1px solid #e5e5e5;
		}
		.el-dialog__footer {
			border-top: 1px solid #e5e5e5;
		}
		.el-input__count {
			right: 12px;
			bottom: 1px;
		}
	}
}
.tip-text {
	border-radius: 4px;
	background: #f7f7f7;
	color: #333333;
	font-size: 14px;
	padding: 8px 0;
	text-align: center;
	margin-bottom: 26px;
	margin-top: -10px;
}
.captcha-wrap {
	.captcha-text {
		font-size: 12px;
		color: #2a58ad;
		position: absolute;
		right: 0;
		top: 2px;
		margin-right: 10px;
		cursor: pointer;
		width: 100px;
		height: 32px;
		text-align: right;
	}
}
.import-btn {
	position: relative;
}
.import-input {
	opacity: 0;
	position: absolute;
	right: 0;
	top: 0;
	width: 98px;
	height: 36px;
	cursor: pointer;
	font-size: 0;
}
</style>