interview.vue 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  1. <template>
  2. <view class="container">
  3. <!-- <view class="header">
  4. <text class="title">手部照片采集</text>
  5. </view> -->
  6. <view class="photo-container">
  7. <view class="instruction">
  8. <text>{{ instructions[currentStep] }}</text>
  9. </view>
  10. <view class="preview-container">
  11. <image v-if="photos[currentStep]" :src="photos[currentStep]" mode="aspectFit" class="preview-image"></image>
  12. <view v-else class="empty-preview">
  13. <text>{{ previewTexts[currentStep] }}</text>
  14. </view>
  15. </view>
  16. <view class="thumbnails">
  17. <view v-for="(photo, index) in photos" :key="index"
  18. class="thumbnail-item"
  19. :class="{ active: currentStep === index, completed: photo }">
  20. <image v-if="photo" :src="photo" mode="aspectFill" class="thumbnail-image" @click="previewPhoto(index)"></image>
  21. <view v-else class="thumbnail-placeholder">{{ index + 1 }}</view>
  22. </view>
  23. </view>
  24. </view>
  25. <view class="controls">
  26. <button class="camera-btn" @click="takePhoto">{{ photos[currentStep] ? '重新拍摄' : '拍摄照片' }}</button>
  27. <view class="navigation-btns">
  28. <button class="nav-btn prev" :disabled="currentStep === 0" @click="prevStep">上一步</button>
  29. <button class="nav-btn next" :disabled="!canGoNext" @click="nextStep">
  30. {{ currentStep === 5 && photos[5] ? '完成' : '下一步' }}
  31. </button>
  32. </view>
  33. </view>
  34. <!-- 为小程序环境添加camera组件 -->
  35. <view v-if="!isH5 && showCamera" class="camera-container">
  36. <cover-view class="gesture-hint">
  37. <cover-view class="gesture-hint-text">{{ gestureHints[currentStep] }}</cover-view>
  38. </cover-view>
  39. <camera
  40. :device-position="cameraPosition"
  41. flash="auto"
  42. @error="handleCameraError"
  43. class="camera-component"
  44. :mode="cameraMode"
  45. frame-size="medium"
  46. ></camera>
  47. <!-- 添加手势引导蒙层 -->
  48. <view class="gesture-overlay">
  49. <!-- 添加右上角引导图标 -->
  50. <view class="guide-icon" :style="currentStep>=3?'left:30rpx':'right:30rpx'">
  51. <!-- <image :src="guideIcon[currentStep]" class="guide-image"></image> -->
  52. <video
  53. :src="guideIcon[currentStep]"
  54. id="mpVideo"
  55. autoplay
  56. class="mp-video-player"
  57. :controls="false"
  58. loop
  59. muted
  60. object-fit="contain"
  61. ></video>
  62. <!-- <text class="guide-text">引导</text> -->
  63. </view>
  64. <image
  65. :src="gestureOverlays[currentStep]"
  66. class="gesture-image"
  67. ></image>
  68. </view>
  69. <cover-view class="camera-controls">
  70. <cover-view class="capture-btn" @tap="capturePhotoMP"></cover-view>
  71. <cover-view class="camera-buttons">
  72. <!-- <cover-view class="switch-camera-btn" @tap="switchCamera">切换摄像头</cover-view> -->
  73. <cover-view class="cancel-btn" @tap="cancelCamera">取消</cover-view>
  74. </cover-view>
  75. </cover-view>
  76. </view>
  77. <!-- 为浏览器环境添加视频元素 -->
  78. <view v-if="isH5" class="camera-container" v-show="showCamera">
  79. <!-- <video ref="videoElement" class="camera-video" autoplay></video> -->
  80. <canvas ref="canvasElement" class="camera-canvas" style="display: none;"></canvas>
  81. <!-- 添加H5环境的手势引导蒙层 -->
  82. <view class="gesture-overlay">
  83. <!-- 添加右上角引导图标 -->
  84. <view class="guide-icon" :style="currentStep>=3?'left:30rpx':'right:30rpx'">
  85. <image :src="guideIcon[currentStep]" class="guide-image"></image>
  86. <!-- <text class="guide-text">引导</text> -->
  87. </view>
  88. <image
  89. :src="gestureOverlays[currentStep]"
  90. class="gesture-image"
  91. ></image>
  92. <view class="gesture-hint">
  93. <text class="gesture-hint-text">{{ gestureHints[currentStep] }}</text>
  94. </view>
  95. </view>
  96. <view class="camera-buttons">
  97. <button class="capture-btn" @click="capturePhoto">拍照</button>
  98. <button class="switch-camera-btn" @click="switchCamera">切换摄像头</button>
  99. <button class="cancel-btn" @click="cancelCamera">取消</button>
  100. </view>
  101. </view>
  102. </view>
  103. </template>
  104. <script>
  105. import { apiBaseUrl } from '@/common/config.js'; // Import the base URL from a config file
  106. export default {
  107. data() {
  108. return {
  109. currentStep: 0,
  110. photos: [null, null, null, null, null, null], // 存储6张照片:左手正面、左手反面、左手握拳、右手正面、右手反面、右手握拳
  111. instructions: [
  112. '请将左手掌正面朝上,保持手指自然伸展,确保整个手掌清晰可见',
  113. '请将左手掌反面朝上,保持手指自然伸展,确保整个手背清晰可见',
  114. '请握紧左手拳头,使拳面朝向相机,确保整个拳头清晰可见',
  115. '请将右手掌正面朝上,保持手指自然伸展,确保整个手掌清晰可见',
  116. '请将右手掌反面朝上,保持手指自然伸展,确保整个手背清晰可见',
  117. '请握紧右手拳头,使拳面朝向相机,确保整个拳头清晰可见'
  118. ],
  119. previewTexts: [
  120. '左手掌正面照片预览区',
  121. '左手掌反面照片预览区',
  122. '左手握拳照片预览区',
  123. '右手掌正面照片预览区',
  124. '右手掌反面照片预览区',
  125. '右手握拳照片预览区'
  126. ],
  127. photoTypes: ['left_palm', 'left_back', 'left_fist', 'right_palm', 'right_back', 'right_fist'], // 照片类型标识
  128. isH5: false,
  129. showCamera: false,
  130. mediaStream: null,
  131. cameraContext: null,
  132. cameraMode: 'normal', // 添加相机模式
  133. cameraInitRetries: 0, // 添加重试计数器
  134. gestureOverlays: [
  135. 'http://121.36.251.245:9000/minlong/%E5%B7%A6%E6%89%8B%E6%89%8B%E6%8E%8C.png',
  136. 'http://121.36.251.245:9000/minlong/%E5%B7%A6%E6%89%8B%E6%89%8B%E8%83%8C.png',
  137. 'http://121.36.251.245:9000/minlong/%E5%B7%A6%E6%89%8B%E6%8F%A1%E6%8B%B3.png',
  138. 'http://121.36.251.245:9000/minlong/%E5%8F%B3%E6%89%8B%E6%89%8B%E6%8E%8C.png',
  139. 'http://121.36.251.245:9000/minlong/%E5%8F%B3%E6%89%8B%E6%89%8B%E8%83%8C.png',
  140. 'http://121.36.251.245:9000/minlong/%E5%8F%B3%E6%89%8B%E6%8F%A1%E6%8B%B3.png'
  141. /* '/static/images/palm_overlay.png', // 手掌正面引导图
  142. '/static/images/back_overlay.png', // 手掌反面引导图
  143. '/static/images/fist_overlay.png' // 握拳引导图 */
  144. ],
  145. gestureHints: [
  146. '请将左手掌对准轮廓线',
  147. '请将左手背对准轮廓线',
  148. '请将左手拳头对准轮廓线',
  149. '请将右手掌对准轮廓线',
  150. '请将右手背对准轮廓线',
  151. '请将右手拳头对准轮廓线'
  152. ],
  153. photoLinks: [null, null, null, null, null, null], // 存储上传后的图片链接
  154. cameraPosition: 'front', // 修改默认为前置摄像头
  155. guideIcon: [
  156. 'http://121.36.251.245:9000/minlong/01%E5%B7%A6%E6%89%8B%E6%89%8B%E6%8E%8C.mp4',
  157. 'http://121.36.251.245:9000/minlong/02%E5%B7%A6%E6%89%8B%E6%89%8B%E8%83%8C.mp4',
  158. 'http://121.36.251.245:9000/minlong/03%E5%B7%A6%E6%89%8B%E6%8F%A1%E6%8B%B3.mp4',
  159. 'http://121.36.251.245:9000/minlong/04%E5%8F%B3%E6%89%8B%E6%89%8B%E6%8E%8C.mp4',
  160. 'http://121.36.251.245:9000/minlong/05%E5%8F%B3%E6%89%8B%E6%89%8B%E8%83%8C.mp4',
  161. 'http://121.36.251.245:9000/minlong/06%E5%8F%B3%E6%89%8B%E6%8F%A1%E6%8B%B3.mp4',
  162. ]
  163. }
  164. },
  165. computed: {
  166. canGoNext() {
  167. // 只有当前步骤的照片已拍摄才能进入下一步
  168. return this.photos[this.currentStep] !== null;
  169. }
  170. },
  171. onLoad() {
  172. // 判断当前平台
  173. // #ifdef H5
  174. this.isH5 = true;
  175. // #endif
  176. // 检查相机权限
  177. // #ifdef MP-WEIXIN
  178. uni.authorize({
  179. scope: 'scope.camera',
  180. success: () => {
  181. console.log('相机权限已授权');
  182. },
  183. fail: () => {
  184. uni.showModal({
  185. title: '提示',
  186. content: '请授权相机权限以完成手部照片采集',
  187. success: (res) => {
  188. if (res.confirm) {
  189. uni.openSetting();
  190. }
  191. }
  192. });
  193. }
  194. });
  195. // #endif
  196. },
  197. beforeDestroy() {
  198. // 在组件销毁前停止摄像头流
  199. this.stopMediaStream();
  200. },
  201. methods: {
  202. takePhoto() {
  203. // 根据平台选择不同的拍照方法
  204. if (this.isH5) {
  205. this.startCamera();
  206. } else {
  207. // 小程序环境使用camera组件
  208. this.showCamera = true;
  209. this.cameraInitRetries = 0; // 重置重试计数器
  210. this.$nextTick(() => {
  211. // 创建相机上下文
  212. // #ifdef MP-WEIXIN || MP-BAIDU || MP-QQ || MP-TOUTIAO
  213. try {
  214. this.cameraContext = uni.createCameraContext();
  215. console.log('相机上下文创建成功');
  216. } catch (err) {
  217. console.error('创建相机上下文失败:', err);
  218. this.handleCameraInitError();
  219. }
  220. // #endif
  221. });
  222. }
  223. },
  224. uploadPhoto(filePathOrData, type) {
  225. console.log(`准备上传${type}类型的手部照片`);
  226. uni.showLoading({
  227. title: '上传中...'
  228. });
  229. // 获取用户openid
  230. const userInfo = uni.getStorageSync('userInfo');
  231. const openid = userInfo ? JSON.parse(userInfo).openid || '' : '';
  232. const tenant_id = uni.getStorageSync('tenant_id') || '1';
  233. // 检查用户信息是否完整
  234. if (!tenant_id) {
  235. uni.hideLoading();
  236. uni.showToast({
  237. title: '用户信息不完整,请重新登录',
  238. icon: 'none'
  239. });
  240. return;
  241. }
  242. // 定义更详细的照片描述
  243. const photoDescriptions = {
  244. 'left_palm': '展示完整的左手手掌',
  245. 'left_back': '展示完整的左手手背',
  246. 'left_fist': '展示完整的左手握拳',
  247. 'right_palm': '展示完整的右手手掌',
  248. 'right_back': '展示完整的右手手背',
  249. 'right_fist': '展示完整的右手握拳'
  250. };
  251. // 获取当前照片类型的详细描述
  252. const description = photoDescriptions[type] || `手部照片-${type}`;
  253. // 如果是H5环境且是base64数据
  254. if (this.isH5 && filePathOrData.startsWith('data:image')) {
  255. // 将base64转为blob对象
  256. const base64Data = filePathOrData.split(',')[1];
  257. const blob = this.base64ToBlob(base64Data, 'image/jpeg');
  258. const formData = new FormData();
  259. // 确保文件参数名称正确
  260. formData.append('photo', blob, `${type}.jpg`);
  261. formData.append('application_id', uni.getStorageSync('appId'));
  262. formData.append('tenant_id', tenant_id);
  263. formData.append('description', description);
  264. console.log('H5上传参数:', {
  265. application_id: uni.getStorageSync('appId'),
  266. tenant_id: tenant_id,
  267. description: description
  268. });
  269. // 使用fetch直接上传到 job/upload_posture_photo 接口
  270. fetch(`${apiBaseUrl}/job/upload_posture_photo`, {
  271. method: 'POST',
  272. body: formData
  273. })
  274. .then(response => response.json())
  275. .then(result => {
  276. console.log('照片上传成功:', result);
  277. // 保存图片链接到对应类型的索引位置
  278. const index = this.photoTypes.indexOf(type);
  279. if (index !== -1 && result.data && result.data.url) {
  280. this.$set(this.photoLinks, index, result.data.url);
  281. console.log(`已保存${type}类型照片链接:`, result.data.url);
  282. }
  283. uni.hideLoading();
  284. uni.showToast({
  285. title: '照片上传成功',
  286. icon: 'success',
  287. duration: 1500,
  288. success: () => {
  289. // 上传成功后自动进入下一步
  290. setTimeout(() => {
  291. this.autoNextStep();
  292. }, 1500);
  293. }
  294. });
  295. })
  296. .catch(error => {
  297. console.error('照片上传失败:', error);
  298. uni.hideLoading();
  299. uni.showToast({
  300. title: '照片上传失败,请重试',
  301. icon: 'none'
  302. });
  303. });
  304. } else {
  305. // 小程序环境的上传逻辑
  306. console.log(`小程序环境上传文件路径:`, filePathOrData);
  307. console.log('小程序上传参数:', {
  308. application_id: uni.getStorageSync('appId'),
  309. tenant_id: tenant_id,
  310. description: description
  311. });
  312. uni.uploadFile({
  313. url: `${apiBaseUrl}/job/upload_posture_photo`,
  314. filePath: filePathOrData,
  315. name: 'photo', // 确保文件参数名称正确
  316. formData: { // 使用formData替代data以确保参数正确传递
  317. 'application_id': uni.getStorageSync('appId'),
  318. 'tenant_id': tenant_id,
  319. 'description': description
  320. },
  321. success: (uploadRes) => {
  322. console.log('照片上传成功:', uploadRes);
  323. // 解析返回的JSON字符串
  324. let result;
  325. try {
  326. if (typeof uploadRes.data === 'string') {
  327. result = JSON.parse(uploadRes.data);
  328. } else {
  329. result = uploadRes.data;
  330. }
  331. console.log('解析后的上传结果:', result);
  332. // 保存图片链接到对应类型的索引位置
  333. const index = this.photoTypes.indexOf(type);
  334. if (index !== -1 && result.data && result.data.url) {
  335. this.$set(this.photoLinks, index, result.data.url);
  336. console.log(`已保存${type}类型照片链接:`, result.data.url);
  337. }
  338. } catch (e) {
  339. console.error('解析上传结果失败:', e);
  340. }
  341. uni.hideLoading();
  342. uni.showToast({
  343. title: '照片上传成功',
  344. icon: 'success',
  345. duration: 1500,
  346. success: () => {
  347. // 上传成功后自动进入下一步
  348. setTimeout(() => {
  349. this.autoNextStep();
  350. }, 1500);
  351. }
  352. });
  353. },
  354. fail: (err) => {
  355. console.error('照片上传失败:', err);
  356. uni.hideLoading();
  357. uni.showToast({
  358. title: '照片上传失败,请重试',
  359. icon: 'none'
  360. });
  361. },
  362. complete: () => {
  363. uni.hideLoading();
  364. }
  365. });
  366. }
  367. },
  368. prevStep() {
  369. if (this.currentStep > 0) {
  370. this.currentStep--;
  371. }
  372. },
  373. nextStep() {
  374. if (this.currentStep < 5) {
  375. this.currentStep++;
  376. } else if (this.currentStep === 5 && this.photos[5]) {
  377. // 所有照片都已拍摄完成,可以进行提交或跳转
  378. this.submitAllPhotos();
  379. }
  380. },
  381. previewPhoto(index) {
  382. // 如果照片存在,则预览
  383. if (this.photos[index]) {
  384. uni.previewImage({
  385. urls: [this.photos[index]],
  386. current: this.photos[index]
  387. });
  388. }
  389. },
  390. submitAllPhotos() {
  391. // 检查是否所有照片都已拍摄
  392. if (this.photos.every(photo => photo !== null)) {
  393. uni.showLoading({
  394. title: '处理中...'
  395. });
  396. // 所有照片已经单独上传,这里只需要处理跳转
  397. setTimeout(() => {
  398. uni.hideLoading();
  399. uni.showToast({
  400. title: '手部照片采集完成',
  401. icon: 'success',
  402. duration: 2000,
  403. success: () => {
  404. // 延迟跳转,让用户看到成功提示
  405. setTimeout(() => {
  406. // 使用redirectTo替代navigateTo,避免页面栈溢出
  407. uni.redirectTo({
  408. url: '/pages/interview_success/interview_success'
  409. });
  410. }, 100);
  411. }
  412. });
  413. }, 500);
  414. } else {
  415. uni.showToast({
  416. title: '请完成所有照片拍摄',
  417. icon: 'none'
  418. });
  419. }
  420. },
  421. // H5环境下启动摄像头
  422. startCamera() {
  423. this.showCamera = true;
  424. // 确保浏览器支持getUserMedia
  425. if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
  426. // 根据当前摄像头位置设置facingMode
  427. const facingMode = this.cameraPosition === 'front' ? 'user' : 'environment';
  428. navigator.mediaDevices.getUserMedia({
  429. video: { facingMode: facingMode }
  430. })
  431. .then(stream => {
  432. this.mediaStream = stream;
  433. // 等待DOM更新后再设置视频源
  434. this.$nextTick(() => {
  435. const videoElement = this.$refs.videoElement;
  436. if (videoElement) {
  437. videoElement.srcObject = stream;
  438. }
  439. });
  440. })
  441. .catch(error => {
  442. console.error('获取摄像头失败:', error);
  443. uni.showToast({
  444. title: '无法访问摄像头,请检查权限设置',
  445. icon: 'none'
  446. });
  447. this.showCamera = false;
  448. });
  449. } else {
  450. uni.showToast({
  451. title: '您的浏览器不支持摄像头功能',
  452. icon: 'none'
  453. });
  454. this.showCamera = false;
  455. }
  456. },
  457. // H5环境下拍照
  458. capturePhoto() {
  459. console.log('执行H5拍照方法');
  460. const videoElement = this.$refs.videoElement;
  461. const canvasElement = this.$refs.canvasElement;
  462. if (videoElement && canvasElement) {
  463. try {
  464. const context = canvasElement.getContext('2d');
  465. // 设置canvas尺寸与视频一致
  466. canvasElement.width = videoElement.videoWidth;
  467. canvasElement.height = videoElement.videoHeight;
  468. // 在canvas上绘制当前视频帧
  469. context.drawImage(videoElement, 0, 0, canvasElement.width, canvasElement.height);
  470. // 将canvas内容转为base64图片
  471. const photoData = canvasElement.toDataURL('image/jpeg');
  472. // 更新照片数据
  473. this.$set(this.photos, this.currentStep, photoData);
  474. // 上传照片
  475. this.uploadPhoto(photoData, this.photoTypes[this.currentStep]);
  476. // 关闭摄像头
  477. this.stopMediaStream();
  478. this.showCamera = false;
  479. } catch (err) {
  480. console.error('H5拍照失败:', err);
  481. uni.showToast({
  482. title: '拍照失败,请重试',
  483. icon: 'none'
  484. });
  485. }
  486. }
  487. },
  488. // 小程序环境下拍照
  489. capturePhotoMP() {
  490. console.log('执行小程序拍照方法');
  491. if (!this.cameraContext) {
  492. console.error('相机上下文不存在');
  493. this.handleCameraInitError();
  494. return;
  495. }
  496. try {
  497. uni.showLoading({
  498. title: '拍照中...',
  499. mask: true
  500. });
  501. this.cameraContext.takePhoto({
  502. quality: 'high',
  503. success: (res) => {
  504. console.log('拍照成功:', res);
  505. uni.hideLoading();
  506. // 更新当前步骤的照片
  507. this.$set(this.photos, this.currentStep, res.tempImagePath);
  508. // 上传照片
  509. this.uploadPhoto(res.tempImagePath, this.photoTypes[this.currentStep]);
  510. // 关闭相机
  511. this.showCamera = false;
  512. },
  513. fail: (err) => {
  514. uni.hideLoading();
  515. console.error('拍照失败:', err);
  516. uni.showToast({
  517. title: '拍照失败,请重试',
  518. icon: 'none'
  519. });
  520. // 尝试使用替代方法
  521. this.fallbackToChooseImage();
  522. }
  523. });
  524. } catch (err) {
  525. uni.hideLoading();
  526. console.error('takePhoto方法执行失败:', err);
  527. this.fallbackToChooseImage();
  528. }
  529. },
  530. // 添加相机初始化错误处理
  531. handleCameraInitError() {
  532. if (this.cameraInitRetries < 3) {
  533. this.cameraInitRetries++;
  534. uni.showToast({
  535. title: `相机初始化失败,正在重试(${this.cameraInitRetries}/3)`,
  536. icon: 'none'
  537. });
  538. setTimeout(() => {
  539. this.showCamera = false;
  540. setTimeout(() => {
  541. this.takePhoto();
  542. }, 500);
  543. }, 1000);
  544. } else {
  545. uni.showToast({
  546. title: '相机初始化失败,将使用系统相机',
  547. icon: 'none'
  548. });
  549. this.fallbackToChooseImage();
  550. }
  551. },
  552. // 添加备用的系统相机方法
  553. fallbackToChooseImage() {
  554. this.showCamera = false;
  555. // 使用系统相机作为备选方案
  556. uni.chooseImage({
  557. count: 1,
  558. sourceType: ['camera'],
  559. success: (res) => {
  560. // 更新当前步骤的照片
  561. this.$set(this.photos, this.currentStep, res.tempFilePaths[0]);
  562. // 上传照片
  563. this.uploadPhoto(res.tempFilePaths[0], this.photoTypes[this.currentStep]);
  564. },
  565. fail: (err) => {
  566. console.error('选择图片失败:', err);
  567. uni.showToast({
  568. title: '拍照失败,请重试',
  569. icon: 'none'
  570. });
  571. }
  572. });
  573. },
  574. // 修改相机错误处理方法
  575. handleCameraError(e) {
  576. console.error('相机错误:', e.detail);
  577. // 记录更详细的错误信息
  578. const errorInfo = JSON.stringify(e.detail);
  579. console.log('详细错误信息:', errorInfo);
  580. uni.showToast({
  581. title: '相机启动失败,将使用系统相机',
  582. icon: 'none'
  583. });
  584. // 使用备用方法
  585. this.fallbackToChooseImage();
  586. },
  587. // 取消拍照 - 更新为同时支持H5和小程序
  588. cancelCamera() {
  589. if (this.isH5) {
  590. this.stopMediaStream();
  591. }
  592. this.showCamera = false;
  593. },
  594. // 停止摄像头流
  595. stopMediaStream() {
  596. if (this.mediaStream) {
  597. this.mediaStream.getTracks().forEach(track => {
  598. track.stop();
  599. });
  600. this.mediaStream = null;
  601. }
  602. },
  603. // base64转blob工具方法
  604. base64ToBlob(base64, mimeType) {
  605. const byteCharacters = atob(base64);
  606. const byteArrays = [];
  607. for (let offset = 0; offset < byteCharacters.length; offset += 512) {
  608. const slice = byteCharacters.slice(offset, offset + 512);
  609. const byteNumbers = new Array(slice.length);
  610. for (let i = 0; i < slice.length; i++) {
  611. byteNumbers[i] = slice.charCodeAt(i);
  612. }
  613. const byteArray = new Uint8Array(byteNumbers);
  614. byteArrays.push(byteArray);
  615. }
  616. return new Blob(byteArrays, { type: mimeType });
  617. },
  618. retryCamera() {
  619. this.showCamera = false;
  620. setTimeout(() => {
  621. this.takePhoto();
  622. }, 500);
  623. },
  624. // 可以添加一个方法来切换蒙层显示
  625. toggleGestureOverlay() {
  626. // 实现蒙层显示/隐藏的逻辑
  627. // 如果需要的话
  628. },
  629. // 切换摄像头
  630. switchCamera() {
  631. // 切换摄像头位置
  632. this.cameraPosition = this.cameraPosition === 'front' ? 'back' : 'front';
  633. // 如果是H5环境,需要重新启动摄像头
  634. if (this.isH5 && this.showCamera) {
  635. this.stopMediaStream();
  636. this.startCamera();
  637. }
  638. },
  639. // 添加自动进入下一步的方法
  640. autoNextStep() {
  641. if (this.currentStep < 5) {
  642. this.currentStep++;
  643. // 如果不是最后一步,自动开始拍摄下一张照片
  644. if (this.currentStep < 6) {
  645. this.takePhoto();
  646. }
  647. } else if (this.currentStep === 5 && this.photos[5]) {
  648. // 所有照片都已拍摄完成,进行提交
  649. this.submitAllPhotos();
  650. }
  651. }
  652. }
  653. }
  654. </script>
  655. <style>
  656. .container {
  657. display: flex;
  658. flex-direction: column;
  659. height: 94vh;
  660. background-color: #f5f5f5;
  661. }
  662. .header {
  663. padding: 20rpx;
  664. background-color: #007AFF;
  665. text-align: center;
  666. }
  667. .title {
  668. color: #ffffff;
  669. font-size: 36rpx;
  670. font-weight: bold;
  671. }
  672. .photo-container {
  673. flex: 1;
  674. padding: 20rpx;
  675. display: flex;
  676. flex-direction: column;
  677. }
  678. .instruction {
  679. padding: 20rpx;
  680. background-color: #E5E5EA;
  681. border-radius: 10rpx;
  682. margin-bottom: 20rpx;
  683. }
  684. .preview-container {
  685. flex: 1;
  686. display: flex;
  687. justify-content: center;
  688. align-items: center;
  689. background-color: #E5E5EA;
  690. border-radius: 10rpx;
  691. margin-bottom: 20rpx;
  692. overflow: hidden;
  693. }
  694. .preview-image {
  695. width: 100%;
  696. height: 100%;
  697. }
  698. .empty-preview {
  699. display: flex;
  700. justify-content: center;
  701. align-items: center;
  702. width: 100%;
  703. height: 100%;
  704. color: #999;
  705. }
  706. .thumbnails {
  707. display: flex;
  708. justify-content: space-around;
  709. margin-bottom: 20rpx;
  710. }
  711. .thumbnail-item {
  712. width: 150rpx;
  713. height: 150rpx;
  714. border-radius: 10rpx;
  715. overflow: hidden;
  716. background-color: #E5E5EA;
  717. display: flex;
  718. justify-content: center;
  719. align-items: center;
  720. border: 4rpx solid transparent;
  721. }
  722. .thumbnail-item.active {
  723. border-color: #007AFF;
  724. }
  725. .thumbnail-item.completed {
  726. background-color: #ffffff;
  727. }
  728. .thumbnail-image {
  729. width: 100%;
  730. height: 100%;
  731. }
  732. .thumbnail-placeholder {
  733. font-size: 40rpx;
  734. color: #999;
  735. }
  736. .controls {
  737. padding: 20rpx;
  738. background-color: #ffffff;
  739. border-top: 1px solid #0039b3;
  740. }
  741. .camera-btn {
  742. width: 100%;
  743. height: 80rpx;
  744. line-height: 80rpx;
  745. text-align: center;
  746. background-color: #0039b3;
  747. color: white;
  748. border-radius: 40rpx;
  749. margin-bottom: 20rpx;
  750. }
  751. .navigation-btns {
  752. display: flex;
  753. justify-content: space-between;
  754. }
  755. .nav-btn {
  756. width: 48%;
  757. height: 80rpx;
  758. line-height: 80rpx;
  759. text-align: center;
  760. border-radius: 40rpx;
  761. }
  762. .nav-btn.prev {
  763. background-color: #E5E5EA;
  764. color: #333;
  765. }
  766. .nav-btn.next {
  767. background-color: #34C759;
  768. color: white;
  769. }
  770. .nav-btn[disabled] {
  771. opacity: 0.5;
  772. }
  773. /* 浏览器摄像头相关样式 */
  774. .camera-container {
  775. position: fixed;
  776. top: 0;
  777. left: 0;
  778. width: 100%;
  779. height: 100%;
  780. background-color: #000;
  781. z-index: 999;
  782. display: flex;
  783. flex-direction: column;
  784. justify-content: center;
  785. align-items: center;
  786. }
  787. .camera-video {
  788. width: 100%;
  789. height: 70%;
  790. object-fit: cover;
  791. }
  792. .camera-canvas {
  793. display: none;
  794. }
  795. .capture-btn {
  796. width: 200rpx;
  797. height: 200rpx;
  798. border-radius: 50%;
  799. background-color: #ffffff;
  800. border: 10rpx solid #0039b3;
  801. margin: 30rpx 0;
  802. }
  803. .cancel-btn {
  804. width: 200rpx;
  805. height: 80rpx;
  806. line-height: 80rpx;
  807. background-color: rgba(255, 255, 255, 0.3);
  808. color: #ffffff;
  809. text-align: center;
  810. border-radius: 40rpx;
  811. }
  812. /* 小程序相机组件样式 */
  813. .camera-component {
  814. width: 100%;
  815. height: 100%;
  816. display: block;
  817. }
  818. .camera-controls {
  819. width: 100%;
  820. display: flex;
  821. flex-direction: column;
  822. align-items: center;
  823. position: absolute;
  824. bottom: 50rpx;
  825. z-index: 9999;
  826. }
  827. /* 添加手势引导蒙层样式 */
  828. .gesture-overlay {
  829. position: absolute;
  830. top: 0;
  831. left: 0;
  832. width: 100%;
  833. height: 80%;
  834. display: flex;
  835. flex-direction: column;
  836. justify-content: center;
  837. align-items: center;
  838. pointer-events: none; /* 允许点击穿透 */
  839. z-index: 10;
  840. }
  841. .gesture-image {
  842. width: 100%;
  843. height: 100%;
  844. /* opacity: 0.6; */
  845. }
  846. .gesture-hint {
  847. position: fixed;
  848. top: 1px;
  849. background-color: rgba(0, 0, 0, 0.5);
  850. padding: 10rpx 20rpx;
  851. border-radius: 10rpx;
  852. margin-top: 20rpx;
  853. z-index: 999;
  854. }
  855. .gesture-hint-text {
  856. color: white;
  857. font-size: 28rpx;
  858. text-align: center;
  859. }
  860. /* 添加切换摄像头按钮样式 */
  861. .switch-camera-btn {
  862. width: 200rpx;
  863. height: 80rpx;
  864. line-height: 80rpx;
  865. background-color: rgba(255, 255, 255, 0.3);
  866. color: #ffffff;
  867. text-align: center;
  868. border-radius: 40rpx;
  869. margin: 20rpx 0;
  870. }
  871. .camera-buttons {
  872. display: flex;
  873. flex-direction: row;
  874. justify-content: center;
  875. width: 100%;
  876. margin-top: 20rpx;
  877. }
  878. .capture-btn {
  879. width: 200rpx;
  880. height: 200rpx;
  881. border-radius: 50%;
  882. background-color: #ffffff;
  883. border: 10rpx solid #0039b3;
  884. margin: 30rpx 0;
  885. }
  886. .switch-camera-btn, .cancel-btn {
  887. width: 200rpx;
  888. height: 80rpx;
  889. line-height: 80rpx;
  890. background-color: rgba(255, 255, 255, 0.3);
  891. color: #ffffff;
  892. text-align: center;
  893. border-radius: 40rpx;
  894. margin: 0 20rpx;
  895. }
  896. /* 添加右上角引导图标样式 */
  897. .guide-icon {
  898. position: absolute;
  899. top: 30rpx;
  900. right: 30rpx;
  901. width: 160rpx;
  902. height: 200rpx;
  903. display: flex;
  904. flex-direction: column;
  905. align-items: center;
  906. justify-content: center;
  907. background-color: rgba(0, 0, 0, 0.5);
  908. border-radius: 10rpx;
  909. z-index: 11;
  910. }
  911. .mp-video-player{
  912. width: 100%;
  913. height: 100%;
  914. border-radius: 10rpx;
  915. }
  916. .guide-image {
  917. width: 160rpx;
  918. height: 200rpx;
  919. }
  920. .guide-text {
  921. color: white;
  922. font-size: 24rpx;
  923. margin-top: 5rpx;
  924. }
  925. </style>