index.vue 21 KB

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