This commit is contained in:
Felix
2025-12-03 11:15:04 +08:00
parent cc46cfbd79
commit 4722978753
4 changed files with 188 additions and 37 deletions

View File

@@ -573,26 +573,6 @@
.overlay-word.show .overlay-text { font-weight: 600; }
.overlay-word.zoom { transform-origin: center center; transition: transform 500ms ease; }
.page-loading-mask {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: transparent;
z-index: 1000;
}
.recording-mask {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: transparent;
z-index: 99;
}
.bottom-button {
padding: 20rpx;
border-radius: 50%;

View File

@@ -51,7 +51,8 @@ Page({
photoExpandTransition: '',
showExpandLayer: false,
photoExpandCurrentWidth: 0,
expandBorderStyle: ''
expandBorderStyle: '',
shouldResetOnReturn: false
},
onLoad() {
@@ -111,9 +112,12 @@ Page({
},
onShow() {
// 每次显示页面时检查登录状态和清理处理状态
this.updateLoginStatus()
this.setData({ isProcessing: false }) // 清理处理状态
this.setData({ isProcessing: false })
if (this.data.shouldResetOnReturn) {
this.resetPageState()
this.setData({ shouldResetOnReturn: false })
}
},
// 处理页面滚动
@@ -683,10 +687,10 @@ Page({
wx.hideLoading()
this.setData({ photoPath: imagePath, takePhoto: true })
console.log('拍照成功:', imagePath)
try { wx.nextTick(() => { this.scheduleExpandTransform() }) } catch (err) {}
// try { wx.nextTick(() => { this.scheduleExpandTransform() }) } catch (err) {}
// 直接跳转到结果页面
// this.navigateToResult(imagePath)
this.navigateToResult(imagePath)
} catch (error: any) {
wx.hideLoading()
@@ -719,10 +723,10 @@ Page({
this.setData({ photoPath: imagePath, takePhoto: true })
console.log('选择图片成功:', imagePath)
try { wx.nextTick(() => { this.scheduleExpandTransform() }) } catch (err) {}
// try { wx.nextTick(() => { this.scheduleExpandTransform() }) } catch (err) {}
// 直接跳转到结果页面
// this.navigateToResult(imagePath)
this.navigateToResult(imagePath)
} catch (error: any) {
wx.hideLoading()
@@ -748,9 +752,9 @@ Page({
const safeBottom: number = (win.safeArea && win.safeArea.bottom) || 0
const safeHeight: number = (win.safeArea && (win.safeArea.height || (safeBottom - safeTop))) || ((win.windowHeight || sys.windowHeight || 667) - statusBar)
const safeWidth: number = (win.safeArea && (win.safeArea.width || (win.windowWidth || vw))) || (win.windowWidth || vw)
const targetWidth = Math.floor(safeWidth * 0.8)
const targetWidth = Math.floor(safeWidth * 0.72)
// 等待 photo-wrapper 动画完成后再读取其最终位置
this.setData({ showExpandLayer: false, expandBorderStyle: '' })
this.setData({ showExpandLayer: false, expandBorderStyle: 'opacity: 0;' })
const waitMs = 4500
setTimeout(() => {
const query2 = wx.createSelectorQuery().in(this as any)
@@ -776,7 +780,7 @@ Page({
photoExpandTransition: 'transition: transform 0ms',
showExpandLayer: true,
photoExpandCurrentWidth: Math.round(rect.width),
expandBorderStyle: 'opacity: 1;'
expandBorderStyle: 'opacity: 0;'
})
setTimeout(() => {
this.setData({ photoExpandTransition: 'transition: transform 900ms ease-in-out' })
@@ -787,21 +791,50 @@ Page({
} catch (e) {}
},
resetPageState() {
this.setData({
takePhoto: false,
photoPath: '',
showExpandLayer: false,
photoExpandTransform: '',
photoExpandTransition: '',
photoExpandCurrentWidth: 0,
expandBorderStyle: ''
})
},
// 跳转到结果页面
navigateToResult(imagePath: string) {
async navigateToResult(imagePath: string) {
try {
console.log('跳转到结果页面:', imagePath)
try { wx.nextTick(() => { this.scheduleExpandTransform() }) } catch (err) {}
// 跳转到结果页面并传递图片路径
wx.navigateTo({
url: `/pages/result/result?imagePath=${encodeURIComponent(imagePath)}`
})
// wx.navigateTo({
// url: `/pages/result/result?imagePath=${encodeURIComponent(imagePath)}`
// })
// const compressedImagePath = await imageManager.compressImage(this.data.imagePath, {
// quality: 80,
// maxWidth: 1200,
// maxHeight: 1200
// })
// const result = await apiManager.uploadImage(compressedImagePath)
const result = await apiManager.uploadImage(imagePath)
if (result.image_id) {
this.setData({ shouldResetOnReturn: true })
wx.navigateTo({
url: `/pages/assessment/assessment?imageId=${result.image_id}`
})
}
} catch (error) {
console.error('跳转结果页面失败:', error)
this.setData({ isProcessing: false })
this.resetPageState()
wx.showToast({
title: '操作失败',
title: '上传失败',
icon: 'none'
})
}

View File

@@ -60,7 +60,7 @@
<view class="btn"></view>
<view class="bottom"></view>
</view>
<view class="photo-wrapper">
<view class="photo-wrapper" wx:if="{{(todaySummary.length == 0 && groupedHistory.length == 0) || takePhoto}}">
<view class="photo">
<view class="photo-inner">
<image class="photo-image" src="{{takePhoto ? photoPath : photoSvgData}}" mode="{{takePhoto ? 'widthFix' : 'aspectFit'}}"></image>

View File

@@ -793,6 +793,140 @@ class ApiManager {
})
}
// 上传文件第一步上传文件获取ID
async uploadImageFile(filePath: string, retryCount: number = 0): Promise<string> {
const maxRetries = 1 // 最多重试1次
return new Promise(async (resolve, reject) => {
console.log('上传文件请求:', { filePath, retryCount })
wx.uploadFile({
url: `${BASE_URL}/api/v1/file/upload_image`,
filePath,
name: 'file', // 根据API要求调整参数名
header: this.getHeaders(),
success: async (res) => {
console.log('上传文件响应:', {
statusCode: res.statusCode,
data: res.data,
retryCount
})
if (res.statusCode === 200) {
try {
const response = JSON.parse(res.data) as IApiResponse<{ id: string }>
if (response.code === 0 || response.code === 200) {
if (response.data?.id) {
console.log('文件上传成功获得ID:', response.data.id)
resolve(response.data.id)
} else {
reject(new Error('服务器返回数据中缺少文件ID'))
}
} else {
const errorMsg = response.message || response.msg || '文件上传失败'
console.error('文件上传业务错误:', errorMsg, response)
wx.showToast({
title: errorMsg,
icon: 'none'
})
reject(new Error(errorMsg))
}
} catch (error) {
console.error('文件上传响应解析错误:', error)
wx.showToast({
title: '数据解析错误',
icon: 'none'
})
reject(error)
}
} else if (res.statusCode === 401) {
console.warn('文件上传401认证失败尝试自动登录', { retryCount, maxRetries })
// 如果还有重试机会,尝试自动登录并重试请求
if (retryCount < maxRetries) {
try {
console.log('开始文件上传401自动登录重试')
// 清理过期的认证数据
this.clearAuthData()
// 进行智能登录
const loginResult = await this.smartLogin(true) // 强制刷新token
if (loginResult) {
console.log('文件上传401自动登录成功重试请求')
// 登录成功后重试原请求
try {
const retryResult = await this.uploadFile(filePath, retryCount + 1)
resolve(retryResult)
} catch (retryError) {
reject(retryError)
}
} else {
console.error('文件上传401自动登录失败')
this.handleTokenExpired()
reject(new Error('登录已过期'))
}
} catch (loginError) {
console.error('文件上传401自动登录发生错误:', loginError)
this.handleTokenExpired()
reject(new Error('登录已过期'))
}
} else {
// 重试次数耗尽直接处理token过期
console.error('文件上传401重试次数耗尽跳转登录页')
this.handleTokenExpired()
reject(new Error('登录已过期'))
}
} else {
// Check for 413 Request Entity Too Large error
if (res.statusCode === 413) {
// Get file size for debugging
const fs = wx.getFileSystemManager();
fs.getFileInfo({
filePath: filePath,
success: (infoRes) => {
const fileSizeInMB = (infoRes.size / (1024 * 1024)).toFixed(4);
console.error('文件上传413错误 - 请求实体过大:', {
statusCode: res.statusCode,
fileSize: `${fileSizeInMB} MB`,
fileSizeInBytes: infoRes.size,
filePath: filePath,
data: res.data
});
},
fail: (infoError) => {
console.error('文件上传413错误 - 无法获取文件大小:', {
statusCode: res.statusCode,
filePath: filePath,
fileInfoError: infoError,
data: res.data
});
}
});
} else {
console.error('文件上传HTTP错误:', res.statusCode, res.data);
}
wx.showToast({
title: '文件上传失败',
icon: 'none'
});
reject(new Error(`HTTP ${res.statusCode}`));
}
},
fail: (error) => {
console.error('文件上传请求失败:', error)
wx.showToast({
title: '文件上传失败',
icon: 'none'
})
reject(error)
}
})
})
}
// 图片识别第二步通过文件ID进行识别
async recognizeImageAsync(fileId: string, type: string = 'word'): Promise<{task_id: string, status: string, message: string}> {
console.log('开始图片识别请求:', { fileId, type })
@@ -828,7 +962,7 @@ class ApiManager {
console.log('开始图片上传和识别流程:', { filePath, type })
// 第一步上传文件获取ID
const fileId = await this.uploadFile(filePath)
const fileId = await this.uploadImageFile(filePath)
// 更新加载提示
// wx.showLoading({ title: '识别中...' })
@@ -846,6 +980,10 @@ class ApiManager {
recognitionResult = res
break
}
if (res.status === 'failed') {
recognitionResult = res
throw new Error(recognitionResult.error_message || '图片识别失败')
}
await new Promise(resolve => setTimeout(resolve, 3000)) // 2秒轮询一次
} catch (error) {
console.error('轮询获取识别结果失败:', error)