upload.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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 in recommendedProducts"
  26. :key="getProductKey(item)"
  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. },
  267. getImageUrl: function(path) {
  268. return config.API_BASE_URL + path
  269. },
  270. getProductKey: function(item) {
  271. return item.id
  272. }
  273. }
  274. }
  275. </script>
  276. <style scoped>
  277. .container {
  278. padding: 30rpx;
  279. background: #F8F8F8;
  280. min-height: 100vh;
  281. }
  282. .header {
  283. text-align: center;
  284. padding: 40rpx 0 30rpx;
  285. }
  286. .title {
  287. font-size: 40rpx;
  288. font-weight: bold;
  289. color: #333;
  290. display: block;
  291. margin-bottom: 10rpx;
  292. }
  293. .subtitle {
  294. font-size: 26rpx;
  295. color: #999;
  296. }
  297. .form {
  298. background: #fff;
  299. border-radius: 20rpx;
  300. padding: 30rpx;
  301. }
  302. .form-item {
  303. margin-bottom: 30rpx;
  304. }
  305. .label {
  306. font-size: 28rpx;
  307. color: #333;
  308. display: block;
  309. margin-bottom: 16rpx;
  310. font-weight: bold;
  311. }
  312. .child-select {
  313. display: flex;
  314. align-items: center;
  315. height: 76rpx;
  316. border: 1rpx solid #E0E0E0;
  317. border-radius: 10rpx;
  318. padding: 0 20rpx;
  319. }
  320. .select-text {
  321. flex: 1;
  322. font-size: 28rpx;
  323. color: #333;
  324. }
  325. .select-placeholder {
  326. flex: 1;
  327. font-size: 28rpx;
  328. color: #ccc;
  329. }
  330. .select-arrow {
  331. font-size: 32rpx;
  332. color: #ccc;
  333. }
  334. .upload-area {
  335. display: flex;
  336. flex-direction: column;
  337. align-items: center;
  338. justify-content: center;
  339. height: 200rpx;
  340. border: 2rpx dashed #ccc;
  341. border-radius: 16rpx;
  342. background: #fafafa;
  343. }
  344. .upload-icon {
  345. font-size: 64rpx;
  346. color: #999;
  347. line-height: 1;
  348. }
  349. .upload-text {
  350. font-size: 26rpx;
  351. color: #999;
  352. margin-top: 10rpx;
  353. }
  354. .file-name {
  355. font-size: 28rpx;
  356. color: #667eea;
  357. word-break: break-all;
  358. text-align: center;
  359. }
  360. .btn-submit {
  361. width: 100%;
  362. height: 88rpx;
  363. line-height: 88rpx;
  364. background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  365. color: #fff;
  366. border-radius: 44rpx;
  367. font-size: 32rpx;
  368. font-weight: bold;
  369. text-align: center;
  370. }
  371. .btn-submit[disabled] {
  372. opacity: 0.5;
  373. }
  374. .preview-card {
  375. background: #f9f9ff;
  376. border-radius: 12rpx;
  377. padding: 24rpx;
  378. margin-bottom: 30rpx;
  379. }
  380. .preview-row {
  381. display: flex;
  382. margin-bottom: 16rpx;
  383. }
  384. .preview-label {
  385. font-size: 26rpx;
  386. color: #666;
  387. width: 160rpx;
  388. flex-shrink: 0;
  389. }
  390. .preview-value {
  391. font-size: 26rpx;
  392. color: #333;
  393. flex: 1;
  394. }
  395. .preview-value.summary {
  396. max-height: 200rpx;
  397. overflow: hidden;
  398. text-overflow: ellipsis;
  399. display: -webkit-box;
  400. -webkit-line-clamp: 4;
  401. -webkit-box-orient: vertical;
  402. }
  403. .picker-input {
  404. height: 76rpx;
  405. line-height: 76rpx;
  406. border: 1rpx solid #E0E0E0;
  407. border-radius: 10rpx;
  408. padding: 0 20rpx;
  409. font-size: 28rpx;
  410. color: #333;
  411. }
  412. .btn-group {
  413. display: flex;
  414. gap: 20rpx;
  415. }
  416. .btn-secondary {
  417. flex: 1;
  418. height: 88rpx;
  419. line-height: 88rpx;
  420. background: #fff;
  421. color: #667eea;
  422. border: 2rpx solid #667eea;
  423. border-radius: 44rpx;
  424. font-size: 30rpx;
  425. font-weight: bold;
  426. text-align: center;
  427. }
  428. .btn-group .btn-submit {
  429. flex: 1;
  430. }
  431. .purchase-section {
  432. background: #F0F9FF;
  433. border-radius: 16rpx;
  434. padding: 24rpx;
  435. margin-bottom: 30rpx;
  436. border: 2rpx solid #BAE6FD;
  437. }
  438. .purchase-header {
  439. display: flex;
  440. flex-direction: column;
  441. margin-bottom: 16rpx;
  442. }
  443. .purchase-title {
  444. font-size: 28rpx;
  445. font-weight: bold;
  446. color: #0369A1;
  447. }
  448. .purchase-subtitle {
  449. font-size: 24rpx;
  450. color: #0284C7;
  451. margin-top: 6rpx;
  452. }
  453. .purchase-scroll {
  454. display: flex;
  455. flex-direction: row;
  456. white-space: nowrap;
  457. }
  458. .purchase-card {
  459. display: inline-flex;
  460. flex-direction: column;
  461. width: 220rpx;
  462. background: #fff;
  463. border-radius: 12rpx;
  464. padding: 16rpx;
  465. margin-right: 16rpx;
  466. flex-shrink: 0;
  467. }
  468. .purchase-cover {
  469. width: 188rpx;
  470. height: 120rpx;
  471. border-radius: 8rpx;
  472. background: #F1F5F9;
  473. margin-bottom: 8rpx;
  474. }
  475. .purchase-name {
  476. font-size: 22rpx;
  477. color: #333;
  478. line-height: 1.3;
  479. overflow: hidden;
  480. text-overflow: ellipsis;
  481. white-space: nowrap;
  482. margin-bottom: 4rpx;
  483. }
  484. .purchase-price {
  485. font-size: 24rpx;
  486. color: #EF4444;
  487. font-weight: bold;
  488. margin-bottom: 8rpx;
  489. }
  490. .purchase-btn {
  491. background: #3B82F6;
  492. border-radius: 8rpx;
  493. padding: 8rpx 0;
  494. text-align: center;
  495. }
  496. .purchase-btn-text {
  497. font-size: 22rpx;
  498. color: #fff;
  499. font-weight: 500;
  500. }
  501. </style>