upload.vue 14 KB

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