|
| 1 | +# Mock 数据 |
| 2 | + |
| 3 | +为了方便前期前端快速开发,不需要等待后端接口,系统提供了mock功能。更多配置请参考[mocker-api](https://github.com/jaywcjlove/mocker-api) |
| 4 | + |
| 5 | +### 一、编写mock |
| 6 | + |
| 7 | +> 在 mocker 下创建新的 js 文件。 如果有多个相关页面,您可以创建一个新文件夹来放置相关文件 |
| 8 | +```bash |
| 9 | + mocker |
| 10 | ++ index.js |
| 11 | ++ user.mock.js |
| 12 | + ... |
| 13 | +src |
| 14 | + models |
| 15 | + pages |
| 16 | +... |
| 17 | +package.json |
| 18 | +``` |
| 19 | + |
| 20 | +> 在/mocker/user.mock.js目录下进行mock数据编写,比如: |
| 21 | +
|
| 22 | +```ts |
| 23 | +module.exports = { |
| 24 | + 'POST /api/login': (req, res) => { |
| 25 | + let {username, password} = req.body; |
| 26 | + if (username === 'admin' && password === 'admin!') { |
| 27 | + return res.status(201).json({ |
| 28 | + message: 'Login successful!', |
| 29 | + token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9', |
| 30 | + data: { |
| 31 | + nikename: 'Hello App', |
| 32 | + }, |
| 33 | + }); |
| 34 | + } |
| 35 | + res.status(401).json({ |
| 36 | + message: 'username or password is error.', |
| 37 | + }); |
| 38 | + }, |
| 39 | +}; |
| 40 | +``` |
| 41 | + |
| 42 | +### 二、界面渲染 |
| 43 | + |
| 44 | +``` |
| 45 | +import React, { useState } from 'react'; |
| 46 | +import { connect } from 'react-redux'; |
| 47 | +import AsyncStorage from '@react-native-async-storage/async-storage'; |
| 48 | +import { Button } from '@uiw/react-native'; |
| 49 | +import Global from '../../global'; |
| 50 | +import { useLogin } from '../../hooks/users' |
| 51 | +
|
| 52 | +const Dome = ({ update }) => { |
| 53 | + |
| 54 | + const { mutate, isLoading } = useLogin({ |
| 55 | + update, |
| 56 | + formData, |
| 57 | + }) |
| 58 | +
|
| 59 | + const loginIn = () => mutate?.({ ...formData }) |
| 60 | +
|
| 61 | + return ( |
| 62 | + <Button |
| 63 | + textStyle={{ fontSize: 16, fontWeight: '200' }} |
| 64 | + bordered={false} |
| 65 | + color="#BFBFBF" |
| 66 | + loading={isLoading} |
| 67 | + disabled={isLoading} |
| 68 | + onPress={loginIn}> |
| 69 | + Sign In |
| 70 | + </Button> |
| 71 | + ); |
| 72 | +} |
| 73 | +
|
| 74 | +export default connect( |
| 75 | + ({ }) => ({}), |
| 76 | + ({ global }) => ({ |
| 77 | + update: global.update |
| 78 | + }), |
| 79 | +)(Dome); |
| 80 | +
|
| 81 | +``` |
| 82 | +### 三、modal文件处理 |
| 83 | + |
| 84 | +``` |
| 85 | +import { Alert } from 'react-native'; |
| 86 | +import AsyncStorage from '@react-native-async-storage/async-storage'; |
| 87 | +import { userLogin } from '../services/users'; |
| 88 | +import { useQuery, useMutation } from 'react-query' |
| 89 | +
|
| 90 | +// 登录 |
| 91 | +export const useLogin = ({ config = {}, update, formData, remember }) => { |
| 92 | + const mutation = useMutation({ |
| 93 | + mutationFn: userLogin, |
| 94 | + onSuccess: async (data) => { |
| 95 | + console.log('data',data) |
| 96 | + }, |
| 97 | + ...config |
| 98 | + }) |
| 99 | + return mutation |
| 100 | +} |
| 101 | +
|
| 102 | +
|
| 103 | +``` |
| 104 | + |
| 105 | +### 四、services文件调用 |
| 106 | + |
| 107 | +> 配合系统封装的request进行mock数据请求。如需区分是mock数据,还是真实后端数据,调用真实数据时,注释mocker数据配置即可 |
| 108 | +
|
| 109 | +```ts |
| 110 | + import { request } from "@uiw-admin/utils" |
| 111 | + |
| 112 | + export async function userLogin(params) { |
| 113 | + return fetch('/api/login', { |
| 114 | + body: params, |
| 115 | + }); |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +注:mock功能只推荐在开发模式下开启。 |
| 120 | +<!--rehype:style=border-left: 8px solid #ffe564;background-color: #ffe56440;padding: 12px 16px;--> |
| 121 | + |
| 122 | +## License |
| 123 | + |
| 124 | +Licensed under the MIT License. |
0 commit comments