<script setup lang="ts">
|
import { computed, ref } from "vue";
|
import { onPullDownRefresh, onShow } from "@dcloudio/uni-app";
|
import {
|
deleteMeasureUnit,
|
getMeasureUnitPage,
|
type MeasureUnit,
|
} from "../../../api/basic-data";
|
import { useAuth } from "../../../stores/auth";
|
const { hasPermission, isLoggedIn, state } = useAuth();
|
const units = ref<MeasureUnit[]>([]);
|
const keyword = ref("");
|
const pageNo = ref(1);
|
const total = ref(0);
|
const loading = ref(false);
|
const loadError = ref("");
|
const canQuery = computed(() => hasPermission("mdm:unit:query"));
|
const canCreate = computed(() => hasPermission("mdm:unit:create"));
|
const canUpdate = computed(() => hasPermission("mdm:unit:update"));
|
const canDelete = computed(() => hasPermission("mdm:unit:delete"));
|
const hasMore = computed(() => units.value.length < total.value);
|
async function load(reset = false) {
|
if (!state.accessToken || loading.value || !canQuery.value) return;
|
if (reset) {
|
pageNo.value = 1;
|
units.value = [];
|
}
|
loading.value = true;
|
loadError.value = "";
|
try {
|
const data = await getMeasureUnitPage(state.accessToken, {
|
pageNo: pageNo.value,
|
pageSize: 15,
|
name: keyword.value.trim() || undefined,
|
});
|
total.value = data.total;
|
units.value = reset ? data.list : [...units.value, ...data.list];
|
pageNo.value++;
|
} catch (e) {
|
loadError.value = e instanceof Error ? e.message : "单位加载失败";
|
} finally {
|
loading.value = false;
|
}
|
}
|
function go(id?: number) {
|
uni.navigateTo({
|
url: `/pages/basic-data/unit/form${id ? `?id=${id}` : ""}`,
|
});
|
}
|
function remove(x: MeasureUnit) {
|
if (!state.accessToken || !x.id) return;
|
uni.showModal({
|
title: "确认删除",
|
content: `确定删除单位“${x.name}”吗?`,
|
success: async r => {
|
if (!r.confirm) return;
|
try {
|
await deleteMeasureUnit(state.accessToken!, x.id!);
|
uni.showToast({ title: "删除成功", icon: "success" });
|
load(true);
|
} catch (e) {
|
uni.showToast({
|
title: e instanceof Error ? e.message : "删除失败",
|
icon: "none",
|
});
|
}
|
},
|
});
|
}
|
function loadMore() {
|
if (hasMore.value) load();
|
}
|
onShow(() => load(true));
|
onPullDownRefresh(async () => {
|
await load(true);
|
uni.stopPullDownRefresh();
|
});
|
</script>
|
<template>
|
<view class="page">
|
<template v-if="isLoggedIn">
|
<view v-if="canQuery"
|
class="search"><input v-model="keyword"
|
placeholder="搜索单位名称"
|
confirm-type="search"
|
@confirm="load(true)" /><text v-if="keyword"
|
@click="keyword = ''; load(true)">清除</text></view>
|
<scroll-view v-if="canQuery"
|
class="content-scroll"
|
scroll-y
|
lower-threshold="80"
|
@scrolltolower="loadMore">
|
<view v-if="loadError"
|
class="state error"
|
@click="load(true)">{{ loadError }},点击重试</view>
|
<view v-else-if="units.length"
|
class="list">
|
<view v-for="x in units"
|
:key="x.id"
|
class="row">
|
<view class="row-head">
|
<view class="mark">{{ x.name.slice(0, 1) }}</view>
|
<view class="main">
|
<text>{{ x.name }}</text>
|
<text>{{ x.code }}</text>
|
</view>
|
<text :class="x.status === 0 ? 'enabled' : 'disabled'">{{ x.status === 0 ? '启用' : '停用' }}</text>
|
</view>
|
<view class="actions"><button v-if="canUpdate"
|
@click.stop="go(x.id)">编辑</button><button v-if="canDelete"
|
class="danger"
|
@click.stop="remove(x)">删除</button></view>
|
</view><text class="more">{{ loading ? '加载中...' : hasMore ? '上拉加载更多' : '没有更多单位了' }}</text>
|
</view>
|
<view v-else
|
class="state">{{ loading ? '加载中...' : keyword ? '没有匹配的计量单位' : '暂无计量单位数据' }}</view>
|
</scroll-view>
|
<scroll-view v-else
|
class="content-scroll"
|
scroll-y>
|
<view class="state">暂无计量单位查看权限</view>
|
</scroll-view>
|
</template>
|
<scroll-view v-else
|
class="content-scroll"
|
scroll-y>
|
<view class="state">登录后查看计量单位</view>
|
</scroll-view>
|
<button v-if="canCreate"
|
class="create-fab"
|
@click="go()">
|
<view class="fab-plus"></view>
|
</button>
|
</view>
|
</template>
|
<style lang="scss" scoped>
|
.page {
|
position: fixed;
|
top: var(--window-top);
|
right: 0;
|
bottom: var(--window-bottom);
|
left: 0;
|
width: 100%;
|
min-height: 0;
|
display: flex;
|
flex-direction: column;
|
padding: 0 30rpx;
|
box-sizing: border-box;
|
overflow: hidden;
|
background: #f3f6fb;
|
}
|
.content-scroll {
|
min-height: 0;
|
height: 0;
|
flex: 1;
|
padding-bottom: 132rpx;
|
box-sizing: border-box;
|
}
|
.row-head,
|
.main,
|
.actions,
|
.search {
|
display: flex;
|
}
|
.main {
|
min-width: 0;
|
flex: 1;
|
flex-direction: column;
|
gap: 7rpx;
|
}
|
.main > text:last-child {
|
overflow: hidden;
|
color: #78879b;
|
font-size: 22rpx;
|
text-overflow: ellipsis;
|
white-space: nowrap;
|
}
|
.search {
|
flex: none;
|
align-items: center;
|
height: 80rpx;
|
margin: 20rpx 0 16rpx;
|
padding: 0 20rpx;
|
background: #fff;
|
border-radius: 16rpx;
|
box-shadow: 0 8rpx 20rpx rgba(43, 69, 107, 0.06);
|
}
|
.search input {
|
flex: 1;
|
font-size: 27rpx;
|
}
|
.search text {
|
font-size: 24rpx;
|
color: var(--color-primary, #1769d8);
|
}
|
.list {
|
display: grid;
|
grid-template-columns: repeat(2, minmax(0, 1fr));
|
gap: 16rpx;
|
background: transparent;
|
}
|
.row {
|
display: flex;
|
flex-direction: column;
|
min-height: 168rpx;
|
padding: 22rpx 18rpx;
|
margin-bottom: 0;
|
box-sizing: border-box;
|
background: #fff;
|
border-radius: 16rpx;
|
box-shadow: 0 10rpx 24rpx rgba(43, 69, 107, 0.08);
|
}
|
.row-head {
|
align-items: flex-start;
|
gap: 12rpx;
|
}
|
.mark {
|
width: 52rpx;
|
height: 52rpx;
|
flex: none;
|
color: #26895d;
|
line-height: 52rpx;
|
text-align: center;
|
background: #e9f8ef;
|
border-radius: 14rpx;
|
}
|
.main > text:first-child {
|
overflow: hidden;
|
font-size: 26rpx;
|
font-weight: 600;
|
color: #20334c;
|
text-overflow: ellipsis;
|
white-space: nowrap;
|
}
|
.enabled,
|
.disabled {
|
flex: none;
|
padding: 3rpx 8rpx;
|
font-size: 20rpx;
|
line-height: 28rpx;
|
border-radius: 7rpx;
|
}
|
.enabled {
|
color: #1c9b68;
|
background: #e8f8f0;
|
}
|
.disabled {
|
color: #8d9bad;
|
background: #f0f3f6;
|
}
|
.actions {
|
justify-content: flex-end;
|
flex-wrap: wrap;
|
gap: 10rpx;
|
padding-top: 14rpx;
|
margin-top: auto;
|
}
|
.actions button {
|
min-height: 50rpx;
|
padding: 0 14rpx;
|
margin: 0;
|
color: var(--color-primary, #1769d8);
|
font-size: 22rpx;
|
font-weight: 600;
|
line-height: 48rpx;
|
background: var(--color-primary-soft, #f5f8ff);
|
border: 1rpx solid var(--color-primary-border, #8ab3ee);
|
border-radius: 10rpx;
|
}
|
.actions button::after,
|
.create-fab::after {
|
border: 0;
|
}
|
.actions .danger {
|
color: #c65050;
|
background: #fff7f7;
|
border-color: #e3a0a0;
|
}
|
.more {
|
grid-column: 1 / -1;
|
}
|
.more,
|
.state {
|
display: block;
|
padding: 35rpx;
|
color: #8795a8;
|
text-align: center;
|
}
|
.error {
|
color: #c3654d;
|
}
|
.create-fab {
|
position: fixed;
|
right: 30rpx;
|
bottom: calc(32rpx + env(safe-area-inset-bottom));
|
z-index: 10;
|
display: flex;
|
flex-direction: column;
|
align-items: center;
|
justify-content: center;
|
width: 104rpx; height: 104rpx; min-height: 104rpx;
|
padding: 0;
|
margin: 0;
|
color: #fff;
|
font-size: 25rpx;
|
font-weight: 600;
|
line-height: 32rpx;
|
background: var(--color-primary, #1769d8);
|
border-radius: 50%;
|
box-shadow: 0 12rpx 24rpx rgba(23, 105, 216, 0.3);
|
}
|
.fab-plus,
|
.fab-plus::before,
|
.fab-plus::after {
|
display: block;
|
background: currentColor;
|
border-radius: 4rpx;
|
}
|
.fab-plus {
|
position: relative;
|
width: 36rpx;
|
height: 8rpx;
|
}
|
.fab-plus::before,
|
.fab-plus::after {
|
position: absolute;
|
top: 50%;
|
left: 50%;
|
content: "";
|
transform: translate(-50%, -50%);
|
}
|
.fab-plus::before {
|
width: 36rpx;
|
height: 8rpx;
|
}
|
.fab-plus::after {
|
width: 8rpx;
|
height: 36rpx;
|
}
|
</style>
|