From c7bad0e5e58b1f128e5c98bc498d4d70a654df6e Mon Sep 17 00:00:00 2001 From: Leo Date: Thu, 15 Jan 2026 21:50:57 +0800 Subject: [PATCH] 1 --- src/api/jarvis/wps365.js | 24 + .../docSync/components/WPS365Config.vue | 552 +++++++++++++----- 2 files changed, 446 insertions(+), 130 deletions(-) diff --git a/src/api/jarvis/wps365.js b/src/api/jarvis/wps365.js index 2c4f53d..1bb87b4 100644 --- a/src/api/jarvis/wps365.js +++ b/src/api/jarvis/wps365.js @@ -153,3 +153,27 @@ export function batchUpdateWPS365Cells(data) { }) } +// ==================== AirSheet相关 ==================== + +/** + * 读取AirSheet工作表数据 + */ +export function readWPS365AirSheetCells(params) { + return request({ + url: '/jarvis/wps365/readAirSheetCells', + method: 'get', + params + }) +} + +/** + * 更新AirSheet工作表数据 + */ +export function updateWPS365AirSheetCells(data) { + return request({ + url: '/jarvis/wps365/updateAirSheetCells', + method: 'post', + data + }) +} + diff --git a/src/views/jarvis/docSync/components/WPS365Config.vue b/src/views/jarvis/docSync/components/WPS365Config.vue index f9932fc..728b29a 100644 --- a/src/views/jarvis/docSync/components/WPS365Config.vue +++ b/src/views/jarvis/docSync/components/WPS365Config.vue @@ -1,90 +1,166 @@ @@ -92,8 +168,8 @@ import { getWPS365AuthUrl, getWPS365TokenStatus, - getWPS365UserInfo, - getWPS365FileList + readWPS365AirSheetCells, + updateWPS365AirSheetCells } from '@/api/jarvis/wps365' export default { @@ -101,50 +177,77 @@ export default { data() { return { isAuthorized: false, - userInfo: null, - userId: null, // 从token状态中获取 - tokenInfo: null + userId: null, + tokenInfo: null, + config: { + isConfigured: false, + hint: '' + }, + form: { + fileId: '', + headerRow: 2, + startRow: 3 + }, + rules: { + fileId: [ + { required: true, message: '请输入文件ID', trigger: 'blur' } + ], + headerRow: [ + { required: true, message: '请输入表头行号', trigger: 'blur' }, + { type: 'number', min: 1, message: '表头行号必须大于0', trigger: 'blur' } + ], + startRow: [ + { required: true, message: '请输入数据起始行', trigger: 'blur' }, + { type: 'number', min: 1, message: '数据起始行必须大于0', trigger: 'blur' } + ] + }, + saveLoading: false, + testLoading: false, + clearLoading: false } }, created() { this.checkAuthStatus() + this.loadConfig() }, methods: { /** 刷新配置 */ refresh() { this.checkAuthStatus() + this.loadConfig() }, /** 检查授权状态 */ async checkAuthStatus() { try { - // 不传userId,让后端自动查找token const response = await getWPS365TokenStatus(this.userId) if (response.code === 200) { this.isAuthorized = response.data.hasToken && response.data.isValid this.tokenInfo = response.data - // 如果找到了token,更新userId if (response.data.userId) { this.userId = response.data.userId } - if (this.isAuthorized) { - this.loadUserInfo() - } } } catch (error) { console.error('检查授权状态失败', error) } }, - /** 加载用户信息 */ - async loadUserInfo() { + /** 加载配置 */ + async loadConfig() { try { - const response = await getWPS365UserInfo(this.userId) - if (response.code === 200) { - this.userInfo = response.data + // TODO: 从后端加载配置 + // 暂时从localStorage读取 + const savedConfig = localStorage.getItem('wps365_auto_write_config') + if (savedConfig) { + const config = JSON.parse(savedConfig) + this.form.fileId = config.fileId || '' + this.form.headerRow = config.headerRow || 2 + this.form.startRow = config.startRow || 3 + this.config.isConfigured = !!(config.fileId) } } catch (error) { - console.error('加载用户信息失败', error) + console.error('加载配置失败', error) } }, @@ -166,15 +269,12 @@ export default { this.$message.success('授权页面已打开,请在新窗口中完成授权') - // 监听授权完成消息 const messageHandler = (event) => { if (event.data && event.data.type === 'wps365_oauth_callback') { window.removeEventListener('message', messageHandler) - // 如果回调消息包含userId,更新它 if (event.data.userId) { this.userId = event.data.userId } - // 延迟一下再刷新,确保后端已保存token setTimeout(() => { this.checkAuthStatus() this.$message.success('授权完成') @@ -183,7 +283,6 @@ export default { } window.addEventListener('message', messageHandler) - // 3秒后刷新配置状态(作为备用,防止消息监听失败) setTimeout(() => { this.checkAuthStatus() }, 3000) @@ -199,32 +298,138 @@ export default { this.$message.success('授权状态已刷新') }, - /** 测试API调用 */ - async testApi() { + /** 测试读取 */ + async handleTestRead() { if (!this.isAuthorized || !this.userId) { this.$message.warning('请先完成授权') return } - - this.$message.info('正在测试API调用...') + if (!this.form.fileId) { + this.$message.warning('请输入文件ID') + return + } + + this.$message.info('正在测试读取...') try { - // 测试获取文件列表API - const response = await getWPS365FileList({ + const response = await readWPS365AirSheetCells({ userId: this.userId, - page: 1, - pageSize: 10 + worksheetId: this.form.fileId, + range: 'A1:B5' }) - if (response.code === 200) { - this.$message.success('API调用成功!授权有效。') - console.log('API响应:', response.data) + this.$message.success('读取成功!文件ID正确。') + console.log('读取结果:', response.data) } else { - this.$message.warning('API调用返回错误: ' + (response.msg || '未知错误')) + this.$message.warning('读取失败: ' + (response.msg || '未知错误')) } } catch (error) { - this.$message.error('API调用失败: ' + (error.msg || error.message)) - console.error('API调用错误:', error) + this.$message.error('读取失败: ' + (error.msg || error.message)) + console.error('读取错误:', error) } + }, + + /** 保存配置 */ + async handleSave() { + this.$refs.form.validate(async (valid) => { + if (!valid) { + return false + } + if (!this.isAuthorized) { + this.$message.warning('请先完成授权') + return + } + + this.saveLoading = true + try { + // TODO: 保存到后端 + // 暂时保存到localStorage + const config = { + fileId: this.form.fileId, + headerRow: this.form.headerRow, + startRow: this.form.startRow + } + localStorage.setItem('wps365_auto_write_config', JSON.stringify(config)) + this.config.isConfigured = true + this.config.hint = 'H-TF订单将自动写入WPS365在线表格' + this.$message.success('配置保存成功') + } catch (error) { + this.$message.error('保存配置失败: ' + (error.msg || error.message)) + } finally { + this.saveLoading = false + } + }) + }, + + /** 测试配置 */ + async handleTest() { + if (!this.isAuthorized || !this.userId) { + this.$message.warning('请先完成授权') + return + } + this.$refs.form.validate(async (valid) => { + if (!valid) { + return false + } + + this.testLoading = true + try { + // 测试读取 + const readResponse = await readWPS365AirSheetCells({ + userId: this.userId, + worksheetId: this.form.fileId, + range: 'A1:B5' + }) + if (readResponse.code !== 200) { + this.$message.error('读取测试失败: ' + (readResponse.msg || '未知错误')) + return + } + + // 测试写入(写入测试数据) + const testRange = `A${this.form.startRow}:B${this.form.startRow}` + const testValues = [['测试数据1', '测试数据2']] + const writeResponse = await updateWPS365AirSheetCells({ + userId: this.userId, + worksheetId: this.form.fileId, + range: testRange, + values: testValues + }) + if (writeResponse.code === 200) { + this.$message.success('配置测试成功!读写功能正常。') + } else { + this.$message.warning('写入测试失败: ' + (writeResponse.msg || '未知错误')) + } + } catch (error) { + this.$message.error('配置测试失败: ' + (error.msg || error.message)) + console.error('测试错误:', error) + } finally { + this.testLoading = false + } + }) + }, + + /** 清除配置 */ + async handleClear() { + this.$confirm('确定要清除配置吗?', '提示', { + confirmButtonText: '确定', + cancelButtonText: '取消', + type: 'warning' + }).then(() => { + this.clearLoading = true + try { + // TODO: 清除后端配置 + localStorage.removeItem('wps365_auto_write_config') + this.form.fileId = '' + this.form.headerRow = 2 + this.form.startRow = 3 + this.config.isConfigured = false + this.config.hint = '' + this.$message.success('配置已清除') + } catch (error) { + this.$message.error('清除配置失败: ' + (error.msg || error.message)) + } finally { + this.clearLoading = false + } + }).catch(() => {}) } } } @@ -235,34 +440,121 @@ export default { padding: 0; } -.auth-card { +.config-container { + display: flex; + gap: 20px; +} + +.config-left { + flex: 1; + min-width: 0; +} + +.config-right { + width: 300px; + flex-shrink: 0; +} + +.config-section { margin-bottom: 20px; } +.section-header { + display: flex; + align-items: center; + gap: 8px; +} + .auth-status { display: flex; align-items: center; } -.user-info { +.token-info { margin-top: 15px; } -.usage-info { - line-height: 1.8; +.status-card-wrapper { + margin-bottom: 20px; } -.usage-info ol { - margin: 10px 0; - padding-left: 20px; +.status-card { + display: flex; + align-items: center; + padding: 15px; + border-radius: 4px; } -.usage-info code { - background: #f5f5f5; - padding: 2px 6px; - border-radius: 3px; - font-family: 'Courier New', monospace; - color: #e83e8c; +.status-card.success { + background-color: #f0f9ff; + border: 1px solid #b3d8ff; +} + +.status-card.warning { + background-color: #fef0f0; + border: 1px solid #fbc4c4; +} + +.status-icon { + width: 40px; + height: 40px; + border-radius: 50%; + display: flex; + align-items: center; + justify-content: center; + font-size: 20px; + margin-right: 15px; +} + +.status-icon.success { + background-color: #67c23a; + color: white; +} + +.status-icon.warning { + background-color: #e6a23c; + color: white; +} + +.status-text { + flex: 1; +} + +.status-title { + font-size: 16px; + font-weight: bold; + margin-bottom: 5px; +} + +.status-desc { + font-size: 12px; + color: #909399; +} + +.help-card-wrapper { + margin-bottom: 20px; +} + +.card-header { + display: flex; + align-items: center; + gap: 8px; +} + +.help-content { + padding: 10px 0; +} + +.help-item { + display: flex; + align-items: center; + gap: 8px; + margin-bottom: 10px; + font-size: 13px; + color: #606266; +} + +.help-item i { + color: #67c23a; } -