adjustDialog.vue 9.49 KB
<!-- 积分调整 -->
<template>
    <d2-dialog btn-ok-label="提交审核"
               class="adjustDialog"
               :is-visible.sync="visible"
               title="积分调整"
               width="640px"
               @closeDialog="handleClose"
               @confirmDialog="handleConfirm">
        <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"
                         :is-block="isBlock"
                         :label-width="labelWidth">
            <template #scoreHtml>
                <div class="scroe-wrap">
                    <el-input v-model="editData.score"
                              maxlength="9"
                              placeholder="请输入"
                              :prefix-icon="prefixIcon"
                              style="width: 136px;">
                    </el-input>
                </div>
            </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 { limitPrice } from "@/common/format";
import { getAdmin, getSms, manualAdd, manualSubtract } from "@/api/point";
import { deepClone, Util } from "@/common/util";

export default {
	name: "AdjustDialog",

	props: {
		visible: {
			type: Boolean,
			default: false,
		},
		infoData: {
			type: Object,
			default: () => {},
		},
	},
	data() {
		return {
			prefixIcon: "el-icon-plus",
			labelWidth: "110px",
			editChildRef: "editChildRef",
			isBlock: true,
			defaultWidth: "338px",
			isCodeDisabled: false,
			count: "",
			timer: null,
			editData: {
				type: 1,
				score: "",
				captcha: "",
				phone: "",
				orderCode: "",
				admin: "",
			},
			editFormData: [
				{
					label: "积分变更",
					name: "type",
					type: "radio",
					optionList: [],
					optionId: "confId",
					optionLabel: "confValue",
					optionsName: "pointChangeOpt",
					handleRadioChange: (val) => {
						this.prefixIcon = val === 1 ? "el-icon-plus" : "el-icon-minus";
					},
				},
				{
					label: "",
					name: "score",
					maxlength: 9,
					addHtml: "scoreHtml",
					style: {
						marginLeft: "110px",
					},
				},
				{
					label: "关联订单编号",
					name: "orderCode",
					type: "input",
					maxlength: "30",
					placeholder: "请输入",
					isClear: true,
				},
				{
					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",
				},
				{
					label: "备注",
					name: "remark",
					type: "input",
					inputType: "textarea",
					maxlength: "200",
					width: "436px",
					rows: 4,
					showLimit: true,
				},
			],
			formRules: {
				orderCode: [{ required: true, message: "请输入关联订单编号", trigger: "blur" }],
				score: [
					{ required: true, message: "请输入积分", trigger: "blur" },
					{
						validator(rule, value, callback) {
							if (value === "" || value === null || value === undefined) {
								callback(new Error("请输入积分"));
								return;
							}
							const str = String(value).trim();
							// 仅允许:0 或非0开头的整数,且可选最多两位小数
							const pattern = /^(?:0|[1-9]\d*)(?:\.\d{1,2})?$/;
							if (!pattern.test(str)) {
								callback(new Error("积分必须为非负数,最多保留两位小数"));
								return;
							}
							const num = Number(str);
							if (!isFinite(num) || num < 0) {
								callback(new Error("积分必须为非负数"));
								return;
							}
							callback();
						},
						trigger: ["blur", "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" }],
				remark: [{ required: true, message: "请输入备注", trigger: "blur" }],
			},
			baseData: {
				pointChangeOpt: [
					{
						confId: 1,
						confValue: "积分增加",
					},
					{
						confId: 2,
						confValue: "积分减少",
					},
				],
			},
		};
	},

	created() {
		this.getAdminData();
	},

	methods: {
		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) => {
						console.log(res, "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);
						}
					});
			}
		},
		handleConfirm() {
			this.$refs.editFormRef.$refs[this.editChildRef].validate((valid1) => {
				console.log(valid1);
				if (valid1) {
					let Methods = this.editData.type === 1 ? manualAdd : manualSubtract;
					let params = {
						phone: this.editData.phone,
						score: this.editData.score,
						orderCode: this.editData.orderCode,
						remark: this.editData.remark,
						captcha: this.editData.captcha,
						merchantId: this.infoData.merchantId,
						staffPhone: this.infoData.staffPhone,
					};
					Methods(params).then((res) => {
						if (res) {
							this.$message.success("积分调整成功");
							this.$emit("handleConfirm");
						}
					});
				}
			});
		},
		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;
	}
}
</style>