Skip to content

Commit

Permalink
Merge pull request #475 from LucienShui/chore/optimize_api_resolve
Browse files Browse the repository at this point in the history
optimize api error resolve
  • Loading branch information
LucienShui authored Oct 6, 2021
2 parents b9db23b + 86011f4 commit 564d618
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 71 deletions.
94 changes: 54 additions & 40 deletions src/assets/js/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,69 @@ function getLast(value) {
return value[value.length - 1];
}

/**
* 判断是否属于后端已定义的返回值
* @param error
* @returns boolean
*/
function validator(error) {
return error.response && error.response.data && error.response.data.code &&
error.response.data.message && typeof error.response.data.code === 'number' &&
error.response.data.code > 20000;
}

export default {
get: function (url, params = {}, alert_error = true) {
return new Promise((resolve, reject) => {
axios.get(url, {
params: params,
headers: {
'Accept': 'application/json'
}
}).then(response => {
resolve(response.data);
}).catch(error => {
this.errorHandler(error, alert_error);
reject(error);
});
});
},
post: function (url, params = {}) {
return new Promise((resolve, reject) => {
axios.post(url, params).then(response => {
resolve(response.data);
}).catch(error => {
this.errorHandler(error);
reject(error);
});
});
},
put: function (url, params = {}) {
function defaultErrorHandler(error, alert_error = true) {
if (alert_error) {
if (validator(error)) {
let data = error.response.data;
alert(data.code + ': ' + data.message);
} else {
alert(JSON.stringify({
message: error.message,
method: error.config.method,
url: error.config.url,
params: error.config.params
}))
}
}
}

function wrapper(func) {
return function (url, params = {}, acceptedCode = [], errorHandler = defaultErrorHandler) {
return new Promise((resolve, reject) => {
axios.put(url, params).then(response => {
func(url, params).then(response => {
resolve(response.data);
}).catch(error => {
this.errorHandler(error);
if (validator(error)) {
let data = error.response.data;
if (acceptedCode.includes(data.code)) {
resolve(data)
return
}
}
errorHandler(error);
reject(error);
});
});
},
}
}

export default {
get: wrapper(function (url, params) {
return axios.get(url, {
params: params,
headers: {
'Accept': 'application/json'
}
})
}),
post: wrapper(axios.post),
put: wrapper(axios.put),
delete: wrapper(axios.delete),
patch: wrapper(axios.patch),
join: function (...args) {
let result = args.map(pathPart => pathPart.replace(/(^\/|\/$)/g, "")).join("/");
return result + (getLast(getLast(args)) === '/' ? '/' : '');
},
errorHandler: function (error, alert_error = true) {
if (alert_error) {
alert(JSON.stringify({
message: error.message,
method: error.config.method,
url: error.config.url,
params: error.config.params
}))
}
}

}
26 changes: 10 additions & 16 deletions src/components/PasswordAuth.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,17 @@ export default {
methods: {
onSubmit() {
const sendUrl = this.api.join(this.$store.getters.config.api.backend, 'paste', this.$route.params.key);
this.api.get(sendUrl, this.form, false).then(({content, lang}) => {
this.updateContent(content);
this.updateLang(lang === "plain" ? "plaintext" : lang);
this.updateView("paste_view");
}).catch(error => {
if (error.response) {
let data = error.response.data;
if (data.code === 40301) {
this.flag = false;
this.form.password = null;
return
} else if (data.code === 40402) {
this.$router.push("What_are_you_nong_sha_lei?");
return
}
this.api.get(sendUrl, this.form, [40301, 40402]).then(({code, content, lang}) => {
if (code === 40301) {
this.flag = false;
this.form.password = null;
} else if (code === 40402) {
this.$router.push("What_are_you_nong_sha_lei?");
} else {
this.updateContent(content);
this.updateLang(lang === "plain" ? "plaintext" : lang);
this.updateView("paste_view");
}
this.api.errorHandler(error);
});
}
}
Expand Down
24 changes: 9 additions & 15 deletions src/views/View.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,16 @@ export default {
this.updateView("loading");
let url = this.api.join(this.config.api.backend, 'paste', this.$route.params.key)
this.api.get(url, {}, false).then(({content, lang}) => {
this.updateView("paste_view");
this.updateContent(content);
this.updateLang(lang === "plain" ? "plaintext" : lang);
}).catch(error => {
if (error.response) {
let data = error.response.data;
if (data.code === 40301) {
this.updateView("password_auth");
return
} else if (data.code === 40402) {
this.$router.push("What_are_you_nong_sha_lei?");
return
}
this.api.get(url, {}, [40301, 40402]).then(({code, content, lang}) => {
if (code === 40301) {
this.updateView("password_auth");
} else if (code === 40402) {
this.$router.push("What_are_you_nong_sha_lei?");
} else {
this.updateView("paste_view");
this.updateContent(content);
this.updateLang(lang === "plain" ? "plaintext" : lang);
}
this.api.errorHandler(error);
});
},
},
Expand Down

0 comments on commit 564d618

Please sign in to comment.