MyChat.vue 5.9 KB
Newer Older
1
<template>
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
  <div>
    <el-container>
      <el-header style="text-align: right; font-size: 12px">
        <i class="el-icon-refresh header-button-item" @click="refreshPage"></i>
        <el-dropdown>
          <i class="el-icon-setting header-button-item"></i>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item>新增</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
        <a
          href="http://qinyingjie.top/"
          target="_blank"
          class="header-button-item"
          >kwan</a
        >
      </el-header>
19

20 21 22 23 24 25 26 27 28 29 30 31 32
      <el-main>
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="问题">
            <el-input
              clearable
              v-model="formInline.question"
              placeholder="请输入问题"
              @keydown.enter.native="initCartList"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="initCartList">查询</el-button>
          </el-form-item>
33 34 35 36 37
          <el-form-item>
            <el-button type="primary" @click.prevent="addChat">
              新增
            </el-button>
          </el-form-item>
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
        </el-form>
        <el-table border :data="userlist" v-loading="loading">
          <el-table-column
            prop="id"
            label="序号"
            width="50"
            sortable
          ></el-table-column>
          <el-table-column
            prop="question"
            label="问题"
            width="240"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            prop="response"
            label="答案"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column label="创建时间" width="170">
            <template slot-scope="props">
              {{ props.row.createTime | dateFormat }}
            </template>
          </el-table-column>
          <el-table-column prop="详情" label="详情" width="180">
            <template slot-scope="props">
64 65 66 67
              <el-button
                type="success"
                @click.prevent="gotoDetail(props.row.id)"
                >详情
68
              </el-button>
69
              <el-button type="danger" @click="onDelete(props.row.id)"
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          class="pagination"
          background
          :key="elementui_page_component_key"
          :current-page.sync="currentPage"
          :page-size="pageSize"
          :total="total"
          @current-change="handleCurrentChange"
        ></el-pagination>
      </el-main>
      <el-backtop class="backtop"></el-backtop>
    </el-container>
  </div>
88 89 90
</template>

<script>
91
import axios from "axios";
92
export default {
93
  name: "MyChat",
94

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
  data() {
    return {
      // 用户列表数据
      userlist: [],
      loading: false,
      elementui_page_component_key: 0,
      currentPage: Number(localStorage.getItem("lastPage")) || 1,
      pageSize: 14,
      total: 0,
      formInline: {
        question: "",
      },
    };
  },
  watch: {
    "formInline.question"(newVal) {
      if (newVal === "") {
        this.currentPage = 1;
        localStorage.setItem("lastPage", this.currentPage);
114
        this.initCartList();
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
      }
    },
  },
  created() {
    this.$nextTick(() => {
      const foo = this.$route.query.back;
      if (foo === "back") {
        this.currentPage = Number(localStorage.getItem("lastPage")) || 1;
        this.formInline.question = localStorage.getItem("lastQuestion") || "";
      } else {
        localStorage.setItem("lastPage", 1);
        localStorage.setItem("lastQuestion", "");
        this.currentPage = 1;
        this.formInline.question = "";
      }
      // 调用请求数据的方法
      this.initCartList();
    });
  },
  mounted() {
    this.currentPage = Number(localStorage.getItem("lastPage")) || 1;
    this.formInline.question = localStorage.getItem("lastQuestion");
    this.elementui_page_component_key++;
    this.initCartList();
  },
  methods: {
    async onDelete(id) {
      this.loading = true;
      const { data: res } = await axios.get(
144
        "http://localhost:8888/chatbot/delete",
145 146 147 148 149 150 151 152
        {
          params: {
            id: id,
          },
        }
      );
      this.initCartList();
      this.loading = false;
153
    },
154 155 156 157 158
    refreshPage() {
      location.reload();
    },
    gotoDetail(id) {
      this.$router.push("/home/chatinfo/" + id);
159
    },
160 161 162
    addChat() {
      this.$router.push("/home/addChat/");
    },
163

164 165 166
    async initCartList() {
      this.loading = true;
      const { data: res } = await axios.get(
167
        "http://localhost:8888/chatbot/page",
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
        {
          params: {
            page: this.currentPage,
            pageSize: this.pageSize,
            question: this.formInline.question,
          },
        }
      );
      if (res.code === 200) {
        this.userlist = res.result.records;
        this.total = res.result.total;
        localStorage.setItem("lastPage", this.currentPage);
        localStorage.setItem("lastQuestion", this.formInline.question);
      }
      this.loading = false;
    },
184

185 186 187
    handleCurrentChange(currentPage) {
      this.currentPage = currentPage;
      this.initCartList();
188
    },
189
  },
190 191 192 193 194
};
</script>

<style lang="less" scoped>
.el-header {
195 196 197
  background-color: #b3c0d1;
  color: #333;
  line-height: 60px;
198 199 200
}

.el-aside {
201
  color: #333;
202 203 204
}

.pagination {
205 206
  margin-top: 16px;
  text-align: right;
207 208
}
.header-button-item {
209 210
  margin-right: 15px;
  font-size: 20px;
211 212 213
}

.backtop {
214 215 216 217 218 219 220 221 222 223 224 225
  position: fixed;
  bottom: 50px;
  right: 50px;
  height: 40px;
  width: 40px;
  line-height: 40px;
  text-align: center;
  border-radius: 20px;
  background-color: #007aff;
  color: #fff;
  cursor: pointer;
  z-index: 999;
226 227 228
}

.backtop:hover {
229
  background-color: #0050a0;
230
}
231
</style>