2019-03-06 15:49:08 +08:00

184 lines
4.1 KiB
JavaScript

import * as API from 'api/virtual/orders';
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: '',
counts: 0,
order_at: '',
remark: '',
area: [],
address: '',
contacts: '',
mobile: ''
}
};
},
watch: {
show(bool) {
this.my_show = bool;
if (bool) {
this.initCompleteCompanies().then(companies => {
this.companies = companies.filter(item => {
return item.status === 0;
});
});
this.initCompletePackages(this.type).then(packages => {
this.completePackagesFilter = packages.filter(function(item) {
return item.status === 0;
});
});
if (this.data) {
for (let k in this.data) {
if (k in this.params) {
this.params[k] = this.data[k];
}
}
}
}
}
},
methods: {
ok() {
this.params.type = this.type;
if (!this.params.company_id) {
this.$Message.info('请选择企业');
return;
}
if (!this.params.package_id) {
this.$Message.info('请选择套餐');
return;
}
if (!this.params.pay_channel) {
this.$Message.info('请选择支付方式');
return;
}
if (!this.params.counts) {
this.$Message.info('请输入订单卡量');
return;
}
if (!this.params.order_at) {
this.$Message.info('请选择订单时间');
return;
}
if (this.params.contacts && !(/[\s\S]{2,32}/.test(this.params.contacts))) {
this.$Message.info('联系人长度在2-32之间');
return;
}
if (this.params.mobile && !isPhone(this.params.mobile)) {
this.$Message.info('手机号填写不正确');
return;
}
if (this.data) {
// 编辑
API.update(this.params, this.data.id).then(res => {
this.loading = false;
if (res.code == 0) {
this.$emit('update-success');
this.$Message.success('更新成功');
this.clear();
}
}).catch(err => {
this.loading = false;
});
} else {
// 添加
API.create(this.params).then(res => {
this.loading = false;
if (res.code == 0) {
this.$emit('add-success');
this.$Message.success('添加成功');
this.clear();
}
}).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) {
if (type === 1) {
this.params.package_id = '';
this.initCompletePackages(this.type).then(packages => {
this.completePackagesFilter = packages.filter(item => {
return item.status === 0 && item.carrier_operator === this.params.carrier_operator;
});
});
}
if (type === 2) {
let selectPackage = this.completePackagesFilter.find(item => {
return item.id === this.params.package_id;
});
if (selectPackage) {
this.params.carrier_operator = selectPackage.carrier_operator;
}
}
},
cards() {
this.$emit('select-cards');
}
}
};