重构项目文件夹目录

This commit is contained in:
2026-06-15 15:53:55 +08:00
parent 8093e07190
commit 7cbe5b3de5
1098 changed files with 34 additions and 4441 deletions
@@ -0,0 +1,422 @@
/**
* Purpose: messageApi wraps admin system-message endpoints for list, publish, revoke, and target options.
* Usage: call these methods from page event handlers instead of assembling backend URLs in the view layer.
*/
const messageApi = require('../../api/system-message')
/**
* Purpose: navigation handles custom navigation-bar back behavior.
* Usage: call goBack when returning to the management menu.
*/
const navigation = require('../../utils/navigation')
/**
* Purpose: permission checks button-level rights for create and revoke actions.
* Usage: pass stored permission codes into hasPermission before rendering or running guarded actions.
*/
const permission = require('../../utils/permission')
/**
* Purpose: auth exposes the current admin permission list stored after login.
* Usage: read user info and permissions when deciding which mobile controls are available.
*/
const auth = require('../../utils/auth')
/**
* Purpose: format converts backend times into mini-program friendly text.
* Usage: call formatDateTime before assigning message rows to data.list.
*/
const format = require('../../utils/format')
/**
* Purpose: actionGuard prevents duplicate publish and revoke submissions from rapid taps.
* Usage: wrap create and revoke requests with actionGuard.run.
*/
const actionGuard = require('../../utils/action-guard')
/**
* Purpose: TARGET_TYPE_OPTIONS defines publish target scopes supported by the backend contract.
* Usage: render it as chips and submit the selected value as targetType.
*/
const TARGET_TYPE_OPTIONS = [
{ label: '全平台', value: 'PLATFORM_ALL' },
{ label: '指定店铺', value: 'SELECTED_SHOPS' },
{ label: '指定用户', value: 'SELECTED_USERS' }
]
/**
* Purpose: BUSINESS_TYPE_OPTIONS lists common business navigation types used by order review and artist-change messages.
* Usage: render it as chips and store the selected value in the publish form.
*/
const BUSINESS_TYPE_OPTIONS = [
{ label: '无', value: '' },
{ label: '订单', value: 'ORDER' },
{ label: '评价提醒', value: 'ORDER_ARTIST_REVIEW' },
{ label: '改派申请', value: 'ORDER_ARTIST_CHANGE' }
]
/**
* Purpose: initialForm returns a clean publish form with popup enabled by default.
* Usage: call it when opening the form and after successful publish.
*/
function initialForm() {
return {
title: '',
content: '',
targetType: 'PLATFORM_ALL',
targetId: '',
popupEnabled: true,
businessType: '',
businessId: '',
actionPath: ''
}
}
Page({
/**
* Purpose: data stores system-message list, publish form state, permissions, and picker options.
* Usage: WXML binds these fields and event handlers update them through setData.
*/
data: {
/**
* Purpose: loading shows whether the message list is being refreshed.
* Usage: bind it to page loading lines and refresh controls.
*/
loading: false,
/**
* Purpose: submitting shows whether publish or revoke is currently running.
* Usage: disable action buttons while true.
*/
submitting: false,
/**
* Purpose: list contains formatted system messages visible to the current admin scope.
* Usage: render it in WXML list cards.
*/
list: [],
/**
* Purpose: targetOptions stores backend-provided shops and users for scoped publishing.
* Usage: resolve targetId names and validate selected target choices.
*/
targetOptions: { shops: [], users: [] },
/**
* Purpose: formVisible controls whether the mobile publish panel is shown.
* Usage: toggle it from header and cancel buttons.
*/
formVisible: false,
/**
* Purpose: form holds publish fields sent to createMessage after validation.
* Usage: update individual properties from inputs and chips.
*/
form: initialForm(),
/**
* Purpose: targetTypeOptions renders publish target scope choices.
* Usage: iterate it in WXML chips.
*/
targetTypeOptions: TARGET_TYPE_OPTIONS,
/**
* Purpose: businessTypeOptions renders common business navigation choices.
* Usage: iterate it in WXML chips.
*/
businessTypeOptions: BUSINESS_TYPE_OPTIONS,
/**
* Purpose: canCreate controls whether the publish action is visible for the current permission set.
* Usage: derive it from message:create permission during onLoad.
*/
canCreate: false,
/**
* Purpose: canRevoke controls whether revoke buttons are visible for the current permission set.
* Usage: derive it from message:delete permission during onLoad.
*/
canRevoke: false
},
/**
* Purpose: onLoad initializes permission flags and fetches message list data.
* Usage: mini-program runtime calls it when the operator opens the system-message page.
*/
onLoad() {
const userInfo = auth.getStoredUserInfo() || {}
const codes = userInfo.permissions || []
this.setData({
canCreate: permission.hasPermission(codes, 'message:create'),
canRevoke: permission.hasPermission(codes, 'message:delete')
})
this.loadMessages()
},
/**
* Purpose: onPullDownRefresh reloads system messages and target options.
* Usage: mini-program runtime calls it after a manual refresh gesture.
*/
async onPullDownRefresh() {
await this.loadMessages()
wx.stopPullDownRefresh()
},
/**
* Purpose: loadMessages refreshes list data and target options for the publish form.
* Usage: call it on page entry, pull refresh, publish success, and revoke success.
*/
async loadMessages() {
this.setData({ loading: true })
try {
const [messages, targetOptions] = await Promise.all([
messageApi.listMessages(),
this.data.canCreate ? messageApi.getTargetOptions() : Promise.resolve(this.data.targetOptions)
])
this.setData({
list: (Array.isArray(messages) ? messages : []).map(item => this.formatMessage(item)),
targetOptions: targetOptions || { shops: [], users: [] }
})
} catch (error) {
wx.showToast({
title: error.message || '系统消息加载失败',
icon: 'none'
})
} finally {
this.setData({ loading: false })
}
},
/**
* Purpose: formatMessage normalizes backend message metadata for compact card display.
* Usage: call it on every list item before assigning to data.list.
*/
formatMessage(item) {
return {
...item,
displayTime: format.formatDateTime(item.publishTime || item.createTime),
statusText: Number(item.status) === 1 ? '已发布' : '已撤回',
popupText: Number(item.popupEnabled) === 1 ? '弹窗' : '不弹窗',
businessText: item.businessType || '无业务跳转',
actionText: item.actionPath || (item.businessId ? `业务 ID:${item.businessId}` : '无')
}
},
/**
* Purpose: handleShowForm opens a clean publish panel for authorized operators.
* Usage: bind it to the publish button.
*/
handleShowForm() {
this.setData({
formVisible: true,
form: initialForm()
})
},
/**
* Purpose: handleHideForm closes the publish panel without submitting.
* Usage: bind it to cancel controls.
*/
handleHideForm() {
this.setData({ formVisible: false })
},
/**
* Purpose: patchForm updates one field in the publish form.
* Usage: call it from input and chip handlers with a partial form object.
*/
patchForm(patch) {
this.setData({
form: {
...this.data.form,
...patch
}
})
},
/**
* Purpose: handleTitleInput stores the publish title input value.
* Usage: bind it to the title input.
*/
handleTitleInput(event) {
this.patchForm({ title: event.detail.value })
},
/**
* Purpose: handleContentInput stores the publish content textarea value.
* Usage: bind it to the content textarea.
*/
handleContentInput(event) {
this.patchForm({ content: event.detail.value })
},
/**
* Purpose: handleTargetIdInput stores selected shop or user id values.
* Usage: bind it to the target id input when targetType is SHOP or USER.
*/
handleTargetIdInput(event) {
this.patchForm({ targetId: event.detail.value })
},
/**
* Purpose: handleBusinessIdInput stores the optional business id used for message navigation.
* Usage: bind it to the business id input.
*/
handleBusinessIdInput(event) {
this.patchForm({ businessId: event.detail.value })
},
/**
* Purpose: handleActionPathInput stores the optional frontend action path used by popup and list taps.
* Usage: bind it to the action path input.
*/
handleActionPathInput(event) {
this.patchForm({ actionPath: event.detail.value })
},
/**
* Purpose: handleTargetTypeChange changes publish target scope and clears stale target ids.
* Usage: bind it to target type chips with data-value.
*/
handleTargetTypeChange(event) {
this.patchForm({
targetType: event.currentTarget.dataset.value,
targetId: ''
})
},
/**
* Purpose: handleBusinessTypeChange changes the optional business navigation type.
* Usage: bind it to business type chips with data-value.
*/
handleBusinessTypeChange(event) {
this.patchForm({ businessType: event.currentTarget.dataset.value || '' })
},
/**
* Purpose: handlePopupToggle flips whether recipients should see a one-time popup.
* Usage: bind it to the popup switch control.
*/
handlePopupToggle(event) {
this.patchForm({ popupEnabled: Boolean(event.detail.value) })
},
/**
* Purpose: buildPublishPayload validates the form and converts it to backend DTO shape.
* Usage: call it inside handlePublish before createMessage; it returns null after showing validation errors.
*/
buildPublishPayload() {
const form = this.data.form
const title = String(form.title || '').trim()
const content = String(form.content || '').trim()
if (!title || !content) {
wx.showToast({
title: '请填写标题和内容',
icon: 'none'
})
return null
}
const payload = {
title,
content,
targetType: form.targetType,
popupEnabled: form.popupEnabled ? 1 : 0,
businessType: form.businessType || undefined,
businessId: form.businessId ? Number(form.businessId) : undefined,
actionPath: String(form.actionPath || '').trim() || undefined
}
if (form.targetType === 'SELECTED_SHOPS') {
if (!form.targetId) {
wx.showToast({ title: '请输入店铺 ID', icon: 'none' })
return null
}
payload.shopIds = [Number(form.targetId)]
}
if (form.targetType === 'SELECTED_USERS') {
if (!form.targetId) {
wx.showToast({ title: '请输入用户 ID', icon: 'none' })
return null
}
payload.userIds = [Number(form.targetId)]
}
return payload
},
/**
* Purpose: handlePublish submits the publish form with duplicate-tap protection.
* Usage: bind it to the primary publish button.
*/
async handlePublish() {
const payload = this.buildPublishPayload()
if (!payload) {
return
}
try {
await actionGuard.run(this, 'system-message:publish', async () => {
this.setData({ submitting: true })
await messageApi.createMessage(payload)
wx.showToast({
title: '消息已发布',
icon: 'none'
})
this.setData({
formVisible: false,
form: initialForm()
})
await this.loadMessages()
}, {
message: '消息发布中,请勿重复提交'
})
} catch (error) {
if (actionGuard.isGuardError(error)) {
return
}
wx.showToast({
title: error.message || '消息发布失败',
icon: 'none'
})
} finally {
this.setData({ submitting: false })
}
},
/**
* Purpose: handleRevoke revokes one message after operator confirmation.
* Usage: bind it to list-row revoke buttons with data-id.
*/
async handleRevoke(event) {
const id = event.currentTarget.dataset.id
if (!id) {
return
}
const confirmed = await new Promise(resolve => {
wx.showModal({
title: '撤回消息',
content: '确认撤回这条系统消息吗?',
confirmText: '撤回',
success: res => resolve(Boolean(res.confirm))
})
})
if (!confirmed) {
return
}
try {
await actionGuard.run(this, `system-message:revoke:${id}`, async () => {
await messageApi.revokeMessage(id)
wx.showToast({
title: '已撤回',
icon: 'none'
})
await this.loadMessages()
}, {
message: '消息撤回中,请勿重复操作'
})
} catch (error) {
if (actionGuard.isGuardError(error)) {
return
}
wx.showToast({
title: error.message || '撤回失败',
icon: 'none'
})
}
},
/**
* Purpose: handleBack returns to the management menu.
* Usage: bind it to the custom navigation bar back event.
*/
handleBack() {
navigation.goBack('/pages/manage/index')
}
})
@@ -0,0 +1,5 @@
{
"usingComponents": {
"navigation-bar": "/admin/components/navigation-bar/navigation-bar"
}
}
@@ -0,0 +1,89 @@
<navigation-bar title="系统消息" back="{{true}}" bindback="handleBack" color="#f6efe7" background="#081518"></navigation-bar>
<view class="page-shell">
<view class="page-stack">
<view class="hero-panel">
<text class="hero-kicker">Message Center</text>
<text class="hero-title">消息发布与弹窗配置</text>
<text class="hero-desc">查看消息列表、弹窗开关、业务跳转和发布状态。</text>
</view>
<view wx:if="{{canCreate && !formVisible}}" class="primary-btn" bindtap="handleShowForm">发布消息</view>
<view wx:if="{{formVisible}}" class="surface-card form-card">
<text class="section-title">发布系统消息</text>
<input class="field-input" placeholder="标题" placeholder-class="placeholder" value="{{form.title}}" bindinput="handleTitleInput"></input>
<textarea class="field-textarea" placeholder="消息内容" placeholder-class="placeholder" value="{{form.content}}" maxlength="2000" bindinput="handleContentInput"></textarea>
<text class="field-label">接收范围</text>
<view class="chip-row">
<block wx:for="{{targetTypeOptions}}" wx:key="value">
<view class="chip-btn {{form.targetType === item.value ? 'active' : ''}}" data-value="{{item.value}}" bindtap="handleTargetTypeChange">{{item.label}}</view>
</block>
</view>
<input wx:if="{{form.targetType !== 'PLATFORM_ALL'}}" class="field-input" placeholder="{{form.targetType === 'SELECTED_SHOPS' ? '店铺 ID' : '用户 ID'}}" placeholder-class="placeholder" value="{{form.targetId}}" bindinput="handleTargetIdInput"></input>
<view wx:if="{{form.targetType === 'SELECTED_SHOPS' && targetOptions.shops.length}}" class="option-hint">可选店铺:{{targetOptions.shops.length}} 个,输入对应 ID 发布</view>
<view wx:if="{{form.targetType === 'SELECTED_USERS' && targetOptions.users.length}}" class="option-hint">可选用户:{{targetOptions.users.length}} 个,输入对应 ID 发布</view>
<view class="switch-row">
<text class="field-label">弹窗显示一次</text>
<switch checked="{{form.popupEnabled}}" color="#f07e57" bindchange="handlePopupToggle"></switch>
</view>
<text class="field-label">业务跳转</text>
<view class="chip-row">
<block wx:for="{{businessTypeOptions}}" wx:key="value">
<view class="chip-btn {{form.businessType === item.value ? 'active' : ''}}" data-value="{{item.value}}" bindtap="handleBusinessTypeChange">{{item.label}}</view>
</block>
</view>
<input class="field-input" placeholder="业务 ID,可选" placeholder-class="placeholder" value="{{form.businessId}}" bindinput="handleBusinessIdInput"></input>
<input class="field-input" placeholder="跳转路径,如 /orders?orderId=123" placeholder-class="placeholder" value="{{form.actionPath}}" bindinput="handleActionPathInput"></input>
<view class="actions-row">
<button class="secondary-btn" bindtap="handleHideForm">取消</button>
<button class="primary-btn" loading="{{submitting}}" bindtap="handlePublish">发布</button>
</view>
</view>
<block wx:for="{{list}}" wx:key="id">
<view class="list-card">
<view class="list-head">
<view>
<text class="list-title">{{item.title}}</text>
<text class="list-subtitle">{{item.publisherName || '系统'}} · {{item.displayTime}}</text>
</view>
<text class="badge">{{item.popupText}}</text>
</view>
<text wx:if="{{item.content}}" class="message-content">{{item.content}}</text>
<view class="meta-grid">
<view class="meta-item">
<text class="meta-label">状态</text>
<text class="meta-value">{{item.statusText}}</text>
</view>
<view class="meta-item">
<text class="meta-label">目标</text>
<text class="meta-value">{{item.targetTypeName || item.targetType || '--'}}</text>
</view>
<view class="meta-item">
<text class="meta-label">业务类型</text>
<text class="meta-value">{{item.businessText}}</text>
</view>
<view class="meta-item">
<text class="meta-label">跳转</text>
<text class="meta-value">{{item.actionText}}</text>
</view>
</view>
<view wx:if="{{canRevoke && item.status === 1}}" class="card-actions">
<view class="mini-btn" data-id="{{item.id}}" bindtap="handleRevoke">撤回</view>
</view>
</view>
</block>
<view wx:if="{{!list.length && !loading}}" class="empty-state">
<text class="empty-title">暂无系统消息</text>
<text class="empty-desc">发布后的消息会显示弹窗配置和业务跳转信息。</text>
</view>
<view wx:if="{{loading}}" class="loading-line">加载中...</view>
</view>
</view>
@@ -0,0 +1,66 @@
@import "../../app.wxss";
.form-card {
display: flex;
flex-direction: column;
gap: 16rpx;
}
.field-label {
color: #f6efe7;
font-size: 24rpx;
font-weight: 700;
}
.field-textarea {
width: 100%;
min-height: 180rpx;
box-sizing: border-box;
padding: 18rpx 20rpx;
border-radius: 8rpx;
background: rgba(255, 255, 255, 0.08);
border: 1rpx solid rgba(255, 255, 255, 0.12);
color: #f6efe7;
font-size: 26rpx;
line-height: 1.6;
}
.switch-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 20rpx;
}
.option-hint {
color: #9fb3ad;
font-size: 22rpx;
line-height: 1.5;
}
.actions-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 16rpx;
margin-top: 8rpx;
}
.message-content {
display: block;
margin-top: 14rpx;
color: #9fb3ad;
font-size: 24rpx;
line-height: 1.6;
}
.card-actions {
display: flex;
justify-content: flex-end;
margin-top: 18rpx;
}
.loading-line {
color: #7e8f8c;
font-size: 24rpx;
text-align: center;
padding: 24rpx 0;
}