mirror of
https://github.com/qier222/YesPlayMusic.git
synced 2024-11-24 21:09:23 +08:00
Make configuring baseURL easier and make the errors friendly (#9)
* feat(utils/request): make configuring baseURL easier
* docs(config/request): update the corresponding changes
5bc29f9200
* feat(utils/request): make the errors friendly
I replaced the ugly if-statement with Map, and
I also make the message user-friendly
since this project is written for users, not only for developers.
And also, I removed the useless "alert", it is useless and annoying for users.
This commit is contained in:
parent
c860745bfe
commit
bce09351cd
|
@ -44,7 +44,7 @@ git clone https://github.com/qier222/YesPlayMusic.git
|
|||
npm install
|
||||
```
|
||||
|
||||
4. 替换 `/src/utils/request.js` 里面 `baseURL` 的值为网易云 API 地址
|
||||
4. 替换 `/src/config/request.js` 里面 `baseURL` 的值为网易云 API 地址
|
||||
|
||||
```JS
|
||||
baseURL: "http://example.com",
|
||||
|
|
6
src/config/request.js
Normal file
6
src/config/request.js
Normal file
|
@ -0,0 +1,6 @@
|
|||
/**
|
||||
* The base url of your API.
|
||||
*
|
||||
* The API can be found at https://github.com/Binaryify/NeteaseCloudMusicApi
|
||||
*/
|
||||
export const baseURL = "";
|
|
@ -1,31 +1,40 @@
|
|||
import axios from "axios";
|
||||
import { baseURL } from "@/config/request";
|
||||
|
||||
const service = axios.create({
|
||||
baseURL: "/api",
|
||||
baseURL: baseURL,
|
||||
withCredentials: true,
|
||||
timeout: 15000,
|
||||
});
|
||||
|
||||
const errors = new Map([
|
||||
[401, 'The token you are using has expired.'],
|
||||
[502, null],
|
||||
[301, 'You must login to use this feature.'],
|
||||
[-1, 'An unexpected error has occurred: '],
|
||||
]);
|
||||
|
||||
service.interceptors.response.use(
|
||||
(response) => {
|
||||
const res = response.data;
|
||||
|
||||
if (res.code !== 200) {
|
||||
if (res.code === 401) {
|
||||
alert("token expired");
|
||||
} else if (res.code === 502) {
|
||||
alert(res.msg);
|
||||
} else if (res.code === 301) {
|
||||
alert("login required");
|
||||
} else {
|
||||
alert("unknown error");
|
||||
}
|
||||
alert(
|
||||
errors.has(res.code)
|
||||
? errors.get(res.code)
|
||||
// null = `The server returned ${res.msg}`
|
||||
|| `The server returned ${res.msg}`
|
||||
// -1 = default expection message
|
||||
: errors.get(-1) + res.code
|
||||
);
|
||||
} else {
|
||||
return res;
|
||||
}
|
||||
},
|
||||
(error) => {
|
||||
console.log("err" + error);
|
||||
alert("err " + error);
|
||||
const errMsg = `error: ${error}`;
|
||||
console.log(errMsg);
|
||||
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
|
Loading…
Reference in New Issue
Block a user