mockjs 模拟实现增删改查
- /*mUtils.js用于解析get请求的参数*/
- export const param2Obj = url => {
- const search = url.split('?')[1]
- if (!search) {
- return {}
- }
- return JSON.parse('{"' + decodeURIComponent(search).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"') + '"}')
- }
- /*money.js*/
- import Mock from 'mockjs'
- import * as mUtils from '@/utils/mUtils'
- let List = []
- const count = 60
- let typelist = [1, 2, 3, 4, 5, 6, 7, 8]
- for (let i = 0; i < count; i++) {
- List.push(Mock.mock({
- id: Mock.Random.guid(),
- username: Mock.Random.cname(),
- address: Mock.mock('@county(true)'),
- createTime: Mock.Random.datetime(),
- income: Mock.Random.float(0, 9999, 2,2),
- pay: Mock.Random.float(-5999, 0, 2,2),
- accoutCash: Mock.Random.float(0, 9999, 2,2),
- 'incomePayType|1': typelist
- }))
- }
- export default {
- /* 获取列表getMoneyList*/
- getMoneyList: config => {
- const { name, page = 1, limit = 20 } = mUtils.param2Obj(config.url)
- console.log(mUtils.param2Obj(config.url))
- console.log(page+"++"+limit)
- 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,
- moneyList: pageList
- }
- }
- },
- /** 增加资金信息createMoney*/
- createMoney: config => {
- const { username, address, income, pay , accoutCash, incomePayType } = mUtils.param2Obj(config.url)
- List.push({
- id: Mock.Random.guid(),
- username: username,
- address: address,
- createTime: Mock.Random.now(),
- income: income,
- pay: pay,
- accoutCash: accoutCash,
- incomePayType: incomePayType
- })
- return {
- code: 200,
- data: {
- message: '添加成功'
- }
- }
- },
- /*** 删除用户deleteMoney */
- deleteMoney: config => {
- const { id } = mUtils.param2Obj(config.url)
- if (!id) {
- return {
- code: -999,
- message: '参数不正确'
- }
- } else {
- List = List.filter(u => u.id !== id)
- return {
- code: 200,
- data: {
- message: '删除成功'
- }
- }
- }
- },
- /* 批量删除 */
- batchremoveMoney: config => {
- console.log(config);
- // console.log(mUtils.param2Obj(config.url));
- let { ids } = mUtils.param2Obj(config.url)
- console.log(ids);
- ids = ids.split(',')
- List = List.filter(u => !ids.includes(u.id))
- return {
- code: 200,
- data: {
- message: '批量删除成功'
- }
- }
- },
- /*修改用户 */
- updateMoney: config => {
- const { id,username, address, income, pay , accoutCash, incomePayType } = mUtils.param2Obj(config.url)
- List.some(u => {
- if (u.id === id) {
- u.username = username
- u.address = address
- u.income = income
- u.pay = pay
- u.accoutCash = accoutCash
- u.incomePayType = incomePayType
- return true
- }
- })
- return {
- code: 200,
- data: {
- message: '编辑成功'
- }
- }
- }
- }
- /*index.js*/
- import Mock from 'mockjs'
- import tableAPI from './money'
- import salesAPI from './sales'
- import userAPI from './user'
- // 设置全局延时 没有延时的话有时候会检测不到数据变化 建议保留
- Mock.setup({
- timeout: '300-600'
- })
- // 资金相关
- Mock.mock(/\/money\/get/, 'get', tableAPI.getMoneyList)
- Mock.mock(/\/money\/remove/, 'get', tableAPI.deleteMoney)
- Mock.mock(/\/money\/batchremove/, 'get', tableAPI.batchremoveMoney)
- Mock.mock(/\/money\/add/, 'get', tableAPI.createMoney)
- Mock.mock(/\/money\/edit/, 'get', tableAPI.updateMoney)
- // sales相关
- Mock.mock(/\/sales\/get/, 'get', salesAPI.getSalesList)
- // user相关
- Mock.mock(/\/user\/login/, 'get', userAPI.login)
- Mock.mock(/\/user\/logout/, 'get', userAPI.logout)
- Mock.mock(/\/user\/info\/get/, 'get', userAPI.getUserInfo)
- Mock.mock(/\/user\/list\/get/, 'get', userAPI.getUserList)
- /*Api.js接口管理*/
import request from '@/utils/axios'- export function getMoneyIncomePay(params) {
- return request({
- url: '/money/get',
- method: 'get',
- params: params
- })
- }
- export function addMoney(params) {
- return request({
- url: '/money/add',
- method: 'get',
- params: params
- })
- }
- export function removeMoney(params) {
- return request({
- url: '/money/remove',
- method: 'get',
- params: params
- })
- }
- export function batchremoveMoney(params) {
- return request({
- url: '/money/batchremove',
- method: 'get',
- params: params
- })
- }
- export function updateMoney(params) {
- return request({
- url: '/money/edit',
- method: 'get',
- params: params
- })
- }
- /*在组件里使用*/
<template>- <div class="fillcontain">
- <search-item @showDialog="showAddFundDialog" @searchList="getMoneyList" @onBatchDelMoney="onBatchDelMoney"></search-item>
- <div class="table_container">
- <el-table
- v-loading="loading"
- :data="tableData"
- style="width: 100%"
- align='center'
- @select="selectTable"
- @select-all="selectAll"
- >
- <el-table-column
- v-if="idFlag"
- prop="id"
- label="id"
- align='center'
- width="180">
- </el-table-column>
- <el-table-column
- type="selection"
- align='center'
- width="40">
- </el-table-column>
- <el-table-column
- prop="username"
- label="用户姓名"
- align='center'
- width="80">
- </el-table-column>
- <el-table-column
- prop="address"
- label="籍贯"
- align='center'
- >
- </el-table-column>
- <el-table-column
- prop="createTime"
- label="投资时间"
- align='center'
- sortable
- width="170">
- <template slot-scope="scope">
- <el-icon name="time"></el-icon>
- <span style="margin-left: 10px">{{ scope.row.createTime }}</span>
- </template>
- </el-table-column>
- <el-table-column
- prop="incomePayType"
- label="收支类型"
- align='center'
- width="130"
- :formatter="formatterType"
- :filters="fields.incomePayType.filter.list"
- :filter-method="filterType">
- </el-table-column>
- <el-table-column
- prop="income"
- label="收入"
- align='center'
- width="130"
- sortable>
- <template slot-scope="scope">
- <span style="color:#00d053">+ {{ scope.row.income }}</span>
- </template>
- </el-table-column>
- <el-table-column
- prop="pay"
- label="支出"
- align='center'
- width="130"
- sortable>
- <template slot-scope="scope">
- <span style="color:#f56767">{{ getPay(scope.row.pay) }}</span>
- </template>
- </el-table-column>
- <el-table-column
- prop="accoutCash"
- label="账户现金"
- align='center'
- width="130"
- sortable>
- <template slot-scope="scope">
- <span style="color:#4db3ff">{{ scope.row.accoutCash }}</span>
- </template>
- </el-table-column>
- <el-table-column
- prop="operation"
- align='center'
- label="操作"
- width="180">
- <template slot-scope='scope'>
- <el-button
- type="warning"
- icon='edit'
- size="mini"
- @click='onEditMoney(scope.row)'
- >编辑</el-button>
- <el-button
- type="danger"
- icon='delete'
- size="mini"
- @click='onDeleteMoney(scope.row,scope.$index)'
- >删除</el-button>
- </template>
- </el-table-column>
- </el-table>
- <pagination :pageTotal="pageTotal" @handleCurrentChange="handleCurrentChange" @handleSizeChange="handleSizeChange"></pagination>
- <addFundDialog v-if="addFundDialog.show" :isShow="addFundDialog.show" :dialogRow="addFundDialog.dialogRow" @getFundList="getMoneyList" @closeDialog="hideAddFundDialog"></addFundDialog>
- </div>
- </div>
- </template>
- <script>
- import { mapGetters } from "vuex";
- import * as mutils from '@/utils/mUtils'
- import SearchItem from "./components/searchItem";
- import AddFundDialog from "./components/addFundDialog";
- import Pagination from "@/components/pagination";
- import { getMoneyIncomePay , removeMoney, batchremoveMoney } from "@/api/money";
- import axios from "axios"
- export default {
- data(){
- return {
- tableData: [],
- tableHeight:0,
- loading:true,
- idFlag:false,
- isShow:false, // 是否显示资金modal,默认为falselimit
- editid:'',
- rowIds:[],
- sortnum:0,
- format_type_list: {
- 0: '提现',
- 1: '提现手续费',
- 2: '提现锁定',
- 3: '理财服务退出',
- 4: '购买宜定盈',
- 5: '充值',
- 6: '优惠券',
- 7: '充值礼券',
- 8: '转账'
- },
- addFundDialog:{
- show:false,
- dialogRow:{}
- },
- incomePayData:{
- page:1,
- limit:20,
- name:''
- },
- pageTotal:0,
- // 用于列表筛选
- fields: {
- incomePayType:{
- filter: {
- list: [{
- text: '提现',
- value: 0
- },{
- text: '提现手续费',
- value: 1
- }, {
- text: '提现锁定',
- value: 2
- }, {
- text: '理财服务退出',
- value: 3
- }, {
- text: '购买宜定盈',
- value: 4
- }, {
- text: '充值',
- value: 5
- }, {
- text: '优惠券',
- value: 6
- }, {
- text: '充值礼券',
- value: 7
- }, {
- text: '转账',
- value: 8
- }],
- multiple: true
- }
- },
- },
- }
- },
- components:{
- SearchItem,
- AddFundDialog,
- Pagination
- },
- computed:{
- ...mapGetters(['search'])
- },
- mounted() {
- this.getMoneyList();
- // this.setTableHeight();
- // window.onresize = () => {
- // this.setTableHeight();
- // }
- },
- methods: {
- setTableHeight(){
- this.$nextTick(() => {
- this.tableHeight = document.body.clientHeight - 300
- })
- },
- // 获取资金列表数据
- getMoneyList(){
- const para = Object.assign({},this.incomePayData,this.search);
- getMoneyIncomePay(para).then(res => {
- console.log(res)
- this.loading = false;
- this.pageTotal = res.data.total
- // console.log(res)
- this.tableData = res.data.moneyList
- })
- },
- // 显示资金弹框
- showAddFundDialog(val){
- this.$store.commit('SET_DIALOG_TITLE', val)
- this.addFundDialog.show = true;
- },
- hideAddFundDialog(){
- this.addFundDialog.show = false;
- },
- // 上下分页
- handleCurrentChange(val){
- this.incomePayData.page = val;
- this.getMoneyList()
- },
- // 每页显示多少条
- handleSizeChange(val){
- this.incomePayData.limit = val;
- this.getMoneyList()
- },
- getPay(val){
- if(mutils.isInteger(val)){
- return -val;
- }else{
- return val;
- }
- },
- /**
- * 格式化状态
- */
- formatterType(item) {
- const type = parseInt(item.incomePayType);
- return this.format_type_list[type];
- },
- filterType(value, item) {
- const type = parseInt(item.incomePayType);
- return this.format_type_list[value] == this.format_type_list[type];
- },
- // 编辑操作方法
- onEditMoney(row){
- this.addFundDialog.dialogRow = row;
- this.showAddFundDialog();
- },
- // 删除数据
- onDeleteMoney(row){
- this.$confirm('确认删除该记录吗?', '提示', {
- type: 'warning'
- })
- .then(() => {
- const para = { id: row.id }
- removeMoney(para).then(res => {
- this.$message({
- message: '删除成功',
- type: 'success'
- })
- this.getMoneyList()
- })
- })
- .catch(() => {})
- },
- onBatchDelMoney(){
- this.$confirm('确认批量删除记录吗?', '提示', {
- type: 'warning'
- })
- .then(() => {
- const ids = this.rowIds.map(item => item.id).toString()
- const para = { ids: ids }
- batchremoveMoney(para).then(res => {
- this.$message({
- message: '批量删除成功',
- type: 'success'
- })
- this.getMoneyList()
- })
- })
- .catch(() => {})
- },
- // 当用户手动勾选数据行的 Checkbox 时触发的事件
- selectTable(val, row){
- this.setSearchBtn(val);
- },
- // 用户全选checkbox时触发该事件
- selectAll(val){
- val.forEach((item) => {
- this.rowIds.push(item.id);
- });
- this.setSearchBtn(val);
- },
- setSearchBtn(val){
- let isFlag = true;
- if(val.length > 0){
- isFlag = false;
- }else{
- isFlag = true;
- }
- this.$store.commit('SET_SEARCHBTN_DISABLED',isFlag);
- }
- },
- }
- </script>
- <style lang="less" scoped>
- .table_container{
- padding: 10px;
- background: #fff;
- border-radius: 2px;
- }
- .el-dialog--small{
- width: 600px !important;
- }
- .pagination{
- text-align: left;
- margin-top: 10px;
- }
- </style>
mockjs 模拟实现增删改查的更多相关文章
- python函数模拟mysql增删改查功能
import os list1 = ['staff_id', 'name', 'age', 'phone', 'dept', 'enroll_date'] def staff_info(): #获取员 ...
- Vue+Mock.js模拟登录和表格的增删改查
有三类人不适合此篇文章: "喜欢站在道德制高点的圣母婊" -- 适合去教堂 "无理取闹的键盘侠" -- 国际新闻版块欢迎你去 "有一定基础但又喜欢逼逼 ...
- 用Java中的File类模拟实现对系统文件的增删改查效果
码字不易,三连支持一波吧 IO操作向来是各大语言的热区,而对文件的操作也是重中之重. 那么在Java中也给我们提供了很多关于文件操作的类.今天我就用一个比较基本的File类来模拟实现对文件的增删改查效 ...
- 作业三:模拟 mysql 进行增删改查
# !/usr/bin/env python3 # _*_coding:utf-8_*_ def help_sql(cmd): if cmd in func_dic.keys(): print('{} ...
- 模拟admin组件自己开发stark组件之增删改查
增删改查,针对视图 我们需要modelform来创建,可自动生成标签,我们还要考虑用户是不是自己定制,依然解决方法是,继承和重写 app01下的joker.py文件 class BookModelFo ...
- Python 模拟SQL对文件进行增删改查
#!/usr/bin/env python # _*_ coding:UTF-8 _*_ # __auth__: Dalhhin # Python 3.5.2,Pycharm 2016.3.2 # 2 ...
- Mock.js简易教程,脱离后端独立开发,实现增删改查功能(转)
在我们的生产实际中,后端的接口往往是较晚才会出来,并且还要写接口文档,于是我们的前端的许多开发都要等到接口给我们才能进行,这样对于我们前端来说显得十分的被动,于是有没有可以制造假数据来模拟后端接口呢, ...
- 如何在Vue中使用Mockjs模拟数据的增删查改
之前一直使用json-server在前端开发时,搭建本地数据接口测试,但有时又需要将做好的项目放于 github page上做项目演示.在本地时,json server很好使用,但一旦放在github ...
- mock.js 的用法 -- 脱离后端独立开发,实现增删改查功能
在我们的生产实际中,后端的接口往往是较晚才会出来,并且还要写接口文档,于是我们的前端的许多开发都要等到接口给我们才能进行,这样对于我们前端来说显得十分的被动,于是有没有可以制造假数据来模拟后端接口呢, ...
- 【面向对象版】HashMap(增删改查)
前言: 关于什么是HashMap,HashMap可以用来做些什么,这些定义类的描述,请参照[简易版]HashMap(增删改查)的内容. 这章节主要是面向实例,直接进行HashMap(增删改查)的演示. ...
随机推荐
- 英语单词组件- 单词在句子中,上面显示中文下面显示音标 css样式
原先效果: 改进demo效果 优化点 音标长度超出,或者中文超出,总宽度会按照最长的走 居中显示 再次优化 line-height: 22px; 加入这个 对齐中间行(字号大小会让绝对上下高度,对不齐 ...
- 在运行程序是出现sh: 行 1: cls: 未找到命令
在运行程序是出现sh: 行 1: cls: 未找到命令 原因是system("cls");--这是在程序中调用系统命令,但是linux识别不了.功能是清除当前的终端显示数据.找到l ...
- Android TextView自动缩放能够完整显示出一行
原文地址: Android TextView自动缩放能够完整显示出一行 - Stars-One的杂货小窝 app开发中,需要TextView可以在不同的屏幕大小要完整显示出文字,而不是显示省略号 可以 ...
- cpprest示例微服务链路嵌套调用层数1000以及跟踪
本demo使用本人两个github项目cpprestsdk4mingw,zhepler-wxWdigets编写,一个简单的rest服务器cpprestbox,只提供GET方法方便测试,可以添加修改ap ...
- 云VR:虚拟现实专业化的下一步
传统的VR通常需要功能强大的计算机和其他高性能设备来提供良好的用户体验.但是,如果有一种方法可以从任何设备和任何地方处理VR内容呢?这就是云VR对VR用户的承诺.随着5G和其他网络的到来,VR技术的未 ...
- [.NET项目实战] Elsa开源工作流组件应用(二):内核解读
@ 目录 定义 变量 内存寄存器类 寄存器中的存储区块类 变量到存储的映射类 上下文对象 活动上下文(ActivityExecutionContext) 工作流执行上下文(WorkflowExecut ...
- 【福利】JetBrains 全家桶永久免费使用
Jetbrains系列的IDE公认是最好的集成开发工具,但是收费且挺贵.我们以PhpStorm为例,新用户第一年需要199$,注意是$,还不是人民币,这个价格一上来肯定筛选掉一大批用户.确实好用,所以 ...
- KingbaseES V8R3 备份恢复系列之 -- sys_rman备份过程分析
案例说明: 本案例通过对KingbaseES sys_rman物理备份过程的详细描述,有助于在执行sys_rman过程中发生故障的分析. 适用版本: KingbaseES V8R3 一.sys_r ...
- 机器语言编写helloworld
kvmtool下载编译 git clone https://github.com/kvmtool/kvmtool.git 下载后进入到目录执行make即可. 补码 计算机怎么表示负数?以四位有符号数为 ...
- 微服务集成Spring Cloud Alibaba Seata(一)Seata服务搭建
1.Seata介绍 Seata是阿里开源的一款分布式事务解决方案,致力于提供高性能和简单易用的分布式事务服务.数据库事务我们都知道,事务都是遵循ACID原则.而通过使用Seata可以实现在两个服务模块 ...