mirror of
https://github.com/OpenBMB/MiniCPM-V.git
synced 2026-02-05 02:09:20 +08:00
Update to MiniCPM-o 2.6
This commit is contained in:
61
web_demos/minicpm-o_2.6/web_server/src/hooks/useHttp.js
Normal file
61
web_demos/minicpm-o_2.6/web_server/src/hooks/useHttp.js
Normal file
@@ -0,0 +1,61 @@
|
||||
import axios from 'axios';
|
||||
import { setNewUserId, getNewUserId } from './useRandomId';
|
||||
|
||||
// 创建实例时配置默认值
|
||||
const service = axios.create({
|
||||
baseURL: '/',
|
||||
timeout: 30000,
|
||||
responseType: 'json'
|
||||
});
|
||||
|
||||
// 请求拦截器
|
||||
service.interceptors.request.use(config => {
|
||||
if (config.url.includes('stream')) {
|
||||
config.timeout = 3000;
|
||||
}
|
||||
if (window.location.search) {
|
||||
config.url += window.location.search;
|
||||
}
|
||||
Object.assign(config.headers, ajaxHeader());
|
||||
return config;
|
||||
});
|
||||
|
||||
// 响应拦截器
|
||||
service.interceptors.response.use(
|
||||
response => {
|
||||
let res = response.data;
|
||||
if (response?.status === 200) {
|
||||
return Promise.resolve({
|
||||
code: 0,
|
||||
message: '',
|
||||
data: res
|
||||
});
|
||||
}
|
||||
return Promise.resolve({ code: -1, message: '网络异常,请稍后再试', data: null });
|
||||
},
|
||||
error => {
|
||||
const res = { code: -1, message: error?.response?.data?.detail || '网络异常,请稍后再试', data: null };
|
||||
return Promise.resolve(res);
|
||||
}
|
||||
);
|
||||
|
||||
export const ajaxHeader = () => {
|
||||
if (!localStorage.getItem('uid')) {
|
||||
setNewUserId();
|
||||
}
|
||||
return {
|
||||
'Content-Type': 'application/json;charset=UTF-8',
|
||||
Accept: 'application/json',
|
||||
service: 'minicpmo-server',
|
||||
uid: getNewUserId()
|
||||
};
|
||||
};
|
||||
|
||||
export default {
|
||||
get(url, params, config = {}) {
|
||||
return service.get(url, { params, ...config });
|
||||
},
|
||||
post(url, data, config = {}) {
|
||||
return service.post(url, data, { ...config });
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user