2019-04-16 11:56:10 +08:00

523 lines
13 KiB
JavaScript

import * as API from "api/virtual/orders";
import { mapGetters } from "vuex";
import { isPhone } from "validate";
export default {
components: {
MDrawer: resolve => require(["components/drawer"], resolve)
},
props: {
show: {
type: Boolean,
default: false
},
source: {
type: Number,
default: 0
},
type: {
type: Number,
default: 0
},
data: {
type: Object,
default: {}
}
},
data() {
return {
alShow: true,
my_show: false,
isUpdate: false,
loading: false,
companies: [],
completePackagesFilter: [],
transaction_nos: [],
params: {
company_id: "",
carrier_operator: "",
package_id: "",
unit_price: 0,
pay_channel: "",
transaction_no: "",
counts: 0,
order_at: "",
remark: "",
area: [],
address: "",
contacts: "",
mobile: ""
},
groupIndex: "",
group: {}
};
},
computed: {
...mapGetters([
"selected",
"selectedMapped",
"counts",
"orders",
"order_group",
"relations"
])
},
watch: {
show(bool) {
this.my_show = bool;
if (bool) {
this.initCompleteCompanies().then(companies => {
this.companies = companies.filter(item => {
return item.status === 0;
});
});
this.init();
if (this.data) {
for (let k in this.data) {
if (k in this.params) {
this.params[k] = this.data[k];
}
}
}
}
},
counts(val) {
if (this.my_show && this.source === 0 && this.type === 0 && val) {
this.init();
}
}
},
methods: {
init() {
this.handleChange(0);
if (this.selectedMapped.length && !Object.keys(this.order_group).length) {
this.setParamsByReal(this.selected);
this.params.counts = this.counts;
}
if (Object.keys(this.order_group).length) {
let key = Object.keys(this.order_group)[0];
this.selectGroup(this.order_group[key], key);
}
},
ok() {
this.params.type = this.type;
console.log(this.params);
if (!this.params.company_id) {
return this.$Message.info("请选择企业");
}
if (!this.params.package_id) {
return this.$Message.info("请选择套餐");
}
if (!this.params.pay_channel) {
return this.$Message.info("请选择支付方式");
}
if (!this.params.counts) {
return this.$Message.info("请输入订单量");
}
if (!this.params.order_at) {
return this.$Message.info("请选择订单时间");
}
if (this.params.unit_price === null) {
return this.$Message.info("请输入价格");
}
if (this.params.contacts && !/[\s\S]{2,32}/.test(this.params.contacts)) {
return this.$Message.info("联系人长度在2-32之间");
}
if (this.params.mobile && !isPhone(this.params.mobile)) {
return this.$Message.info("手机号填写不正确");
}
if (Object.keys(this.order_group).length) {
if (!this.group.length) {
return this.$Message.error("请选择一个订单组");
}
this.params.selected = this.group.map(el => {
return { order_id: el.order_id, counts: el.counts, sim: el.sim };
});
if (this.groupIndex === "0_0") {
if (this.type !== 1) {
return this.$Message.error("暂不支持续费包及加油包转销售");
}
// 卡不在VD上转销售
this.params.sign = 1;
this.post();
} else if (this.params.company_id !== this.group[0].company_id) {
if (this.type !== 1) {
return this.$Message.error("暂不支持续费包及加油包改企业");
}
// 改企业的
this.params.sign = 2;
this.$Modal.confirm({
title: "提示",
content: "是否确认修改所选卡的企业?",
onOk: () => {
this.post();
}
});
} else {
this.post();
}
} else if (this.selectedMapped.length) {
this.params.selected = this.selected.map(el => {
return { order_id: el.order_id, counts: el.counts, sim: el.sim };
});
this.post();
} else {
this.post();
}
},
post() {
this.loading = true;
let params = JSON.parse(JSON.stringify(this.params));
params.order_at = this.moment(params.order_at).format(
"YYYY-MM-DD HH:mm:ss"
);
if (this.data) {
// 编辑
API.update(params, this.data.id)
.then(res => {
this.loading = false;
if (res.code == 0) {
this.handelSuccess();
}
})
.catch(err => {
this.loading = false;
});
} else {
// 添加
API.create(params)
.then(res => {
this.loading = false;
if (res.code == 0) {
this.handelSuccess();
}
})
.catch(err => {
this.loading = false;
});
}
},
visibleChange(bool) {
if (!bool) {
this.$emit("update:show", false);
}
},
clear() {
for (let k in this.params) {
this.params[k] = "";
}
this.params.unit_price = 0;
this.params.counts = 0;
this.transaction_nos = [];
this.alShow = false;
this.params.area = [];
this.$nextTick(() => {
this.alShow = true;
});
this.my_show = false;
this.groupIndex = "";
if (this.source === 0) {
this.$store.dispatch("initOrder");
}
},
handleChange(type) {
let packageType = this.type === 1 ? 0 : this.type;
this.initCompletePackages(packageType).then(packages => {
packages = packages.filter(item => {
return item.status === 0;
});
if (type === 1) {
this.params.package_id = "";
}
if (type === 2) {
let selectPackage = packages.find(item => {
return item.id === this.params.package_id;
});
if (selectPackage) {
this.params.carrier_operator = selectPackage.carrier_operator;
}
}
if (typeof this.params.carrier_operator === "number") {
packages = packages.filter(item => {
return item.carrier_operator === this.params.carrier_operator;
});
}
this.completePackagesFilter = packages;
});
},
cards() {
if (this.params.company_id) {
this.$store.commit("SET_RELATION_OBJ", {
virtual_company_id: this.params.company_id,
virtual_package_id: this.params.package_id
});
this.$store.dispatch("getRelations").then(() => {
this.$emit("select-cards", {
carrier_operator: this.params.carrier_operator
});
});
} else {
this.$store.commit("SET_RELATION_OBJ", {});
this.$emit("select-cards", {
carrier_operator: this.params.carrier_operator
});
}
},
selectGroup(item, index) {
this.transaction_nos = [];
this.groupIndex = index;
this.group = item;
this.setParamsByReal(item);
this.params.counts = item.reduce((acc, cur) => {
return acc + cur.counts;
}, 0);
this.params.company_id = item[0].company_id;
if ([0, 1].indexOf(this.type)) {
this.params.package_id = item[0].package_id;
}
this.handleChange(2);
},
setParamsByReal(array) {
let tmp = array
.map(el => {
return el.company_id + "_" + el.package_id;
})
.filter((v, i, a) => {
return a.indexOf(v) === i;
});
if (tmp.length > 1) {
return;
}
let order_ids = array
.map(el => {
return el.order_id;
})
.filter((v, i, a) => {
return a.indexOf(v) === i;
});
let orders = this.orders.filter(el => {
return order_ids.indexOf(el.id) !== -1;
});
let pay_channel_name = orders
.map(el => {
return el.pay_channel_name;
})
.filter((v, i, a) => {
return a.indexOf(v) === i;
});
if (pay_channel_name.length === 1) {
switch (pay_channel_name[0]) {
case "银行转账":
this.params.pay_channel = "bank";
break;
case "微信支付":
this.params.pay_channel = "wx";
break;
case "支付宝":
this.params.pay_channel = "alipay";
break;
// case '余额支付':
// this.params.pay_channel = 'account';
// break;
// case '天猫续费':
// this.params.pay_channel = 'tmall';
// break;
default:
break;
}
}
let needParams = [
"transaction_no",
"carrier_operator",
"unit_price",
"order_at",
"contacts",
"mobile",
"remark",
"address"
];
for (let index = 0; index < needParams.length; index++) {
const element = needParams[index];
let node = orders
.map(el => {
return el[element];
})
.filter((v, i, a) => {
return a.indexOf(v) === i;
});
if (node.length > 1 && element === "transaction_no") {
this.transaction_nos = node;
}
if (node.length === 1) {
if (element === "unit_price") {
this.params[element] = Number(orders[0][element]);
} else {
this.params[element] = orders[0][element];
}
}
}
this.$store.dispatch("getRelations").then(res => {
let relations = res.filter(el => {
return (
orders.findIndex(e => {
return (
e.company_id === el.real_company_id &&
e.package_id === el.real_package_id
);
}) !== -1
);
});
if (relations.length) {
let mappedCompany = this.companies.map((el, i) => {
return { index: i, company_id: el.id };
});
mappedCompany.map(el => {
el.display = "0";
if (
this.params.company_id !== "" &&
this.params.company_id !== undefined &&
this.params.company_id === el.company_id
) {
el.display = "9999";
return el;
}
let relation = relations.find(e => {
return e.virtual_company_id === el.company_id;
});
if (relation) {
el.display = relation.updated_at;
}
return el;
});
mappedCompany = mappedCompany.sort((a, b) => {
return a.display > b.display ? -1 : a.display < b.display ? 1 : 0;
});
this.companies = mappedCompany.map(el => {
return this.companies[el.index];
});
if (this.companies.length) {
this.params.company_id = this.companies[0].id;
}
let mappedPackage = this.completePackagesFilter.map((el, i) => {
return { index: i, package_id: el.id };
});
mappedPackage = mappedPackage.map(el => {
el.display = "0";
if (
this.params.package_id !== "" &&
this.params.package_id !== undefined &&
this.params.package_id === el.package_id
) {
el.display = "9999";
return el;
}
let relation = relations.find(e => {
return e.virtual_package_id === el.package_id;
});
if (relation) {
el.display = relation.updated_at;
}
return el;
});
mappedPackage.sort((a, b) => {
return a.display > b.display ? -1 : a.display < b.display ? 1 : 0;
});
this.completePackagesFilter = mappedPackage.map(el => {
return this.completePackagesFilter[el.index];
});
if (this.completePackagesFilter.length) {
this.params.package_id = this.completePackagesFilter[0].id;
}
}
});
},
handelSuccess() {
if (Object.keys(this.order_group).length >= 1) {
let mapped = JSON.parse(JSON.stringify(this.order_group));
this.$store.dispatch("removeSelected", mapped[this.groupIndex]);
delete mapped[this.groupIndex];
this.$store.commit("SET_ORDER_GROUP", mapped);
}
this.$Message.success("操作成功");
this.clear();
if (!this.order_group || Object.keys(this.order_group).length < 1) {
if (this.data) {
this.$emit("update-success");
} else {
this.$emit("add-success");
}
}
}
}
};