我使用 yarn create @umijs/umi-app 搭建了脚手架,项目内用到了 dva, axios, mock,现在我用 yarn start 启动项目,发送 post 请求返回的状态码是200,开发者工具中 network-preview 没有看到 mock 数据。同时控制台报错:
我把部分涉及请求的代码贴上了,请高手指教!
myApp.js
import { connect } from 'umi';
class MyApp extends Component {
//获取所有未读消息
getUnreadMes = () => {
let userInfo = utils.ssGet('userInfo') || {id: 1234};
return this.props.dispatch({
type: 'myAppModel/getUnreadMes',
payload: {
userId: userInfo.id,
},
});
}
}
export default connect(state => state)(MyApp);
myAppModel.js
import * as Ajax from '@/services/myAppServices';
export default {
reducers: {
changeState(state,{payload}){
return {
...state,
...payload
}
},
},
effects: {
//获取所有未读消息
*getUnreadMes({payload}, {call, put}){
let reply = yield Ajax.queryUnreadMesFuction(payload);
if(reply){
yield put({
type: 'changeState',
payload: {
unreadMessages: reply.unreadList,
}
});
}
},
}
}
myAppServices.js
import axios from '@/utils/axios.js';
//获取所有未读消息
export function queryUnreadMesFuction(data) {
return axios.post('/message/queryUnreadMesFuction', data);
}
axios.js
import axios from 'axios';
import {message} from 'antd';
axios.defaults.baseURL = process.env.NODE_ENV == 'development' ? 'http://localhost:8000/beyondSoft.infomation/api' : '//beyondSoft.infomation/api';
axios.defaults.withCredentials = true;
axios.defaults.headers['X-Requested-With'] = 'XMLHttpRequest';
axios.defaults.headers['Content-Type'] = 'application/json';
axios.interceptors.response.use(res => {
if(typeof res.reply !== 'object'){
//返回数据不是对象
message.error('服务端异常!');
return Promise.reject(res);
}
if(res.reply && res.reply.returnCode.type !== 'S'){
//调用接口失败
if(res.reply.message){
//返回数据中有消息
message.error(res.reply.message);
}
/* if(res.data.resultCode == 416){
//需要登录
router.push({path: '/login'});
} */
return Promise.reject(res.reply);
}
return res.reply;
});
export default axios;
mock/message.js
export default {
//获取所有未读消息
'POST /message/queryUnreadMesFuction': {
reply: {
returnCode: {
type: 'S',
code: 'AAAAAA',
domain: null,
},
unreadList: [
{
author: 'John Doe',
createTime: '2022-02-17 14:27:04',
abstract: 'Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s...',
title: 'Hello World',
messageId: '0',
},
],
}
},
}