fix: resolve 8 bugs and add admin features
Miniprogram: - Profile "我的设备": show unbind option when device bound, scan when not - Add device status indicator (已绑定/未绑定) to profile menu Admin console: - Login: add @confirm to inputs so Enter key submits the form - Record detail: wire up 详情 link with modal showing full record info - Dashboard: add subscription stats row (月卡/年卡/试用/收入) - Subscription: add 取消 action for active subscriptions - Format: fix -8h timezone display for UTC ISO date strings Server: - POST /api/v1/admin/subscriptions/cancel: cancel active subscriptions - Dashboard API: include sub_stats (plan counts + monthly revenue) - IP extraction: add X-Forwarded-For fallback for logging
这个提交包含在:
@@ -23,6 +23,25 @@
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view class="stat-grid secondary-stats" v-if="subStats.monthly_count != null">
|
||||
<view class="stat-card">
|
||||
<text class="stat-value">{{ subStats.monthly_count || 0 }}</text>
|
||||
<text class="stat-label">月卡会员</text>
|
||||
</view>
|
||||
<view class="stat-card">
|
||||
<text class="stat-value">{{ subStats.yearly_count || 0 }}</text>
|
||||
<text class="stat-label">年卡会员</text>
|
||||
</view>
|
||||
<view class="stat-card">
|
||||
<text class="stat-value">{{ subStats.trial_count || 0 }}</text>
|
||||
<text class="stat-label">试用中</text>
|
||||
</view>
|
||||
<view class="stat-card">
|
||||
<text class="stat-value">¥{{ subStats.monthly_revenue || 0 }}</text>
|
||||
<text class="stat-label">本月收入</text>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view class="main-row">
|
||||
<view class="card table-card">
|
||||
<view class="card-header">
|
||||
@@ -91,6 +110,7 @@
|
||||
|
||||
<script>
|
||||
import { get } from '../../utils/request'
|
||||
import { formatDate as formatDateUtil } from '../../utils/format'
|
||||
import AdminLayout from '../../components/AdminLayout.vue'
|
||||
|
||||
export default {
|
||||
@@ -98,6 +118,7 @@ export default {
|
||||
data() {
|
||||
return {
|
||||
stats: {},
|
||||
subStats: {},
|
||||
recentTreatments: []
|
||||
}
|
||||
},
|
||||
@@ -117,11 +138,12 @@ export default {
|
||||
} catch (e) {
|
||||
this.recentTreatments = []
|
||||
}
|
||||
try {
|
||||
const subData = await get('/api/v1/admin/subscriptions', { page: 1, page_size: 1, tab: 'all' })
|
||||
if (subData.stats) this.subStats = subData.stats
|
||||
} catch (e) {}
|
||||
},
|
||||
formatDate(d) {
|
||||
if (!d) return '--'
|
||||
return String(d).slice(0, 19).replace('T', ' ')
|
||||
},
|
||||
formatDate: formatDateUtil,
|
||||
onNav(path) {
|
||||
uni.redirectTo({ url: path })
|
||||
}
|
||||
@@ -324,4 +346,8 @@ export default {
|
||||
font-size: 14px;
|
||||
color: #999;
|
||||
}
|
||||
|
||||
.secondary-stats {
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
</style>
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
v-model="form.username"
|
||||
placeholder="请输入管理员账号"
|
||||
placeholder-class="input-placeholder"
|
||||
@confirm="onLogin"
|
||||
/>
|
||||
</view>
|
||||
|
||||
@@ -23,6 +24,7 @@
|
||||
placeholder="请输入密码"
|
||||
:password="true"
|
||||
placeholder-class="input-placeholder"
|
||||
@confirm="onLogin"
|
||||
/>
|
||||
</view>
|
||||
|
||||
|
||||
@@ -45,7 +45,7 @@
|
||||
<text class="t-td flex1">{{ Math.floor((item.total_duration_ms || 0) / 60000) }}分</text>
|
||||
<text class="t-td flex2">{{ formatDate(item.start_time) }}</text>
|
||||
<text class="t-td flex1">
|
||||
<text class="action-link">详情</text>
|
||||
<text class="action-link" @click="onDetail(item)">详情</text>
|
||||
</text>
|
||||
</view>
|
||||
</view>
|
||||
@@ -127,6 +127,22 @@ export default {
|
||||
}
|
||||
return names.join(',') || '-'
|
||||
},
|
||||
onDetail(item) {
|
||||
var regions = this.formatRegions(item.regions)
|
||||
var mode = item.mode === 1 ? '智能模式' : '普通模式'
|
||||
var duration = Math.floor((item.total_duration_ms || 0) / 60000)
|
||||
var content = '用户: ' + (item.nickname || item.user_id || '-') +
|
||||
'\n设备: ' + (item.device_id || '-') +
|
||||
'\n模式: ' + mode +
|
||||
'\n区域: ' + regions +
|
||||
'\n时长: ' + duration + '分钟' +
|
||||
'\n时间: ' + this.formatDate(item.start_time)
|
||||
uni.showModal({
|
||||
title: '护理记录详情 #' + (item.record_id || ''),
|
||||
content: content,
|
||||
showCancel: false
|
||||
})
|
||||
},
|
||||
formatDate: formatDateUtil,
|
||||
async onExport() {
|
||||
try {
|
||||
|
||||
@@ -62,6 +62,7 @@
|
||||
<text class="t-td flex1">
|
||||
<text class="action-link" @click="onDetail(item)">详情</text>
|
||||
<text class="action-link" v-if="item.status === 1" @click="onExtend(item)">延期</text>
|
||||
<text class="action-link" v-if="item.status === 1" @click="onCancel(item)">取消</text>
|
||||
<text class="action-link" v-if="item.status === 2 || item.status === 3" @click="onRenew(item)">续费</text>
|
||||
</text>
|
||||
</view>
|
||||
@@ -193,6 +194,26 @@ export default {
|
||||
}
|
||||
})
|
||||
},
|
||||
onCancel(item) {
|
||||
var self = this
|
||||
uni.showModal({
|
||||
title: '取消订阅',
|
||||
content: '确定要取消用户 #' + item.user_id + ' 的订阅吗?',
|
||||
success: async function (res) {
|
||||
if (res.confirm) {
|
||||
try {
|
||||
await post('/api/v1/admin/subscriptions/cancel', {
|
||||
subscription_id: item.subscription_id
|
||||
})
|
||||
uni.showToast({ title: '已取消', icon: 'success' })
|
||||
self.loadSubscriptions()
|
||||
} catch (e) {
|
||||
uni.showToast({ title: '操作失败', icon: 'none' })
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
onRenew(item) {
|
||||
this.createForm.user_id = String(item.user_id || '')
|
||||
this.createForm.plan = item.plan || 'monthly'
|
||||
|
||||
@@ -1,9 +1,21 @@
|
||||
export function formatDate(d) {
|
||||
if (!d) return '--'
|
||||
if (d instanceof Date || (typeof d === 'string' && d.includes('T') && d.endsWith('Z'))) {
|
||||
var date = new Date(d)
|
||||
if (isNaN(date.getTime())) return '--'
|
||||
var pad = function(n) { return String(n).padStart(2, '0') }
|
||||
return date.getFullYear() + '-' + pad(date.getMonth() + 1) + '-' + pad(date.getDate()) + ' ' + pad(date.getHours()) + ':' + pad(date.getMinutes()) + ':' + pad(date.getSeconds())
|
||||
}
|
||||
return String(d).slice(0, 19).replace('T', ' ')
|
||||
}
|
||||
|
||||
export function formatDateShort(d) {
|
||||
if (!d) return '--'
|
||||
if (d instanceof Date || (typeof d === 'string' && d.includes('T') && d.endsWith('Z'))) {
|
||||
var date = new Date(d)
|
||||
if (isNaN(date.getTime())) return '--'
|
||||
var pad = function(n) { return String(n).padStart(2, '0') }
|
||||
return date.getFullYear() + '-' + pad(date.getMonth() + 1) + '-' + pad(date.getDate())
|
||||
}
|
||||
return String(d).slice(0, 10)
|
||||
}
|
||||
|
||||
@@ -31,7 +31,32 @@ Page({
|
||||
},
|
||||
|
||||
onManageDevice: function () {
|
||||
wx.navigateTo({ url: '/pages/scan/scan' })
|
||||
var self = this
|
||||
if (this.data.deviceCount > 0) {
|
||||
wx.showActionSheet({
|
||||
itemList: ['解绑当前设备'],
|
||||
success: function (res) {
|
||||
if (res.tapIndex === 0) {
|
||||
wx.showModal({
|
||||
title: '确认解绑',
|
||||
content: '解绑后将无法使用该设备,确定要解绑吗?',
|
||||
success: function (modalRes) {
|
||||
if (modalRes.confirm) {
|
||||
http.post('/api/v1/device/unbind', {}).then(function () {
|
||||
wx.showToast({ title: '已解绑', icon: 'success' })
|
||||
self.loadProfile()
|
||||
}).catch(function (err) {
|
||||
wx.showToast({ title: err.message || '解绑失败', icon: 'none' })
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
} else {
|
||||
wx.navigateTo({ url: '/pages/scan/scan' })
|
||||
}
|
||||
},
|
||||
|
||||
onViewSubscription: function () {
|
||||
|
||||
@@ -20,6 +20,8 @@
|
||||
<view class="menu-item" bindtap="onManageDevice">
|
||||
<view class="menu-icon">📱</view>
|
||||
<view class="menu-text">我的设备</view>
|
||||
<text class="menu-device-status" wx:if="{{deviceCount > 0}}">已绑定</text>
|
||||
<text class="menu-device-status menu-device-unbound" wx:else>未绑定</text>
|
||||
<text class="menu-arrow">›</text>
|
||||
</view>
|
||||
<view class="menu-item" bindtap="onViewHistory">
|
||||
|
||||
@@ -98,6 +98,15 @@
|
||||
font-size: 32rpx;
|
||||
}
|
||||
|
||||
.menu-device-status {
|
||||
font-size: 24rpx;
|
||||
color: #52c41a;
|
||||
margin-right: 8rpx;
|
||||
}
|
||||
.menu-device-unbound {
|
||||
color: #999;
|
||||
}
|
||||
|
||||
.logout-btn {
|
||||
text-align: center;
|
||||
color: #ff4d4f;
|
||||
|
||||
@@ -38,7 +38,7 @@ function createContext(event) {
|
||||
query: parseQuery(event),
|
||||
body: parseBody(event),
|
||||
params: {},
|
||||
ip: event.requestContext && event.requestContext.sourceIp || ''
|
||||
ip: event.requestContext && event.requestContext.sourceIp || (event.headers && (event.headers['x-forwarded-for'] || event.headers['X-Forwarded-For'] || '').split(',')[0].trim()) || ''
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+24
-1
@@ -59,7 +59,19 @@ function register(router) {
|
||||
query('SELECT COUNT(*) AS total FROM treatment_records', {}),
|
||||
query('SELECT COUNT(*) AS total FROM subscriptions WHERE status = 1 AND expire_time > NOW()', {})
|
||||
])
|
||||
return ok({ device_count: rows[0][0].total, user_count: rows[1][0].total, treatment_count: rows[2][0].total, subscription_count: rows[3][0].total })
|
||||
const subStats = await query('SELECT ' +
|
||||
'SUM(CASE WHEN plan = \'monthly\' AND status = 1 AND expire_time > NOW() THEN 1 ELSE 0 END) AS monthly_count, ' +
|
||||
'SUM(CASE WHEN plan = \'yearly\' AND status = 1 AND expire_time > NOW() THEN 1 ELSE 0 END) AS yearly_count, ' +
|
||||
'SUM(CASE WHEN plan = \'trial\' AND status = 1 AND expire_time > NOW() THEN 1 ELSE 0 END) AS trial_count, ' +
|
||||
'COALESCE(SUM(CASE WHEN MONTH(start_time) = MONTH(NOW()) AND YEAR(start_time) = YEAR(NOW()) THEN amount ELSE 0 END), 0) AS monthly_revenue ' +
|
||||
'FROM subscriptions', {})
|
||||
return ok({
|
||||
device_count: rows[0][0].total,
|
||||
user_count: rows[1][0].total,
|
||||
treatment_count: rows[2][0].total,
|
||||
subscription_count: rows[3][0].total,
|
||||
sub_stats: subStats[0] || {}
|
||||
})
|
||||
})
|
||||
|
||||
router.get('/api/v1/admin/devices', async ctx => {
|
||||
@@ -246,6 +258,17 @@ function register(router) {
|
||||
return ok({ message: 'success' })
|
||||
})
|
||||
|
||||
router.post('/api/v1/admin/subscriptions/cancel', async ctx => {
|
||||
const admin = await requireAdmin(ctx)
|
||||
if (!admin) return fail(1002, '未授权,请重新登录')
|
||||
const subscriptionId = ctx.body.subscription_id
|
||||
if (!subscriptionId) return fail(2001, 'subscription_id required')
|
||||
const result = await query('UPDATE subscriptions SET status = 3 WHERE subscription_id = :subscription_id AND status = 1', { subscription_id: subscriptionId })
|
||||
if (result.affectedRows === 0) return fail(2001, '未找到有效订阅')
|
||||
await writeLog({ admin_id: admin.admin_id, action: 'subscription_cancel', detail: '取消订阅 #' + subscriptionId, ip: ctx.ip })
|
||||
return ok({ message: 'success' })
|
||||
})
|
||||
|
||||
router.get('/api/v1/admin/records', async ctx => {
|
||||
const admin = await requireAdmin(ctx)
|
||||
if (!admin) return fail(1002, '未授权,请重新登录')
|
||||
|
||||
在新工单中引用
屏蔽一个用户