upload.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. <template>
  2. <view class="container">
  3. <view class="header">
  4. <text class="title">上传测评报告</text>
  5. <text class="subtitle">上传PDF测评报告,自动解析数据</text>
  6. </view>
  7. <view class="form" v-if="step === 1">
  8. <view class="form-item">
  9. <text class="label">成员</text>
  10. <view class="child-select" @click="selectChild">
  11. <text class="select-text" v-if="selectedChild">{{ selectedChild.nickname || selectedChild.name }}</text>
  12. <text class="select-placeholder" v-else>请选择成员</text>
  13. <text class="select-arrow">›</text>
  14. </view>
  15. </view>
  16. <!-- 成长报告推荐区 -->
  17. <view class="purchase-section" v-if="recommendedProducts.length > 0">
  18. <view class="purchase-header">
  19. <text class="purchase-title">还没有成长报告?</text>
  20. <text class="purchase-subtitle">购买测评,获取专业成长报告</text>
  21. </view>
  22. <scroll-view class="purchase-scroll" scroll-x enable-flex show-scrollbar="false">
  23. <view
  24. class="purchase-card"
  25. v-for="(item, pi) in recommendedProducts"
  26. :key="pi"
  27. @click="goToProduct(item.id)"
  28. >
  29. <image class="purchase-cover" :src="getImageUrl(item.coverImage) || '/static/default-product.png'" mode="aspectFill" />
  30. <text class="purchase-name">{{ item.name }}</text>
  31. <text class="purchase-price">{{ formatPrice(item.price) }}</text>
  32. <view class="purchase-btn">
  33. <text class="purchase-btn-text">查看详情</text>
  34. </view>
  35. </view>
  36. </scroll-view>
  37. </view>
  38. <view class="form-item">
  39. <text class="label">选择报告文件</text>
  40. <view class="upload-area" @click="chooseFile">
  41. <text class="upload-icon" v-if="!fileName">+</text>
  42. <text class="upload-text" v-if="!fileName">点击选择PDF文件</text>
  43. <text class="file-name" v-if="fileName">{{ fileName }}</text>
  44. </view>
  45. </view>
  46. <button class="btn-submit" @click="uploadAndParse" :disabled="!canUpload">上传并解析</button>
  47. </view>
  48. <view class="form" v-if="step === 2">
  49. <view class="form-item">
  50. <text class="label">解析结果预览</text>
  51. </view>
  52. <view class="preview-card" v-if="parsedData">
  53. <view class="preview-row" v-if="parsedData.overallScore">
  54. <text class="preview-label">综合得分</text>
  55. <text class="preview-value">{{ parsedData.overallScore }}</text>
  56. </view>
  57. <view class="preview-row" v-if="reportDateStr">
  58. <text class="preview-label">报告日期</text>
  59. <text class="preview-value">{{ reportDateStr }}</text>
  60. </view>
  61. <view class="preview-row" v-if="parsedData.assessmentSummary">
  62. <text class="preview-label">测评总结</text>
  63. <text class="preview-value summary">{{ parsedData.assessmentSummary }}</text>
  64. </view>
  65. </view>
  66. <view class="form-item" v-if="!parsedData || !parsedData.reportDate">
  67. <text class="label">报告日期(手动输入)</text>
  68. <picker mode="date" :value="manualDate" @change="onDateChange">
  69. <view class="picker-input">{{ manualDate || '请选择日期' }}</view>
  70. </picker>
  71. </view>
  72. <view class="btn-group">
  73. <button class="btn-secondary" @click="reset">重新上传</button>
  74. <button class="btn-submit" @click="confirmCreate">确认创建</button>
  75. </view>
  76. </view>
  77. </view>
  78. </template>
  79. <script>
  80. import { uploadAndParseReport, createGrowthRecordFromUpload, checkGrowthDuplicate, getChildren, productList, getChildRecords } from '../../utils/api.js'
  81. import config from '@/config.js'
  82. export default {
  83. data() {
  84. return {
  85. step: 1,
  86. children: [],
  87. selectedChild: null,
  88. fileName: '',
  89. filePath: '',
  90. sourceFileUrl: '',
  91. parsedData: null,
  92. reportDateStr: '',
  93. manualDate: '',
  94. recommendedProducts: [],
  95. growthLoading: false
  96. }
  97. },
  98. computed: {
  99. canUpload() {
  100. return this.selectedChild && this.filePath
  101. }
  102. },
  103. onLoad(options) {
  104. this.loadChildren(options.memberId)
  105. },
  106. methods: {
  107. async loadChildren(targetChildId) {
  108. try {
  109. var res = await getChildren()
  110. if (res.code === 200) {
  111. this.children = res.data || []
  112. var target = null
  113. if (targetChildId) {
  114. target = this.children.find(function(c) { return String(c.id) === String(targetChildId) })
  115. }
  116. if (!target && this.children.length > 0) {
  117. target = this.children[0]
  118. }
  119. if (target) {
  120. this.selectedChild = target
  121. this.loadRecommend()
  122. }
  123. }
  124. } catch (e) {
  125. console.error(e)
  126. }
  127. },
  128. selectChild() {
  129. var items = this.children.map(function(c) { return c.nickname || c.name })
  130. var self = this
  131. uni.showActionSheet({
  132. itemList: items,
  133. success: function(res) {
  134. var idx = res.tapIndex
  135. if (idx >= 0 && idx < self.children.length) {
  136. self.selectedChild = self.children[idx]
  137. self.loadRecommend()
  138. }
  139. }
  140. })
  141. },
  142. chooseFile() {
  143. var self = this
  144. uni.chooseMessageFile({
  145. count: 1,
  146. type: 'file',
  147. extension: ['pdf'],
  148. success: function(res) {
  149. if (res.tempFiles && res.tempFiles.length > 0) {
  150. self.fileName = res.tempFiles[0].name
  151. self.filePath = res.tempFiles[0].path
  152. }
  153. },
  154. fail: function() {
  155. uni.showToast({ title: '请选择PDF文件', icon: 'none' })
  156. }
  157. })
  158. },
  159. async uploadAndParse() {
  160. if (!this.canUpload) return
  161. uni.showLoading({ title: '上传解析中...' })
  162. try {
  163. var res = await uploadAndParseReport(this.filePath)
  164. uni.hideLoading()
  165. if (res.code === 200) {
  166. this.sourceFileUrl = res.data.sourceFileUrl
  167. this.parsedData = res.data.parsed
  168. this.reportDateStr = res.data.reportDateStr
  169. this.step = 2
  170. } else {
  171. uni.showToast({ title: res.message || '解析失败', icon: 'none' })
  172. }
  173. } catch (e) {
  174. uni.hideLoading()
  175. uni.showToast({ title: e.message || '上传失败', icon: 'none' })
  176. }
  177. },
  178. onDateChange(e) {
  179. this.manualDate = e.detail.value
  180. },
  181. async confirmCreate() {
  182. var reportDate = this.manualDate || this.reportDateStr
  183. if (!reportDate && (!this.parsedData || !this.parsedData.reportDate)) {
  184. uni.showToast({ title: '请输入报告日期', icon: 'none' })
  185. return
  186. }
  187. uni.showLoading({ title: '创建中...' })
  188. try {
  189. var submitData = {
  190. memberId: this.selectedChild.id,
  191. sourceFileUrl: this.sourceFileUrl
  192. }
  193. if (this.parsedData) {
  194. if (this.parsedData.overallScore) submitData.overallScore = this.parsedData.overallScore
  195. if (this.parsedData.attentionScore) submitData.attentionScore = this.parsedData.attentionScore
  196. if (this.parsedData.focusScore) submitData.focusScore = this.parsedData.focusScore
  197. if (this.parsedData.memoryScore) submitData.memoryScore = this.parsedData.memoryScore
  198. if (this.parsedData.logicScore) submitData.logicScore = this.parsedData.logicScore
  199. if (this.parsedData.perceptionScore) submitData.perceptionScore = this.parsedData.perceptionScore
  200. if (this.parsedData.spatialScore) submitData.spatialScore = this.parsedData.spatialScore
  201. if (this.parsedData.processingSpeedScore) submitData.processingSpeedScore = this.parsedData.processingSpeedScore
  202. if (this.parsedData.emotionScore) submitData.emotionScore = this.parsedData.emotionScore
  203. if (this.parsedData.resilienceScore) submitData.resilienceScore = this.parsedData.resilienceScore
  204. if (this.parsedData.assessmentSummary) submitData.assessmentSummary = this.parsedData.assessmentSummary
  205. if (this.parsedData.growthSuggestions) submitData.growthSuggestions = this.parsedData.growthSuggestions
  206. }
  207. if (this.manualDate) {
  208. submitData.manualReportDate = this.manualDate
  209. }
  210. var res = await createGrowthRecordFromUpload(submitData)
  211. uni.hideLoading()
  212. if (res.code === 200) {
  213. uni.showToast({ title: '创建成功', icon: 'success' })
  214. setTimeout(function() {
  215. uni.navigateTo({ url: '/pages/growth/detail?recordId=' + res.data.id })
  216. }, 500)
  217. } else {
  218. uni.showToast({ title: res.message || '创建失败', icon: 'none' })
  219. }
  220. } catch (e) {
  221. uni.hideLoading()
  222. uni.showToast({ title: e.message || '创建失败', icon: 'none' })
  223. }
  224. },
  225. reset() {
  226. this.step = 1
  227. this.fileName = ''
  228. this.filePath = ''
  229. this.sourceFileUrl = ''
  230. this.parsedData = null
  231. this.reportDateStr = ''
  232. this.manualDate = ''
  233. },
  234. loadRecommend: async function() {
  235. this.recommendedProducts = []
  236. var child = this.selectedChild
  237. if (!child) {
  238. this.growthLoading = false
  239. return
  240. }
  241. this.growthLoading = true
  242. try {
  243. var res = await getChildRecords(child.id)
  244. if (res && res.code === 200) {
  245. var records = res.data || []
  246. if (records.length === 0) {
  247. var prodRes = await productList({ growthRelated: true, size: 10 })
  248. if (prodRes && prodRes.code === 200) {
  249. var arr = (prodRes.data && prodRes.data.list) || (prodRes.data && prodRes.data.records) || []
  250. this.recommendedProducts = arr
  251. }
  252. }
  253. }
  254. } catch (e) {
  255. this.recommendedProducts = []
  256. } finally {
  257. this.growthLoading = false
  258. }
  259. },
  260. goToProduct: function(id) {
  261. uni.navigateTo({ url: '/pages/discover-detail/product-detail/product-detail?id=' + id })
  262. },
  263. formatPrice: function(cents) {
  264. if (cents === null || cents === undefined) return '¥0'
  265. return '¥' + (cents / 100).toFixed(2)
  266. getImageUrl: function(path) {
  267. if (!path || path === 'null' || path === 'undefined' || path === '/null') return ''
  268. var s = String(path).trim()
  269. if (!s || s === 'null' || s === '/null') return ''
  270. if (s.indexOf('http://') === 0 || s.indexOf('https://') === 0) return s
  271. return config.API_BASE_URL + (s.charAt(0) === '/' ? s : '/' + s)
  272. } cff388913 (fix(image): 云文件绝对 URL 不再重复叠加 API_BASE_URL 前缀)
  273. }
  274. }
  275. }
  276. </script>
  277. <style scoped>
  278. .container {
  279. padding: 30rpx;
  280. background: #F8F8F8;
  281. min-height: 100vh;
  282. }
  283. .header {
  284. text-align: center;
  285. padding: 40rpx 0 30rpx;
  286. }
  287. .title {
  288. font-size: 40rpx;
  289. font-weight: bold;
  290. color: #333;
  291. display: block;
  292. margin-bottom: 10rpx;
  293. }
  294. .subtitle {
  295. font-size: 26rpx;
  296. color: #999;
  297. }
  298. .form {
  299. background: #fff;
  300. border-radius: 20rpx;
  301. padding: 30rpx;
  302. }
  303. .form-item {
  304. margin-bottom: 30rpx;
  305. }
  306. .label {
  307. font-size: 28rpx;
  308. color: #333;
  309. display: block;
  310. margin-bottom: 16rpx;
  311. font-weight: bold;
  312. }
  313. .child-select {
  314. display: flex;
  315. align-items: center;
  316. height: 76rpx;
  317. border: 1rpx solid #E0E0E0;
  318. border-radius: 10rpx;
  319. padding: 0 20rpx;
  320. }
  321. .select-text {
  322. flex: 1;
  323. font-size: 28rpx;
  324. color: #333;
  325. }
  326. .select-placeholder {
  327. flex: 1;
  328. font-size: 28rpx;
  329. color: #ccc;
  330. }
  331. .select-arrow {
  332. font-size: 32rpx;
  333. color: #ccc;
  334. }
  335. .upload-area {
  336. display: flex;
  337. flex-direction: column;
  338. align-items: center;
  339. justify-content: center;
  340. height: 200rpx;
  341. border: 2rpx dashed #ccc;
  342. border-radius: 16rpx;
  343. background: #fafafa;
  344. }
  345. .upload-icon {
  346. font-size: 64rpx;
  347. color: #999;
  348. line-height: 1;
  349. }
  350. .upload-text {
  351. font-size: 26rpx;
  352. color: #999;
  353. margin-top: 10rpx;
  354. }
  355. .file-name {
  356. font-size: 28rpx;
  357. color: #667eea;
  358. word-break: break-all;
  359. text-align: center;
  360. }
  361. .btn-submit {
  362. width: 100%;
  363. height: 88rpx;
  364. line-height: 88rpx;
  365. background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  366. color: #fff;
  367. border-radius: 44rpx;
  368. font-size: 32rpx;
  369. font-weight: bold;
  370. text-align: center;
  371. }
  372. .btn-submit[disabled] {
  373. opacity: 0.5;
  374. }
  375. .preview-card {
  376. background: #f9f9ff;
  377. border-radius: 12rpx;
  378. padding: 24rpx;
  379. margin-bottom: 30rpx;
  380. }
  381. .preview-row {
  382. display: flex;
  383. margin-bottom: 16rpx;
  384. }
  385. .preview-label {
  386. font-size: 26rpx;
  387. color: #666;
  388. width: 160rpx;
  389. flex-shrink: 0;
  390. }
  391. .preview-value {
  392. font-size: 26rpx;
  393. color: #333;
  394. flex: 1;
  395. }
  396. .preview-value.summary {
  397. max-height: 200rpx;
  398. overflow: hidden;
  399. text-overflow: ellipsis;
  400. display: -webkit-box;
  401. -webkit-line-clamp: 4;
  402. -webkit-box-orient: vertical;
  403. }
  404. .picker-input {
  405. height: 76rpx;
  406. line-height: 76rpx;
  407. border: 1rpx solid #E0E0E0;
  408. border-radius: 10rpx;
  409. padding: 0 20rpx;
  410. font-size: 28rpx;
  411. color: #333;
  412. }
  413. .btn-group {
  414. display: flex;
  415. gap: 20rpx;
  416. }
  417. .btn-secondary {
  418. flex: 1;
  419. height: 88rpx;
  420. line-height: 88rpx;
  421. background: #fff;
  422. color: #667eea;
  423. border: 2rpx solid #667eea;
  424. border-radius: 44rpx;
  425. font-size: 30rpx;
  426. font-weight: bold;
  427. text-align: center;
  428. }
  429. .btn-group .btn-submit {
  430. flex: 1;
  431. }
  432. .purchase-section {
  433. background: #F0F9FF;
  434. border-radius: 16rpx;
  435. padding: 24rpx;
  436. margin-bottom: 30rpx;
  437. border: 2rpx solid #BAE6FD;
  438. }
  439. .purchase-header {
  440. display: flex;
  441. flex-direction: column;
  442. margin-bottom: 16rpx;
  443. }
  444. .purchase-title {
  445. font-size: 28rpx;
  446. font-weight: bold;
  447. color: #0369A1;
  448. }
  449. .purchase-subtitle {
  450. font-size: 24rpx;
  451. color: #0284C7;
  452. margin-top: 6rpx;
  453. }
  454. .purchase-scroll {
  455. display: flex;
  456. flex-direction: row;
  457. white-space: nowrap;
  458. }
  459. .purchase-card {
  460. display: inline-flex;
  461. flex-direction: column;
  462. width: 220rpx;
  463. background: #fff;
  464. border-radius: 12rpx;
  465. padding: 16rpx;
  466. margin-right: 16rpx;
  467. flex-shrink: 0;
  468. }
  469. .purchase-cover {
  470. width: 188rpx;
  471. height: 120rpx;
  472. border-radius: 8rpx;
  473. background: #F1F5F9;
  474. margin-bottom: 8rpx;
  475. }
  476. .purchase-name {
  477. font-size: 22rpx;
  478. color: #333;
  479. line-height: 1.3;
  480. overflow: hidden;
  481. text-overflow: ellipsis;
  482. white-space: nowrap;
  483. margin-bottom: 4rpx;
  484. }
  485. .purchase-price {
  486. font-size: 24rpx;
  487. color: #EF4444;
  488. font-weight: bold;
  489. margin-bottom: 8rpx;
  490. }
  491. .purchase-btn {
  492. background: #3B82F6;
  493. border-radius: 8rpx;
  494. padding: 8rpx 0;
  495. text-align: center;
  496. }
  497. .purchase-btn-text {
  498. font-size: 22rpx;
  499. color: #fff;
  500. font-weight: 500;
  501. }
  502. </style>