diff --git a/src/assets/js/api.js b/src/assets/js/api.js index 9b097993..af4ac423 100644 --- a/src/assets/js/api.js +++ b/src/assets/js/api.js @@ -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 - })) - } - } + } diff --git a/src/components/PasswordAuth.vue b/src/components/PasswordAuth.vue index a2bcfbd3..c2db9662 100644 --- a/src/components/PasswordAuth.vue +++ b/src/components/PasswordAuth.vue @@ -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); }); } } diff --git a/src/views/View.vue b/src/views/View.vue index c5515ab5..72cd29dc 100644 --- a/src/views/View.vue +++ b/src/views/View.vue @@ -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); }); }, },