From 9c0bfbe4596ca70149254098e6fa7ac9d5656bfa Mon Sep 17 00:00:00 2001 From: Pengxiansen <2422914688@qq.com> Date: Thu, 13 Feb 2025 18:39:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/growthRequest.js | 160 +++++++++++++++++++++++++++++ src/views/growth/growthList.vue | 2 +- src/views/growth/growthRoadmap.vue | 2 +- 3 files changed, 162 insertions(+), 2 deletions(-) create mode 100644 src/api/growthRequest.js diff --git a/src/api/growthRequest.js b/src/api/growthRequest.js new file mode 100644 index 0000000..2f70240 --- /dev/null +++ b/src/api/growthRequest.js @@ -0,0 +1,160 @@ +import router from "@/router"; +import {reactive, ref, toRefs, watch} from "vue"; +import axios from 'axios'; +import {getCookie} from "@/api/utils"; +import JSONBigInt from 'json-bigint'; +import {ElMessage} from "element-plus"; + +const JSONBigIntStr = JSONBigInt({storeAsString: true}); + +export function usePage(_url, param, callback) { + + const state = reactive({ + data: {}, + loading: false, + total: 0, + size: 10, + current: 1, + params: {pageNo: 1, pageSize: 10, ...param} + }) + + watch(param, () => { + state.params = {...state.params, ...param} + fetchData() + }) + + function fetchData() { + state.loading = true + request(_url, state.params).then(r => { + state.params.pageNo === 1 ? (state.data = (r.data.records || r.data.rows)) : (state.data = [...state.data, ...(r.data.records || r.data.rows)]) + state.size = r.data.size + state.total = r.data.total + state.current = r.data.current + state.loading = false + callback && callback(r) + }) + } + + fetchData() + return { + ...toRefs(state), + fetchData, + }; +} + +export function useRequest(_url, params = {}, callback) { + + const state = reactive({ + data: {}, + loading: false, + + }) + + watch(params, () => { + fetchData() + }) + + function fetchData() { + state.loading = true + request(_url, params).then(r => { + state.data = r.data + state.loading = false + callback&&callback(r) + }) + } + + fetchData() + return { + ...toRefs(state), + fetchData, + }; +} + +export async function request(_url, params) { + const s = _url.split(' ') + let url = s[0] + const method = s[1]?.toLowerCase() || 'get' + if (method === 'get') { + let paramsArray = []; + //拼接参数 + if (params) { + Object.keys(params).forEach(key => paramsArray.push(key + '=' + params[key])) + if (url.search(/\?/) === -1) { + url += '?' + paramsArray.join('&') + } else { + url += '&' + paramsArray.join('&') + } + } + } + const body = method !== 'get' ? params || {} : {} + return axios({ + url, + method, + headers: { + 'token': getCookie('token'), + ...method !== 'get' ? {'Content-Type': 'application/json'} : {} + }, + ...method !== 'get' ? {data: JSON.stringify(body)} : {} + }).then(resp => resp.data).then(response => { + if (response.code !== 200 && response.code !== 0) { + if (response.code === 1000) (import.meta.env.MODE === 'development' || import.meta.env.MODE === 'test') ? router.push({path: '/login'}) : window.open(window.location.protocol + import.meta.env.VITE_BASE_LOGIN_URL, '_top') + + else if (response.code === 2001) router.push({path: '/FaceTeachSignUp', query: {courseId: router.currentRoute.value.query.courseId,type:3}}) + + else if (response.code === 2002) router.push({path: '/FaceTeachNoCommon', query: {courseId: router.currentRoute.value.query.courseId,type:3}}) + else if (response.code === 9000) ElMessage.warning("该数据已经被删除或停用,请联系管理员"); + + // if (import.meta.env.DEV && response.code === 1000) { + // router.push({path: '/login'}) + // } else { + // window.open() + // response.showMsg && notification.open({ + // message: response.showMsg, + // duration: 2, + // }); + // } + } + return response + }) +} + +export async function boeRequest(_url, params) { + const s = _url.split(' ') + let url = s[0] + const method = s[1]?.toLowerCase() || 'get' + if (method === 'get') { + let paramsArray = []; + //拼接参数 + if (params) { + Object.keys(params).forEach(key => paramsArray.push(key + '=' + params[key])) + if (url.search(/\?/) === -1) { + url += '?' + paramsArray.join('&') + } else { + url += '&' + paramsArray.join('&') + } + } + } + const body = method !== 'get' ? params || {} : {} + return fetch(url, { + method, + headers: { + token: getCookie('token'), + ...method !== 'get' ? {'Content-Type': 'application/json'} : {} + }, + ...method !== 'get' ? {body: JSON.stringify(body)} : {} + }).then(res => { + return res.text() + }).then(res => { + return JSONBigIntStr.parse(res) + }) +} + +const httpupload = axios.create({ + baseURL: process.env.VUE_APP_BASE_API, + timeout: 1000 * 15, + headers: { "Content-Type": "multipart/form-data" }, +}); + +export const fileUp = (data) => httpupload.post( import.meta.env.VITE_BASE_API+"/file/upload", data, { + headers: { "Content-Type": "multipart/form-data" }, +}); \ No newline at end of file diff --git a/src/views/growth/growthList.vue b/src/views/growth/growthList.vue index b05a377..e8ab46d 100644 --- a/src/views/growth/growthList.vue +++ b/src/views/growth/growthList.vue @@ -456,7 +456,7 @@