297 lines
8.0 KiB
JavaScript
297 lines
8.0 KiB
JavaScript
|
|
/**
|
|||
|
|
* 头像上传调试脚本
|
|||
|
|
* 用于分析和解决头像上传问题
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
// 调试配置
|
|||
|
|
const DEBUG_CONFIG = {
|
|||
|
|
baseUrl: 'http://192.168.2.143:4601',
|
|||
|
|
uploadUrl: '/app/user/avatar',
|
|||
|
|
timeout: 60000
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 调试工具类
|
|||
|
|
class UploadDebugger {
|
|||
|
|
constructor() {
|
|||
|
|
this.logs = []
|
|||
|
|
this.testResults = []
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 添加日志
|
|||
|
|
addLog(type, message, data = null) {
|
|||
|
|
const log = {
|
|||
|
|
type,
|
|||
|
|
message,
|
|||
|
|
data,
|
|||
|
|
timestamp: new Date().toISOString()
|
|||
|
|
}
|
|||
|
|
this.logs.push(log)
|
|||
|
|
console.log(`[${log.timestamp}] ${type.toUpperCase()}: ${message}`, data || '')
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 检查token
|
|||
|
|
checkToken() {
|
|||
|
|
try {
|
|||
|
|
const token = uni.getStorageSync('token')
|
|||
|
|
const result = {
|
|||
|
|
exists: !!token,
|
|||
|
|
length: token ? token.length : 0,
|
|||
|
|
preview: token ? token.substring(0, 20) + '...' : 'none',
|
|||
|
|
valid: this.validateToken(token)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
this.addLog('info', 'Token检查完成', result)
|
|||
|
|
return result
|
|||
|
|
} catch (error) {
|
|||
|
|
this.addLog('error', 'Token检查失败', error)
|
|||
|
|
return { exists: false, error: error.message }
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 验证token格式
|
|||
|
|
validateToken(token) {
|
|||
|
|
if (!token) return false
|
|||
|
|
|
|||
|
|
// 检查是否是JWT格式
|
|||
|
|
const parts = token.split('.')
|
|||
|
|
if (parts.length === 3) {
|
|||
|
|
try {
|
|||
|
|
// 尝试解码payload部分
|
|||
|
|
const payload = JSON.parse(atob(parts[1]))
|
|||
|
|
return payload && payload.exp && payload.exp > Date.now() / 1000
|
|||
|
|
} catch (e) {
|
|||
|
|
return false
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return true // 如果不是JWT格式,假设是其他格式的token
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 检查网络连接
|
|||
|
|
checkNetwork() {
|
|||
|
|
return new Promise((resolve) => {
|
|||
|
|
uni.getNetworkType({
|
|||
|
|
success: (res) => {
|
|||
|
|
this.addLog('info', '网络状态检查', res)
|
|||
|
|
resolve(res)
|
|||
|
|
},
|
|||
|
|
fail: (err) => {
|
|||
|
|
this.addLog('error', '网络状态检查失败', err)
|
|||
|
|
resolve({ networkType: 'unknown', error: err })
|
|||
|
|
}
|
|||
|
|
})
|
|||
|
|
})
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 检查服务器连接
|
|||
|
|
checkServerConnection() {
|
|||
|
|
return new Promise((resolve) => {
|
|||
|
|
uni.request({
|
|||
|
|
url: DEBUG_CONFIG.baseUrl + '/health', // 假设有健康检查接口
|
|||
|
|
method: 'GET',
|
|||
|
|
timeout: 5000,
|
|||
|
|
success: (res) => {
|
|||
|
|
this.addLog('info', '服务器连接检查成功', res)
|
|||
|
|
resolve({ connected: true, status: res.statusCode })
|
|||
|
|
},
|
|||
|
|
fail: (err) => {
|
|||
|
|
this.addLog('error', '服务器连接检查失败', err)
|
|||
|
|
resolve({ connected: false, error: err })
|
|||
|
|
}
|
|||
|
|
})
|
|||
|
|
})
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 测试文件上传
|
|||
|
|
async testFileUpload(filePath) {
|
|||
|
|
this.addLog('info', '开始文件上传测试', { filePath })
|
|||
|
|
|
|||
|
|
try {
|
|||
|
|
// 检查文件信息
|
|||
|
|
const fileInfo = await this.getFileInfo(filePath)
|
|||
|
|
this.addLog('info', '文件信息', fileInfo)
|
|||
|
|
|
|||
|
|
// 检查文件大小
|
|||
|
|
if (fileInfo.size > 10 * 1024 * 1024) { // 10MB
|
|||
|
|
this.addLog('warn', '文件过大,可能影响上传', { size: fileInfo.size })
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 执行上传
|
|||
|
|
const result = await this.performUpload(filePath)
|
|||
|
|
this.addLog('info', '上传测试完成', result)
|
|||
|
|
|
|||
|
|
return result
|
|||
|
|
} catch (error) {
|
|||
|
|
this.addLog('error', '上传测试失败', error)
|
|||
|
|
throw error
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 获取文件信息
|
|||
|
|
getFileInfo(filePath) {
|
|||
|
|
return new Promise((resolve, reject) => {
|
|||
|
|
uni.getFileInfo({
|
|||
|
|
filePath: filePath,
|
|||
|
|
success: resolve,
|
|||
|
|
fail: reject
|
|||
|
|
})
|
|||
|
|
})
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 执行上传
|
|||
|
|
performUpload(filePath) {
|
|||
|
|
return new Promise((resolve, reject) => {
|
|||
|
|
const token = uni.getStorageSync('token')
|
|||
|
|
|
|||
|
|
// 构建请求头
|
|||
|
|
let authorization = token
|
|||
|
|
// #ifdef H5
|
|||
|
|
authorization = token ? `Bearer ${token}` : ''
|
|||
|
|
// #endif
|
|||
|
|
|
|||
|
|
const header = {
|
|||
|
|
Authorization: authorization
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
this.addLog('info', '开始上传请求', {
|
|||
|
|
url: DEBUG_CONFIG.baseUrl + DEBUG_CONFIG.uploadUrl,
|
|||
|
|
header: header,
|
|||
|
|
filePath: filePath
|
|||
|
|
})
|
|||
|
|
|
|||
|
|
uni.uploadFile({
|
|||
|
|
url: DEBUG_CONFIG.baseUrl + DEBUG_CONFIG.uploadUrl,
|
|||
|
|
filePath: filePath,
|
|||
|
|
name: 'avatarfile',
|
|||
|
|
header: header,
|
|||
|
|
timeout: DEBUG_CONFIG.timeout,
|
|||
|
|
success: (res) => {
|
|||
|
|
this.addLog('info', '上传响应', {
|
|||
|
|
statusCode: res.statusCode,
|
|||
|
|
header: res.header,
|
|||
|
|
data: res.data
|
|||
|
|
})
|
|||
|
|
|
|||
|
|
try {
|
|||
|
|
const data = JSON.parse(res.data)
|
|||
|
|
if (res.statusCode === 200 && data.code === 200) {
|
|||
|
|
resolve({ success: true, data: data })
|
|||
|
|
} else {
|
|||
|
|
reject({ success: false, error: data.msg || '上传失败', data: data })
|
|||
|
|
}
|
|||
|
|
} catch (parseError) {
|
|||
|
|
reject({ success: false, error: '响应解析失败', parseError: parseError })
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
fail: (err) => {
|
|||
|
|
this.addLog('error', '上传失败', err)
|
|||
|
|
reject({ success: false, error: err.errMsg || '网络错误', details: err })
|
|||
|
|
}
|
|||
|
|
})
|
|||
|
|
})
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 生成诊断报告
|
|||
|
|
generateReport() {
|
|||
|
|
const report = {
|
|||
|
|
timestamp: new Date().toISOString(),
|
|||
|
|
logs: this.logs,
|
|||
|
|
summary: {
|
|||
|
|
totalLogs: this.logs.length,
|
|||
|
|
errors: this.logs.filter(log => log.type === 'error').length,
|
|||
|
|
warnings: this.logs.filter(log => log.type === 'warn').length,
|
|||
|
|
info: this.logs.filter(log => log.type === 'info').length
|
|||
|
|
},
|
|||
|
|
recommendations: this.generateRecommendations()
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
console.log('=== 诊断报告 ===')
|
|||
|
|
console.log(JSON.stringify(report, null, 2))
|
|||
|
|
|
|||
|
|
return report
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 生成建议
|
|||
|
|
generateRecommendations() {
|
|||
|
|
const recommendations = []
|
|||
|
|
const errors = this.logs.filter(log => log.type === 'error')
|
|||
|
|
const warnings = this.logs.filter(log => log.type === 'warn')
|
|||
|
|
|
|||
|
|
// 检查token问题
|
|||
|
|
const tokenErrors = errors.filter(log => log.message.includes('token') || log.message.includes('Token'))
|
|||
|
|
if (tokenErrors.length > 0) {
|
|||
|
|
recommendations.push('检查用户登录状态和token有效性')
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 检查网络问题
|
|||
|
|
const networkErrors = errors.filter(log => log.message.includes('网络') || log.message.includes('timeout'))
|
|||
|
|
if (networkErrors.length > 0) {
|
|||
|
|
recommendations.push('检查网络连接和服务器状态')
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 检查文件问题
|
|||
|
|
const fileErrors = errors.filter(log => log.message.includes('文件') || log.message.includes('file'))
|
|||
|
|
if (fileErrors.length > 0) {
|
|||
|
|
recommendations.push('检查文件格式和大小是否符合要求')
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 检查服务器问题
|
|||
|
|
const serverErrors = errors.filter(log => log.message.includes('服务器') || log.message.includes('server'))
|
|||
|
|
if (serverErrors.length > 0) {
|
|||
|
|
recommendations.push('检查服务器接口是否正常工作')
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (recommendations.length === 0) {
|
|||
|
|
recommendations.push('所有检查都通过,问题可能在其他方面')
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return recommendations
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 运行完整诊断
|
|||
|
|
async runFullDiagnosis() {
|
|||
|
|
this.addLog('info', '开始完整诊断')
|
|||
|
|
|
|||
|
|
// 1. 检查token
|
|||
|
|
const tokenResult = this.checkToken()
|
|||
|
|
|
|||
|
|
// 2. 检查网络
|
|||
|
|
const networkResult = await this.checkNetwork()
|
|||
|
|
|
|||
|
|
// 3. 检查服务器连接
|
|||
|
|
const serverResult = await this.checkServerConnection()
|
|||
|
|
|
|||
|
|
// 4. 生成报告
|
|||
|
|
const report = this.generateReport()
|
|||
|
|
|
|||
|
|
return {
|
|||
|
|
token: tokenResult,
|
|||
|
|
network: networkResult,
|
|||
|
|
server: serverResult,
|
|||
|
|
report: report
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 导出调试器
|
|||
|
|
export default UploadDebugger
|
|||
|
|
|
|||
|
|
// 使用示例
|
|||
|
|
export function debugUpload() {
|
|||
|
|
const debugger = new UploadDebugger()
|
|||
|
|
return debugger.runFullDiagnosis()
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 在页面中使用
|
|||
|
|
export function useUploadDebugger() {
|
|||
|
|
const debugger = new UploadDebugger()
|
|||
|
|
|
|||
|
|
return {
|
|||
|
|
debugger,
|
|||
|
|
checkToken: () => debugger.checkToken(),
|
|||
|
|
checkNetwork: () => debugger.checkNetwork(),
|
|||
|
|
testUpload: (filePath) => debugger.testFileUpload(filePath),
|
|||
|
|
runDiagnosis: () => debugger.runFullDiagnosis(),
|
|||
|
|
getLogs: () => debugger.logs
|
|||
|
|
}
|
|||
|
|
}
|