极客园pc项目总结一 登录功能
1.utils中request封装
import axios from 'axios'
const instance=axios.create({
baseURL: 'http://geek.itheima.net/v1_0/',
timeout: 5000,
})
instance.interceptors.request.use(function (config) {
return config;
}, function (error) {
return Promise.reject(error);
});
instance.interceptors.response.use(function (response) {
return response.data;
}, function (error) {
return Promise.reject(error);
});
export default instance;
2.login api封装
import request from 'utils/request'
export const login = (mobile,code) => {
return request({
method:'post',
url:'/authorizations',
data:{
mobile,
code
}
})
}
3.表单提交请求
const onFinish = async ({mobile, code}) => {
try {
const res = await login(mobile, code)
const {token}=res.data
localStorage.setItem('token',token)
navigate('/home')
message.success('登录成功')
} catch (error) {
message.warning(error.response.data.message)
}
}