/** * 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') } })