upload.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. <view class="form-item">
  17. <text class="label">选择报告文件</text>
  18. <view class="upload-area" @click="chooseFile">
  19. <text class="upload-icon" v-if="!fileName">+</text>
  20. <text class="upload-text" v-if="!fileName">点击选择PDF文件</text>
  21. <text class="file-name" v-if="fileName">{{ fileName }}</text>
  22. </view>
  23. </view>
  24. <button class="btn-submit" @click="uploadAndParse" :disabled="!canUpload">上传并解析</button>
  25. </view>
  26. <view class="form" v-if="step === 2">
  27. <view class="form-item">
  28. <text class="label">解析结果预览</text>
  29. </view>
  30. <view class="preview-card" v-if="parsedData">
  31. <view class="preview-row" v-if="parsedData.overallScore">
  32. <text class="preview-label">综合得分</text>
  33. <text class="preview-value">{{ parsedData.overallScore }}</text>
  34. </view>
  35. <view class="preview-row" v-if="reportDateStr">
  36. <text class="preview-label">报告日期</text>
  37. <text class="preview-value">{{ reportDateStr }}</text>
  38. </view>
  39. <view class="preview-row" v-if="parsedData.assessmentSummary">
  40. <text class="preview-label">测评总结</text>
  41. <text class="preview-value summary">{{ parsedData.assessmentSummary }}</text>
  42. </view>
  43. </view>
  44. <view class="form-item" v-if="!parsedData || !parsedData.reportDate">
  45. <text class="label">报告日期(手动输入)</text>
  46. <picker mode="date" :value="manualDate" @change="onDateChange">
  47. <view class="picker-input">{{ manualDate || '请选择日期' }}</view>
  48. </picker>
  49. </view>
  50. <view class="btn-group">
  51. <button class="btn-secondary" @click="reset">重新上传</button>
  52. <button class="btn-submit" @click="confirmCreate">确认创建</button>
  53. </view>
  54. </view>
  55. </view>
  56. </template>
  57. <script>
  58. import { uploadAndParseReport, createGrowthRecordFromUpload, checkGrowthDuplicate, getChildren } from '../../utils/api.js'
  59. export default {
  60. data() {
  61. return {
  62. step: 1,
  63. children: [],
  64. selectedChild: null,
  65. fileName: '',
  66. filePath: '',
  67. sourceFileUrl: '',
  68. parsedData: null,
  69. reportDateStr: '',
  70. manualDate: ''
  71. }
  72. },
  73. computed: {
  74. canUpload() {
  75. return this.selectedChild && this.filePath
  76. }
  77. },
  78. onLoad(options) {
  79. this.loadChildren(options.childId)
  80. },
  81. methods: {
  82. async loadChildren(targetChildId) {
  83. try {
  84. var res = await getChildren()
  85. if (res.code === 200) {
  86. this.children = res.data || []
  87. var target = null
  88. if (targetChildId) {
  89. target = this.children.find(function(c) { return String(c.id) === String(targetChildId) })
  90. }
  91. if (!target && this.children.length > 0) {
  92. target = this.children[0]
  93. }
  94. if (target) {
  95. this.selectedChild = target
  96. }
  97. }
  98. } catch (e) {
  99. console.error(e)
  100. }
  101. },
  102. selectChild() {
  103. var items = this.children.map(function(c) { return c.nickname || c.name })
  104. var self = this
  105. uni.showActionSheet({
  106. itemList: items,
  107. success: function(res) {
  108. var idx = res.tapIndex
  109. if (idx >= 0 && idx < self.children.length) {
  110. self.selectedChild = self.children[idx]
  111. }
  112. }
  113. })
  114. },
  115. chooseFile() {
  116. var self = this
  117. uni.chooseMessageFile({
  118. count: 1,
  119. type: 'file',
  120. extension: ['pdf'],
  121. success: function(res) {
  122. if (res.tempFiles && res.tempFiles.length > 0) {
  123. self.fileName = res.tempFiles[0].name
  124. self.filePath = res.tempFiles[0].path
  125. }
  126. },
  127. fail: function() {
  128. uni.showToast({ title: '请选择PDF文件', icon: 'none' })
  129. }
  130. })
  131. },
  132. async uploadAndParse() {
  133. if (!this.canUpload) return
  134. uni.showLoading({ title: '上传解析中...' })
  135. try {
  136. var res = await uploadAndParseReport(this.filePath)
  137. uni.hideLoading()
  138. if (res.code === 200) {
  139. this.sourceFileUrl = res.data.sourceFileUrl
  140. this.parsedData = res.data.parsed
  141. this.reportDateStr = res.data.reportDateStr
  142. this.step = 2
  143. } else {
  144. uni.showToast({ title: res.message || '解析失败', icon: 'none' })
  145. }
  146. } catch (e) {
  147. uni.hideLoading()
  148. uni.showToast({ title: e.message || '上传失败', icon: 'none' })
  149. }
  150. },
  151. onDateChange(e) {
  152. this.manualDate = e.detail.value
  153. },
  154. async confirmCreate() {
  155. var reportDate = this.manualDate || this.reportDateStr
  156. if (!reportDate && (!this.parsedData || !this.parsedData.reportDate)) {
  157. uni.showToast({ title: '请输入报告日期', icon: 'none' })
  158. return
  159. }
  160. uni.showLoading({ title: '创建中...' })
  161. try {
  162. var submitData = {
  163. childId: this.selectedChild.id,
  164. sourceFileUrl: this.sourceFileUrl
  165. }
  166. if (this.parsedData) {
  167. if (this.parsedData.overallScore) submitData.overallScore = this.parsedData.overallScore
  168. if (this.parsedData.attentionScore) submitData.attentionScore = this.parsedData.attentionScore
  169. if (this.parsedData.focusScore) submitData.focusScore = this.parsedData.focusScore
  170. if (this.parsedData.memoryScore) submitData.memoryScore = this.parsedData.memoryScore
  171. if (this.parsedData.logicScore) submitData.logicScore = this.parsedData.logicScore
  172. if (this.parsedData.perceptionScore) submitData.perceptionScore = this.parsedData.perceptionScore
  173. if (this.parsedData.spatialScore) submitData.spatialScore = this.parsedData.spatialScore
  174. if (this.parsedData.processingSpeedScore) submitData.processingSpeedScore = this.parsedData.processingSpeedScore
  175. if (this.parsedData.emotionScore) submitData.emotionScore = this.parsedData.emotionScore
  176. if (this.parsedData.resilienceScore) submitData.resilienceScore = this.parsedData.resilienceScore
  177. if (this.parsedData.assessmentSummary) submitData.assessmentSummary = this.parsedData.assessmentSummary
  178. if (this.parsedData.growthSuggestions) submitData.growthSuggestions = this.parsedData.growthSuggestions
  179. }
  180. if (this.manualDate) {
  181. submitData.manualReportDate = this.manualDate
  182. }
  183. var res = await createGrowthRecordFromUpload(submitData)
  184. uni.hideLoading()
  185. if (res.code === 200) {
  186. uni.showToast({ title: '创建成功', icon: 'success' })
  187. setTimeout(function() {
  188. uni.navigateTo({ url: '/pages/growth/detail?recordId=' + res.data.id })
  189. }, 500)
  190. } else {
  191. uni.showToast({ title: res.message || '创建失败', icon: 'none' })
  192. }
  193. } catch (e) {
  194. uni.hideLoading()
  195. uni.showToast({ title: e.message || '创建失败', icon: 'none' })
  196. }
  197. },
  198. reset() {
  199. this.step = 1
  200. this.fileName = ''
  201. this.filePath = ''
  202. this.sourceFileUrl = ''
  203. this.parsedData = null
  204. this.reportDateStr = ''
  205. this.manualDate = ''
  206. }
  207. }
  208. }
  209. </script>
  210. <style scoped>
  211. .container {
  212. padding: 30rpx;
  213. background: #F8F8F8;
  214. min-height: 100vh;
  215. }
  216. .header {
  217. text-align: center;
  218. padding: 40rpx 0 30rpx;
  219. }
  220. .title {
  221. font-size: 40rpx;
  222. font-weight: bold;
  223. color: #333;
  224. display: block;
  225. margin-bottom: 10rpx;
  226. }
  227. .subtitle {
  228. font-size: 26rpx;
  229. color: #999;
  230. }
  231. .form {
  232. background: #fff;
  233. border-radius: 20rpx;
  234. padding: 30rpx;
  235. }
  236. .form-item {
  237. margin-bottom: 30rpx;
  238. }
  239. .label {
  240. font-size: 28rpx;
  241. color: #333;
  242. display: block;
  243. margin-bottom: 16rpx;
  244. font-weight: bold;
  245. }
  246. .child-select {
  247. display: flex;
  248. align-items: center;
  249. height: 76rpx;
  250. border: 1rpx solid #E0E0E0;
  251. border-radius: 10rpx;
  252. padding: 0 20rpx;
  253. }
  254. .select-text {
  255. flex: 1;
  256. font-size: 28rpx;
  257. color: #333;
  258. }
  259. .select-placeholder {
  260. flex: 1;
  261. font-size: 28rpx;
  262. color: #ccc;
  263. }
  264. .select-arrow {
  265. font-size: 32rpx;
  266. color: #ccc;
  267. }
  268. .upload-area {
  269. display: flex;
  270. flex-direction: column;
  271. align-items: center;
  272. justify-content: center;
  273. height: 200rpx;
  274. border: 2rpx dashed #ccc;
  275. border-radius: 16rpx;
  276. background: #fafafa;
  277. }
  278. .upload-icon {
  279. font-size: 64rpx;
  280. color: #999;
  281. line-height: 1;
  282. }
  283. .upload-text {
  284. font-size: 26rpx;
  285. color: #999;
  286. margin-top: 10rpx;
  287. }
  288. .file-name {
  289. font-size: 28rpx;
  290. color: #667eea;
  291. word-break: break-all;
  292. text-align: center;
  293. }
  294. .btn-submit {
  295. width: 100%;
  296. height: 88rpx;
  297. line-height: 88rpx;
  298. background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  299. color: #fff;
  300. border-radius: 44rpx;
  301. font-size: 32rpx;
  302. font-weight: bold;
  303. text-align: center;
  304. }
  305. .btn-submit[disabled] {
  306. opacity: 0.5;
  307. }
  308. .preview-card {
  309. background: #f9f9ff;
  310. border-radius: 12rpx;
  311. padding: 24rpx;
  312. margin-bottom: 30rpx;
  313. }
  314. .preview-row {
  315. display: flex;
  316. margin-bottom: 16rpx;
  317. }
  318. .preview-label {
  319. font-size: 26rpx;
  320. color: #666;
  321. width: 160rpx;
  322. flex-shrink: 0;
  323. }
  324. .preview-value {
  325. font-size: 26rpx;
  326. color: #333;
  327. flex: 1;
  328. }
  329. .preview-value.summary {
  330. max-height: 200rpx;
  331. overflow: hidden;
  332. text-overflow: ellipsis;
  333. display: -webkit-box;
  334. -webkit-line-clamp: 4;
  335. -webkit-box-orient: vertical;
  336. }
  337. .picker-input {
  338. height: 76rpx;
  339. line-height: 76rpx;
  340. border: 1rpx solid #E0E0E0;
  341. border-radius: 10rpx;
  342. padding: 0 20rpx;
  343. font-size: 28rpx;
  344. color: #333;
  345. }
  346. .btn-group {
  347. display: flex;
  348. gap: 20rpx;
  349. }
  350. .btn-secondary {
  351. flex: 1;
  352. height: 88rpx;
  353. line-height: 88rpx;
  354. background: #fff;
  355. color: #667eea;
  356. border: 2rpx solid #667eea;
  357. border-radius: 44rpx;
  358. font-size: 30rpx;
  359. font-weight: bold;
  360. text-align: center;
  361. }
  362. .btn-group .btn-submit {
  363. flex: 1;
  364. }
  365. </style>