index.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. <template>
  2. <view class="folder-select-page">
  3. <!-- 顶部导航栏 -->
  4. <view class="header-bg" :style="{ paddingTop: statusBarHeight + 'px' }">
  5. <view class="header-content">
  6. <view class="back-btn" @click="handleBack">
  7. <text class="back-icon">‹</text>
  8. </view>
  9. <text class="header-title">选择文件夹</text>
  10. <view class="placeholder"></view>
  11. </view>
  12. </view>
  13. <!-- 项目信息 -->
  14. <view class="project-info-bar">
  15. <text class="project-label">当前项目:</text>
  16. <text class="project-name">{{ projectName }}</text>
  17. </view>
  18. <!-- 选择表单 -->
  19. <view class="form-container">
  20. <view v-if="loading" class="loading-tip">
  21. <text class="loading-text">加载中...</text>
  22. </view>
  23. <view v-else class="form-content">
  24. <!-- 国家-中心联动选择 -->
  25. <view class="form-item">
  26. <view class="form-label">国家 - 中心:</view>
  27. <picker
  28. mode="multiSelector"
  29. :range="pickerRange"
  30. :value="[selectedCountryIndex, selectedCenterIndex]"
  31. @change="handleMultiPickerChange"
  32. @columnchange="handleColumnChange"
  33. >
  34. <view class="picker-display">
  35. <text class="picker-text" :class="{ placeholder: selectedCountryIndex === -1 || selectedCenterIndex === -1 }">
  36. {{ getDisplayText() }}
  37. </text>
  38. <text class="picker-arrow">›</text>
  39. </view>
  40. </picker>
  41. </view>
  42. <!-- 名称输入 -->
  43. <view class="form-item">
  44. <view class="form-label">名称:</view>
  45. <view class="input-wrapper">
  46. <input
  47. v-model="fileName"
  48. type="text"
  49. placeholder="请输入名称"
  50. class="input-field"
  51. />
  52. </view>
  53. </view>
  54. <!-- 提交按钮 -->
  55. <button
  56. class="confirm-btn"
  57. :disabled="selectedCountryIndex < 0 || selectedCenterIndex < 0 || !fileName.trim()"
  58. @click="handleSubmit"
  59. >
  60. 提交
  61. </button>
  62. <!-- 调试信息 -->
  63. <view class="debug-section">
  64. <view class="debug-title">调试信息:</view>
  65. <view class="debug-item">国家数量: {{ countryList.length }}</view>
  66. <view class="debug-item">中心数量: {{ centerList.length }}</view>
  67. <view class="debug-item">选中国家: {{ selectedCountryIndex === -1 ? '未选择' : countryList[selectedCountryIndex]?.name }}</view>
  68. <view class="debug-item">选中中心: {{ selectedCenterIndex === -1 ? '未选择' : centerList[selectedCenterIndex]?.name }}</view>
  69. </view>
  70. <!-- 原始数据展示 -->
  71. <view class="raw-data-section">
  72. <view class="raw-data-title" @click="showRawData = !showRawData">
  73. 原始数据 {{ showRawData ? '▼' : '▶' }}
  74. </view>
  75. <view v-if="showRawData" class="json-display">{{ JSON.stringify(rawData, null, 2) }}</view>
  76. </view>
  77. </view>
  78. </view>
  79. </view>
  80. </template>
  81. <script>
  82. import { getFolderList } from '@/apis/scan'
  83. import request from '@/utils/request.js'
  84. export default {
  85. data() {
  86. return {
  87. statusBarHeight: 0,
  88. projectId: 0,
  89. projectName: '',
  90. rawData: null,
  91. loading: false,
  92. showRawData: false,
  93. // 原始数据列表(用于查找独立的中心)
  94. allFolders: [],
  95. // 国家列表
  96. countryList: [],
  97. selectedCountryIndex: 0, // 默认选中第一个(NA)
  98. // 中心列表
  99. centerList: [],
  100. selectedCenterIndex: 0, // 默认选中第一个(NA)
  101. // 名称输入
  102. fileName: ''
  103. }
  104. },
  105. computed: {
  106. // 构建选择器的 range 数据
  107. pickerRange() {
  108. // 第一列:国家名称数组
  109. const countryNames = this.countryList.map(item => item.name)
  110. // 第二列:中心显示名称数组
  111. const centerNames = this.centerList.map(item => item.displayName || item.name)
  112. return [countryNames, centerNames]
  113. }
  114. },
  115. onLoad(options) {
  116. console.log('页面参数:', options)
  117. this.projectId = parseInt(options.projectId) || 0
  118. this.projectName = decodeURIComponent(options.projectName || '')
  119. console.log('解析后的projectId:', this.projectId)
  120. console.log('解析后的projectName:', this.projectName)
  121. },
  122. onReady() {
  123. // 获取系统信息
  124. const windowInfo = uni.getWindowInfo()
  125. this.statusBarHeight = windowInfo.statusBarHeight || 0
  126. // 加载数据
  127. if (this.projectId) {
  128. this.loadFolderList()
  129. } else {
  130. console.error('projectId 为空,无法加载文件夹列表')
  131. }
  132. },
  133. methods: {
  134. // 返回上一页
  135. handleBack() {
  136. uni.navigateBack()
  137. },
  138. // 加载文件夹列表
  139. async loadFolderList() {
  140. if (this.loading) return
  141. try {
  142. this.loading = true
  143. console.log('========== 开始加载文件夹列表 ==========')
  144. console.log('projectId:', this.projectId)
  145. const response = await getFolderList({
  146. projectId: this.projectId
  147. })
  148. console.log('========== 后端返回的原始数据 ==========')
  149. console.log('完整响应:', response)
  150. // 保存原始数据
  151. this.rawData = response
  152. // 解析数据
  153. if (response.code === 200 && response.data && Array.isArray(response.data)) {
  154. this.allFolders = response.data
  155. this.parseCountryList(response.data)
  156. }
  157. } catch (error) {
  158. console.error('========== 加载失败 ==========')
  159. console.error('错误信息:', error)
  160. uni.showToast({
  161. title: '加载失败',
  162. icon: 'none'
  163. })
  164. } finally {
  165. this.loading = false
  166. console.log('========== 加载完成 ==========')
  167. }
  168. },
  169. // 解析国家列表(type === 1 为国家)
  170. parseCountryList(data) {
  171. const countries = data.filter(item => {
  172. return item.type === 1 // 国家类型
  173. })
  174. // 在列表开头添加 NA 选项
  175. this.countryList = [
  176. { id: 'NA', name: 'NA', type: 1, children: [], displayName: 'NA' },
  177. ...countries
  178. ]
  179. console.log('解析后的国家列表:', this.countryList)
  180. // 初始化时加载第一个国家(NA)的中心列表
  181. if (this.countryList.length > 0) {
  182. this.loadCenterList(0)
  183. }
  184. },
  185. // 加载中心列表
  186. loadCenterList(countryIndex) {
  187. if (countryIndex >= 0 && this.countryList[countryIndex]) {
  188. const country = this.countryList[countryIndex]
  189. // 如果选择的是 NA(第一个选项)
  190. if (country.id === 'NA') {
  191. // 获取所有不属于任何国家的中心
  192. this.centerList = this.getIndependentCenters()
  193. } else {
  194. // 选择了具体国家,显示该国家下的所有中心(包括子中心)
  195. const allCenters = this.getAllCentersRecursive(country.children || [])
  196. // 在列表开头添加 NA 选项
  197. this.centerList = [
  198. { id: 'NA', name: 'NA', type: 2, displayName: 'NA' },
  199. ...allCenters
  200. ]
  201. }
  202. console.log('国家:', country.name, '中心列表:', this.centerList)
  203. } else {
  204. this.centerList = [{ id: 'NA', name: 'NA', type: 2, displayName: 'NA' }]
  205. }
  206. },
  207. // 递归获取所有中心(包括子中心)
  208. getAllCentersRecursive(nodes, prefix = '') {
  209. let centers = []
  210. nodes.forEach(node => {
  211. // 如果是中心类型(type === 2)
  212. if (node.type === 2) {
  213. // 添加当前中心,名称带层级前缀
  214. const displayName = prefix ? `${prefix} > ${node.name}` : node.name
  215. centers.push({
  216. ...node,
  217. displayName: displayName, // 用于显示的名称
  218. originalName: node.name // 保留原始名称
  219. })
  220. // 如果有子节点,递归获取
  221. if (node.children && node.children.length > 0) {
  222. const subCenters = this.getAllCentersRecursive(node.children, displayName)
  223. centers = centers.concat(subCenters)
  224. }
  225. } else if (node.children && node.children.length > 0) {
  226. // 如果不是中心但有子节点,继续递归
  227. const subCenters = this.getAllCentersRecursive(node.children, prefix)
  228. centers = centers.concat(subCenters)
  229. }
  230. })
  231. return centers
  232. },
  233. // 多列选择器列变化
  234. handleColumnChange(e) {
  235. const column = e.detail.column // 哪一列变化了
  236. const value = e.detail.value // 变化后的值
  237. console.log('列变化:', column, '值:', value)
  238. // 如果是第一列(国家)变化
  239. if (column === 0) {
  240. this.selectedCountryIndex = value
  241. // 重新加载中心列表
  242. this.loadCenterList(value)
  243. // 重置中心选择为第一个
  244. this.selectedCenterIndex = 0
  245. }
  246. },
  247. // 多列选择器确认
  248. handleMultiPickerChange(e) {
  249. const values = e.detail.value
  250. this.selectedCountryIndex = values[0]
  251. this.selectedCenterIndex = values[1]
  252. console.log('选择完成 - 国家索引:', this.selectedCountryIndex, '中心索引:', this.selectedCenterIndex)
  253. if (this.countryList[this.selectedCountryIndex]) {
  254. console.log('选中国家:', this.countryList[this.selectedCountryIndex].name)
  255. }
  256. if (this.centerList[this.selectedCenterIndex]) {
  257. console.log('选中中心:', this.centerList[this.selectedCenterIndex].name)
  258. }
  259. },
  260. // 获取显示文本
  261. getDisplayText() {
  262. if (this.selectedCountryIndex >= 0 && this.selectedCenterIndex >= 0) {
  263. const country = this.countryList[this.selectedCountryIndex]
  264. const center = this.centerList[this.selectedCenterIndex]
  265. if (country && center) {
  266. const centerName = center.displayName || center.name
  267. return `${country.name} - ${centerName}`
  268. }
  269. }
  270. return '请选择国家和中心'
  271. },
  272. // 获取所有不属于任何国家的中心
  273. getIndependentCenters() {
  274. // 收集所有国家下的中心ID
  275. const centerIdsInCountries = new Set()
  276. this.allFolders.forEach(folder => {
  277. if (folder.type === 1 && folder.children) { // 国家类型
  278. this.collectAllCenterIds(folder.children, centerIdsInCountries)
  279. }
  280. })
  281. // 找出所有不在国家下的中心(直接在根目录下的中心)
  282. const independentCenters = this.allFolders.filter(folder => {
  283. return folder.type === 2 && !centerIdsInCountries.has(folder.id)
  284. })
  285. // 递归获取这些独立中心及其子中心
  286. const allIndependentCenters = this.getAllCentersRecursive(independentCenters)
  287. // 在列表开头添加 NA 选项
  288. return [
  289. { id: 'NA', name: 'NA', type: 2, displayName: 'NA' },
  290. ...allIndependentCenters
  291. ]
  292. },
  293. // 收集所有中心ID(递归)
  294. collectAllCenterIds(nodes, centerIds) {
  295. nodes.forEach(node => {
  296. if (node.type === 2) {
  297. centerIds.add(node.id)
  298. }
  299. if (node.children && node.children.length > 0) {
  300. this.collectAllCenterIds(node.children, centerIds)
  301. }
  302. })
  303. },
  304. // 中心选择变化(保留以防需要)
  305. handleCenterChange(e) {
  306. const index = parseInt(e.detail.value)
  307. this.selectedCenterIndex = index
  308. if (index >= 0 && this.centerList[index]) {
  309. console.log('选中中心:', this.centerList[index].name)
  310. }
  311. },
  312. // 提交
  313. async handleSubmit() {
  314. if (this.selectedCountryIndex < 0) {
  315. uni.showToast({
  316. title: '请选择国家',
  317. icon: 'none'
  318. })
  319. return
  320. }
  321. if (this.selectedCenterIndex < 0) {
  322. uni.showToast({
  323. title: '请选择中心',
  324. icon: 'none'
  325. })
  326. return
  327. }
  328. if (!this.fileName.trim()) {
  329. uni.showToast({
  330. title: '请输入名称',
  331. icon: 'none'
  332. })
  333. return
  334. }
  335. const selectedCountry = this.countryList[this.selectedCountryIndex]
  336. const selectedCenter = this.centerList[this.selectedCenterIndex]
  337. // 从全局数据中获取扫描的fileBase64List
  338. const fileBase64List = getApp().globalData.scannedFileBase64List
  339. if (!fileBase64List || fileBase64List.length === 0) {
  340. uni.showToast({
  341. title: '未找到扫描文件',
  342. icon: 'none'
  343. })
  344. return
  345. }
  346. try {
  347. uni.showLoading({
  348. title: '提交中...',
  349. mask: true
  350. })
  351. // 构建请求参数
  352. const params = {
  353. projectId: this.projectId,
  354. country: selectedCountry.id === 'NA' ? 0 : selectedCountry.id,
  355. center: selectedCenter.id === 'NA' ? 0 : selectedCenter.id,
  356. name: this.fileName.trim(),
  357. files: fileBase64List
  358. }
  359. console.log('========== 提交参数 ==========')
  360. console.log('params:', params)
  361. // 调用上传接口
  362. const response = await request({
  363. url: '/applet/scan/upload',
  364. method: 'POST',
  365. data: params
  366. })
  367. console.log('========== 上传响应 ==========')
  368. console.log('response:', response)
  369. uni.hideLoading()
  370. if (response.code === 200) {
  371. uni.showToast({
  372. title: '提交成功',
  373. icon: 'success',
  374. duration: 2000
  375. })
  376. // 清空全局数据
  377. getApp().globalData.scannedFileBase64List = []
  378. // 延迟返回首页
  379. setTimeout(() => {
  380. uni.reLaunch({
  381. url: '/pages/home/index'
  382. })
  383. }, 2000)
  384. } else {
  385. throw new Error(response.msg || '提交失败')
  386. }
  387. } catch (error) {
  388. uni.hideLoading()
  389. console.error('========== 提交失败 ==========')
  390. console.error('错误信息:', error)
  391. uni.showToast({
  392. title: error.message || '提交失败',
  393. icon: 'none'
  394. })
  395. }
  396. },
  397. // 确认选择(保留旧方法以防其他地方调用)
  398. handleConfirm() {
  399. this.handleSubmit()
  400. }
  401. }
  402. }
  403. </script>
  404. <style lang="scss" scoped>
  405. .folder-select-page {
  406. width: 100%;
  407. height: 100vh;
  408. display: flex;
  409. flex-direction: column;
  410. background-color: #f5f5f5;
  411. // 顶部导航栏
  412. .header-bg {
  413. background: linear-gradient(180deg, #1ec9c9 0%, #1eb8b8 100%);
  414. position: relative;
  415. z-index: 100;
  416. .header-content {
  417. height: 88rpx;
  418. display: flex;
  419. align-items: center;
  420. justify-content: space-between;
  421. padding: 0 24rpx;
  422. .back-btn {
  423. width: 60rpx;
  424. height: 60rpx;
  425. display: flex;
  426. align-items: center;
  427. justify-content: flex-start;
  428. .back-icon {
  429. font-size: 56rpx;
  430. color: #ffffff;
  431. font-weight: 300;
  432. }
  433. }
  434. .header-title {
  435. flex: 1;
  436. text-align: center;
  437. font-size: 32rpx;
  438. font-weight: 600;
  439. color: #ffffff;
  440. }
  441. .placeholder {
  442. width: 60rpx;
  443. }
  444. }
  445. }
  446. // 项目信息栏
  447. .project-info-bar {
  448. padding: 24rpx;
  449. background: #ffffff;
  450. display: flex;
  451. align-items: center;
  452. border-bottom: 1rpx solid #f5f5f5;
  453. .project-label {
  454. font-size: 28rpx;
  455. color: #666666;
  456. margin-right: 8rpx;
  457. }
  458. .project-name {
  459. font-size: 28rpx;
  460. font-weight: 600;
  461. color: #1ec9c9;
  462. }
  463. }
  464. // 表单容器
  465. .form-container {
  466. flex: 1;
  467. overflow-y: auto;
  468. }
  469. .form-content {
  470. padding: 32rpx;
  471. }
  472. .form-item {
  473. margin-bottom: 32rpx;
  474. .form-label {
  475. font-size: 28rpx;
  476. color: #333333;
  477. font-weight: 600;
  478. margin-bottom: 16rpx;
  479. }
  480. .picker-display {
  481. background: #ffffff;
  482. border: 2rpx solid #e0e0e0;
  483. border-radius: 12rpx;
  484. padding: 24rpx;
  485. display: flex;
  486. align-items: center;
  487. justify-content: space-between;
  488. transition: all 0.3s;
  489. &.disabled {
  490. background: #f5f5f5;
  491. opacity: 0.6;
  492. }
  493. .picker-text {
  494. flex: 1;
  495. font-size: 28rpx;
  496. color: #333333;
  497. &.placeholder {
  498. color: #999999;
  499. }
  500. }
  501. .picker-arrow {
  502. font-size: 40rpx;
  503. color: #999999;
  504. font-weight: 300;
  505. margin-left: 16rpx;
  506. }
  507. }
  508. }
  509. .confirm-btn {
  510. width: 100%;
  511. height: 88rpx;
  512. background: linear-gradient(135deg, #1ec9c9 0%, #1eb8b8 100%);
  513. color: #ffffff;
  514. font-size: 32rpx;
  515. font-weight: 600;
  516. border-radius: 16rpx;
  517. border: none;
  518. margin-top: 32rpx;
  519. transition: all 0.3s;
  520. box-shadow: 0 6rpx 20rpx rgba(30, 201, 201, 0.3);
  521. &:disabled {
  522. background: #cccccc;
  523. box-shadow: none;
  524. opacity: 0.6;
  525. }
  526. &:active:not(:disabled) {
  527. opacity: 0.9;
  528. transform: scale(0.98);
  529. }
  530. &::after {
  531. border: none;
  532. }
  533. }
  534. // 调试信息
  535. .debug-section {
  536. margin-top: 48rpx;
  537. padding: 24rpx;
  538. background: #fff3cd;
  539. border-radius: 12rpx;
  540. border: 1rpx solid #ffc107;
  541. .debug-title {
  542. font-size: 28rpx;
  543. font-weight: 600;
  544. color: #856404;
  545. margin-bottom: 12rpx;
  546. }
  547. .debug-item {
  548. font-size: 24rpx;
  549. color: #856404;
  550. line-height: 1.8;
  551. }
  552. }
  553. // 原始数据
  554. .raw-data-section {
  555. margin-top: 32rpx;
  556. .raw-data-title {
  557. font-size: 28rpx;
  558. font-weight: 600;
  559. color: #666666;
  560. padding: 16rpx;
  561. background: #f5f5f5;
  562. border-radius: 8rpx;
  563. cursor: pointer;
  564. &:active {
  565. background: #e0e0e0;
  566. }
  567. }
  568. .json-display {
  569. margin-top: 16rpx;
  570. background: #f5f5f5;
  571. padding: 24rpx;
  572. border-radius: 8rpx;
  573. font-size: 22rpx;
  574. color: #333333;
  575. font-family: 'Courier New', monospace;
  576. line-height: 1.6;
  577. word-break: break-all;
  578. white-space: pre-wrap;
  579. }
  580. }
  581. .loading-tip {
  582. padding: 200rpx 0;
  583. text-align: center;
  584. .loading-text {
  585. font-size: 28rpx;
  586. color: #999999;
  587. }
  588. }
  589. }
  590. </style>