Authored by zhangwenxue

feature(invoice-manager): add validattion

1 <template> 1 <template>
2 <Modal v-model="visible" :title="title" @on-ok="ok" @on-cancel="cancel"> 2 <Modal v-model="visible" :title="title" @on-ok="ok" @on-cancel="cancel">
3 - <Form :model="formItem" :label-width="80"> 3 + <Form ref="modal" :model="formItem" :label-width="80" :rules="rules">
4 <FormItem label="订单编号"> 4 <FormItem label="订单编号">
5 <i-input v-model="formItem.orderCode" readonly></i-input> 5 <i-input v-model="formItem.orderCode" readonly></i-input>
6 </FormItem> 6 </FormItem>
@@ -13,8 +13,19 @@ @@ -13,8 +13,19 @@
13 </FormItem> 13 </FormItem>
14 <template v-if="formItem.status !== CANCELLED"> 14 <template v-if="formItem.status !== CANCELLED">
15 <template v-if="formItem.type == ELECTRONIC"> 15 <template v-if="formItem.type == ELECTRONIC">
16 - <FormItem label="上传PDF">  
17 - <drag-file-upload @success="uploadPDFSuccess" @remove="uploadPDFRemove"> </drag-file-upload> 16 + <FormItem label="上传PDF" prop="pdfUrl">
  17 + <!-- TODO: 设置文件类型,文件大小,及提示信息 -->
  18 + <drag-file-upload
  19 + upload-icon-title="上传发票"
  20 + bucket="invoice"
  21 + :format="['pdf']"
  22 + :format-err-msg="formatPdfFileTypeMsg"
  23 + :max-size="5 * 1024"
  24 + :format-max-size-msg="formatMaxSizeMsg"
  25 + @success="uploadPDFSuccess"
  26 + @remove="uploadPDFRemove"
  27 + >
  28 + </drag-file-upload>
18 </FormItem> 29 </FormItem>
19 </template> 30 </template>
20 <template v-else> 31 <template v-else>
@@ -22,14 +33,18 @@ @@ -22,14 +33,18 @@
22 <Radio :label="LOGISTICS_YES">需要物流</Radio> 33 <Radio :label="LOGISTICS_YES">需要物流</Radio>
23 <Radio :label="LOGISTICS_NO">不需要物流</Radio> 34 <Radio :label="LOGISTICS_NO">不需要物流</Radio>
24 </RadioGroup> 35 </RadioGroup>
25 - <FormItem v-if="formItem.logisticsSwitch" label=" ">  
26 - <Select v-model="formItem.logisticsCompanyName">  
27 - <Option v-for="(item, idx) in expressList" :key="idx" :value="item.companyName">  
28 - {item.companyName}  
29 - </Option>  
30 - </Select>  
31 - <i-input v-model="formItem.expressNumber" type="text" placeholder="物流单号"></i-input>  
32 - </FormItem> 36 + <template v-if="formItem.logisticsSwitch === LOGISTICS_YES">
  37 + <FormItem v-model="formItem.logisticsId" prop="logisticsId">
  38 + <Select>
  39 + <Option v-for="(item, idx) in expressList" :key="idx" :value="item.id">
  40 + {item.companyName}
  41 + </Option>
  42 + </Select>
  43 + </FormItem>
  44 + <FormItem prop="expressNumber">
  45 + <i-input v-model="formItem.expressNumber" type="text" placeholder="物流单号"></i-input>
  46 + </FormItem>
  47 + </template>
33 </template> 48 </template>
34 </template> 49 </template>
35 </Form> 50 </Form>
@@ -64,6 +79,45 @@ export default { @@ -64,6 +79,45 @@ export default {
64 // api result data 79 // api result data
65 expressList: ExpressCompanyList, 80 expressList: ExpressCompanyList,
66 formItem: {}, 81 formItem: {},
  82 +
  83 + // rules
  84 + rules: {
  85 + pdfUrl: {
  86 + trigger: 'blur',
  87 + validator(rule, value, data) {
  88 + if (data.type === InvoiceTypeName2Id.ELECTRONIC) {
  89 + if (!value) {
  90 + return new Error('请上传发票PDF文件');
  91 + }
  92 + }
  93 + },
  94 + },
  95 + logisticsId: {
  96 + trigger: 'blur',
  97 + validator(rule, value, data) {
  98 + if (data.type === InvoiceTypeName2Id.PAPER) {
  99 + if (!value) {
  100 + return new Error('请选择物流公司');
  101 + }
  102 + }
  103 + },
  104 + },
  105 + expressNumber: {
  106 + trigger: 'blur',
  107 + validator(rule, value, data) {
  108 + if (data.type === InvoiceTypeName2Id.PAPER) {
  109 + if (!value) {
  110 + return new Error('请输入物流单号');
  111 + }
  112 +
  113 + if (!/^[\w\d_-]{5,+}$/.test(value)) {
  114 + // TODO: refs https://www.cnblogs.com/Jerseyblog/p/10077136.html
  115 + return new Error('请输入有效的物流单号'); // TODO: 描述下什么是有效的?
  116 + }
  117 + }
  118 + },
  119 + },
  120 + },
67 }; 121 };
68 }, 122 },
69 created() { 123 created() {
@@ -84,6 +138,7 @@ export default { @@ -84,6 +138,7 @@ export default {
84 } 138 }
85 }, 139 },
86 show(invoice) { 140 show(invoice) {
  141 + this.$refs.modal.resetFields();
87 this.setTitle(invoice); 142 this.setTitle(invoice);
88 // initial data 143 // initial data
89 this.invoiceData = invoice; 144 this.invoiceData = invoice;
@@ -164,21 +219,25 @@ export default { @@ -164,21 +219,25 @@ export default {
164 const params = { ...this.formItem }; 219 const params = { ...this.formItem };
165 220
166 // validate 221 // validate
  222 + this.$refs.modal.validate(valid => {
  223 + if (valid) {
  224 + if (!this.updateState(params)) {
  225 + this.close(true);
  226 + return;
  227 + }
  228 + this.invoiceAPI
  229 + .update(params)
  230 + .then(() => {
  231 + this.$emit('updated', params);
  232 + this.close();
  233 + })
  234 + .catch(() => {
  235 + this.$methods.warn('更新失败');
  236 + });
  237 + }
  238 + });
167 // TODO: 239 // TODO:
168 // update status 240 // update status
169 - if (!this.updateState(params)) {  
170 - this.close(true);  
171 - return;  
172 - }  
173 - this.invoiceAPI  
174 - .update(params)  
175 - .then(() => {  
176 - this.$emit('updated', params);  
177 - this.close();  
178 - })  
179 - .catch(() => {  
180 - this.$methods.warn('更新失败');  
181 - });  
182 }, 241 },
183 ok() { 242 ok() {
184 debugger; 243 debugger;
@@ -198,6 +257,12 @@ export default { @@ -198,6 +257,12 @@ export default {
198 uploadPDFRemove() { 257 uploadPDFRemove() {
199 this.formItem.pdfUrl = ''; 258 this.formItem.pdfUrl = '';
200 }, 259 },
  260 + formatPdfFileTypeMsg(file) {
  261 + return `文件${file.name}格式不正确, 请上传pdf文件`;
  262 + },
  263 + formatMaxSizeMsg(file) {
  264 + return `文件${file.name}太大,不能超过5M`;
  265 + },
201 }, 266 },
202 }; 267 };
203 </script> 268 </script>
@@ -127,16 +127,19 @@ export default { @@ -127,16 +127,19 @@ export default {
127 this.activate = tab || 'all'; 127 this.activate = tab || 'all';
128 this.search(); 128 this.search();
129 }, 129 },
130 - filterValues() { 130 + filterValues(usePage = true) {
131 const params = { 131 const params = {
132 - orderCode: +this.filters.orderCode.model, 132 + orderCode: this.filters.orderCode.model,
133 shopId: this.$user.currentShop.shopsId, 133 shopId: this.$user.currentShop.shopsId,
134 beginTime: this.startTime, 134 beginTime: this.startTime,
135 endTime: this.endTime, 135 endTime: this.endTime,
136 - pageSize: this.pageData.pageSize,  
137 - pageNo: this.pageData.pageNo,  
138 }; 136 };
139 137
  138 + if (usePage) {
  139 + params.pageSize = this.pageData.pageSize;
  140 + params.pageNo = this.pageData.pageNo;
  141 + }
  142 +
140 if (this.activate !== 'all') { 143 if (this.activate !== 'all') {
141 params.status = InvoiceStatusName2Id[this.activate]; 144 params.status = InvoiceStatusName2Id[this.activate];
142 } 145 }
@@ -172,7 +175,7 @@ export default { @@ -172,7 +175,7 @@ export default {
172 exportList() { 175 exportList() {
173 // TODO: check timerange 176 // TODO: check timerange
174 // 30天 177 // 30天
175 - const params = { ...this.filterValues() }; 178 + const params = { ...this.filterValues(false) };
176 let msg; 179 let msg;
177 if (params.endTime === 0 || params.startTime === 0) { 180 if (params.endTime === 0 || params.startTime === 0) {
178 // timeRange is required 181 // timeRange is required
@@ -196,9 +199,9 @@ export default { @@ -196,9 +199,9 @@ export default {
196 199
197 window.open(href, '_blank'); 200 window.open(href, '_blank');
198 }, 201 },
199 - invoiceUpdated(updateInvoiceInfo) {  
200 - console.log(updateInvoiceInfo);  
201 - debugger; 202 + invoiceUpdated() {
  203 + // 刷新
  204 + this.search();
202 }, 205 },
203 }, 206 },
204 }; 207 };