upload.vue 11 KB

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