From d39e5d5e3bf6c697afc3a6174f28a40c2b09cb91 Mon Sep 17 00:00:00 2001 From: RogerWork Date: Fri, 15 Dec 2023 18:18:05 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9C=80=E8=A6=81=E5=AD=A6=E4=B9=A0async?= =?UTF-8?q?=E5=92=8Cawait?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/apis/update.js | 9 ++ src/request/config.js | 5 +- src/request/index.js | 172 +++++++++++++++---------------------- src/views/update/index.vue | 90 ++++++++++++++----- 4 files changed, 147 insertions(+), 129 deletions(-) create mode 100644 src/apis/update.js diff --git a/src/apis/update.js b/src/apis/update.js new file mode 100644 index 0000000..205d1f3 --- /dev/null +++ b/src/apis/update.js @@ -0,0 +1,9 @@ +import request from "@/request/index.js"; + +export const cinema_list = () => { + // console.log('cinema_list') + return request({ + url: '/update/cinema/', + method: 'get' + }) +} \ No newline at end of file diff --git a/src/request/config.js b/src/request/config.js index ad3bbc9..1b26e40 100644 --- a/src/request/config.js +++ b/src/request/config.js @@ -1,8 +1,9 @@ export default { method: 'get', - baseURL: 'http://localhost:8001', + baseURL: 'http://127.0.0.1:8000', headers: { - 'Content-Type': 'application/json;charset=UTF-8' + 'Content-Type': 'application/json;charset=UTF-8', + 'Authorization': 'token 4e63b8854974c1fdac9deb891fd74dc145b53f85' }, timeout: 10000, withCredentials: true, diff --git a/src/request/index.js b/src/request/index.js index 8d078e3..cdedf6a 100644 --- a/src/request/index.js +++ b/src/request/index.js @@ -1,111 +1,73 @@ import axios from "axios"; import config from "@/request/config.js"; -import router from "@/router"; -import { ElMessage } from 'element-plus'; +// import {ElMessage} from "element-plus"; +// import {rejects} from "node:assert"; +// import router from "@/router"; -export default function request(options) { - return new Promise((resolve, reject) => { - const instance = axios.create({...config}); - // 请求拦截器 - instance.interceptors.request.use( - (config) => { - let token = localStorage.getItem('pm_token'); - if (token) { - config.headers.token = token; - } else { - router.push('/update') - } - return config; - }, - (error) => { - console.log("request:", error); - if (error.code === "ECONNABORTED" && error.message.indexOf("timeout") !== -1) { - ElMessage({message: "请求超时", type: "error", showClose: true}); - } - return Promise.reject(error) - } - ); - // 响应拦截器 - instance.interceptors.response.use( - (response) => { - return response.data; - }, - (err) => { - if (err && err.response) { - switch (err.response.status) { - case 400: - err.message = "请求错误"; - break - case 401: - err.message = "未授权"; - break - case 403: - err.message = "拒绝访问"; - break - case 404: - err.message = `请求地址出错:${err.response.config.url}`; - break - case 408: - err.message = "请求超时"; - break; - case 500: - err.message = "服务器内部错误"; - break; - case 501: - err.message = "服务未实现"; - break; - case 502: - err.message = "网关错误"; - break; - case 503: - err.message = "服务不可用"; - break; - case 504: - err.message = "网关超时"; - break; - case 505: - err.message = "HTTP版本不受支持"; - break; - default: - } - } - console.log(err); - if (err.message) { - ElMessage({message: err.message, type: 'error', showClose: true}); - } - return Promise.reject(err); - } - ); - //请求处理 - instance(options).then( - /** - * response 统一格式 - * { - * code: 200, - * msg: '消息[String]', - * data: '返回数据[Any]' - * } - * code说明: - * 200 成功 - * -1 失败,可能网络不通,可能后台服务异常或其它异常 - * -2 登录失效跳回登录 - */ - (res) => { - if (res.code === 200) { - resolve(res); - }else{ - if (res.code === -2) { - router.push("/login"); - } - ElMessage({message: res.msg || "操作失败", type: "error", showClose: true}); - reject(res); - } - } - ).catch( - (error) => { - reject(error); - } - ) +// const config = { +// method: 'get', +// baseURL: 'http://127.0.0.1:8000', +// headers: { +// 'Content-Type': 'application/json;charset=UTF-8', +// 'Authorization': 'token 4e63b8854974c1fdac9deb891fd74dc145b53f85' +// }, +// timeout: 10000, +// withCredentials: true, +// responseType: 'json', +// } + +console.log('axios') + +function request(options) { + const instance = axios.create({...config}) + + options = {...options, instance} // 使用创建的实例 + + // 请求拦截器 + instance.interceptors.request.use( + config => { + // 处理请求前的逻辑... + + return config + }, + error => { + // 请求错误处理 + + return Promise.reject(error) + } + ) + + // 响应拦截器 + instance.interceptors.response.use( + response => { + // 处理响应数据 + + return response + }, + error => { + // 处理响应错误 + + return Promise.reject(error) + } + ) + + return new Promise((resolve, reject) => { + instance(options) + .then(res => { + resolve(res.data) + // console.log(res.data) + }) + .catch(err => { + reject(err) + }) }) } + +// request({ +// url: '/update/cinema/' +// }).then(res => { +// console.log(res) +// }) + +export default request \ No newline at end of file diff --git a/src/views/update/index.vue b/src/views/update/index.vue index 580448a..b3da357 100644 --- a/src/views/update/index.vue +++ b/src/views/update/index.vue @@ -1,5 +1,7 @@