insurance-add.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. <template>
  2. <scroll-view class="container" scroll-y :style="{ '--font-scale': fontScale }">
  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. import fontScaleMixin from '@/mixins/font-scale.js'
  124. export default {
  125. mixins: [fontScaleMixin],
  126. data() {
  127. return {
  128. isEdit: false,
  129. submitting: false,
  130. uploading: false,
  131. showTerms: false,
  132. showForm: true,
  133. policyTypes: ['医疗险', '重疾险', '意外险', '教育金', '养老险'],
  134. policyTypeValues: ['medical', 'life', 'accident', 'education', 'endowment'],
  135. policyTypeIndex: -1,
  136. renewalTypes: ['年缴', '月缴', '趸交'],
  137. renewalTypeValues: ['annual', 'monthly', 'single'],
  138. renewalTypeIndex: -1,
  139. form: {
  140. policyName: '',
  141. insuranceCompany: '',
  142. policyType: '',
  143. insuredPerson: '',
  144. premium: '',
  145. sumInsured: '',
  146. startDate: '',
  147. endDate: '',
  148. renewalType: 'annual',
  149. policyNo: '',
  150. remark: ''
  151. },
  152. parsedResult: null
  153. }
  154. },
  155. onLoad(options) {
  156. if (options && options.id) {
  157. this.isEdit = true
  158. this.loadDetail(options.id)
  159. }
  160. },
  161. methods: {
  162. async loadDetail(id) {
  163. try {
  164. const res = await getInsuranceDetail(id)
  165. if (res && res.code === 200 && res.data) {
  166. const data = res.data
  167. this.form.policyName = data.policyName || ''
  168. this.form.insuranceCompany = data.insuranceCompany || ''
  169. this.form.policyType = data.policyType || ''
  170. this.form.insuredPerson = data.insuredPerson || ''
  171. this.form.premium = data.premium ? String(data.premium) : ''
  172. this.form.sumInsured = data.sumInsured ? String(data.sumInsured) : ''
  173. this.form.startDate = data.startDate ? data.startDate.substring(0, 10) : ''
  174. this.form.endDate = data.endDate ? data.endDate.substring(0, 10) : ''
  175. this.form.renewalType = data.renewalType || 'annual'
  176. this.form.policyNo = data.policyNo || ''
  177. this.form.remark = data.remark || ''
  178. this.policyTypeIndex = this.policyTypeValues.indexOf(data.policyType)
  179. this.renewalTypeIndex = this.renewalTypeValues.indexOf(data.renewalType)
  180. uni.setNavigationBarTitle({ title: '保单详情' })
  181. }
  182. } catch (e) {
  183. console.error('加载保单详情失败', e)
  184. }
  185. },
  186. onTypeChange(e) {
  187. this.policyTypeIndex = parseInt(e.detail.value)
  188. this.form.policyType = this.policyTypeValues[this.policyTypeIndex]
  189. },
  190. onRenewalChange(e) {
  191. this.renewalTypeIndex = parseInt(e.detail.value)
  192. this.form.renewalType = this.renewalTypeValues[this.renewalTypeIndex]
  193. },
  194. onStartDateChange(e) {
  195. this.form.startDate = e.detail.value
  196. },
  197. onEndDateChange(e) {
  198. this.form.endDate = e.detail.value
  199. },
  200. chooseFile() {
  201. var self = this
  202. uni.showActionSheet({
  203. itemList: ['从聊天选取PDF', '从聊天选取图片', '拍照/相册上传'],
  204. success: function(res) {
  205. if (res.tapIndex === 0) {
  206. self.chooseChatFile()
  207. } else if (res.tapIndex === 1) {
  208. self.chooseChatImage()
  209. } else if (res.tapIndex === 2) {
  210. self.takePhoto()
  211. }
  212. },
  213. fail: function() {}
  214. })
  215. },
  216. chooseChatFile() {
  217. var self = this
  218. uni.chooseMessageFile({
  219. count: 1,
  220. type: 'file',
  221. success: function(res) {
  222. var files = res.tempFiles
  223. if (files && files.length > 0) {
  224. self.uploadAndParse(files[0].path, files[0].name || '保单文件')
  225. }
  226. },
  227. fail: function() {}
  228. })
  229. },
  230. chooseChatImage() {
  231. var self = this
  232. uni.chooseMessageFile({
  233. count: 1,
  234. type: 'image',
  235. success: function(res) {
  236. var files = res.tempFiles
  237. if (files && files.length > 0) {
  238. self.uploadAndParse(files[0].path, files[0].name || '聊天图片')
  239. }
  240. },
  241. fail: function() {}
  242. })
  243. },
  244. takePhoto() {
  245. var self = this
  246. uni.chooseImage({
  247. count: 1,
  248. sourceType: ['camera', 'album'],
  249. success: function(res) {
  250. if (res.tempFilePaths && res.tempFilePaths.length > 0) {
  251. self.uploadAndParse(res.tempFilePaths[0], '照片')
  252. }
  253. }
  254. })
  255. },
  256. async uploadAndParse(filePath, fileName) {
  257. var self = this
  258. self.uploading = true
  259. uni.showLoading({ title: '正在解析...' })
  260. try {
  261. var res = await uploadPolicy(filePath)
  262. if (res && res.code === 200 && res.data) {
  263. self.parsedResult = res.data
  264. // 尝试回填表单字段
  265. var ext = res.data.extracted || {}
  266. if (ext.insuranceCompany) self.form.insuranceCompany = ext.insuranceCompany
  267. if (ext.policyType) self.form.policyType = ext.policyType
  268. if (ext.insuredPerson) self.form.insuredPerson = ext.insuredPerson
  269. if (ext.policyNo) self.form.policyNo = ext.policyNo
  270. if (ext.premium) self.form.premium = ext.premium
  271. if (ext.sumInsured) self.form.sumInsured = ext.sumInsured
  272. if (ext.policyName) self.form.policyName = ext.policyName
  273. // 转换保费/保额为数字
  274. if (ext.premium) {
  275. var p = parseFloat(ext.premium)
  276. if (!isNaN(p)) self.form.premium = String(p)
  277. }
  278. if (ext.sumInsured) {
  279. var s = parseFloat(ext.sumInsured)
  280. if (!isNaN(s)) self.form.sumInsured = String(s)
  281. }
  282. // 设置保单类型索引
  283. if (ext.policyType) {
  284. var idx = self.policyTypeValues.indexOf(ext.policyType)
  285. if (idx >= 0) self.policyTypeIndex = idx
  286. }
  287. uni.hideLoading()
  288. } else {
  289. uni.hideLoading()
  290. uni.showToast({ title: (res && res.message) || '解析失败', icon: 'none' })
  291. }
  292. } catch (e) {
  293. uni.hideLoading()
  294. console.error('上传解析失败', e)
  295. uni.showToast({ title: '上传解析失败,请重试', icon: 'none' })
  296. } finally {
  297. self.uploading = false
  298. }
  299. },
  300. confirmParsed() {
  301. this.showForm = true
  302. },
  303. async submitForm() {
  304. if (!this.form.policyName) {
  305. uni.showToast({ title: '请输入保单名称', icon: 'none' })
  306. return
  307. }
  308. if (!this.form.insuredPerson) {
  309. uni.showToast({ title: '请输入被保人姓名', icon: 'none' })
  310. return
  311. }
  312. this.submitting = true
  313. try {
  314. var payload = {
  315. policyName: this.form.policyName,
  316. insuranceCompany: this.form.insuranceCompany,
  317. policyType: this.form.policyType,
  318. insuredPerson: this.form.insuredPerson,
  319. premium: this.form.premium ? parseFloat(this.form.premium) : null,
  320. sumInsured: this.form.sumInsured ? parseFloat(this.form.sumInsured) : null,
  321. startDate: this.form.startDate || null,
  322. endDate: this.form.endDate || null,
  323. renewalType: this.form.renewalType,
  324. policyNo: this.form.policyNo,
  325. remark: this.form.remark
  326. }
  327. // 如果有解析结果,保存原始条款
  328. if (this.parsedResult && this.parsedResult.rawText) {
  329. payload.policyTerms = this.parsedResult.rawText
  330. }
  331. var res
  332. if (this.isEdit) {
  333. payload.id = this.$mp.query && this.$mp.query.id
  334. res = await updateInsurance(payload)
  335. } else {
  336. res = await createInsurance(payload)
  337. }
  338. if (res && res.code === 200) {
  339. uni.showToast({ title: this.isEdit ? '更新成功' : '创建成功', icon: 'success' })
  340. setTimeout(function() {
  341. uni.navigateBack()
  342. }, 1500)
  343. } else {
  344. uni.showToast({ title: res && res.message || '提交失败', icon: 'none' })
  345. }
  346. } catch (e) {
  347. uni.showToast({ title: '网络错误', icon: 'none' })
  348. } finally {
  349. this.submitting = false
  350. }
  351. }
  352. }
  353. }
  354. </script>
  355. <style lang="scss">
  356. .container {
  357. min-height: 100vh;
  358. background: $app-bg;
  359. padding: 20rpx $uni-space-page 120rpx;
  360. width: 100%;
  361. box-sizing: border-box;
  362. overflow-x: hidden;
  363. }
  364. /* 上传区域 */
  365. .upload-section {
  366. margin-bottom: 24rpx;
  367. }
  368. .upload-card {
  369. background: $app-surface;
  370. border-radius: 24rpx;
  371. padding: 60rpx 40rpx;
  372. display: flex;
  373. flex-direction: column;
  374. align-items: center;
  375. border: 2rpx dashed $brand-primary;
  376. }
  377. .upload-icon {
  378. font-size: calc($uni-font-size-5xl * var(--font-scale, 1));
  379. margin-bottom: 20rpx;
  380. }
  381. .upload-title {
  382. font-size: calc(32rpx * var(--font-scale, 1));
  383. font-weight: 600;
  384. color: $facade-text-strong;
  385. margin-bottom: 12rpx;
  386. }
  387. .upload-desc {
  388. font-size: calc(24rpx * var(--font-scale, 1));
  389. color: $facade-text-muted;
  390. }
  391. .upload-status {
  392. display: flex;
  393. flex-direction: column;
  394. align-items: center;
  395. padding: 40rpx 0;
  396. }
  397. .upload-status-text {
  398. font-size: calc(28rpx * var(--font-scale, 1));
  399. color: $brand-primary;
  400. margin-bottom: 16rpx;
  401. }
  402. .spinner {
  403. width: 48rpx;
  404. height: 48rpx;
  405. border: 4rpx solid $brand-primary-light-2;
  406. border-top-color: $brand-primary;
  407. border-radius: 50%;
  408. animation: spin 1s linear infinite;
  409. }
  410. @keyframes spin {
  411. 0% { transform: rotate(0deg); }
  412. 100% { transform: rotate(360deg); }
  413. }
  414. /* 解析结果预览 */
  415. .parsed-result {
  416. background: $app-surface;
  417. border-radius: 24rpx;
  418. padding: $uni-space-lg;
  419. margin-bottom: 24rpx;
  420. border: 2rpx solid $status-success-soft-2;
  421. }
  422. .result-header {
  423. display: flex;
  424. flex-direction: row;
  425. justify-content: space-between;
  426. align-items: center;
  427. margin-bottom: 20rpx;
  428. padding-bottom: 16rpx;
  429. border-bottom: 1rpx solid $facade-fill-2;
  430. }
  431. .result-title {
  432. font-size: calc(30rpx * var(--font-scale, 1));
  433. font-weight: 600;
  434. color: $status-success-text;
  435. }
  436. .result-hint {
  437. font-size: calc(24rpx * var(--font-scale, 1));
  438. color: $facade-text-muted;
  439. }
  440. .result-summary {
  441. display: flex;
  442. flex-direction: row;
  443. padding: 12rpx 0;
  444. font-size: calc(26rpx * var(--font-scale, 1));
  445. }
  446. .summary-label {
  447. color: $facade-text-body;
  448. width: 160rpx;
  449. flex-shrink: 0;
  450. }
  451. .summary-value {
  452. color: $facade-text-strong;
  453. flex: 1;
  454. }
  455. .summary-value.orange {
  456. color: $brand-primary;
  457. font-weight: 600;
  458. }
  459. .confirm-btn {
  460. margin-top: 24rpx;
  461. padding: 16rpx 0;
  462. text-align: center;
  463. background: $tint-sky-soft;
  464. border-radius: 16rpx;
  465. color: $status-info-deep-2;
  466. font-size: calc(28rpx * var(--font-scale, 1));
  467. font-weight: 500;
  468. }
  469. /* 条款原文 */
  470. .terms-section {
  471. background: $app-surface;
  472. border-radius: 24rpx;
  473. padding: $uni-space-lg;
  474. margin-bottom: 24rpx;
  475. border: 2rpx solid $status-warning-soft;
  476. }
  477. .terms-header {
  478. display: flex;
  479. flex-direction: row;
  480. justify-content: space-between;
  481. align-items: center;
  482. margin-bottom: 16rpx;
  483. }
  484. .terms-title {
  485. font-size: calc(28rpx * var(--font-scale, 1));
  486. font-weight: 600;
  487. color: $status-warning-strong;
  488. }
  489. .terms-toggle {
  490. font-size: calc(24rpx * var(--font-scale, 1));
  491. color: $dim-wealth;
  492. }
  493. .terms-content {
  494. max-height: 400rpx;
  495. }
  496. .terms-text {
  497. font-size: calc(24rpx * var(--font-scale, 1));
  498. color: $facade-text-deep;
  499. line-height: 1.8;
  500. white-space: pre-wrap;
  501. word-break: break-all;
  502. }
  503. /* 表单 */
  504. .form-card {
  505. background: $app-surface;
  506. border-radius: 24rpx;
  507. padding: $uni-space-lg;
  508. margin-bottom: 24rpx;
  509. }
  510. .form-group {
  511. margin-bottom: 28rpx;
  512. }
  513. .form-label {
  514. display: block;
  515. font-size: calc(28rpx * var(--font-scale, 1));
  516. color: $facade-text-strong;
  517. font-weight: 500;
  518. margin-bottom: 12rpx;
  519. }
  520. .required {
  521. color: $status-danger-deep;
  522. }
  523. .form-input {
  524. width: 100%;
  525. height: 88rpx;
  526. line-height: 88rpx;
  527. background: $app-bg;
  528. border-radius: 16rpx;
  529. padding: 0 20rpx;
  530. font-size: calc(28rpx * var(--font-scale, 1));
  531. color: $facade-text-strong;
  532. box-sizing: border-box;
  533. }
  534. .form-picker {
  535. width: 100%;
  536. height: 80rpx;
  537. line-height: 80rpx;
  538. background: $app-bg;
  539. border-radius: 16rpx;
  540. padding: 0 20rpx;
  541. box-sizing: border-box;
  542. }
  543. .picker-value {
  544. font-size: calc(28rpx * var(--font-scale, 1));
  545. color: $facade-text-strong;
  546. line-height: 80rpx;
  547. }
  548. .form-textarea {
  549. width: 100%;
  550. height: 160rpx;
  551. background: $app-bg;
  552. border-radius: 16rpx;
  553. padding: 20rpx;
  554. font-size: calc(28rpx * var(--font-scale, 1));
  555. color: $facade-text-strong;
  556. box-sizing: border-box;
  557. }
  558. .form-row {
  559. display: flex;
  560. flex-direction: row;
  561. justify-content: space-between;
  562. }
  563. .form-group.half {
  564. width: 48%;
  565. }
  566. .submit-btn-wrap {
  567. padding: 20rpx 0 40rpx;
  568. }
  569. .submit-btn {
  570. width: 100%;
  571. height: 88rpx;
  572. line-height: 88rpx;
  573. text-align: center;
  574. background: $brand-primary;
  575. color: $app-surface;
  576. font-size: calc(30rpx * var(--font-scale, 1));
  577. font-weight: 600;
  578. border-radius: 24rpx;
  579. border: none;
  580. }
  581. .submit-btn::after {
  582. border: none;
  583. }
  584. </style>