mockjs

Mock 文件夹下新建 mockServe.js

import Mock from "mockjs";
import testData from './test.js'
import goodData from './goods.js'
import infoData from './info.js'

Mock.mock(/\/login\/info/, 'get', infoData.getInfo)

Mock.mock(/\/money\/get/, 'get', testData.getMoneyList)
Mock.mock(/\/money\/del/, 'get', testData.deleteMoney)
Mock.mock(/\/money\/info/, 'get', testData.moneyInfo)

Mock.mock(/\/goods\/get/, 'get', goodData.getGoodList)

export default {
    
}

test.js

import Mock from "mockjs";
import * as mUtils from './mUtils'
const Random = Mock.Random  // Mock.Random 是一个工具类,用于生成各种随机数据
 
let List = []
const count = 5
for (let i = 0; i < count; i++) {
    List.push(Mock.mock({
        id: "@increment",
        deptName: '@cname', 
        remark: "@csentence(50)",
        date: "@date(yyyy-MM-dd hh:mm:ss)",
    }))
}
export default {
    getMoneyList: config=> {
        const { name, page = 1, limit = 20 } = mUtils.param2Obj(config.url)
        const mockList = List.filter(user => {
            if (name && user.username.indexOf(name) === -1) return false
            return true
        })
        const pageList = mockList.filter((item, index) => index < limit * page && index >= limit * (page - 1))
        return {
            code: 200,
            data: {
                total: mockList.length,
                list: pageList
            }
        }
    },
    // 获取详情
    moneyInfo: config => {
        const { id } = mUtils.param2Obj(config.url)
        if (!id) {
            return {
                code: -999,
                message: '参数不正确!!!!'
            }
        } else {
            let data = List.filter(u => u.id == id)
            return {
                code: 200,
                data: {
                    message: '操作成功',
                    info: data.length != 0 ? data[0] : {}
                }
            }
        }
    },
    // 删除用户deleteMoney 
    deleteMoney: config => {
        console.log(config.url)
        const { id } = mUtils.param2Obj(config.url)
        if (!id) {
            return {
                code: -999,
                message: '参数不正确!!!!'
            }
        } else {
            List = List.filter(u => u.id != id)
            console.log(List)
            return {
                code: 200,
                data: {
                    message: '删除成功',
                    data: List
                }
            }
        }
    },
}

封装接口

import http from "./require.js"
import dataSql from  '../Mock/mockServe'
// 接口说明
export const API_tag = params => http.post('/saas/common/tags', params)
export const API_proList = params => http.post('/saas/shop/serviceShop', params)

// 个人资料
export const getInfo = params => http.get(`/login/info`, params)
// moni数据
export const getMoneyList = params => http.get(`/money/get`, params)
export const getMoneyDel = params => http.get(`/money/del`, params)
export const getMoneyInfo = params => http.get(`/money/info`, params)
// goods
export const getGoodList = params => http.get(`/goods/get`, params)

 

posted @ 2022-11-18 10:40  番茄西红柿u  阅读(32)  评论(0编辑  收藏  举报