Files
yunzerwebsiteallinone/uniapp/components/sub/SubHome.vue
T
2026-11-16 17:22:50 +08:00

992 lines
21 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<view class="page" :class="{ embedded }">
<view v-if="!embedded" class="hero" :class="sub.tint" :style="headPad">
<view class="hero-main">
<view class="hero-icon">
<FaIcon :name="sub.icon" color="#ffffff" :size="26" />
</view>
<view class="hero-meta">
<text class="hero-name">{{ sub.title }}</text>
<text class="hero-desc">{{ sub.desc }}</text>
</view>
</view>
</view>
<view class="body">
<view v-if="!enabled" class="lock-box">
<FaIcon name="lock" color="#f04438" :size="28" />
<text class="lock-title">该子系统未开通</text>
<text class="lock-desc">当前租户套餐未包含「{{ sub.name }}」,请联系管理员开通后使用。</text>
</view>
<block v-else>
<view v-if="loadError" class="err-box" @tap="load(true)">
<FaIcon name="triangle-exclamation" color="#f04438" :size="18" />
<view class="err-body">
<text class="err-title">子系统加载失败</text>
<text class="err-msg">{{ loadError }},点击重试</text>
</view>
<FaIcon name="rotate" color="#f04438" :size="16" />
</view>
<block v-else>
<view class="home-stack">
<SubOaDashboard
v-if="isOa && dash"
part="top"
:data="dash"
@view-notices="goNotices"
@view-schedules="goSchedules"
/>
<block v-if="!(isOa && dash)">
<view class="kpi-card">
<view class="kpi-item">
<text class="kpi-value">{{ leafMenus.length }}</text>
<text class="kpi-label">可用功能</text>
</view>
<view class="kpi-item">
<text class="kpi-value">{{ pending.length }}</text>
<text class="kpi-label">待办</text>
</view>
<view class="kpi-item">
<text class="kpi-value">{{ doneCount }}</text>
<text class="kpi-label">已完成</text>
</view>
<view class="kpi-item">
<text class="kpi-value" :class="{ warn: overdue > 0 }">{{ overdue }}</text>
<text class="kpi-label">已逾期</text>
</view>
</view>
<view class="block">
<view class="block-head">
<text class="block-title">日程待办</text>
<view class="block-more" @tap="goSchedules">
<text class="block-more-text">全部</text>
<FaIcon name="chevron-right" color="#8a94ad" :size="11" />
</view>
</view>
<view class="list-card">
<view v-if="pending.length === 0" class="empty-box">
<FaIcon name="circle-check" color="#c0c4cc" :size="26" />
<text class="empty-text">暂无待办事项</text>
</view>
<view
v-for="item in pending.slice(0, 3)"
:key="item.id"
class="list-item"
@tap="goSchedules"
>
<view class="dot" :class="{ warn: isOverdue(item) }" />
<view class="list-body">
<text class="list-title">{{ item.content || item.title || '日程' }}</text>
<text class="list-time" :class="{ warn: isOverdue(item) }">{{ item.schedule_time }}</text>
</view>
</view>
</view>
</view>
<view v-if="isOa" class="block">
<view class="block-head">
<text class="block-title">最新公告</text>
<view class="block-more" @tap="goNotices">
<text class="block-more-text">全部</text>
<FaIcon name="chevron-right" color="#8a94ad" :size="11" />
</view>
</view>
<view class="list-card">
<view v-if="notices.length === 0" class="empty-box">
<FaIcon name="bullhorn" color="#c0c4cc" :size="26" />
<text class="empty-text">暂无公告</text>
</view>
<view
v-for="item in notices"
:key="item.id"
class="list-item"
@tap="goNoticeDetail(item.id)"
>
<view class="dot" :class="{ 'dot-top': item.is_top }" />
<view class="list-body">
<text class="list-title">{{ item.title }}</text>
<text class="list-time">{{ item.publish_time || item.publisher_name || '' }}</text>
</view>
</view>
</view>
</view>
</block>
<view class="quick-card">
<view class="quick-head">
<view class="quick-head-left">
<view class="head-icon">
<FaIcon name="star" color="#f59e0b" :size="12" />
</view>
<text class="quick-title">常用功能</text>
</view>
<view class="block-more" @tap="goFunctions">
<text class="block-more-text">全部</text>
<FaIcon name="chevron-right" color="#94a3b8" :size="11" />
</view>
</view>
<view class="grid">
<view
v-for="(item, index) in quickMenus"
:key="item.id"
class="cell"
@tap="openMenu(item, item.title)"
>
<view class="fn-card" :class="{ locked: item.locked }">
<view class="tint-icon fn-icon" :class="item.locked ? 'tint-off' : 'tint-' + (index % 8)">
<FaIcon :name="item.icon || fallbackIcon" color="#ffffff" :size="18" />
</view>
<text class="fn-name">{{ item.title }}</text>
</view>
</view>
<view class="cell">
<view class="fn-card fn-add" @tap="openPicker">
<view class="add-icon">
<FaIcon name="plus" color="#8a94ad" :size="18" />
</view>
<text class="fn-name">添加</text>
</view>
</view>
</view>
</view>
<SubOaDashboard
v-if="isOa && dash"
part="bottom"
:data="dash"
@view-notices="goNotices"
@view-schedules="goSchedules"
/>
</view>
</block>
</block>
<view class="bottom-space" />
</view>
<AppTabbar v-if="!embedded" :group="slug" current="home" />
<view v-if="pickerVisible" class="picker-mask" @tap="pickerVisible = false">
<view class="picker-sheet" @tap.stop>
<view class="picker-head">
<text class="picker-cancel" @tap="pickerVisible = false">取消</text>
<text class="picker-title">选择常用功能</text>
<text class="picker-save" @tap="savePicker">完成</text>
</view>
<text class="picker-tip">最多选 {{ QUICK_MAX }} 个,排在前 {{ QUICK_MAX }} 个会显示在首页;按住右侧手柄拖动可调整</text>
<scroll-view class="picker-body" scroll-y>
<view v-if="pickerItems.length === 0" class="picker-empty">
当前子系统暂无可添加的功能
</view>
<view
v-for="(item, index) in pickerItems"
:key="item.id"
class="picker-row"
:class="{ on: index < pickerSelectedCount, dragging: dragId === item.id }"
:style="dragRowStyle(item)"
@tap="togglePick(item.id)"
>
<view class="tint-icon picker-icon" :class="item.locked ? 'tint-off' : 'tint-' + (index % 8)">
<FaIcon :name="item.icon || fallbackIcon" color="#ffffff" :size="16" />
</view>
<text class="picker-name">{{ item.title }}</text>
<view class="picker-state">
<text v-if="index < pickerSelectedCount" class="picker-num">{{ index + 1 }}</text>
<view v-else class="picker-check" />
</view>
<view
class="picker-handle"
@tap.stop="noop"
@touchstart.stop="onDragStart($event, item)"
@touchmove.stop.prevent="onDragMove"
@touchend.stop="onDragEnd"
@touchcancel.stop="onDragEnd"
>
<FaIcon
name="grip-vertical"
:color="dragId === item.id ? '#2563eb' : '#c3cbd9'"
:size="14"
/>
</view>
</view>
</scroll-view>
</view>
</view>
</view>
</template>
<script setup>
import { ref, computed, onMounted } from 'vue'
import { mpHeadPad } from '@/utils/mpHead.js'
const headPad = mpHeadPad()
import { onShow } from '@dcloudio/uni-app'
import { isLoggedIn } from '@/utils/auth.js'
import { getGuestToken } from '@/utils/guest.js'
import { request } from '@/api/request.js'
import { clearHomeCache } from '@/api/workspace.js'
import { openMenu } from '@/utils/nav.js'
import { useSubsystem } from '@/utils/subsystem.js'
import AppTabbar from '@/components/AppTabbar.vue'
import SubOaDashboard from '@/components/sub/SubOaDashboard.vue'
const props = defineProps({
slug: { type: String, required: true },
embedded: { type: Boolean, default: false }
})
const emit = defineEmits(['switch-tab', 'back'])
const { sub, loading, loadError, enabled, allMenus, leafMenus, quickMenuIds, load } = useSubsystem(props.slug)
const pending = ref([])
const doneCount = ref(0)
const notices = ref([])
const dash = ref(null)
const isOa = computed(() => String(sub.module || '').toLowerCase() === 'oa')
const fallbackIcon = computed(() => {
const map = {
crm: 'handshake',
erp: 'boxes-stacked',
oa: 'clipboard-check',
cms: 'globe',
bianjiegongju: 'wrench'
}
return map[String(sub.module || '').toLowerCase()] || 'file-lines'
})
function isDashboardMenu(m) {
const title = String(m.title || '').trim()
const path = String(m.path || '')
return title === '仪表盘' || /\/dashboard\/?$/.test(path)
}
const quickCandidates = computed(() =>
allMenus.value
.filter((m) => m.leaf !== false && !isDashboardMenu(m))
.slice()
.sort((a, b) => (a.sort || 0) - (b.sort || 0))
)
const QUICK_MAX = 7
const quickMenus = computed(() => {
const base = quickCandidates.value
const byId = {}
base.forEach((m) => { byId[m.id] = m })
const ids = (quickMenuIds.value || []).map((v) => Number(v)).filter((v) => byId[v])
if (ids.length) return ids.map((id) => byId[id]).slice(0, QUICK_MAX)
return base.slice(0, QUICK_MAX)
})
const pickerVisible = ref(false)
const pickerIds = ref([])
const pickerSelectedCount = ref(0)
const pickerItems = computed(() => {
const byId = {}
quickCandidates.value.forEach((m) => { byId[m.id] = m })
return pickerIds.value.map((id) => byId[id]).filter(Boolean)
})
function openPicker() {
resetDrag()
const picked = quickMenus.value.map((m) => m.id).slice(0, QUICK_MAX)
const pickedSet = new Set(picked)
const rest = quickCandidates.value.map((m) => m.id).filter((id) => !pickedSet.has(id))
pickerIds.value = picked.concat(rest)
pickerSelectedCount.value = picked.length
pickerVisible.value = true
}
function togglePick(id) {
const ids = pickerIds.value.slice()
const i = ids.indexOf(id)
if (i < 0) return
if (i < pickerSelectedCount.value) {
ids.splice(i, 1)
ids.splice(Math.max(0, pickerSelectedCount.value - 1), 0, id)
pickerSelectedCount.value = Math.max(0, pickerSelectedCount.value - 1)
} else {
if (pickerSelectedCount.value >= QUICK_MAX) {
uni.showToast({ title: `最多添加 ${QUICK_MAX} 个常用功能`, icon: 'none' })
return
}
ids.splice(i, 1)
ids.splice(pickerSelectedCount.value, 0, id)
pickerSelectedCount.value += 1
}
pickerIds.value = ids
}
const SORT_ROW_RPX = 100
const dragId = ref(null)
const dragOffset = ref(0)
let dragStartY = 0
let dragRowH = 1
let dragFromIndex = -1
function dragRowStyle(item) {
if (dragId.value !== item.id) return ''
return `transform: translateY(${dragOffset.value}px);`
}
function noop() {}
function onDragStart(e, item) {
const touch = (e.touches && e.touches[0]) || {}
const from = pickerIds.value.indexOf(item.id)
if (from < 0) return
dragStartY = Number(touch.clientY || 0)
dragRowH = uni.upx2px(SORT_ROW_RPX) || 1
dragFromIndex = from
dragOffset.value = 0
dragId.value = item.id
}
function onDragMove(e) {
if (dragId.value === null || dragFromIndex < 0) return
const touch = (e.touches && e.touches[0]) || {}
const delta = Number(touch.clientY || 0) - dragStartY
const last = pickerIds.value.length - 1
const target = Math.max(0, Math.min(last, dragFromIndex + Math.round(delta / dragRowH)))
const cur = pickerIds.value.indexOf(dragId.value)
if (cur >= 0 && target !== cur) {
const ids = pickerIds.value.slice()
ids.splice(cur, 1)
ids.splice(target, 0, dragId.value)
pickerIds.value = ids
}
const residual = delta - (target - dragFromIndex) * dragRowH
dragOffset.value = Math.max(-dragRowH, Math.min(dragRowH, residual))
}
function resetDrag() {
dragId.value = null
dragOffset.value = 0
dragFromIndex = -1
}
function onDragEnd() {
resetDrag()
}
async function savePicker() {
try {
await request({
url: '/api/quick-menus',
method: 'POST',
data: { menu_ids: pickerIds.value.slice(0, pickerSelectedCount.value) }
})
clearHomeCache()
await load(true)
pickerVisible.value = false
uni.showToast({ title: '已保存', icon: 'success' })
} catch (e) {
uni.showToast({ title: (e && e.message) || '保存失败', icon: 'none' })
}
}
async function loadDashboard() {
if (!isOa.value) return
try {
dash.value = await request({ url: '/api/oa/dashboard', method: 'GET', silent: true })
} catch (e) {
dash.value = null
}
}
const overdue = computed(() => pending.value.filter((s) => isOverdue(s)).length)
async function loadTodo() {
try {
const [p, d] = await Promise.all([
request({ url: '/api/schedule/list', data: { status: 'pending', pageSize: 50 } }),
request({ url: '/api/schedule/list', data: { status: 'done', pageSize: 50 } })
])
pending.value = (p && p.list) || []
doneCount.value = ((d && d.list) || []).length
} catch (e) {
pending.value = []
doneCount.value = 0
}
}
async function loadNotices() {
if (!isOa.value) return
try {
const data = await request({ url: '/api/oa/notice/list', data: { pageSize: 3 } })
notices.value = (data && data.list) || []
} catch (e) {
notices.value = []
}
}
function isOverdue(item) {
const t = String(item.schedule_time || item.created_at || '')
return t.length >= 10 && t.slice(0, 10) < todayStr()
}
function todayStr() {
const d = new Date()
const p = (n) => String(n).padStart(2, '0')
return d.getFullYear() + '-' + p(d.getMonth() + 1) + '-' + p(d.getDate())
}
function goBackNav() {
if (props.embedded) {
emit('back')
return
}
uni.reLaunch({ url: '/pages/nav/nav' })
}
function goFunctions() {
if (props.embedded) {
emit('switch-tab', 1)
return
}
uni.redirectTo({ url: `/pages/${props.slug}/functions/functions` })
}
function goSchedules() {
uni.navigateTo({ url: '/pages/tools/schedule/index' })
}
function goNotices() {
uni.navigateTo({ url: '/pages/oa/notice/index' })
}
function goNoticeDetail(id) {
uni.navigateTo({ url: `/pages/oa/notice/detail?id=${id}` })
}
onMounted(() => {
load()
if (!isLoggedIn() && !getGuestToken()) return
loadTodo()
loadNotices()
loadDashboard()
})
onShow(() => {
if ((isLoggedIn() || getGuestToken()) && !loading.value) {
loadTodo()
loadNotices()
loadDashboard()
}
})
</script>
<style lang="scss" scoped>
@import '@/src/styles/page-common.scss';
.page {
@include page-shell;
&.embedded {
height: auto;
}
}
.hero {
flex-shrink: 0;
padding: calc(var(--status-bar-height, 44px) + 20rpx) $page-padding-x 40rpx;
/* #ifdef MP-WEIXIN */
padding-top: calc(env(safe-area-inset-top, 25px) + 84rpx);
/* #endif */
color: #ffffff;
}
.hero-main {
display: flex;
align-items: center;
gap: 20rpx;
margin-top: 40rpx;
}
.hero-icon {
width: 92rpx;
height: 92rpx;
border-radius: 24rpx;
background: rgba(255, 255, 255, 0.2);
border: 1rpx solid rgba(255, 255, 255, 0.32);
display: flex;
align-items: center;
justify-content: center;
flex-shrink: 0;
}
.hero-meta {
flex: 1;
min-width: 0;
}
.hero-name {
display: block;
font-size: 38rpx;
font-weight: 600;
letter-spacing: 1rpx;
}
.hero-desc {
display: block;
margin-top: 8rpx;
font-size: 22rpx;
color: rgba(255, 255, 255, 0.8);
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.body {
flex: 1;
min-height: 0;
overflow-y: auto;
-webkit-overflow-scrolling: touch;
padding: 28rpx $page-padding-x 0;
}
.home-stack {
display: flex;
flex-direction: column;
gap: 24rpx;
}
.block-head {
@include block-head;
}
.block-title {
@include section-title;
}
.block-more {
display: flex;
align-items: center;
gap: 4rpx;
}
.block-more-text {
font-size: 24rpx;
color: $color-text-muted;
}
.kpi-card {
@include card;
display: flex;
padding: 30rpx 0;
}
.kpi-item {
flex: 1;
display: flex;
flex-direction: column;
align-items: center;
gap: 6rpx;
position: relative;
& + &::before {
content: '';
position: absolute;
left: 0;
top: 6rpx;
bottom: 6rpx;
width: 1rpx;
background: $color-divider;
}
}
.kpi-value {
font-size: 40rpx;
font-weight: 600;
line-height: 1.1;
color: $color-text;
&.warn {
color: $color-danger;
}
}
.kpi-label {
font-size: 22rpx;
color: $color-text-muted;
}
.list-card {
@include card;
overflow: hidden;
}
.list-item {
display: flex;
align-items: center;
gap: 18rpx;
padding: 26rpx 24rpx;
& + & {
border-top: 1rpx solid $color-divider;
}
&:active {
background: $color-bg-page;
}
}
.list-body {
flex: 1;
min-width: 0;
}
.list-title {
display: block;
font-size: 28rpx;
color: $color-text;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.list-meta,
.list-time {
display: block;
margin-top: 4rpx;
font-size: 22rpx;
color: $color-text-muted;
}
.list-time.warn {
color: $color-danger;
}
.dot {
width: 14rpx;
height: 14rpx;
border-radius: 50%;
background: $color-primary;
flex-shrink: 0;
&.warn {
background: $color-danger;
}
&.dot-top {
background: $color-danger;
}
}
.empty-box {
padding: 56rpx 0;
display: flex;
flex-direction: column;
align-items: center;
gap: 14rpx;
}
.empty-text {
font-size: 25rpx;
color: $color-text-muted;
}
.quick-card {
@include card;
padding: 26rpx 24rpx 10rpx;
}
.quick-head {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 22rpx;
}
.quick-head-left {
display: flex;
align-items: center;
gap: 12rpx;
}
.head-icon {
width: 40rpx;
height: 40rpx;
border-radius: 12rpx;
background: #fff7e6;
display: flex;
align-items: center;
justify-content: center;
}
.quick-title {
font-size: 28rpx;
font-weight: 600;
color: $color-text;
}
.grid {
display: flex;
flex-wrap: wrap;
margin-right: -16rpx;
}
.cell {
width: 25%;
padding-right: 16rpx;
margin-bottom: 16rpx;
box-sizing: border-box;
}
.fn-card {
height: 100%;
padding: 18rpx 6rpx 16rpx;
border-radius: $radius-md;
background: transparent;
display: flex;
flex-direction: column;
align-items: center;
gap: 12rpx;
transition: transform 0.15s ease;
&:active {
transform: scale(0.95);
}
}
.fn-icon {
@include tint-icon(64rpx, 18rpx);
}
.fn-name {
max-width: 100%;
font-size: 22rpx;
line-height: 1.3;
color: $color-text;
text-align: center;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.fn-card.locked .fn-name {
color: $color-text-muted;
}
.fn-card.fn-add {
background: transparent;
border: 1rpx dashed #c7cede;
}
.add-icon {
width: 64rpx;
height: 64rpx;
border-radius: 18rpx;
background: #eef1f6;
display: flex;
align-items: center;
justify-content: center;
}
.picker-mask {
position: fixed;
left: 0;
top: 0;
right: 0;
bottom: 0;
background: rgba(15, 23, 42, 0.45);
z-index: 100;
display: flex;
align-items: flex-end;
@include mask-responsive;
}
.picker-sheet {
width: 100%;
max-height: 72vh;
background: $color-card;
border-radius: 28rpx 28rpx 0 0;
display: flex;
flex-direction: column;
padding-bottom: env(safe-area-inset-bottom);
@include sheet-responsive;
}
.picker-head {
display: flex;
align-items: center;
justify-content: space-between;
padding: 28rpx $page-padding-x;
border-bottom: 1rpx solid $color-divider;
}
.picker-title {
font-size: 30rpx;
font-weight: 600;
color: $color-text;
}
.picker-cancel {
font-size: 28rpx;
color: $color-text-muted;
}
.picker-save {
font-size: 28rpx;
font-weight: 600;
color: $color-primary;
}
.picker-tip {
padding: 18rpx $page-padding-x 4rpx;
font-size: 22rpx;
line-height: 1.5;
color: $color-text-muted;
}
.picker-body {
flex: 1;
min-height: 0;
max-height: 52vh;
padding: 8rpx $page-padding-x 24rpx;
}
.picker-empty {
padding: 80rpx 0;
text-align: center;
font-size: 26rpx;
color: $color-text-muted;
}
.picker-row {
position: relative;
display: flex;
align-items: center;
gap: 18rpx;
height: 100rpx;
box-sizing: border-box;
padding: 0 0 0 16rpx;
border-bottom: 1rpx solid $color-divider;
&.on {
background: #f2f7ff;
}
&.dragging {
z-index: 3;
background: #ffffff;
border-radius: 16rpx;
border-bottom-color: transparent;
box-shadow: 0 10rpx 26rpx rgba(15, 23, 42, 0.14);
}
}
.picker-state {
width: 40rpx;
flex-shrink: 0;
display: flex;
align-items: center;
justify-content: center;
}
.picker-num {
font-size: 22rpx;
font-weight: 600;
color: $color-primary;
}
.picker-handle {
width: 64rpx;
height: 100rpx;
flex-shrink: 0;
display: flex;
align-items: center;
justify-content: center;
&:active {
opacity: 0.55;
}
}
.picker-icon {
@include tint-icon(56rpx, 16rpx);
flex-shrink: 0;
}
.picker-name {
flex: 1;
min-width: 0;
font-size: 28rpx;
color: $color-text;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
.picker-check {
width: 36rpx;
height: 36rpx;
border-radius: 50%;
border: 2rpx solid #c7cede;
box-sizing: border-box;
}
.err-box {
display: flex;
align-items: center;
gap: 16rpx;
padding: 24rpx 22rpx;
background: #fef3f2;
border: 1rpx solid #fdd8d4;
border-radius: $radius-md;
}
.err-body {
flex: 1;
min-width: 0;
}
.err-title {
display: block;
font-size: 27rpx;
font-weight: 500;
color: $color-danger;
}
.err-msg {
display: block;
margin-top: 6rpx;
font-size: 22rpx;
color: #b42318;
word-break: break-all;
}
.lock-box {
@include card;
padding: 60rpx 32rpx;
display: flex;
flex-direction: column;
align-items: center;
gap: 14rpx;
}
.lock-title {
font-size: 30rpx;
font-weight: 600;
color: $color-danger;
}
.lock-desc {
font-size: 24rpx;
color: $color-text-muted;
text-align: center;
line-height: 1.6;
}
.bottom-space {
height: 32rpx;
}
</style>