refactor: ♻️ mock 解决方案方案替换为 vite-plugin-mock-dev-server

This commit is contained in:
郝先瑞
2024-01-10 23:03:25 +08:00
parent c82b055fe8
commit 5d168689e5
22 changed files with 711 additions and 1593 deletions

View File

@@ -1,27 +0,0 @@
<template>
<div>
<div class="mb-[15px]">Your roles: {{ roles }}</div>
Switch roles:
<el-radio-group v-model="switchRoles">
<el-radio-button label="EDITOR" />
<el-radio-button label="ADMIN" />
</el-radio-group>
</div>
</template>
<script setup lang="ts">
import { useUserStoreHook } from "@/store/modules/user";
import { storeToRefs } from "pinia";
const emit = defineEmits(["change"]);
const store = storeToRefs(useUserStoreHook());
const { roles } = store.user.value;
const switchRoles = computed({
get: () => roles[0],
set: (val) => {
Object.assign(roles, [val]);
emit("change");
},
});
</script>

View File

@@ -1,21 +0,0 @@
<template>
<div class="app-container">
<switch-roles @change="handleRolesChange" />
</div>
</template>
<script setup lang="ts">
import router from "@/router";
import SwitchRoles from "./components/SwitchRoles.vue";
defineOptions({
// eslint-disable-next-line
name: "PagePermission",
inheritAttrs: false,
});
function handleRolesChange() {
console.log("roles changed");
router.push({ path: "/permission/page?" + new Date() });
}
</script>

View File

@@ -1,257 +0,0 @@
/* eslint-disable */
import * as XLSX from "xlsx";
// TODO: this is a toy example, may be file-saver is a better choice
// import { saveAs } from 'file-saver'
function saveAs(blob, fileName) {
const type = fileName.split(".")[1];
console.log(type);
const file = new window.File([blob], fileName, { type: type });
console.log(file);
// 创建一个指向 File 对象的 URL
const url = URL.createObjectURL(file);
// 创建一个 a 标签
const a = document.createElement("a");
a.href = url;
a.download = fileName;
// 将 a 标签添加到文档中
document.body.appendChild(a);
// 模拟点击 a 标签,开始下载
a.click();
// 下载完成后,从文档中移除 a 标签,并释放 URL
document.body.removeChild(a);
URL.revokeObjectURL(url);
return file;
}
function generateArray(table) {
var out = [];
var rows = table.querySelectorAll("tr");
var ranges = [];
for (var R = 0; R < rows.length; ++R) {
var outRow = [];
var row = rows[R];
var columns = row.querySelectorAll("td");
for (var C = 0; C < columns.length; ++C) {
var cell = columns[C];
var colspan = cell.getAttribute("colspan");
var rowspan = cell.getAttribute("rowspan");
var cellValue = cell.innerText;
if (cellValue !== "" && cellValue == +cellValue) cellValue = +cellValue;
//Skip ranges
ranges.forEach(function (range) {
if (
R >= range.s.r &&
R <= range.e.r &&
outRow.length >= range.s.c &&
outRow.length <= range.e.c
) {
for (var i = 0; i <= range.e.c - range.s.c; ++i) outRow.push(null);
}
});
//Handle Row Span
if (rowspan || colspan) {
rowspan = rowspan || 1;
colspan = colspan || 1;
ranges.push({
s: {
r: R,
c: outRow.length,
},
e: {
r: R + rowspan - 1,
c: outRow.length + colspan - 1,
},
});
}
//Handle Value
outRow.push(cellValue !== "" ? cellValue : null);
//Handle Colspan
if (colspan) for (var k = 0; k < colspan - 1; ++k) outRow.push(null);
}
out.push(outRow);
}
return [out, ranges];
}
function datenum(v, date1904) {
if (date1904) v += 1462;
var epoch = Date.parse(v);
return (epoch - new Date(Date.UTC(1899, 11, 30))) / (24 * 60 * 60 * 1000);
}
function sheet_from_array_of_arrays(data, opts) {
var ws = {};
var range = {
s: {
c: 10000000,
r: 10000000,
},
e: {
c: 0,
r: 0,
},
};
for (var R = 0; R != data.length; ++R) {
for (var C = 0; C != data[R].length; ++C) {
if (range.s.r > R) range.s.r = R;
if (range.s.c > C) range.s.c = C;
if (range.e.r < R) range.e.r = R;
if (range.e.c < C) range.e.c = C;
var cell = {
v: data[R][C],
};
if (cell.v == null) continue;
var cell_ref = XLSX.utils.encode_cell({
c: C,
r: R,
});
if (typeof cell.v === "number") cell.t = "n";
else if (typeof cell.v === "boolean") cell.t = "b";
else if (cell.v instanceof Date) {
cell.t = "n";
cell.z = XLSX.SSF._table[14];
cell.v = datenum(cell.v);
} else cell.t = "s";
ws[cell_ref] = cell;
}
}
if (range.s.c < 10000000) ws["!ref"] = XLSX.utils.encode_range(range);
return ws;
}
function Workbook() {
if (!(this instanceof Workbook)) return new Workbook();
this.SheetNames = [];
this.Sheets = {};
}
function s2ab(s) {
var buf = new ArrayBuffer(s.length);
var view = new Uint8Array(buf);
for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xff;
return buf;
}
export function export_table_to_excel(id) {
var theTable = document.getElementById(id);
var oo = generateArray(theTable);
var ranges = oo[1];
/* original data */
var data = oo[0];
var ws_name = "SheetJS";
var wb = new Workbook(),
ws = sheet_from_array_of_arrays(data);
/* add ranges to worksheet */
// ws['!cols'] = ['apple', 'banan'];
ws["!merges"] = ranges;
/* add worksheet to workbook */
wb.SheetNames.push(ws_name);
wb.Sheets[ws_name] = ws;
var wbout = XLSX.write(wb, {
bookType: "xlsx",
bookSST: false,
type: "binary",
});
saveAs(
new Blob([s2ab(wbout)], {
type: "application/octet-stream",
}),
"test.xlsx"
);
}
export function export_json_to_excel({
multiHeader = [],
header,
data,
filename,
merges = [],
autoWidth = true,
bookType = "xlsx",
} = {}) {
/* original data */
filename = filename || "excel-list";
data = [...data];
data.unshift(header);
for (let i = multiHeader.length - 1; i > -1; i--) {
data.unshift(multiHeader[i]);
}
var ws_name = "SheetJS";
var wb = new Workbook(),
ws = sheet_from_array_of_arrays(data);
if (merges.length > 0) {
if (!ws["!merges"]) ws["!merges"] = [];
merges.forEach((item) => {
ws["!merges"].push(XLSX.utils.decode_range(item));
});
}
if (autoWidth) {
/*设置worksheet每列的最大宽度*/
const colWidth = data.map((row) =>
row.map((val) => {
/*先判断是否为null/undefined*/
if (val == null) {
return {
wch: 10,
};
} else if (val.toString().charCodeAt(0) > 255) {
/*再判断是否为中文*/
return {
wch: val.toString().length * 2,
};
} else {
return {
wch: val.toString().length,
};
}
})
);
/*以第一行为初始值*/
let result = colWidth[0];
for (let i = 1; i < colWidth.length; i++) {
for (let j = 0; j < colWidth[i].length; j++) {
if (result[j]["wch"] < colWidth[i][j]["wch"]) {
result[j]["wch"] = colWidth[i][j]["wch"];
}
}
}
ws["!cols"] = result;
}
/* add worksheet to workbook */
wb.SheetNames.push(ws_name);
wb.Sheets[ws_name] = ws;
var wbout = XLSX.write(wb, {
bookType: bookType,
bookSST: false,
type: "binary",
});
saveAs(
new Blob([s2ab(wbout)], {
type: "application/octet-stream",
}),
`${filename}.${bookType}`
);
}

View File

@@ -1,175 +0,0 @@
<template>
<div class="app-container">
<!-- Note that row-key is necessary to get a correct row order. -->
<el-table
class="draggable"
ref="dragTable"
v-loading="listLoading"
:data="list"
row-key="id"
border
fit
highlight-current-row
style="width: 100%"
>
<el-table-column align="center" label="ID" width="65">
<template #default="{ row }">
<span>{{ row.id }}</span>
</template>
</el-table-column>
<el-table-column width="180px" align="center" label="Date">
<template #default="{ row }">
<span>{{ formatDate(row.timestamp) }}</span>
</template>
</el-table-column>
<el-table-column min-width="300px" label="Title">
<template #default="{ row }">
<span>{{ row.title }}</span>
</template>
</el-table-column>
<el-table-column width="110px" align="center" label="Author">
<template #default="{ row }">
<span>{{ row.author }}</span>
</template>
</el-table-column>
<el-table-column width="110px" label="Importance">
<template #default="{ row }">
<svg-icon
v-for="n in +row.importance ? +row.importance : 0"
:key="n"
icon-class="star"
class="icon-star"
/>
</template>
</el-table-column>
<el-table-column align="center" label="Readings" width="95">
<template #default="{ row }">
<span>{{ row.pageviews }}</span>
</template>
</el-table-column>
<el-table-column class-name="status-col" label="Status" width="110">
<template #default="{ row }">
<el-tag :type="statusType(row.status)">
{{ row.status }}
</el-tag>
</template>
</el-table-column>
<el-table-column align="center" label="Drag" width="80">
<template #default="{}">
<svg-icon class="drag-handler" icon-class="drag" />
</template>
</el-table-column>
</el-table>
<div class="show-d"><el-tag>The default order :</el-tag> {{ oldList }}</div>
<div class="show-d">
<el-tag>The after dragging order :</el-tag>
{{ list.map((v: any) => v.id) }}
</div>
</div>
</template>
<script setup lang="ts">
import { default as Sortable, SortableEvent } from "sortablejs";
import { fetchList } from "@/api/article";
defineOptions({
// eslint-disable-next-line
name: "DragTable",
inheritAttrs: false,
});
interface List {
id: number;
timestamp: number;
title: string;
pageviews: number;
status: string;
}
const listLoading = ref<boolean>(true);
const list: Ref = ref<List[]>([]);
const oldList = ref<List[]>([]);
const formatDate = (timestamp: any) => {
const date = new Date(timestamp);
return date
.toLocaleString("zh-CN", {
year: "numeric",
month: "2-digit",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
hour12: false,
})
.replace(/\//g, "-");
};
const statusType = (status: string): any => {
const statusMap = {
published: "success",
draft: "info",
deleted: "danger",
};
return statusMap[status as keyof typeof statusMap];
};
// 行拖拽
const rowDrag = function () {
// 要拖拽元素的父容器
const tbody = document.querySelector(
".draggable .el-table__body-wrapper tbody"
);
if (!tbody) return;
Sortable.create(tbody as HTMLElement, {
// 可被拖拽的子元素
draggable: ".draggable .el-table__row",
onEnd(event: SortableEvent) {
if (event.oldIndex !== undefined && event.newIndex !== undefined) {
const currRow = list.value.splice(event.oldIndex, 1)[0];
list.value.splice(event.newIndex, 0, currRow);
}
},
});
};
onMounted(() => {
fetchList({}).then((res) => {
listLoading.value = false;
list.value = res.data.items;
oldList.value = list.value.map((v: any) => v.id);
rowDrag();
});
});
</script>
<style>
.sortable-ghost {
color: #fff !important;
background: #42b983 !important;
opacity: 0.8;
}
</style>
<style scoped>
.icon-star {
margin-right: 2px;
}
.drag-handler {
width: 20px;
height: 20px;
cursor: pointer;
}
.show-d {
margin-top: 15px;
}
</style>

View File

@@ -1,55 +0,0 @@
<template>
<div class="app-container">
<div class="filter-container">
<el-checkbox-group v-model="checkboxVal">
<el-checkbox label="apple"> apple </el-checkbox>
<el-checkbox label="banana"> banana </el-checkbox>
<el-checkbox label="orange"> orange </el-checkbox>
</el-checkbox-group>
</div>
<el-table
:key="key"
:data="tableData"
border
fit
highlight-current-row
style="width: 100%"
>
<el-table-column prop="name" label="fruitName" width="180" />
<el-table-column v-for="fruit in formThead" :key="fruit" :label="fruit">
<template #default="scope">
{{ scope.row[fruit] }}
</template>
</el-table-column>
</el-table>
</div>
</template>
<script setup lang="ts">
const defaultFormThead = ["apple", "banana"];
const tableData = [
{
name: "fruit-1",
apple: "apple-10",
banana: "banana-10",
orange: "orange-10",
},
{
name: "fruit-2",
apple: "apple-20",
banana: "banana-20",
orange: "orange-20",
},
];
let key = 1; // table key
const formTheadOptions = ["apple", "banana", "orange"];
const checkboxVal = ref(defaultFormThead); // checkboxVal
const formThead = ref(defaultFormThead); // 默认表头 Default header
watch(checkboxVal, (valArr) => {
formThead.value = formTheadOptions.filter((i) => valArr.indexOf(i) >= 0);
key = key + 1; // 为了保证table 每次都会重渲 In order to ensure the table will be re-rendered each time
});
</script>

View File

@@ -1,45 +0,0 @@
<template>
<div class="app-container">
<div class="filter-container">
<el-checkbox-group v-model="formThead">
<el-checkbox label="apple"> apple </el-checkbox>
<el-checkbox label="banana"> banana </el-checkbox>
<el-checkbox label="orange"> orange </el-checkbox>
</el-checkbox-group>
</div>
<el-table
:data="tableData"
border
fit
highlight-current-row
style="width: 100%"
>
<el-table-column prop="name" label="fruitName" width="180" />
<el-table-column v-for="fruit in formThead" :key="fruit" :label="fruit">
<template #default="scope">
{{ scope.row[fruit] }}
</template>
</el-table-column>
</el-table>
</div>
</template>
<script setup lang="ts">
const formThead = ref(["apple", "banana"]);
const tableData = [
{
name: "fruit-1",
apple: "apple-10",
banana: "banana-10",
orange: "orange-10",
},
{
name: "fruit-2",
apple: "apple-20",
banana: "banana-20",
orange: "orange-20",
},
];
</script>

View File

@@ -1,24 +0,0 @@
<template>
<div class="app-container">
<div style="margin: 0 0 5px 20px">
Fixed header, sorted by header order,
</div>
<fixed-thead />
<div style="margin: 30px 0 5px 20px">
Not fixed header, sorted by click order
</div>
<unfixed-thead />
</div>
</template>
<script setup lang="ts">
import FixedThead from "./components/FixedThead.vue";
import UnfixedThead from "./components/UnfixedThead.vue";
defineOptions({
// eslint-disable-next-line
name: "DynamicTable",
inheritAttrs: false,
});
</script>