index.vue 20 KB

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