Skip to content

Commit

Permalink
show Errors
Browse files Browse the repository at this point in the history
  • Loading branch information
szuprefix committed Aug 6, 2019
1 parent b713e09 commit fc3f1f0
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 12 deletions.
33 changes: 28 additions & 5 deletions src/components/creator/BatchCreator.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
<script>
import DataTable from '../table/DataTable.vue'
import TooltipCell from '../table/widgets/TooltipCell.vue'
import {pick, last, uniqWith, isEqual, uniqueId, filter, forOwn} from 'lodash'
import {pick, last, uniqWith, isEqual, isEmpty, uniqueId, filter, forOwn, sortBy} from 'lodash'
import ModelView from '../../mixins/model_view'
import server_response from '../../mixins/server_response'
import Qs from 'qs'
import queueLimit from '../../utils/async_queue'
import {Validator, genFieldRules} from '../../utils/validators'
import {Validator, genFieldRules, clear$Fields} from '../../utils/validators'
export default{
name: 'BatchCreator',
mixins: [ModelView],
mixins: [server_response, ModelView],
props: {
structure: Object,
value: Array
Expand Down Expand Up @@ -144,8 +145,16 @@
}).then(() => {
return queueLimit(this.records, 1, (d => {
if (this.insertMode === 'append' || !d.id) {
if(d.$errors){
return Promise.resolve()
}
return this.$http.post(this.modelListUrl, d).then(({data}) => {
this.$emit('exists', {data: d, structure: this.structure})
}).catch(e => {
this.onServerResponseError(e)
if(e.code === '400'){
d.$errors=e.msg
}
})
} else {
return this.$http.put(this.modelGetDetailUrl(d.id), d).then(({data}) => {
Expand All @@ -158,7 +167,7 @@
},
getRecords(){
let func = this.insertMode === 'ignore' ? (d => !d.id) : (d => true)
this.records = uniqWith(this.value.filter(func), isEqual)
this.records = this.sortByErrors(uniqWith(this.value.filter(func), isEqual))
},
normalizeStructure(){
let r = Object.assign({}, this.structure)
Expand All @@ -176,10 +185,14 @@
}
return r
},
sortByErrors(ds){
return sortBy(ds, a => a.$errors && Object.keys(a.$errors))
},
getForeignKeyData(f){
if (!f.tableItems) {
return []
}
let fcs = this.modelFieldConfigs
let fns = []
let pairs = []
f.tableItems.forEach(ti => {
Expand Down Expand Up @@ -208,6 +221,8 @@
return d
})
data = uniqWith(data, isEqual)
let required = fcs[f.rel].required
data=data.filter(a => !Object.values(clear$Fields(a)).every(isEmpty))
return data
}
},
Expand All @@ -227,7 +242,7 @@
tableItems(){
let r = []
let fcs = this.modelFieldConfigs
if(Object.keys(fcs).length === 0){
if(isEmpty(fcs)){
return r
}
let vs = this.structure.validator || {}
Expand All @@ -242,10 +257,18 @@
return {...fcs[pf], rules, format}
})
this.activeList.forEach(fk => {
let required = fcs[fk.rel].required
fk.tableItems.forEach(ti => {
let label = fk.count > 1 ? `${fk.label}.${ti.label}` : `${fk.label}`
let synonyms = fk.count > 1 ? [] : fk.synonyms
let nti = {...ti, name: `${fk.rel}.${ti.name}`, label, synonyms}
if(!required && fk.count ===1){
nti.rules.forEach(r => {
if(r.required){
r.required = false
}
})
}
r.push(nti)
})
})
Expand Down
6 changes: 3 additions & 3 deletions src/components/sheets/Extractor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<!--<el-step title="创建关联数据"></el-step>-->
</el-steps>

<el-upload :action="actionUrl" v-if="step===0"
<el-upload :action="actionUrl" v-if="step===0" ref="uploader"
:show-file-list="false" :with-credentials="true" :auto-upload="false"
:on-change="submitUpload">
<el-button size="small" type="primary">点击上传</el-button>
Expand All @@ -21,7 +21,7 @@
</template>
<div v-if="step===2">
<el-button @click="step --">上一步</el-button>
<el-button @click="change(); step ++; ">确认</el-button>
<el-button @click="change(); step =0; ">确认</el-button>
<column-bind v-model="fieldMap" :choices="bindChoices"></column-bind>
</div>
<!--<div v-if="step===3">-->
Expand Down Expand Up @@ -56,7 +56,7 @@
components: {Book, EditableLabel, ColumnBind, DataTable},
created () {
},
},
methods: {
genFieldMap(){
let d = {}
Expand Down
7 changes: 6 additions & 1 deletion src/mixins/form_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,12 @@ export default{
let procedure = (valid) => {
if (valid) {
this.formErrors = {}
this._formSubmit().catch(this.onServerResponseError)
this._formSubmit().catch(e => {
let error = this.onServerResponseError(e)
if (error.code === 400) {
this.formErrors = this.joinErrors(error.msg)
}
})
} else {
this.$message({message: '表单检验未通过,请按提示修改', type: 'error'})
return false
Expand Down
5 changes: 3 additions & 2 deletions src/mixins/server_response.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,16 @@ export default {
}
console.log(error)
if (error.code === 400) {
this.errors = this.formErrors = joinErrors(error.msg)
// this.errors = this.formErrors = joinErrors(error.msg)
} else if (error.code === 401) {
this.$store.state.bus.$emit("user-logout")
} else if (error.code === 502){
this.alertError("网关错误")
} else {
this.alertError(error)
}
this.server_response_error = error
return error
// this.server_response_error = error
},

resolveRoutePath(path) {
Expand Down
11 changes: 10 additions & 1 deletion src/utils/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function unicode(v) {
return '' + v
}

const NUM_STR_FORMATERS = [notFloat, unicode, banjiao, noSpace]
export const NUM_STR_FORMATERS = [notFloat, unicode, banjiao, noSpace]

export const HanName = {
type: 'string',
Expand Down Expand Up @@ -126,6 +126,15 @@ export function getFieldRuleType(f) {
return f.type
}

export function clear$Fields(d){
let r = {}
Object.keys(d).forEach(k => {
if(!k.startsWith('$')){
r[k] = d[k]
}
})
return r
}
export function genFieldRules(f) {
let rs = []
if (f.required) {
Expand Down

0 comments on commit fc3f1f0

Please sign in to comment.