insurance-add.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  1. <template>
  2. <scroll-view class="container" scroll-y>
  3. <!-- 上传区域 -->
  4. <view class="upload-section" v-if="!isEdit && !parsedResult">
  5. <view class="upload-card" @click="chooseFile">
  6. <text class="upload-icon">📄</text>
  7. <text class="upload-title">上传保单PDF</text>
  8. <text class="upload-desc">选择保单文件,AI 自动识别条款内容</text>
  9. </view>
  10. <view v-if="uploading" class="upload-status">
  11. <text class="upload-status-text">正在解析保单...</text>
  12. <view class="spinner"></view>
  13. </view>
  14. </view>
  15. <!-- 解析结果预览(编辑模式不显示上传区) -->
  16. <view v-if="parsedResult && !isEdit" class="parsed-result">
  17. <view class="result-header">
  18. <text class="result-title">AI 识别结果</text>
  19. <text class="result-hint">请确认或修改下方信息</text>
  20. </view>
  21. <view class="result-summary">
  22. <text class="summary-label">保险公司:</text>
  23. <text class="summary-value">{{ parsedResult.extracted.insuranceCompany || '未识别' }}</text>
  24. </view>
  25. <view class="result-summary">
  26. <text class="summary-label">保单类型:</text>
  27. <text class="summary-value">{{ parsedResult.extracted.policyType || '未识别' }}</text>
  28. </view>
  29. <view class="result-summary">
  30. <text class="summary-label">保额:</text>
  31. <text class="summary-value orange">{{ parsedResult.extracted.sumInsured || '未识别' }}</text>
  32. </view>
  33. <view class="result-summary">
  34. <text class="summary-label">保费:</text>
  35. <text class="summary-value orange">{{ parsedResult.extracted.premium || '未识别' }}</text>
  36. </view>
  37. <view class="result-summary" v-if="parsedResult.extracted.summary">
  38. <text class="summary-label">摘要:</text>
  39. <text class="summary-value">{{ parsedResult.extracted.summary }}</text>
  40. </view>
  41. <view class="confirm-btn" @click="confirmParsed">
  42. <text>确认并使用此结果</text>
  43. </view>
  44. </view>
  45. <!-- 保单条款原文展示 -->
  46. <view v-if="parsedResult && parsedResult.rawText" class="terms-section">
  47. <view class="terms-header">
  48. <text class="terms-title">保单条款原文</text>
  49. <text class="terms-toggle" @click="showTerms = !showTerms">{{ showTerms ? '收起' : '展开' }}</text>
  50. </view>
  51. <scroll-view v-if="showTerms" class="terms-content" scroll-y>
  52. <text class="terms-text">{{ parsedResult.rawText }}</text>
  53. </scroll-view>
  54. </view>
  55. <!-- 表单 -->
  56. <view class="form-card" :class="{ 'form-hidden': parsedResult && !isEdit && !showForm }">
  57. <view class="form-group">
  58. <text class="form-label">保单名称 <text class="required">*</text></text>
  59. <input class="form-input" v-model="form.policyName" placeholder="请输入保单名称" />
  60. </view>
  61. <view class="form-group">
  62. <text class="form-label">保险公司</text>
  63. <input class="form-input" v-model="form.insuranceCompany" placeholder="请输入保险公司名称" />
  64. </view>
  65. <view class="form-group">
  66. <text class="form-label">保单类型</text>
  67. <picker class="form-picker" :value="policyTypeIndex" :range="policyTypes" @change="onTypeChange">
  68. <view class="picker-value">{{ policyTypes[policyTypeIndex] || '请选择保单类型' }}</view>
  69. </picker>
  70. </view>
  71. <view class="form-group">
  72. <text class="form-label">被保人 <text class="required">*</text></text>
  73. <input class="form-input" v-model="form.insuredPerson" placeholder="请输入被保人姓名" />
  74. </view>
  75. <view class="form-row">
  76. <view class="form-group half">
  77. <text class="form-label">保费</text>
  78. <input class="form-input" v-model="form.premium" placeholder="0.00" type="digit" />
  79. </view>
  80. <view class="form-group half">
  81. <text class="form-label">保额</text>
  82. <input class="form-input" v-model="form.sumInsured" placeholder="0.00" type="digit" />
  83. </view>
  84. </view>
  85. <view class="form-row">
  86. <view class="form-group half">
  87. <text class="form-label">生效日期</text>
  88. <picker class="form-picker" mode="date" :value="form.startDate" @change="onStartDateChange">
  89. <view class="picker-value">{{ form.startDate || '请选择' }}</view>
  90. </picker>
  91. </view>
  92. <view class="form-group half">
  93. <text class="form-label">到期日期</text>
  94. <picker class="form-picker" mode="date" :value="form.endDate" @change="onEndDateChange">
  95. <view class="picker-value">{{ form.endDate || '请选择' }}</view>
  96. </picker>
  97. </view>
  98. </view>
  99. <view class="form-group">
  100. <text class="form-label">续费类型</text>
  101. <picker class="form-picker" :value="renewalTypeIndex" :range="renewalTypes" @change="onRenewalChange">
  102. <view class="picker-value">{{ renewalTypes[renewalTypeIndex] || '请选择续费类型' }}</view>
  103. </picker>
  104. </view>
  105. <view class="form-group">
  106. <text class="form-label">保单号</text>
  107. <input class="form-input" v-model="form.policyNo" placeholder="请输入保单号" />
  108. </view>
  109. <view class="form-group">
  110. <text class="form-label">备注</text>
  111. <textarea class="form-textarea" v-model="form.remark" placeholder="备注信息" />
  112. </view>
  113. </view>
  114. <view class="submit-btn-wrap">
  115. <button class="submit-btn" :loading="submitting" @click="submitForm">
  116. {{ isEdit ? '更新保单' : '提交保存' }}
  117. </button>
  118. </view>
  119. </scroll-view>
  120. </template>
  121. <script>
  122. import { getInsuranceDetail, createInsurance, updateInsurance, uploadPolicy } from '../../utils/api'
  123. export default {
  124. data() {
  125. return {
  126. isEdit: false,
  127. submitting: false,
  128. uploading: false,
  129. showTerms: false,
  130. showForm: true,
  131. policyTypes: ['医疗险', '重疾险', '意外险', '教育金', '养老险'],
  132. policyTypeValues: ['medical', 'life', 'accident', 'education', 'endowment'],
  133. policyTypeIndex: -1,
  134. renewalTypes: ['年缴', '月缴', '趸交'],
  135. renewalTypeValues: ['annual', 'monthly', 'single'],
  136. renewalTypeIndex: -1,
  137. form: {
  138. policyName: '',
  139. insuranceCompany: '',
  140. policyType: '',
  141. insuredPerson: '',
  142. premium: '',
  143. sumInsured: '',
  144. startDate: '',
  145. endDate: '',
  146. renewalType: 'annual',
  147. policyNo: '',
  148. remark: ''
  149. },
  150. parsedResult: null
  151. }
  152. },
  153. onLoad(options) {
  154. if (options && options.id) {
  155. this.isEdit = true
  156. this.loadDetail(options.id)
  157. }
  158. },
  159. methods: {
  160. async loadDetail(id) {
  161. try {
  162. const res = await getInsuranceDetail(id)
  163. if (res && res.code === 200 && res.data) {
  164. const data = res.data
  165. this.form.policyName = data.policyName || ''
  166. this.form.insuranceCompany = data.insuranceCompany || ''
  167. this.form.policyType = data.policyType || ''
  168. this.form.insuredPerson = data.insuredPerson || ''
  169. this.form.premium = data.premium ? String(data.premium) : ''
  170. this.form.sumInsured = data.sumInsured ? String(data.sumInsured) : ''
  171. this.form.startDate = data.startDate ? data.startDate.substring(0, 10) : ''
  172. this.form.endDate = data.endDate ? data.endDate.substring(0, 10) : ''
  173. this.form.renewalType = data.renewalType || 'annual'
  174. this.form.policyNo = data.policyNo || ''
  175. this.form.remark = data.remark || ''
  176. this.policyTypeIndex = this.policyTypeValues.indexOf(data.policyType)
  177. this.renewalTypeIndex = this.renewalTypeValues.indexOf(data.renewalType)
  178. uni.setNavigationBarTitle({ title: '保单详情' })
  179. }
  180. } catch (e) {
  181. console.error('加载保单详情失败', e)
  182. }
  183. },
  184. onTypeChange(e) {
  185. this.policyTypeIndex = parseInt(e.detail.value)
  186. this.form.policyType = this.policyTypeValues[this.policyTypeIndex]
  187. },
  188. onRenewalChange(e) {
  189. this.renewalTypeIndex = parseInt(e.detail.value)
  190. this.form.renewalType = this.renewalTypeValues[this.renewalTypeIndex]
  191. },
  192. onStartDateChange(e) {
  193. this.form.startDate = e.detail.value
  194. },
  195. onEndDateChange(e) {
  196. this.form.endDate = e.detail.value
  197. },
  198. chooseFile() {
  199. var self = this
  200. uni.showActionSheet({
  201. itemList: ['从聊天选取PDF', '拍照上传'],
  202. success: function(res) {
  203. if (res.tapIndex === 0) {
  204. self.chooseChatFile()
  205. } else if (res.tapIndex === 1) {
  206. self.takePhoto()
  207. }
  208. },
  209. fail: function() {}
  210. })
  211. },
  212. chooseChatFile() {
  213. var self = this
  214. uni.chooseMessageFile({
  215. count: 1,
  216. type: 'file',
  217. success: function(res) {
  218. var files = res.tempFiles
  219. if (files && files.length > 0) {
  220. self.uploadAndParse(files[0].path, files[0].name || '保单文件')
  221. }
  222. },
  223. fail: function() {}
  224. })
  225. },
  226. takePhoto() {
  227. var self = this
  228. uni.chooseImage({
  229. count: 1,
  230. sourceType: ['camera', 'album'],
  231. success: function(res) {
  232. if (res.tempFilePaths && res.tempFilePaths.length > 0) {
  233. self.uploadAndParse(res.tempFilePaths[0], '照片')
  234. }
  235. }
  236. })
  237. },
  238. async uploadAndParse(filePath, fileName) {
  239. var self = this
  240. self.uploading = true
  241. uni.showLoading({ title: '正在解析...' })
  242. try {
  243. var res = await uploadPolicy(filePath)
  244. if (res && res.code === 200 && res.data) {
  245. self.parsedResult = res.data
  246. // 尝试回填表单字段
  247. var ext = res.data.extracted || {}
  248. if (ext.insuranceCompany) self.form.insuranceCompany = ext.insuranceCompany
  249. if (ext.policyType) self.form.policyType = ext.policyType
  250. if (ext.insuredPerson) self.form.insuredPerson = ext.insuredPerson
  251. if (ext.policyNo) self.form.policyNo = ext.policyNo
  252. if (ext.premium) self.form.premium = ext.premium
  253. if (ext.sumInsured) self.form.sumInsured = ext.sumInsured
  254. if (ext.policyName) self.form.policyName = ext.policyName
  255. // 转换保费/保额为数字
  256. if (ext.premium) {
  257. var p = parseFloat(ext.premium)
  258. if (!isNaN(p)) self.form.premium = String(p)
  259. }
  260. if (ext.sumInsured) {
  261. var s = parseFloat(ext.sumInsured)
  262. if (!isNaN(s)) self.form.sumInsured = String(s)
  263. }
  264. // 设置保单类型索引
  265. if (ext.policyType) {
  266. var idx = self.policyTypeValues.indexOf(ext.policyType)
  267. if (idx >= 0) self.policyTypeIndex = idx
  268. }
  269. uni.hideLoading()
  270. } else {
  271. uni.hideLoading()
  272. uni.showToast({ title: (res && res.message) || '解析失败', icon: 'none' })
  273. }
  274. } catch (e) {
  275. uni.hideLoading()
  276. console.error('上传解析失败', e)
  277. uni.showToast({ title: '上传解析失败,请重试', icon: 'none' })
  278. } finally {
  279. self.uploading = false
  280. }
  281. },
  282. confirmParsed() {
  283. this.showForm = true
  284. },
  285. async submitForm() {
  286. if (!this.form.policyName) {
  287. uni.showToast({ title: '请输入保单名称', icon: 'none' })
  288. return
  289. }
  290. if (!this.form.insuredPerson) {
  291. uni.showToast({ title: '请输入被保人姓名', icon: 'none' })
  292. return
  293. }
  294. this.submitting = true
  295. try {
  296. var payload = {
  297. policyName: this.form.policyName,
  298. insuranceCompany: this.form.insuranceCompany,
  299. policyType: this.form.policyType,
  300. insuredPerson: this.form.insuredPerson,
  301. premium: this.form.premium ? parseFloat(this.form.premium) : null,
  302. sumInsured: this.form.sumInsured ? parseFloat(this.form.sumInsured) : null,
  303. startDate: this.form.startDate || null,
  304. endDate: this.form.endDate || null,
  305. renewalType: this.form.renewalType,
  306. policyNo: this.form.policyNo,
  307. remark: this.form.remark
  308. }
  309. // 如果有解析结果,保存原始条款
  310. if (this.parsedResult && this.parsedResult.rawText) {
  311. payload.policyTerms = this.parsedResult.rawText
  312. }
  313. var res
  314. if (this.isEdit) {
  315. payload.id = this.$mp.query && this.$mp.query.id
  316. res = await updateInsurance(payload)
  317. } else {
  318. res = await createInsurance(payload)
  319. }
  320. if (res && res.code === 200) {
  321. uni.showToast({ title: this.isEdit ? '更新成功' : '创建成功', icon: 'success' })
  322. setTimeout(function() {
  323. uni.navigateBack()
  324. }, 1500)
  325. } else {
  326. uni.showToast({ title: res && res.message || '提交失败', icon: 'none' })
  327. }
  328. } catch (e) {
  329. uni.showToast({ title: '网络错误', icon: 'none' })
  330. } finally {
  331. this.submitting = false
  332. }
  333. }
  334. }
  335. }
  336. </script>
  337. <style>
  338. .container {
  339. min-height: 100vh;
  340. background: #f5f7fa;
  341. padding: 20rpx 30rpx 120rpx;
  342. width: 100%;
  343. box-sizing: border-box;
  344. overflow-x: hidden;
  345. }
  346. /* 上传区域 */
  347. .upload-section {
  348. margin-bottom: 24rpx;
  349. }
  350. .upload-card {
  351. background: #fff;
  352. border-radius: 20rpx;
  353. padding: 60rpx 40rpx;
  354. display: flex;
  355. flex-direction: column;
  356. align-items: center;
  357. border: 2rpx dashed #F97316;
  358. }
  359. .upload-icon {
  360. font-size: 80rpx;
  361. margin-bottom: 20rpx;
  362. }
  363. .upload-title {
  364. font-size: 32rpx;
  365. font-weight: 600;
  366. color: #333;
  367. margin-bottom: 12rpx;
  368. }
  369. .upload-desc {
  370. font-size: 24rpx;
  371. color: #999;
  372. }
  373. .upload-status {
  374. display: flex;
  375. flex-direction: column;
  376. align-items: center;
  377. padding: 40rpx 0;
  378. }
  379. .upload-status-text {
  380. font-size: 28rpx;
  381. color: #F97316;
  382. margin-bottom: 16rpx;
  383. }
  384. .spinner {
  385. width: 48rpx;
  386. height: 48rpx;
  387. border: 4rpx solid #FED7AA;
  388. border-top-color: #F97316;
  389. border-radius: 50%;
  390. animation: spin 1s linear infinite;
  391. }
  392. @keyframes spin {
  393. 0% { transform: rotate(0deg); }
  394. 100% { transform: rotate(360deg); }
  395. }
  396. /* 解析结果预览 */
  397. .parsed-result {
  398. background: #fff;
  399. border-radius: 20rpx;
  400. padding: 30rpx;
  401. margin-bottom: 24rpx;
  402. border: 2rpx solid #DCFCE7;
  403. }
  404. .result-header {
  405. display: flex;
  406. flex-direction: row;
  407. justify-content: space-between;
  408. align-items: center;
  409. margin-bottom: 20rpx;
  410. padding-bottom: 16rpx;
  411. border-bottom: 1rpx solid #f5f5f5;
  412. }
  413. .result-title {
  414. font-size: 30rpx;
  415. font-weight: 600;
  416. color: #16A34A;
  417. }
  418. .result-hint {
  419. font-size: 24rpx;
  420. color: #999;
  421. }
  422. .result-summary {
  423. display: flex;
  424. flex-direction: row;
  425. padding: 12rpx 0;
  426. font-size: 26rpx;
  427. }
  428. .summary-label {
  429. color: #666;
  430. width: 160rpx;
  431. flex-shrink: 0;
  432. }
  433. .summary-value {
  434. color: #333;
  435. flex: 1;
  436. }
  437. .summary-value.orange {
  438. color: #F97316;
  439. font-weight: 600;
  440. }
  441. .confirm-btn {
  442. margin-top: 24rpx;
  443. padding: 16rpx 0;
  444. text-align: center;
  445. background: #F0F9FF;
  446. border-radius: 12rpx;
  447. color: #0284C7;
  448. font-size: 28rpx;
  449. font-weight: 500;
  450. }
  451. /* 条款原文 */
  452. .terms-section {
  453. background: #fff;
  454. border-radius: 20rpx;
  455. padding: 30rpx;
  456. margin-bottom: 24rpx;
  457. border: 2rpx solid #FEF3C7;
  458. }
  459. .terms-header {
  460. display: flex;
  461. flex-direction: row;
  462. justify-content: space-between;
  463. align-items: center;
  464. margin-bottom: 16rpx;
  465. }
  466. .terms-title {
  467. font-size: 28rpx;
  468. font-weight: 600;
  469. color: #92400E;
  470. }
  471. .terms-toggle {
  472. font-size: 24rpx;
  473. color: #F59E0B;
  474. }
  475. .terms-content {
  476. max-height: 400rpx;
  477. }
  478. .terms-text {
  479. font-size: 24rpx;
  480. color: #555;
  481. line-height: 1.8;
  482. white-space: pre-wrap;
  483. word-break: break-all;
  484. }
  485. /* 表单 */
  486. .form-card {
  487. background: #fff;
  488. border-radius: 20rpx;
  489. padding: 30rpx;
  490. margin-bottom: 24rpx;
  491. }
  492. .form-group {
  493. margin-bottom: 28rpx;
  494. }
  495. .form-label {
  496. display: block;
  497. font-size: 28rpx;
  498. color: #333;
  499. font-weight: 500;
  500. margin-bottom: 12rpx;
  501. }
  502. .required {
  503. color: #DC2626;
  504. }
  505. .form-input {
  506. width: 100%;
  507. height: 88rpx;
  508. line-height: 88rpx;
  509. background: #F5F7FA;
  510. border-radius: 12rpx;
  511. padding: 0 20rpx;
  512. font-size: 28rpx;
  513. color: #333;
  514. box-sizing: border-box;
  515. }
  516. .form-picker {
  517. width: 100%;
  518. height: 80rpx;
  519. line-height: 80rpx;
  520. background: #F5F7FA;
  521. border-radius: 12rpx;
  522. padding: 0 20rpx;
  523. box-sizing: border-box;
  524. }
  525. .picker-value {
  526. font-size: 28rpx;
  527. color: #333;
  528. line-height: 80rpx;
  529. }
  530. .form-textarea {
  531. width: 100%;
  532. height: 160rpx;
  533. background: #F5F7FA;
  534. border-radius: 12rpx;
  535. padding: 20rpx;
  536. font-size: 28rpx;
  537. color: #333;
  538. box-sizing: border-box;
  539. }
  540. .form-row {
  541. display: flex;
  542. flex-direction: row;
  543. justify-content: space-between;
  544. }
  545. .form-group.half {
  546. width: 48%;
  547. }
  548. .submit-btn-wrap {
  549. padding: 20rpx 0 40rpx;
  550. }
  551. .submit-btn {
  552. width: 100%;
  553. height: 88rpx;
  554. line-height: 88rpx;
  555. text-align: center;
  556. background: #F97316;
  557. color: #fff;
  558. font-size: 30rpx;
  559. font-weight: 600;
  560. border-radius: 44rpx;
  561. border: none;
  562. }
  563. .submit-btn::after {
  564. border: none;
  565. }
  566. </style>