重构项目文件夹目录

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,226 @@
/**
* Purpose: auditApi reads immutable high-risk operation audit logs for admin review.
* Usage: call listLogs with filter and pagination params from this page.
*/
const auditApi = require('../../api/security-audit')
/**
* Purpose: navigation centralizes page transitions and fallback back behavior.
* Usage: call goBack when the custom navigation bar back event fires.
*/
const navigation = require('../../utils/navigation')
/**
* Purpose: format prepares backend timestamps for compact mini-program display.
* Usage: call formatDateTime before assigning audit rows to page data.
*/
const format = require('../../utils/format')
/**
* Purpose: RESULT_OPTIONS drives the audit result segmented filter.
* Usage: keep values aligned with backend result strings accepted by /admin/security-audit/logs.
*/
const RESULT_OPTIONS = [
{ label: '全部', value: '' },
{ label: '成功', value: 'SUCCESS' },
{ label: '失败', value: 'FAIL' }
]
Page({
/**
* Purpose: data stores filter, pagination, and rendered audit rows for the read-only audit page.
* Usage: WXML binds these fields and page methods update them through setData.
*/
data: {
/**
* Purpose: loading indicates whether an audit-log query is currently in flight.
* Usage: bind it to the search button and bottom loader to prevent duplicate requests.
*/
loading: false,
/**
* Purpose: list holds formatted audit log rows returned by the backend.
* Usage: append or replace it after listLogs responses.
*/
list: [],
/**
* Purpose: page tracks the current backend page number.
* Usage: increment it only after a successful load-more response.
*/
page: 1,
/**
* Purpose: size controls how many audit rows each request asks for.
* Usage: pass it unchanged to listLogs so paging remains stable.
*/
size: 10,
/**
* Purpose: hasMore tells the page whether another backend page is available.
* Usage: set it from response totals and guard onReachBottom.
*/
hasMore: true,
/**
* Purpose: account filters audit logs by operator account.
* Usage: bind it to the search input and pass non-empty values to the API.
*/
account: '',
/**
* Purpose: action filters audit logs by action code or action keyword.
* Usage: bind it to the search input and pass non-empty values to the API.
*/
action: '',
/**
* Purpose: result filters audit logs by success or failure status.
* Usage: update it from result chips and pass it to the backend query.
*/
result: '',
/**
* Purpose: resultOptions renders available result filter chips.
* Usage: iterate it in WXML and compare each option value with data.result.
*/
resultOptions: RESULT_OPTIONS
},
/**
* Purpose: onLoad performs the initial audit-log query for authorized users.
* Usage: mini-program runtime calls it once when the page is opened from Manage.
*/
onLoad() {
this.loadLogs(true)
},
/**
* Purpose: onPullDownRefresh reloads the first audit-log page after manual refresh.
* Usage: mini-program runtime calls it and the method stops the refresh animation after completion.
*/
async onPullDownRefresh() {
await this.loadLogs(true)
wx.stopPullDownRefresh()
},
/**
* Purpose: onReachBottom loads the next page when the operator scrolls to the bottom.
* Usage: mini-program runtime calls it and the method respects loading and hasMore guards.
*/
onReachBottom() {
if (!this.data.loading && this.data.hasMore) {
this.loadLogs(false)
}
},
/**
* Purpose: buildQuery creates a clean backend query object from page filters and pagination.
* Usage: call it before listLogs so empty filters are not sent.
*/
buildQuery(page) {
const query = {
page,
size: this.data.size
}
;['account', 'action', 'result'].forEach(key => {
const value = String(this.data[key] || '').trim()
if (value) {
query[key] = value
}
})
return query
},
/**
* Purpose: loadLogs queries audit logs and updates list pagination state.
* Usage: pass true for a fresh search and false for infinite-scroll loading.
*/
async loadLogs(reset) {
if (this.data.loading) {
return
}
const nextPage = reset ? 1 : this.data.page + 1
this.setData({ loading: true })
try {
const response = await auditApi.listLogs(this.buildQuery(nextPage))
const records = Array.isArray(response && response.records)
? response.records
: (Array.isArray(response && response.list) ? response.list : [])
const total = Number(response && response.total) || 0
const currentList = reset ? [] : this.data.list
const list = currentList.concat(records.map(item => this.formatLog(item)))
this.setData({
list,
page: nextPage,
hasMore: total ? list.length < total : records.length >= this.data.size
})
} catch (error) {
wx.showToast({
title: error.message || '审计日志加载失败',
icon: 'none'
})
} finally {
this.setData({ loading: false })
}
},
/**
* Purpose: formatLog normalizes one backend audit row for compact display.
* Usage: call it on every record returned from listLogs before rendering.
*/
formatLog(item) {
return {
...item,
displayTime: format.formatDateTime(item.operateTime || item.createTime),
resultText: item.result === 'SUCCESS' ? '成功' : '失败',
resultClass: item.result === 'SUCCESS' ? 'success' : 'fail',
operatorText: item.account || item.operatorAccount || '--',
reasonText: item.reason || item.failureReason || item.errorMessage || item.remark || ''
}
},
/**
* Purpose: handleAccountInput updates the operator-account filter as the input changes.
* Usage: bind it to the account input field.
*/
handleAccountInput(event) {
this.setData({ account: event.detail.value })
},
/**
* Purpose: handleActionInput updates the action-code filter as the input changes.
* Usage: bind it to the action input field.
*/
handleActionInput(event) {
this.setData({ action: event.detail.value })
},
/**
* Purpose: handleResultChange switches between all, success, and fail filters.
* Usage: bind it to result chips with data-result.
*/
handleResultChange(event) {
this.setData({ result: event.currentTarget.dataset.result || '' })
this.loadLogs(true)
},
/**
* Purpose: handleSearch reloads logs using the current text filters.
* Usage: bind it to keyboard confirm and the search button.
*/
handleSearch() {
this.loadLogs(true)
},
/**
* Purpose: handleReset clears filters and reloads the first audit-log page.
* Usage: bind it to the reset button in the filter card.
*/
handleReset() {
this.setData({
account: '',
action: '',
result: ''
})
this.loadLogs(true)
},
/**
* Purpose: handleBack returns to Manage when this page is opened as a standalone stack page.
* 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,64 @@
<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">Security Audit</text>
<text class="hero-title">高危操作留痕</text>
<text class="hero-desc">按账号、动作和结果查看后台关键操作记录,失败原因会保留在详情行中。</text>
</view>
<view class="surface-card">
<text class="section-title">筛选</text>
<input class="field-input" placeholder="操作者账号" placeholder-class="placeholder" value="{{account}}" bindinput="handleAccountInput" confirm-type="search" bindconfirm="handleSearch"></input>
<input class="field-input" placeholder="动作编码" placeholder-class="placeholder" value="{{action}}" bindinput="handleActionInput" confirm-type="search" bindconfirm="handleSearch"></input>
<view class="chip-row">
<block wx:for="{{resultOptions}}" wx:key="value">
<view class="chip-btn {{result === item.value ? 'active' : ''}}" data-result="{{item.value}}" bindtap="handleResultChange">{{item.label}}</view>
</block>
</view>
<view class="actions-row">
<button class="secondary-btn" bindtap="handleReset">重置</button>
<button class="primary-btn" loading="{{loading}}" bindtap="handleSearch">查询</button>
</view>
</view>
<block wx:for="{{list}}" wx:key="id">
<view class="list-card">
<view class="list-head">
<view>
<text class="list-title">{{item.action || '--'}}</text>
<text class="list-subtitle">{{item.operatorText}} · {{item.displayTime}}</text>
</view>
<text class="audit-badge {{item.resultClass}}">{{item.resultText}}</text>
</view>
<view class="meta-grid">
<view class="meta-item">
<text class="meta-label">业务类型</text>
<text class="meta-value">{{item.businessType || '--'}}</text>
</view>
<view class="meta-item">
<text class="meta-label">业务 ID</text>
<text class="meta-value">{{item.businessId || '--'}}</text>
</view>
<view class="meta-item">
<text class="meta-label">IP</text>
<text class="meta-value">{{item.ipAddress || '--'}}</text>
</view>
<view class="meta-item">
<text class="meta-label">原因</text>
<text class="meta-value">{{item.reasonText || '无'}}</text>
</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 wx:if="{{list.length && !hasMore}}" class="loading-line">已加载全部</view>
</view>
</view>
@@ -0,0 +1,36 @@
@import "../../app.wxss";
.field-input {
margin-top: 16rpx;
}
.actions-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 16rpx;
margin-top: 20rpx;
}
.audit-badge {
flex-shrink: 0;
padding: 8rpx 14rpx;
border-radius: 8rpx;
font-size: 22rpx;
font-weight: 700;
}
.audit-badge.success {
color: #73d99f;
background: rgba(115, 217, 159, 0.12);
}
.audit-badge.fail {
color: #ff9b8b;
background: rgba(255, 155, 139, 0.14);
}
.loading-line {
color: #7e8f8c;
font-size: 24rpx;
text-align: center;
padding: 24rpx 0;
}