fastmock

使用fastmock创建在线mock数据
示例: 登录验证

  1. {
  2. "code": "0000",
  3. "data": {
  4. "verifySuccess": function({_req, Mock}) {
  5. let body = _req.body;
  6. return body.username === 'admin' && body.password === '123456';
  7. },
  8. "userInfo": function({_req, Mock}) {
  9. let body = _req.body;
  10. if (body.username === 'admin' && body.password === '123456') {
  11. return Mock.mock({
  12. username: "admin",
  13. email: "@email",
  14. address: "@address"
  15. });
  16. } else {
  17. return null;
  18. }
  19. },
  20. },
  21. "desc": "成功"
  22. }

axios

安装axios

  1. yarn add axios

登录时发送 post 请求

  1. import axios from 'axios'
  2. axios.defaults.baseURL = 'https://www.fastmock.site/mock/xxx';
  3. export default {
  4. setup(){
  5. const data = reactive(
  6. {
  7. username: '',
  8. password: ''
  9. }
  10. )
  11. const handleLogin = () => {
  12. axios.post('/api/user/login', {
  13. username: data.username,
  14. password: data.password
  15. }).then((res) => {
  16. if(res.data.data.verifySuccess === false) {
  17. alert('用户名或密码不正确')
  18. } else {
  19. localStorage.isLogin = true
  20. router.push({name: 'Home'})
  21. }
  22. }).catch(() => {
  23. alert('失败')
  24. })
  25. }
  26. }

封装axios方法

新建utils目录,新建request.ts

  1. import axios from 'axios'
  2. axios.defaults.baseURL = 'https://www.fastmock.site/mock/xxx/jd'
  3. axios.defaults.headers.post['Content-Type'] = 'application/application/json'
  4. const post = async (url: string, data: any = {}) => {
  5. const response = await axios.post(url, data)
  6. return response.data
  7. }
  8. export {
  9. post,
  10. }

使用

  1. import {post} from '@/utils/request'
  2. const handleLogin = async () => {
  3. try {
  4. const result = await post('/user/login', {
  5. username: data.username,
  6. password: data.password
  7. })
  8. if(result.data.verifySuccess === true) {
  9. localStorage.isLogin = true
  10. router.push({name: 'Home'})
  11. } else {
  12. alert('用户名或密码错误')
  13. }
  14. } catch(e) {
  15. console.log(e)
  16. alert('请求失败')
  17. }
  18. }

上面使用 async/await 做的封装,也可以使用 promise 封装

  1. import axios from 'axios'
  2. export const post = (url, data={}) => {
  3. return new Promise((resolve, reject) => {
  4. axios.post(url, data, {
  5. baseURL: 'xxx',
  6. headers: {
  7. 'Content-Type': 'application/json'
  8. }
  9. }
  10. }).then(response => {
  11. resolve(response.data)
  12. }, err => {
  13. reject(err)
  14. })
  15. }

添加get方法

  1. import axios from 'axios'
  2. axios.defaults.baseURL = 'https://www.fastmock.site/mock/xxx/jd'
  3. axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'
  4. const post = async (url: string, data: any = {}) => {
  5. const response = await axios.post(url, data)
  6. return response.data
  7. }
  8. const get = async (url: string) => {
  9. const response = await axios.get(url)
  10. return response.data
  11. }
  12. export { post, get }

instance

另一种方法是创建axios实例,并传入 config 信息,再使用实例

  1. import axios from 'axios'
  2. const instance = axios.create({
  3. baseURL: 'https://www.fastmock.site/mock/xxx/jd',
  4. })
  5. instance.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'
  6. const post = async (url: string, data: any = {}) => {
  7. const response = await instance.post(url, data)
  8. return response.data
  9. }
  10. const get = async (url: string) => {
  11. const response = await instance.get(url)
  12. return response.data
  13. }
  14. export { post, get }