梦学谷项目(下)

梦学谷项目(下)

接上一篇的技术点分析,在整体项目汇中,由于在之前的项目中对mock.js假数据从未有过了解,所以对假数据的使用做出了总结。

第一步:

登录假数据平台:https://www.fastmock.site/#/,完成注册。

第二步:

创建新项目,设置项目的基准路径

第三步:

设置假数据,

设置假数据时,要注意的是请求数据时的数据路径也是需要进行设置的。

梦学谷项目(下)_第1张图片

​ 设置假的数据,在做项目的过程中,是为了方便在后端小伙伴还未完成接口的时候,方便我们能够提前的搭好框架。

小结:

通过对axios的封装,能够方便我们日后对代码的维护。减少工作量。

假数据的使用:可以很大程度的提高我们的工作进度

等这些准备工作都做完后,我们就可以进入我们的项目中进行逻辑的梳理。

纵观整个项目,其实技术点的体现并不明显,我们通过对axios的封装,以及mock.js假数据的使用,已经对整体项目的技术点已经有了体现,下面我通过会员管理模块的业务逻辑的梳理,对整个项目的技术点作出一个大概的总结。

梦学谷项目(下)_第2张图片

首先:是对于api的封装

import request from '../utils/index'
export default {
     
    list(page, size) {
     
        return request({
     
            url: `/userList/${
       page}/${
       size}`,
            method: 'get',
        })
    },
    add(addForm) {
     
        return request({
     
            url: `/mearge`,
            method: 'post',
            data: addForm,
        })
    },
    del(id) {
     
        return request({
     
            url: `/del/${
       id}`,
            method: 'delete'
        })
    },
    edit(id, editForm) {
     
        return request({
     
            url: `/edit/${
       id}`,
            method: 'put',
            data: editForm
        })
    }
}
<template>
  <div id="replace">
    <Bread />
    <!-- 查询栏 -->
    <el-row>
      <el-form
        :inline="true"
        :model="formInline"
        ref="formRef"
        class="demo-form-inline addForm"
      >
        <el-form-item>
          <el-input
            v-model="formInline.cardNum"
            placeholder="会员卡号"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="formInline.name" placeholder="会员名字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-select v-model="formInline.payType" placeholder="支付类型">
            <el-option
              :label="item.label"
              :value="item.value"
              v-for="item in opation"
              :key="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            type="date"
            placeholder="选择日期"
            v-model="formInline.birthday"
            style="width: 100%"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="inquire">查询</el-button>
          <el-button type="primary" @click="add">新增</el-button>
          <el-button type="primary" @click="rest">重置</el-button>
        </el-form-item>
      </el-form>
    </el-row>
    <!-- 表格区域 -->
    <el-table :data="tableData" border>
      <el-table-column type="index" label="序号"> </el-table-column>
      <el-table-column prop="cardNum" label="会员卡号"> </el-table-column>
      <el-table-column prop="name" label="会员姓名"> </el-table-column>
      <el-table-column prop="birthday" label="会员生日"> </el-table-column>
      <el-table-column prop="phone" label="手机号码"> </el-table-column>
      <el-table-column prop="integral" label="可用积分"> </el-table-column>
      <el-table-column prop="money" label="开卡金额"> </el-table-column>
      <el-table-column prop="payType" label="支付类型">
        <template v-slot="scope">
          <span v-if="scope.row.payType == 1">微信</span>
          <span v-if="scope.row.payType == 2">银行卡</span>
          <span v-if="scope.row.payType == 3">支付宝</span>
          <span v-if="scope.row.payType == 4">现金</span>
        </template>
      </el-table-column>
      <el-table-column prop="address" label="会员地址"> </el-table-column>
      <el-table-column label="操作">
        <!-- 操作区域 -->
        <template v-slot="scope">
          <el-button
            size="mini"
            class="el-icon-delete"
            @click="del(scope.row)"
          ></el-button>
          <el-button
            type="danger"
            size="mini"
            class="el-icon-edit"
            @click="edit(scope)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页器 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[3, 6, 12, 18]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- 新增数据对话框 -->
    <el-dialog
      :title="titleText + '用户'"
      :visible.sync="addDialogVisible"
      width="30%"
    >
      <!-- 新增form表单 -->
      <el-form
        :model="addForm"
        :rules="addRules"
        label-width="80px"
        ref="addRef"
      >
        <el-form-item label="会员卡号" prop="cardNum">
          <el-input v-model="addForm.cardNum"></el-input>
        </el-form-item>
        <el-form-item label="会员姓名" prop="name">
          <el-input v-model="addForm.name"></el-input>
        </el-form-item>
        <el-form-item label="会员生日">
          <el-date-picker
            type="date"
            placeholder="会员生日"
            v-model="addForm.birthday"
            style="width: 100%"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="手机号码">
          <el-input v-model="addForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="开卡金额">
          <el-input v-model="addForm.money"></el-input>
        </el-form-item>
        <el-form-item label="可用积分">
          <el-input v-model="addForm.integral"></el-input>
        </el-form-item>
        <el-form-item prop="payType" label="支付类型">
          <el-select v-model="addForm.payType" placeholder="支付类型">
            <el-option
              :label="item.label"
              :value="item.value"
              v-for="item in opation"
              :key="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="会员地址">
          <el-input type="textarea" v-model="addForm.address"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addDialog">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import Bread from "./bread";
import userApi from "../api/Users";
export default {
     
  components: {
     
    Bread,
  },
  data() {
     
    return {
     
      addDialogVisible: false, //添加对话框初始状态
      addForm: {
     
        id: null,
        cardNum: "",
        name: "",
        birthday: "",
        phone: "",
        money: 0,
        integral: 0,
        payType: "",
        address: "",
      },
      formInline: {
     },
      flag: false,
      // 多选数据
      opation: [
        {
      value: 1, label: "微信" },
        {
      value: 2, label: "现金" },
        {
      value: 3, label: "支付宝" },
        {
      value: 4, label: "银行卡" },
      ],
      queryInfo: {
     
        query: "",
        pagesize: 5,
        pagenum: 10,
      },
      tableData: [],
      total: 0,
      // 校验规则
      addRules: {
     
        cardNum: [
          {
      required: true, message: "请输入会员卡号", trigger: "blur" },
        ],
        name: [{
      required: true, message: "请输入会员姓名", trigger: "blur" }],
        payType: [
          {
      required: true, message: "请选择付款方式", trigger: "blur" },
        ],
      },
    };
  },
  created() {
     
    this.getList();
    // console.log(this.titleText);
  },
  methods: {
     
    inquire() {
     
      this.$refs.formRef.validate((valid) => {
     
        if (!valid) return;
        this.getList();
      });
    },
    // 新增
    add() {
     
      this.flag = true;
      this.addDialogVisible = true;
      this.addForm.id = "";
      this.addForm.cardNum = "";
      this.addForm.name = "";
      this.addForm.birthday = "";
      this.addForm.phone = "";
      this.addForm.money = 0;
      this.addForm.integral = 0;
      this.addForm.payType = "";
      this.addForm.address = "";
    },
    async addDialog() {
     
      if (this.flag) {
     
        console.log(this.addForm);
        const {
      data: res } = await userApi.add(this.addForm);
        if (res.code !== 2000) {
     
          this.$message.error("添加用户失败");
        }
        this.$message.success("添加用户成功");
      } else {
     
        const {
      data: res } = await userApi.edit(this.addForm.id, this.addForm);
        if (res.code !== 2000) {
     
          this.$message.error(res.message);
        } else {
     
          this.$message.success(res.message);
        }
      }
      this.addDialogVisible = false;
      this.getList();
    },

    // 获取所有数据
    async getList() {
     
      const {
      data: res } = await userApi.list(
        this.queryInfo.pagenum,
        this.queryInfo.pagesize
      );
      console.log(res);
      this.tableData = res.data.rows;
      this.total = this.tableData.length;
    },
    // 点击进行删除
    async del(id) {
     
      const confirmText = await this.$confirm(
        "此操作将永久删除该文件, 是否继续?",
        "提示",
        {
     
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      if (confirmText !== "confirm") {
     
        return this.$message.info("已取消删除");
      }
      const {
      data: res } = await userApi.del(id);
      if (res.code !== 2000) {
     
        this.$message.error("删除会员失败");
      } else {
     
        this.$message.success("删除成功");
        this.getList();
      }
    },
    rest() {
     
      this.formInline = {
     };
    },
    async edit(item) {
     
      this.flag = false;
      if (!this.flag) {
     
        this.addForm = item.row;
        this.addDialogVisible = true;
      }
    },

    // 分页
    handleSizeChange(val) {
     
      this.queryInfo.pagesize = val;
      this.getList();
      console.log(`每页 ${
       val} 条`);
    },
    handleCurrentChange(val) {
     
      this.queryInfo.pagenum = val;
      this.getList();
      console.log(`当前页: ${
       val}`);
    },
  },
  computed: {
     
    titleText() {
     
      return this.flag ? "添加" : "编辑";
    },
  },
};
</script>
<style lang='scss' scoped>
.el-card {
     
  margin-bottom: 15px;
}
.addForm {
     
  width: 100%;
  display: flex;
  align-items: center;
}
.el-card {
     
  display: flex;
}
.el-card span {
     
  display: flex;
  align-items: center;
  justify-content: center;
  padding-left: 10px;
  box-sizing: border-box;
  border-left: 3px solid green;
}
</style>

在会员管理中,由于修改用户跟添加用户对话框相同,通过computed计算属性,设置状态,将修改用户的对话框跟添加用户的对话框写在一块,通过在不同的状态下,点击展示不同的对话框,从而节省代码,优化了页面。

封装面包屑导航

components中新建bread文件,

<template>
  <div class="bread">
    <el-card>
      <p>
        <span></span>
        <span v-for="(item, index) in breadList" :key="index">{
     {
      item }}</span>
      </p></el-card
    >
  </div>
</template>
<script>
export default {
     
  data() {
     
    return {
     
      breadList: [],
    };
  },
  mounted() {
     
    this.breadList = this.$route.meta.bread;
  },
};
</script>

router.js

   {
     
      path: "/member",
      component: () => import( /* webpackChunkName: "about" */ "../components/member.vue"),
      meta: {
     
        bread: ['会员管理']
      }
    },

在需要的页面中引入组件使用即可。

关于梦雪谷项目的技术难点,大概就是这些了,由于在没有其他特别突出的的项目难点。其余模块均可参考这一模块。完整代码请看梦学谷

你可能感兴趣的:(vue项目相关,vue.js,javascript)