录音时长统调整后端接口

This commit is contained in:
zhonghua.li
2026-02-07 21:24:24 +08:00
parent 3944fa6969
commit b1c4f57c58
10 changed files with 436 additions and 52 deletions

View File

@@ -79,6 +79,9 @@
</template>
<script>
import { post } from '@/common/request.js'
import { getApiUrl } from '@/common/config.js'
// 获取系统信息的辅助函数
function getSystemInfo() {
// #ifdef MP-WEIXIN
@@ -133,17 +136,16 @@ export default {
timeRangeOptions: ['1个月', '3个月', '6个月'],
timeRangeIndex: 0, // 默认选择1个月
showTimeRangeDropdown: false, // 控制下拉菜单显示
totalDuration: '125.5 小时',
todayDuration: '8.2 小时',
dataList: [
{ date: '2024-02-07', description: '客户咨询录音', duration: '2.5 小时' },
{ date: '2024-02-06', description: '销售洽谈录音', duration: '3.2 小时' },
{ date: '2024-02-05', description: '培训录音', duration: '1.8 小时' },
{ date: '2024-02-04', description: '会议录音', duration: '4.1 小时' },
{ date: '2024-02-03', description: '客户反馈录音', duration: '2.9 小时' }
]
totalDuration: '0 小时',
todayDuration: '0 小时',
dataList: [],
loading: false
}
},
onLoad() {
// 页面加载时获取数据
this.loadData();
},
onReady() {
// 页面渲染完成后,查询实际导航栏高度并更新位置
this.$nextTick(() => {
@@ -240,19 +242,235 @@ export default {
onRefresh() {
// 刷新数据
this.loadData();
uni.showToast({
title: '刷新成功',
icon: 'success',
duration: 1500
});
},
loadData() {
// 根据 timeRangeIndex 加载对应时间范围的数据
// 0 -> 1个月, 1 -> 3个月, 2 -> 6个月
const months = this.timeRangeIndex === 0 ? 1 : (this.timeRangeIndex === 1 ? 3 : 6);
console.log(`加载最近${months}个月的数据`);
// TODO: 这里可以调用API加载数据
// 示例:调用接口获取数据
/**
* 获取当前登录用户信息
*/
getCurrentUserInfo() {
try {
const loginResponse = uni.getStorageSync('backend-login-response') || {};
return {
salesId: loginResponse.userId ? String(loginResponse.userId) : null,
salesPhone: loginResponse.phone || null
};
} catch (e) {
console.error('获取用户信息失败:', e);
return { salesId: null, salesPhone: null };
}
},
/**
* 格式化时长(分钟转小时)
*/
formatDuration(minutes) {
if (!minutes || minutes === 0) return '0 小时';
const hours = (minutes / 60).toFixed(1);
return `${hours} 小时`;
},
/**
* 格式化日期
*/
formatDate(dateStr) {
if (!dateStr) return '';
try {
const date = new Date(dateStr);
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
} catch (e) {
return dateStr;
}
},
/**
* 加载统计数据
*/
async loadData() {
// 防止重复请求
if (this.loading) {
return;
}
this.loading = true;
try {
// 获取当前用户信息
const userInfo = this.getCurrentUserInfo();
const { salesId, salesPhone } = userInfo;
// 验证:至少需要提供一个查询条件
if (!salesId && !salesPhone) {
uni.showToast({
title: '无法获取用户信息',
icon: 'none',
duration: 2000
});
this.loading = false;
return;
}
// 根据 timeRangeIndex 确定 dateRangeType
// 0 -> 1个月(1), 1 -> 3个月(2), 2 -> 6个月(3)
const dateRangeType = this.timeRangeIndex + 1;
// 构建查询参数(后端使用 @RequestParam参数需要作为 URL 查询参数传递)
const queryParams = {
dateRangeType: dateRangeType
};
// 添加销售人员ID或电话号码至少提供一个
if (salesId) {
queryParams.salesId = salesId;
}
if (salesPhone) {
queryParams.salesPhone = salesPhone;
}
// 将参数转换为 URL 查询字符串
const queryString = Object.keys(queryParams)
.filter(key => queryParams[key] !== null && queryParams[key] !== undefined && queryParams[key] !== '')
.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(queryParams[key])}`)
.join('&');
// 构建完整URL
const baseUrl = getApiUrl('/api/audio-statistics/sales');
const url = queryString ? `${baseUrl}?${queryString}` : baseUrl;
console.log('[RecordingDuration] 请求参数:', queryParams);
console.log('[RecordingDuration] 请求URL:', url);
// 显示加载提示
uni.showLoading({
title: '加载中...',
mask: true
});
// 发送POST请求参数已在URL的query string中data为空
const res = await post(url, {});
uni.hideLoading();
// 处理响应
if (res.statusCode === 200 && res.data) {
const result = res.data;
// 检查返回结果(根据项目中的返回结构)
if (!result.success) {
const errorMsg = result.msg || result.message || '查询失败';
uni.showToast({
title: errorMsg,
icon: 'none',
duration: 2000
});
this.loading = false;
return;
}
// 获取统计数据列表
const statisticsList = result.data || [];
console.log('[RecordingDuration] 返回数据:', statisticsList);
// 处理统计数据
this.processStatisticsData(statisticsList);
// 刷新成功后显示提示
uni.showToast({
title: '刷新成功',
icon: 'success',
duration: 1500
});
} else {
throw new Error('请求失败,状态码: ' + res.statusCode);
}
} catch (error) {
uni.hideLoading();
console.error('[RecordingDuration] 加载数据失败:', error);
uni.showToast({
title: error.message || '加载失败,请重试',
icon: 'none',
duration: 2000
});
} finally {
this.loading = false;
}
},
/**
* 处理统计数据
*/
processStatisticsData(statisticsList) {
if (!statisticsList || statisticsList.length === 0) {
// 没有数据时重置显示
this.totalDuration = '0 小时';
this.todayDuration = '0 小时';
this.dataList = [];
return;
}
// 计算总录音时长和总录音数量
let totalDurationMinutes = 0;
let totalRecordingCount = 0;
// 构建数据列表
const formattedList = statisticsList.map(item => {
// 累计总时长(使用销售人员录音总时长)
const salesDuration = item.salesTotalDuration || 0;
totalDurationMinutes += salesDuration;
totalRecordingCount += (item.salesRecordingCount || 0);
// 格式化日期
const date = this.formatDate(item.statisticsDate);
// 构建描述信息
let description = '';
if (item.salesName) {
description = `${item.salesName}的录音`;
} else if (item.dealershipName) {
description = `${item.dealershipName}的录音`;
} else {
description = '录音统计';
}
return {
date: date,
description: description,
duration: this.formatDuration(salesDuration),
// 保存原始数据,方便后续使用
rawData: item
};
});
// 按日期倒序排列(最新的在前)
formattedList.sort((a, b) => {
if (a.date > b.date) return -1;
if (a.date < b.date) return 1;
return 0;
});
// 更新页面数据
this.totalDuration = this.formatDuration(totalDurationMinutes);
this.dataList = formattedList;
// 计算今日录音时长(从统计数据中查找今天的记录)
const today = new Date();
const todayStr = this.formatDate(today.toISOString());
const todayItem = statisticsList.find(item => {
const itemDate = this.formatDate(item.statisticsDate);
return itemDate === todayStr;
});
if (todayItem) {
this.todayDuration = this.formatDuration(todayItem.salesTotalDuration || 0);
} else {
this.todayDuration = '0 小时';
}
console.log('[RecordingDuration] 数据处理完成:', {
totalDuration: this.totalDuration,
todayDuration: this.todayDuration,
dataListCount: this.dataList.length
});
}
}
}