<script setup lang="ts">
|
import { computed, ref } from "vue";
|
import { onPullDownRefresh, onShow } from "@dcloudio/uni-app";
|
import {
|
deleteMaterial,
|
getMaterialPage,
|
updateMaterialStatus,
|
updateMaterialSync,
|
type MaterialItem,
|
} from "../../../api/basic-data";
|
import { useAuth } from "../../../stores/auth";
|
const { hasPermission, isLoggedIn, state } = useAuth();
|
const items = ref<MaterialItem[]>([]);
|
const keyword = ref("");
|
const pageNo = ref(1);
|
const total = ref(0);
|
const loading = ref(false);
|
const loadError = ref("");
|
const canQuery = computed(() => hasPermission("mdm:item:query"));
|
const canCreate = computed(() => hasPermission("mdm:item:create"));
|
const canUpdate = computed(() => hasPermission("mdm:item:update"));
|
const canDelete = computed(() => hasPermission("mdm:item:delete"));
|
const hasMore = computed(() => items.value.length < total.value);
|
function statusText(status: number) {
|
return status === 0 ? "启用" : "停用";
|
}
|
function isMesSynced(item: MaterialItem) {
|
return item.syncMes ?? item.syncedMes ?? true;
|
}
|
function message(error: unknown, fallback: string) {
|
return error instanceof Error ? error.message : fallback;
|
}
|
async function loadMaterials(reset = false) {
|
if (!state.accessToken || loading.value || !canQuery.value) return;
|
if (reset) {
|
pageNo.value = 1;
|
items.value = [];
|
}
|
loading.value = true;
|
loadError.value = "";
|
try {
|
const data = await getMaterialPage(state.accessToken, {
|
pageNo: pageNo.value,
|
pageSize: 15,
|
name: keyword.value.trim() || undefined,
|
});
|
total.value = data.total;
|
items.value = reset ? data.list : [...items.value, ...data.list];
|
pageNo.value++;
|
} catch (error) {
|
loadError.value = message(error, "物料加载失败");
|
} finally {
|
loading.value = false;
|
}
|
}
|
function goForm(id?: number) {
|
uni.navigateTo({
|
url: `/pages/basic-data/material/form${id ? `?id=${id}` : ""}`,
|
});
|
}
|
function confirm(title: string, content: string) {
|
return new Promise<boolean>(resolve =>
|
uni.showModal({ title, content, success: res => resolve(res.confirm) })
|
);
|
}
|
async function remove(item: MaterialItem) {
|
if (
|
!state.accessToken ||
|
!item.id ||
|
!(await confirm("确认删除", `确定删除物料“${item.name}”吗?`))
|
)
|
return;
|
try {
|
await deleteMaterial(state.accessToken, item.id);
|
uni.showToast({ title: "删除成功", icon: "success" });
|
loadMaterials(true);
|
} catch (error) {
|
uni.showToast({ title: message(error, "删除失败"), icon: "none" });
|
}
|
}
|
async function changeStatus(item: MaterialItem) {
|
if (!state.accessToken || !item.id) return;
|
try {
|
await updateMaterialStatus(
|
state.accessToken,
|
item.id,
|
item.status === 0 ? 1 : 0
|
);
|
uni.showToast({ title: "状态已更新", icon: "success" });
|
loadMaterials(true);
|
} catch (error) {
|
uni.showToast({ title: message(error, "更新失败"), icon: "none" });
|
}
|
}
|
async function changeSync(item: MaterialItem) {
|
if (!state.accessToken || !item.id) return;
|
const sync = !isMesSynced(item);
|
if (
|
!sync &&
|
!(await confirm(
|
"关闭 MES 同步",
|
"关闭后该物料将不再同步到 MES,确定继续吗?"
|
))
|
)
|
return;
|
try {
|
await updateMaterialSync(state.accessToken, item.id, sync);
|
uni.showToast({ title: "同步设置已更新", icon: "success" });
|
loadMaterials(true);
|
} catch (error) {
|
uni.showToast({ title: message(error, "更新失败"), icon: "none" });
|
}
|
}
|
function loadMore() {
|
if (hasMore.value) loadMaterials();
|
}
|
onShow(() => loadMaterials(true));
|
onPullDownRefresh(async () => {
|
await loadMaterials(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="loadMaterials(true)" /><text v-if="keyword"
|
@click="keyword = ''; loadMaterials(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="loadMaterials(true)">{{ loadError }},点击重试</view>
|
<view v-else-if="items.length"
|
class="list">
|
<view v-for="item in items"
|
:key="item.id"
|
class="row">
|
<view class="main">
|
<view class="card-head"><text class="name">{{ item.name }}</text><text :class="item.status === 0 ? 'enabled' : 'disabled'">{{ statusText(item.status) }}</text></view><text>{{ item.code }}<template v-if="item.specification"> · {{ item.specification }}</template></text><text>{{ item.categoryName || '未分类' }} · {{ item.unitMeasureName || '未设置单位' }}</text>
|
</view>
|
<view class="actions"><button v-if="canUpdate"
|
@click.stop="goForm(item.id)">编辑</button><button v-if="canUpdate"
|
@click.stop="changeStatus(item)">{{ item.status === 0 ? '停用' : '启用' }}</button><button v-if="canUpdate"
|
@click.stop="changeSync(item)">{{ isMesSynced(item) ? '关MES' : '开MES' }}</button><button v-if="canDelete"
|
class="danger"
|
@click.stop="remove(item)">删除</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"><text>登录后查看物料信息</text><button @click="uni.navigateTo({ url: '/pages/login/index' })">立即登录</button></view>
|
</scroll-view>
|
<button v-if="canCreate"
|
class="create-fab"
|
@click="goForm()">
|
<view class="fab-plus"></view>
|
</button>
|
</view>
|
</template>
|
<style lang="scss" scoped>
|
.page {
|
height: 100vh;
|
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;
|
}
|
.main,
|
.card-head,
|
.actions,
|
.search {
|
display: flex;
|
}
|
.main {
|
flex-direction: column;
|
gap: 8rpx;
|
}
|
.main > text {
|
font-size: 23rpx;
|
color: #78879b;
|
}
|
.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 {
|
background: transparent;
|
}
|
.row {
|
padding: 24rpx 22rpx 20rpx;
|
margin-bottom: 18rpx;
|
background: #fff;
|
border-radius: 16rpx;
|
box-shadow: 0 10rpx 24rpx rgba(43, 69, 107, 0.08);
|
}
|
.main {
|
gap: 10rpx;
|
}
|
.card-head {
|
align-items: center;
|
justify-content: space-between;
|
gap: 16rpx;
|
margin-bottom: 2rpx;
|
}
|
.name {
|
min-width: 0;
|
overflow: hidden;
|
font-size: 30rpx;
|
font-weight: 600;
|
color: #20334c;
|
text-overflow: ellipsis;
|
white-space: nowrap;
|
}
|
.enabled,
|
.disabled {
|
flex: none;
|
padding: 4rpx 10rpx;
|
font-size: 21rpx;
|
border-radius: 7rpx;
|
}
|
.enabled {
|
color: #1c9b68;
|
background: #e8f8f0;
|
}
|
.disabled {
|
color: #8d9bad;
|
background: #f0f3f6;
|
}
|
.actions {
|
justify-content: flex-end;
|
flex-wrap: wrap;
|
gap: 12rpx;
|
padding-top: 18rpx;
|
margin-top: 18rpx;
|
border-top: 1rpx solid #edf1f6;
|
}
|
.actions button {
|
min-height: 56rpx;
|
padding: 0 18rpx;
|
margin: 0;
|
color: var(--color-primary, #1769d8);
|
font-size: 24rpx;
|
font-weight: 600;
|
line-height: 54rpx;
|
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,
|
.state {
|
display: block;
|
padding: 35rpx;
|
color: #8795a8;
|
font-size: 26rpx;
|
text-align: center;
|
}
|
.error {
|
color: #c3654d;
|
}
|
.state button {
|
width: 220rpx;
|
margin: 20rpx auto;
|
color: #fff;
|
background: var(--color-primary, #1769d8);
|
border-radius: 12rpx;
|
}
|
.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>
|