2019-03-15 15:39:31 +08:00

369 lines
9.6 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 {
my_show: false,
isUpdate: false,
loading: false,
companies: [],
completePackagesFilter: [],
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', 'relationObj'])
},
watch: {
show(bool) {
this.my_show = bool;
if (bool) {
this.initCompleteCompanies().then(companies => {
this.companies = companies.filter(item => {
return item.status === 0;
});
});
this.handleChange(0);
if (this.data) {
for (let k in this.data) {
if (k in this.params) {
this.params[k] = this.data[k];
}
}
}
if (this.selectedMapped.length && !Object.keys(this.order_group).length) {
this.setParamsByReal(this.selected);
}
if (Object.keys(this.order_group).length) {
let key = Object.keys(this.order_group)[0];
this.selectGroup(this.order_group[key], key);
}
}
},
counts(value) {
if (value) {
if (this.selectedMapped.length && !Object.keys(this.order_group).length) {
this.setParamsByReal(this.selected);
}
this.params.counts = value;
}
}
},
methods: {
ok() {
this.params.type = this.type;
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.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') {
// 卡不在VD上转销售
this.params.type = 0;
this.params.sign = 1;
this.post();
} else if (this.params.company_id !== this.group[0].company_id) {
// 改企业的
this.params.type = 0;
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;
if (this.data) {
// 编辑
API.update(this.params, this.data.id).then(res => {
this.loading = false;
if (res.code == 0) {
this.handelSuccess();
}
}).catch(err => {
this.loading = false;
});
} else {
// 添加
API.create(this.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.params.area = [];
this.my_show = false;
},
handleChange(type) {
let packageType = this.type === 1 ? 0 : this.type;
this.initCompletePackages(packageType).then(packages => {
let completePackagesFilter = [];
completePackagesFilter = packages.filter(item => {
return item.status === 0;
});
if (type === 1) {
this.params.package_id = '';
completePackagesFilter = packages.filter(item => {
return item.carrier_operator === this.params.carrier_operator;
});
}
if (type === 2) {
let selectPackage = packages.find(item => {
return item.id === this.params.package_id;
});
if (selectPackage) {
this.params.carrier_operator = selectPackage.carrier_operator;
}
}
this.completePackagesFilter = completePackagesFilter;
});
},
cards() {
this.relationObj = {
virtual_company_id: this.params.company_id,
virtual_package_id: this.params.package_id
};
this.$emit('select-cards');
},
selectGroup(item, index) {
this.groupIndex = index;
this.group = item;
this.params.company_id = item[0].company_id;
this.params.counts = item.reduce((acc, cur) => { return acc + cur.counts; }, 0);
if ([0, 1].indexOf(this.type)) {
this.params.package_id = item[0].package_id;
this.handleChange(2);
}
this.setParamsByReal(item);
},
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 = this.orders.find(el => { return el.id === array[0].order_id; });
if (order) {
switch (order.pay_channel_name) {
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;
}
this.params.order_at = order.order_at;
this.params.transaction_no = order.transaction_no;
this.params.contacts = order.contacts;
this.params.mobile = order.mobile;
this.params.remark = order.remark;
this.params.address = order.address;
}
let relations = this.relations.filter(el => {
return array.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 = mappedCompany.map(el => {
el.display = 0;
let relation = relations.find(e => { return e.virtual_company_id === el.id; });
if (relation) {
el.display = relation.updated_at;
}
});
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];
});
let mappedPackage = this.completePackagesFilter.map(el, i => {
return { index: i, package_id: el.id };
});
mappedPackage = mappedPackage.map(el => {
el.display = 0;
let relation = relations.find(e => { return e.virtual_package_id === el.id; });
if (relation) {
el.display = relation.updated_at;
}
});
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];
});
}
},
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('操作成功');
if (!this.order_group || Object.keys(this.order_group).length < 1) {
this.clear();
if (this.data) {
this.$emit('update-success');
} else {
this.$emit('add-success');
}
}
}
}
};