upload.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. <template>
  2. <view class="container" :style="{ '--font-scale': fontScale }">
  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" lazy-load />
  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. import fontScaleMixin from '@/mixins/font-scale.js'
  83. export default {
  84. mixins: [fontScaleMixin],
  85. data() {
  86. return {
  87. step: 1,
  88. children: [],
  89. selectedChild: null,
  90. fileName: '',
  91. filePath: '',
  92. sourceFileUrl: '',
  93. parsedData: null,
  94. reportDateStr: '',
  95. manualDate: '',
  96. recommendedProducts: [],
  97. growthLoading: false
  98. }
  99. },
  100. computed: {
  101. canUpload() {
  102. return this.selectedChild && this.filePath
  103. }
  104. },
  105. onLoad(options) {
  106. this.loadChildren(options.memberId)
  107. },
  108. methods: {
  109. async loadChildren(targetChildId) {
  110. try {
  111. var res = await getChildren()
  112. if (res.code === 200) {
  113. this.children = res.data || []
  114. var target = null
  115. if (targetChildId) {
  116. target = this.children.find(function(c) { return String(c.id) === String(targetChildId) })
  117. }
  118. if (!target && this.children.length > 0) {
  119. target = this.children[0]
  120. }
  121. if (target) {
  122. this.selectedChild = target
  123. this.loadRecommend()
  124. }
  125. }
  126. } catch (e) {
  127. console.error(e)
  128. }
  129. },
  130. selectChild() {
  131. var items = this.children.map(function(c) { return c.nickname || c.name })
  132. var self = this
  133. uni.showActionSheet({
  134. itemList: items,
  135. success: function(res) {
  136. var idx = res.tapIndex
  137. if (idx >= 0 && idx < self.children.length) {
  138. self.selectedChild = self.children[idx]
  139. self.loadRecommend()
  140. }
  141. }
  142. })
  143. },
  144. chooseFile() {
  145. var self = this
  146. uni.chooseMessageFile({
  147. count: 1,
  148. type: 'file',
  149. extension: ['pdf'],
  150. success: function(res) {
  151. if (res.tempFiles && res.tempFiles.length > 0) {
  152. self.fileName = res.tempFiles[0].name
  153. self.filePath = res.tempFiles[0].path
  154. }
  155. },
  156. fail: function() {
  157. uni.showToast({ title: '请选择PDF文件', icon: 'none' })
  158. }
  159. })
  160. },
  161. async uploadAndParse() {
  162. if (!this.canUpload) return
  163. uni.showLoading({ title: '上传解析中...' })
  164. try {
  165. var res = await uploadAndParseReport(this.filePath)
  166. uni.hideLoading()
  167. if (res.code === 200) {
  168. this.sourceFileUrl = res.data.sourceFileUrl
  169. this.parsedData = res.data.parsed
  170. this.reportDateStr = res.data.reportDateStr
  171. this.step = 2
  172. } else {
  173. uni.showToast({ title: res.message || '解析失败', icon: 'none' })
  174. }
  175. } catch (e) {
  176. uni.hideLoading()
  177. uni.showToast({ title: e.message || '上传失败', icon: 'none' })
  178. }
  179. },
  180. onDateChange(e) {
  181. this.manualDate = e.detail.value
  182. },
  183. async confirmCreate() {
  184. var reportDate = this.manualDate || this.reportDateStr
  185. if (!reportDate && (!this.parsedData || !this.parsedData.reportDate)) {
  186. uni.showToast({ title: '请输入报告日期', icon: 'none' })
  187. return
  188. }
  189. uni.showLoading({ title: '创建中...' })
  190. try {
  191. var submitData = {
  192. memberId: this.selectedChild.id,
  193. sourceFileUrl: this.sourceFileUrl
  194. }
  195. if (this.parsedData) {
  196. if (this.parsedData.overallScore) submitData.overallScore = this.parsedData.overallScore
  197. if (this.parsedData.attentionScore) submitData.attentionScore = this.parsedData.attentionScore
  198. if (this.parsedData.focusScore) submitData.focusScore = this.parsedData.focusScore
  199. if (this.parsedData.memoryScore) submitData.memoryScore = this.parsedData.memoryScore
  200. if (this.parsedData.logicScore) submitData.logicScore = this.parsedData.logicScore
  201. if (this.parsedData.perceptionScore) submitData.perceptionScore = this.parsedData.perceptionScore
  202. if (this.parsedData.spatialScore) submitData.spatialScore = this.parsedData.spatialScore
  203. if (this.parsedData.processingSpeedScore) submitData.processingSpeedScore = this.parsedData.processingSpeedScore
  204. if (this.parsedData.emotionScore) submitData.emotionScore = this.parsedData.emotionScore
  205. if (this.parsedData.resilienceScore) submitData.resilienceScore = this.parsedData.resilienceScore
  206. if (this.parsedData.assessmentSummary) submitData.assessmentSummary = this.parsedData.assessmentSummary
  207. if (this.parsedData.growthSuggestions) submitData.growthSuggestions = this.parsedData.growthSuggestions
  208. }
  209. if (this.manualDate) {
  210. submitData.manualReportDate = this.manualDate
  211. }
  212. var res = await createGrowthRecordFromUpload(submitData)
  213. uni.hideLoading()
  214. if (res.code === 200) {
  215. uni.showToast({ title: '创建成功', icon: 'success' })
  216. setTimeout(function() {
  217. uni.navigateTo({ url: '/pages/growth/detail?recordId=' + res.data.id })
  218. }, 500)
  219. } else {
  220. uni.showToast({ title: res.message || '创建失败', icon: 'none' })
  221. }
  222. } catch (e) {
  223. uni.hideLoading()
  224. uni.showToast({ title: e.message || '创建失败', icon: 'none' })
  225. }
  226. },
  227. reset() {
  228. this.step = 1
  229. this.fileName = ''
  230. this.filePath = ''
  231. this.sourceFileUrl = ''
  232. this.parsedData = null
  233. this.reportDateStr = ''
  234. this.manualDate = ''
  235. },
  236. loadRecommend: async function() {
  237. this.recommendedProducts = []
  238. var child = this.selectedChild
  239. if (!child) {
  240. this.growthLoading = false
  241. return
  242. }
  243. this.growthLoading = true
  244. try {
  245. var res = await getChildRecords(child.id)
  246. if (res && res.code === 200) {
  247. var records = res.data || []
  248. if (records.length === 0) {
  249. var prodRes = await productList({ growthRelated: true, size: 10 })
  250. if (prodRes && prodRes.code === 200) {
  251. var arr = (prodRes.data && prodRes.data.list) || (prodRes.data && prodRes.data.records) || []
  252. this.recommendedProducts = arr
  253. }
  254. }
  255. }
  256. } catch (e) {
  257. this.recommendedProducts = []
  258. } finally {
  259. this.growthLoading = false
  260. }
  261. },
  262. goToProduct: function(id) {
  263. uni.navigateTo({ url: '/pages/discover-detail/product-detail/product-detail?id=' + id })
  264. },
  265. formatPrice: function(cents) {
  266. if (cents === null || cents === undefined) return '¥0'
  267. return '¥' + (cents / 100).toFixed(2)
  268. },getImageUrl: function(path) {
  269. if (!path || path === 'null' || path === 'undefined' || path === '/null') return ''
  270. var s = String(path).trim()
  271. if (!s || s === 'null' || s === '/null') return ''
  272. if (s.indexOf('http://') === 0 || s.indexOf('https://') === 0) return s
  273. return config.API_BASE_URL + (s.charAt(0) === '/' ? s : '/' + s)
  274. }
  275. }
  276. }
  277. </script>
  278. <style scoped lang="scss">
  279. .container {
  280. padding: $uni-space-page;
  281. background: $facade-fill-page;
  282. min-height: 100vh;
  283. }
  284. .header {
  285. text-align: center;
  286. padding: 40rpx 0 $uni-space-lg;
  287. }
  288. .title {
  289. font-size: calc(40rpx * var(--font-scale, 1));
  290. font-weight: bold;
  291. color: $facade-text-strong;
  292. display: block;
  293. margin-bottom: 10rpx;
  294. }
  295. .subtitle {
  296. font-size: calc(26rpx * var(--font-scale, 1));
  297. color: $facade-text-muted;
  298. }
  299. .form {
  300. background: $app-surface;
  301. border-radius: 24rpx;
  302. padding: $uni-space-lg;
  303. }
  304. .form-item {
  305. margin-bottom: $uni-space-lg;
  306. }
  307. .label {
  308. font-size: calc(28rpx * var(--font-scale, 1));
  309. color: $facade-text-strong;
  310. display: block;
  311. margin-bottom: 16rpx;
  312. font-weight: bold;
  313. }
  314. .child-select {
  315. display: flex;
  316. align-items: center;
  317. height: 76rpx;
  318. border: 1rpx solid $facade-hairline;
  319. border-radius: 8rpx;
  320. padding: 0 20rpx;
  321. }
  322. .select-text {
  323. flex: 1;
  324. font-size: calc(28rpx * var(--font-scale, 1));
  325. color: $facade-text-strong;
  326. }
  327. .select-placeholder {
  328. flex: 1;
  329. font-size: calc(28rpx * var(--font-scale, 1));
  330. color: $facade-text-ghost;
  331. }
  332. .select-arrow {
  333. font-size: calc(32rpx * var(--font-scale, 1));
  334. color: $facade-text-ghost;
  335. }
  336. .upload-area {
  337. display: flex;
  338. flex-direction: column;
  339. align-items: center;
  340. justify-content: center;
  341. height: 200rpx;
  342. border: 2rpx dashed $facade-text-ghost;
  343. border-radius: 16rpx;
  344. background: $facade-fill-ghost;
  345. }
  346. .upload-icon {
  347. font-size: calc($uni-font-size-4xl * var(--font-scale, 1));
  348. color: $facade-text-muted;
  349. line-height: 1;
  350. }
  351. .upload-text {
  352. font-size: calc(26rpx * var(--font-scale, 1));
  353. color: $facade-text-muted;
  354. margin-top: 10rpx;
  355. }
  356. .file-name {
  357. font-size: calc(28rpx * var(--font-scale, 1));
  358. color: $accent-indigo;
  359. word-break: break-all;
  360. text-align: center;
  361. }
  362. .btn-submit {
  363. width: 100%;
  364. height: 88rpx;
  365. line-height: 88rpx;
  366. background: linear-gradient(135deg, $accent-indigo 0%, $accent-indigo-deep 100%);
  367. color: $app-surface;
  368. border-radius: 24rpx;
  369. font-size: calc(32rpx * var(--font-scale, 1));
  370. font-weight: bold;
  371. text-align: center;
  372. }
  373. .btn-submit[disabled] {
  374. opacity: 0.5;
  375. }
  376. .preview-card {
  377. background: $dim-wisdom-tint-5;
  378. border-radius: 16rpx;
  379. padding: 24rpx;
  380. margin-bottom: $uni-space-lg;
  381. }
  382. .preview-row {
  383. display: flex;
  384. margin-bottom: 16rpx;
  385. }
  386. .preview-label {
  387. font-size: calc(26rpx * var(--font-scale, 1));
  388. color: $facade-text-body;
  389. width: 160rpx;
  390. flex-shrink: 0;
  391. }
  392. .preview-value {
  393. font-size: calc(26rpx * var(--font-scale, 1));
  394. color: $facade-text-strong;
  395. flex: 1;
  396. }
  397. .preview-value.summary {
  398. max-height: 200rpx;
  399. overflow: hidden;
  400. text-overflow: ellipsis;
  401. display: -webkit-box;
  402. -webkit-line-clamp: 4;
  403. -webkit-box-orient: vertical;
  404. }
  405. .picker-input {
  406. height: 76rpx;
  407. line-height: 76rpx;
  408. border: 1rpx solid $facade-hairline;
  409. border-radius: 8rpx;
  410. padding: 0 20rpx;
  411. font-size: calc(28rpx * var(--font-scale, 1));
  412. color: $facade-text-strong;
  413. }
  414. .btn-group {
  415. display: flex;
  416. gap: 20rpx;
  417. }
  418. .btn-secondary {
  419. flex: 1;
  420. height: 88rpx;
  421. line-height: 88rpx;
  422. background: $app-surface;
  423. color: $accent-indigo;
  424. border: 2rpx solid $accent-indigo;
  425. border-radius: 24rpx;
  426. font-size: calc(30rpx * var(--font-scale, 1));
  427. font-weight: bold;
  428. text-align: center;
  429. }
  430. .btn-group .btn-submit {
  431. flex: 1;
  432. }
  433. .purchase-section {
  434. background: $tint-sky-soft;
  435. border-radius: 16rpx;
  436. padding: 24rpx;
  437. margin-bottom: $uni-space-lg;
  438. border: 2rpx solid $tint-sky-line;
  439. }
  440. .purchase-header {
  441. display: flex;
  442. flex-direction: column;
  443. margin-bottom: 16rpx;
  444. }
  445. .purchase-title {
  446. font-size: calc(28rpx * var(--font-scale, 1));
  447. font-weight: bold;
  448. color: #0369A1;
  449. }
  450. .purchase-subtitle {
  451. font-size: calc(24rpx * var(--font-scale, 1));
  452. color: $status-info-deep-2;
  453. margin-top: 6rpx;
  454. }
  455. .purchase-scroll {
  456. display: flex;
  457. flex-direction: row;
  458. white-space: nowrap;
  459. }
  460. .purchase-card {
  461. display: inline-flex;
  462. flex-direction: column;
  463. width: 220rpx;
  464. background: $app-surface;
  465. border-radius: 16rpx;
  466. padding: 16rpx;
  467. margin-right: 16rpx;
  468. flex-shrink: 0;
  469. }
  470. .purchase-cover {
  471. width: 188rpx;
  472. height: 120rpx;
  473. border-radius: 8rpx;
  474. background: $facade-fill-slate;
  475. margin-bottom: 8rpx;
  476. }
  477. .purchase-name {
  478. font-size: calc(22rpx * var(--font-scale, 1));
  479. color: $facade-text-strong;
  480. line-height: 1.3;
  481. overflow: hidden;
  482. text-overflow: ellipsis;
  483. white-space: nowrap;
  484. margin-bottom: 4rpx;
  485. }
  486. .purchase-price {
  487. font-size: calc(24rpx * var(--font-scale, 1));
  488. color: $status-danger;
  489. font-weight: bold;
  490. margin-bottom: 8rpx;
  491. }
  492. .purchase-btn {
  493. background: $status-info;
  494. border-radius: 8rpx;
  495. padding: 8rpx 0;
  496. text-align: center;
  497. }
  498. .purchase-btn-text {
  499. font-size: calc(22rpx * var(--font-scale, 1));
  500. color: $app-surface;
  501. font-weight: 500;
  502. }
  503. </style>