index.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <template>
  2. <div class="identity-page">
  3. <van-nav-bar title="自然人开票" fixed placeholder />
  4. <!-- 上传区域 -->
  5. <div class="upload-section">
  6. <div class="upload-item" v-for="(item, index) in uploadList" :key="item.field">
  7. <van-uploader
  8. v-model="item.fileList"
  9. :max-count="1"
  10. :after-read="(file) => handleAfterRead(file, item, index)"
  11. :deletable="true"
  12. accept="image/*"
  13. :multiple="false"
  14. preview-size="auto"
  15. class="custom-uploader"
  16. >
  17. <template #default>
  18. <div
  19. class="upload-box"
  20. v-if="!item.fileList.length"
  21. :style="{ backgroundImage: `url(${item.bg})` }"
  22. >
  23. <div class="overlay">
  24. <van-icon name="photograph" class="camera-icon" />
  25. </div>
  26. </div>
  27. </template>
  28. </van-uploader>
  29. <p class="upload-text">{{ item.label }}</p>
  30. </div>
  31. </div>
  32. <!-- 说明文字 -->
  33. <div class="info-section">
  34. <p class="info-title">当前操作可能用于:</p>
  35. <ol class="info-list">
  36. <li>开具数电发票</li>
  37. <li>上传身份证信息登录电子税务局完成实名认证</li>
  38. <li>办理其他涉税、高风险业务</li>
  39. </ol>
  40. <p class="info-tip">请您在如未认证用途中完成认证操作。</p>
  41. </div>
  42. <!-- 下一步按钮 -->
  43. <div class="btn-section">
  44. <van-button block type="primary" round class="next-btn" :disabled="!isReady" @click="onNext">
  45. 下一步
  46. </van-button>
  47. </div>
  48. </div>
  49. </template>
  50. <script setup lang="ts">
  51. import { ref, computed } from 'vue'
  52. import { showToast, showFailToast } from 'vant'
  53. import { sysFileUploadApi } from '@/services/modules/common'
  54. /** 上传项类型 */
  55. interface UploadFileItem {
  56. url?: string
  57. file?: File
  58. fileId?: string
  59. status?: 'uploading' | 'done' | 'failed'
  60. message?: string
  61. }
  62. interface UploadItem {
  63. label: string
  64. field: 'front' | 'back'
  65. fileList: UploadFileItem[]
  66. bg: string
  67. }
  68. /** 背景图 */
  69. import frontBg from '@/assets/images/id-front-bg.png'
  70. import backBg from '@/assets/images/id-back-bg.png'
  71. /** 上传列表 */
  72. const uploadList = ref<UploadItem[]>([
  73. { label: '请上传身份证人像面', field: 'front', fileList: [], bg: frontBg },
  74. { label: '请上传身份证国徽面', field: 'back', fileList: [], bg: backBg },
  75. ])
  76. /** 上传+OCR识别逻辑 */
  77. const handleAfterRead = async (file: any, item: UploadItem, index: number) => {
  78. const target = uploadList.value[index]
  79. if (!target) return
  80. try {
  81. target.fileList = [{ status: 'uploading', message: '上传中...' }]
  82. const formData = new FormData()
  83. formData.append('file', file.file)
  84. // 调用上传接口
  85. const res = await sysFileUploadApi(formData)
  86. if (res.code === 0 && res.data?.url) {
  87. const baseUrl = import.meta.env.VITE_APP_URL
  88. const fullUrl = res.data.url.startsWith('http') ? res.data.url : `${baseUrl}${res.data.url}`
  89. target.fileList = [{ url: fullUrl, status: 'done', fileId: res.data.fileId }]
  90. showToast(`${item.label} 上传成功`)
  91. // 调用OCR识别
  92. const ocrRes = await handleOcrCheck(item.field, res.data.fileId, fullUrl)
  93. if (!ocrRes.success) {
  94. // OCR 识别失败:清除该图片并提示
  95. target.fileList = []
  96. showFailToast(`识别${item.label}失败,请重新上传`)
  97. }
  98. } else {
  99. target.fileList = [{ status: 'failed', message: '上传失败' }]
  100. showFailToast(res.msg || '上传失败')
  101. }
  102. } catch (error) {
  103. console.error('upload error:', error)
  104. target.fileList = [{ status: 'failed', message: '上传异常' }]
  105. showFailToast('上传异常,请重试')
  106. }
  107. }
  108. /** 模拟OCR识别接口逻辑(你可以替换成实际API) */
  109. const handleOcrCheck = async (
  110. side: 'front' | 'back',
  111. fileId: string,
  112. url: string,
  113. ): Promise<{ success: boolean }> => {
  114. // 示例:这里可以根据 side 调用不同的 OCR 接口
  115. // 例如 front 调用 /ocr/idcard/front back 调用 /ocr/idcard/back
  116. try {
  117. console.log('识别请求 =>', { side, fileId, url })
  118. // 模拟延时识别
  119. await new Promise((resolve) => setTimeout(resolve, 800))
  120. // ✅ 模拟成功 / ❌ 模拟失败
  121. const success = Math.random() > 0.2 // 80% 成功率示例
  122. return { success }
  123. } catch (err) {
  124. console.error('OCR error', err)
  125. return { success: false }
  126. }
  127. }
  128. /** 是否完成上传 */
  129. const isReady = computed(() => uploadList.value.every((item) => item.fileList.length > 0))
  130. /** 下一步按钮点击 */
  131. const onNext = () => {
  132. if (!isReady.value) return showToast('请先上传身份证正反面')
  133. // 拼装最终提交数据
  134. const result = uploadList.value.map((item) => ({
  135. field: item.field,
  136. fileId: item.fileList[0]?.fileId,
  137. url: item.fileList[0]?.url,
  138. }))
  139. console.log('提交数据:', result)
  140. showToast('上传完成,进入下一步')
  141. }
  142. </script>
  143. <style scoped>
  144. .identity-page {
  145. background-color: #f7f8fa;
  146. min-height: 100vh;
  147. padding: 4vw;
  148. box-sizing: border-box;
  149. font-size: 3.6vw;
  150. color: #333;
  151. }
  152. /* 上传区域 */
  153. .upload-section {
  154. display: flex;
  155. justify-content: space-between;
  156. margin: 10vw 0;
  157. }
  158. .upload-item {
  159. flex: 1;
  160. display: flex;
  161. flex-direction: column;
  162. align-items: center;
  163. text-align: center;
  164. }
  165. .upload-box {
  166. width: 35vw;
  167. height: 22vw;
  168. border-radius: 2vw;
  169. background-size: cover;
  170. background-position: center;
  171. background-repeat: no-repeat;
  172. position: relative;
  173. overflow: hidden;
  174. box-shadow: inset 0 0 2vw rgba(0, 0, 0, 0.05);
  175. margin-bottom: 3vw;
  176. }
  177. .overlay {
  178. width: 100%;
  179. height: 100%;
  180. background: rgba(0, 0, 0, 0.25);
  181. display: flex;
  182. align-items: center;
  183. justify-content: center;
  184. }
  185. .camera-icon {
  186. font-size: 8vw;
  187. color: #fff;
  188. opacity: 0.9;
  189. }
  190. .upload-text {
  191. font-size: 3.4vw;
  192. color: #666;
  193. }
  194. /* 预览图尺寸与上传框保持一致 */
  195. .custom-uploader >>> .van-uploader__preview,
  196. .custom-uploader >>> .van-uploader__preview-image {
  197. width: 35vw !important;
  198. height: 22vw !important;
  199. border-radius: 2vw;
  200. object-fit: cover;
  201. }
  202. /* 说明文字 */
  203. .info-section {
  204. margin: 8vw 0 12vw;
  205. line-height: 1.8;
  206. }
  207. .info-title {
  208. font-weight: 600;
  209. margin-bottom: 2vw;
  210. }
  211. .info-list {
  212. padding-left: 5vw;
  213. color: #444;
  214. }
  215. .info-list li {
  216. margin-bottom: 1.5vw;
  217. }
  218. .info-tip {
  219. color: #999;
  220. font-size: 3.2vw;
  221. margin-top: 3vw;
  222. }
  223. /* 按钮 */
  224. .btn-section {
  225. padding: 0 5vw;
  226. }
  227. .next-btn {
  228. font-size: 4vw;
  229. height: 12vw;
  230. background: linear-gradient(90deg, #ff7a00, #ff9800);
  231. border: none;
  232. }
  233. .next-btn:disabled {
  234. background: #ffd1a1;
  235. color: #fff;
  236. }
  237. </style>