interview.vue 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  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. // 如果是H5环境且是base64数据
  243. if (this.isH5 && filePathOrData.startsWith('data:image')) {
  244. // 将base64转为blob对象
  245. const base64Data = filePathOrData.split(',')[1];
  246. const blob = this.base64ToBlob(base64Data, 'image/jpeg');
  247. const formData = new FormData();
  248. // 确保文件参数名称正确
  249. formData.append('photo', blob, `${type}.jpg`);
  250. formData.append('application_id', uni.getStorageSync('appId'));
  251. formData.append('tenant_id', tenant_id);
  252. formData.append('description', `手部照片-${type}`);
  253. console.log('H5上传参数:', {
  254. application_id: uni.getStorageSync('appId'),
  255. tenant_id: tenant_id,
  256. description: `手部照片-${type}`
  257. });
  258. // 使用fetch直接上传到 job/upload_posture_photo 接口
  259. fetch(`${apiBaseUrl}/job/upload_posture_photo`, {
  260. method: 'POST',
  261. body: formData
  262. })
  263. .then(response => response.json())
  264. .then(result => {
  265. console.log('照片上传成功:', result);
  266. // 保存图片链接到对应类型的索引位置
  267. const index = this.photoTypes.indexOf(type);
  268. if (index !== -1 && result.data && result.data.url) {
  269. this.$set(this.photoLinks, index, result.data.url);
  270. console.log(`已保存${type}类型照片链接:`, result.data.url);
  271. }
  272. uni.hideLoading();
  273. uni.showToast({
  274. title: '照片上传成功',
  275. icon: 'success',
  276. duration: 1500,
  277. success: () => {
  278. // 上传成功后自动进入下一步
  279. setTimeout(() => {
  280. this.autoNextStep();
  281. }, 1500);
  282. }
  283. });
  284. })
  285. .catch(error => {
  286. console.error('照片上传失败:', error);
  287. uni.hideLoading();
  288. uni.showToast({
  289. title: '照片上传失败,请重试',
  290. icon: 'none'
  291. });
  292. });
  293. } else {
  294. // 小程序环境的上传逻辑
  295. console.log(`小程序环境上传文件路径:`, filePathOrData);
  296. console.log('小程序上传参数:', {
  297. application_id: uni.getStorageSync('appId'),
  298. tenant_id: tenant_id,
  299. description: `手部照片-${type}`
  300. });
  301. uni.uploadFile({
  302. url: `${apiBaseUrl}/job/upload_posture_photo`,
  303. filePath: filePathOrData,
  304. name: 'photo', // 确保文件参数名称正确
  305. formData: { // 使用formData替代data以确保参数正确传递
  306. 'application_id': uni.getStorageSync('appId'),
  307. 'tenant_id': tenant_id,
  308. 'description': `手部照片-${type}`
  309. },
  310. success: (uploadRes) => {
  311. console.log('照片上传成功:', uploadRes);
  312. // 解析返回的JSON字符串
  313. let result;
  314. try {
  315. if (typeof uploadRes.data === 'string') {
  316. result = JSON.parse(uploadRes.data);
  317. } else {
  318. result = uploadRes.data;
  319. }
  320. console.log('解析后的上传结果:', result);
  321. // 保存图片链接到对应类型的索引位置
  322. const index = this.photoTypes.indexOf(type);
  323. if (index !== -1 && result.data && result.data.url) {
  324. this.$set(this.photoLinks, index, result.data.url);
  325. console.log(`已保存${type}类型照片链接:`, result.data.url);
  326. }
  327. } catch (e) {
  328. console.error('解析上传结果失败:', e);
  329. }
  330. uni.hideLoading();
  331. uni.showToast({
  332. title: '照片上传成功',
  333. icon: 'success',
  334. duration: 1500,
  335. success: () => {
  336. // 上传成功后自动进入下一步
  337. setTimeout(() => {
  338. this.autoNextStep();
  339. }, 1500);
  340. }
  341. });
  342. },
  343. fail: (err) => {
  344. console.error('照片上传失败:', err);
  345. uni.hideLoading();
  346. uni.showToast({
  347. title: '照片上传失败,请重试',
  348. icon: 'none'
  349. });
  350. },
  351. complete: () => {
  352. uni.hideLoading();
  353. }
  354. });
  355. }
  356. },
  357. prevStep() {
  358. if (this.currentStep > 0) {
  359. this.currentStep--;
  360. }
  361. },
  362. nextStep() {
  363. if (this.currentStep < 5) {
  364. this.currentStep++;
  365. } else if (this.currentStep === 5 && this.photos[5]) {
  366. // 所有照片都已拍摄完成,可以进行提交或跳转
  367. this.submitAllPhotos();
  368. }
  369. },
  370. previewPhoto(index) {
  371. // 如果照片存在,则预览
  372. if (this.photos[index]) {
  373. uni.previewImage({
  374. urls: [this.photos[index]],
  375. current: this.photos[index]
  376. });
  377. }
  378. },
  379. submitAllPhotos() {
  380. // 检查是否所有照片都已拍摄
  381. if (this.photos.every(photo => photo !== null)) {
  382. uni.showLoading({
  383. title: '处理中...'
  384. });
  385. // 所有照片已经单独上传,这里只需要处理跳转
  386. setTimeout(() => {
  387. uni.hideLoading();
  388. uni.showToast({
  389. title: '手部照片采集完成',
  390. icon: 'success',
  391. duration: 2000,
  392. success: () => {
  393. // 延迟跳转,让用户看到成功提示
  394. setTimeout(() => {
  395. // 使用redirectTo替代navigateTo,避免页面栈溢出
  396. uni.redirectTo({
  397. url: '/pages/interview_success/interview_success'
  398. });
  399. }, 100);
  400. }
  401. });
  402. }, 500);
  403. } else {
  404. uni.showToast({
  405. title: '请完成所有照片拍摄',
  406. icon: 'none'
  407. });
  408. }
  409. },
  410. // H5环境下启动摄像头
  411. startCamera() {
  412. this.showCamera = true;
  413. // 确保浏览器支持getUserMedia
  414. if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
  415. // 根据当前摄像头位置设置facingMode
  416. const facingMode = this.cameraPosition === 'front' ? 'user' : 'environment';
  417. navigator.mediaDevices.getUserMedia({
  418. video: { facingMode: facingMode }
  419. })
  420. .then(stream => {
  421. this.mediaStream = stream;
  422. // 等待DOM更新后再设置视频源
  423. this.$nextTick(() => {
  424. const videoElement = this.$refs.videoElement;
  425. if (videoElement) {
  426. videoElement.srcObject = stream;
  427. }
  428. });
  429. })
  430. .catch(error => {
  431. console.error('获取摄像头失败:', error);
  432. uni.showToast({
  433. title: '无法访问摄像头,请检查权限设置',
  434. icon: 'none'
  435. });
  436. this.showCamera = false;
  437. });
  438. } else {
  439. uni.showToast({
  440. title: '您的浏览器不支持摄像头功能',
  441. icon: 'none'
  442. });
  443. this.showCamera = false;
  444. }
  445. },
  446. // H5环境下拍照
  447. capturePhoto() {
  448. console.log('执行H5拍照方法');
  449. const videoElement = this.$refs.videoElement;
  450. const canvasElement = this.$refs.canvasElement;
  451. if (videoElement && canvasElement) {
  452. try {
  453. const context = canvasElement.getContext('2d');
  454. // 设置canvas尺寸与视频一致
  455. canvasElement.width = videoElement.videoWidth;
  456. canvasElement.height = videoElement.videoHeight;
  457. // 在canvas上绘制当前视频帧
  458. context.drawImage(videoElement, 0, 0, canvasElement.width, canvasElement.height);
  459. // 将canvas内容转为base64图片
  460. const photoData = canvasElement.toDataURL('image/jpeg');
  461. // 更新照片数据
  462. this.$set(this.photos, this.currentStep, photoData);
  463. // 上传照片
  464. this.uploadPhoto(photoData, this.photoTypes[this.currentStep]);
  465. // 关闭摄像头
  466. this.stopMediaStream();
  467. this.showCamera = false;
  468. } catch (err) {
  469. console.error('H5拍照失败:', err);
  470. uni.showToast({
  471. title: '拍照失败,请重试',
  472. icon: 'none'
  473. });
  474. }
  475. }
  476. },
  477. // 小程序环境下拍照
  478. capturePhotoMP() {
  479. console.log('执行小程序拍照方法');
  480. if (!this.cameraContext) {
  481. console.error('相机上下文不存在');
  482. this.handleCameraInitError();
  483. return;
  484. }
  485. try {
  486. uni.showLoading({
  487. title: '拍照中...',
  488. mask: true
  489. });
  490. this.cameraContext.takePhoto({
  491. quality: 'high',
  492. success: (res) => {
  493. console.log('拍照成功:', res);
  494. uni.hideLoading();
  495. // 更新当前步骤的照片
  496. this.$set(this.photos, this.currentStep, res.tempImagePath);
  497. // 上传照片
  498. this.uploadPhoto(res.tempImagePath, this.photoTypes[this.currentStep]);
  499. // 关闭相机
  500. this.showCamera = false;
  501. },
  502. fail: (err) => {
  503. uni.hideLoading();
  504. console.error('拍照失败:', err);
  505. uni.showToast({
  506. title: '拍照失败,请重试',
  507. icon: 'none'
  508. });
  509. // 尝试使用替代方法
  510. this.fallbackToChooseImage();
  511. }
  512. });
  513. } catch (err) {
  514. uni.hideLoading();
  515. console.error('takePhoto方法执行失败:', err);
  516. this.fallbackToChooseImage();
  517. }
  518. },
  519. // 添加相机初始化错误处理
  520. handleCameraInitError() {
  521. if (this.cameraInitRetries < 3) {
  522. this.cameraInitRetries++;
  523. uni.showToast({
  524. title: `相机初始化失败,正在重试(${this.cameraInitRetries}/3)`,
  525. icon: 'none'
  526. });
  527. setTimeout(() => {
  528. this.showCamera = false;
  529. setTimeout(() => {
  530. this.takePhoto();
  531. }, 500);
  532. }, 1000);
  533. } else {
  534. uni.showToast({
  535. title: '相机初始化失败,将使用系统相机',
  536. icon: 'none'
  537. });
  538. this.fallbackToChooseImage();
  539. }
  540. },
  541. // 添加备用的系统相机方法
  542. fallbackToChooseImage() {
  543. this.showCamera = false;
  544. // 使用系统相机作为备选方案
  545. uni.chooseImage({
  546. count: 1,
  547. sourceType: ['camera'],
  548. success: (res) => {
  549. // 更新当前步骤的照片
  550. this.$set(this.photos, this.currentStep, res.tempFilePaths[0]);
  551. // 上传照片
  552. this.uploadPhoto(res.tempFilePaths[0], this.photoTypes[this.currentStep]);
  553. },
  554. fail: (err) => {
  555. console.error('选择图片失败:', err);
  556. uni.showToast({
  557. title: '拍照失败,请重试',
  558. icon: 'none'
  559. });
  560. }
  561. });
  562. },
  563. // 修改相机错误处理方法
  564. handleCameraError(e) {
  565. console.error('相机错误:', e.detail);
  566. // 记录更详细的错误信息
  567. const errorInfo = JSON.stringify(e.detail);
  568. console.log('详细错误信息:', errorInfo);
  569. uni.showToast({
  570. title: '相机启动失败,将使用系统相机',
  571. icon: 'none'
  572. });
  573. // 使用备用方法
  574. this.fallbackToChooseImage();
  575. },
  576. // 取消拍照 - 更新为同时支持H5和小程序
  577. cancelCamera() {
  578. if (this.isH5) {
  579. this.stopMediaStream();
  580. }
  581. this.showCamera = false;
  582. },
  583. // 停止摄像头流
  584. stopMediaStream() {
  585. if (this.mediaStream) {
  586. this.mediaStream.getTracks().forEach(track => {
  587. track.stop();
  588. });
  589. this.mediaStream = null;
  590. }
  591. },
  592. // base64转blob工具方法
  593. base64ToBlob(base64, mimeType) {
  594. const byteCharacters = atob(base64);
  595. const byteArrays = [];
  596. for (let offset = 0; offset < byteCharacters.length; offset += 512) {
  597. const slice = byteCharacters.slice(offset, offset + 512);
  598. const byteNumbers = new Array(slice.length);
  599. for (let i = 0; i < slice.length; i++) {
  600. byteNumbers[i] = slice.charCodeAt(i);
  601. }
  602. const byteArray = new Uint8Array(byteNumbers);
  603. byteArrays.push(byteArray);
  604. }
  605. return new Blob(byteArrays, { type: mimeType });
  606. },
  607. retryCamera() {
  608. this.showCamera = false;
  609. setTimeout(() => {
  610. this.takePhoto();
  611. }, 500);
  612. },
  613. // 可以添加一个方法来切换蒙层显示
  614. toggleGestureOverlay() {
  615. // 实现蒙层显示/隐藏的逻辑
  616. // 如果需要的话
  617. },
  618. // 切换摄像头
  619. switchCamera() {
  620. // 切换摄像头位置
  621. this.cameraPosition = this.cameraPosition === 'front' ? 'back' : 'front';
  622. // 如果是H5环境,需要重新启动摄像头
  623. if (this.isH5 && this.showCamera) {
  624. this.stopMediaStream();
  625. this.startCamera();
  626. }
  627. },
  628. // 添加自动进入下一步的方法
  629. autoNextStep() {
  630. if (this.currentStep < 5) {
  631. this.currentStep++;
  632. // 如果不是最后一步,自动开始拍摄下一张照片
  633. if (this.currentStep < 6) {
  634. this.takePhoto();
  635. }
  636. } else if (this.currentStep === 5 && this.photos[5]) {
  637. // 所有照片都已拍摄完成,进行提交
  638. this.submitAllPhotos();
  639. }
  640. }
  641. }
  642. }
  643. </script>
  644. <style>
  645. .container {
  646. display: flex;
  647. flex-direction: column;
  648. height: 94vh;
  649. background-color: #f5f5f5;
  650. }
  651. .header {
  652. padding: 20rpx;
  653. background-color: #007AFF;
  654. text-align: center;
  655. }
  656. .title {
  657. color: #ffffff;
  658. font-size: 36rpx;
  659. font-weight: bold;
  660. }
  661. .photo-container {
  662. flex: 1;
  663. padding: 20rpx;
  664. display: flex;
  665. flex-direction: column;
  666. }
  667. .instruction {
  668. padding: 20rpx;
  669. background-color: #E5E5EA;
  670. border-radius: 10rpx;
  671. margin-bottom: 20rpx;
  672. }
  673. .preview-container {
  674. flex: 1;
  675. display: flex;
  676. justify-content: center;
  677. align-items: center;
  678. background-color: #E5E5EA;
  679. border-radius: 10rpx;
  680. margin-bottom: 20rpx;
  681. overflow: hidden;
  682. }
  683. .preview-image {
  684. width: 100%;
  685. height: 100%;
  686. }
  687. .empty-preview {
  688. display: flex;
  689. justify-content: center;
  690. align-items: center;
  691. width: 100%;
  692. height: 100%;
  693. color: #999;
  694. }
  695. .thumbnails {
  696. display: flex;
  697. justify-content: space-around;
  698. margin-bottom: 20rpx;
  699. }
  700. .thumbnail-item {
  701. width: 150rpx;
  702. height: 150rpx;
  703. border-radius: 10rpx;
  704. overflow: hidden;
  705. background-color: #E5E5EA;
  706. display: flex;
  707. justify-content: center;
  708. align-items: center;
  709. border: 4rpx solid transparent;
  710. }
  711. .thumbnail-item.active {
  712. border-color: #007AFF;
  713. }
  714. .thumbnail-item.completed {
  715. background-color: #ffffff;
  716. }
  717. .thumbnail-image {
  718. width: 100%;
  719. height: 100%;
  720. }
  721. .thumbnail-placeholder {
  722. font-size: 40rpx;
  723. color: #999;
  724. }
  725. .controls {
  726. padding: 20rpx;
  727. background-color: #ffffff;
  728. border-top: 1px solid #0039b3;
  729. }
  730. .camera-btn {
  731. width: 100%;
  732. height: 80rpx;
  733. line-height: 80rpx;
  734. text-align: center;
  735. background-color: #0039b3;
  736. color: white;
  737. border-radius: 40rpx;
  738. margin-bottom: 20rpx;
  739. }
  740. .navigation-btns {
  741. display: flex;
  742. justify-content: space-between;
  743. }
  744. .nav-btn {
  745. width: 48%;
  746. height: 80rpx;
  747. line-height: 80rpx;
  748. text-align: center;
  749. border-radius: 40rpx;
  750. }
  751. .nav-btn.prev {
  752. background-color: #E5E5EA;
  753. color: #333;
  754. }
  755. .nav-btn.next {
  756. background-color: #34C759;
  757. color: white;
  758. }
  759. .nav-btn[disabled] {
  760. opacity: 0.5;
  761. }
  762. /* 浏览器摄像头相关样式 */
  763. .camera-container {
  764. position: fixed;
  765. top: 0;
  766. left: 0;
  767. width: 100%;
  768. height: 100%;
  769. background-color: #000;
  770. z-index: 999;
  771. display: flex;
  772. flex-direction: column;
  773. justify-content: center;
  774. align-items: center;
  775. }
  776. .camera-video {
  777. width: 100%;
  778. height: 70%;
  779. object-fit: cover;
  780. }
  781. .camera-canvas {
  782. display: none;
  783. }
  784. .capture-btn {
  785. width: 200rpx;
  786. height: 200rpx;
  787. border-radius: 50%;
  788. background-color: #ffffff;
  789. border: 10rpx solid #0039b3;
  790. margin: 30rpx 0;
  791. }
  792. .cancel-btn {
  793. width: 200rpx;
  794. height: 80rpx;
  795. line-height: 80rpx;
  796. background-color: rgba(255, 255, 255, 0.3);
  797. color: #ffffff;
  798. text-align: center;
  799. border-radius: 40rpx;
  800. }
  801. /* 小程序相机组件样式 */
  802. .camera-component {
  803. width: 100%;
  804. height: 100%;
  805. display: block;
  806. }
  807. .camera-controls {
  808. width: 100%;
  809. display: flex;
  810. flex-direction: column;
  811. align-items: center;
  812. position: absolute;
  813. bottom: 50rpx;
  814. z-index: 9999;
  815. }
  816. /* 添加手势引导蒙层样式 */
  817. .gesture-overlay {
  818. position: absolute;
  819. top: 0;
  820. left: 0;
  821. width: 100%;
  822. height: 80%;
  823. display: flex;
  824. flex-direction: column;
  825. justify-content: center;
  826. align-items: center;
  827. pointer-events: none; /* 允许点击穿透 */
  828. z-index: 10;
  829. }
  830. .gesture-image {
  831. width: 100%;
  832. height: 100%;
  833. /* opacity: 0.6; */
  834. }
  835. .gesture-hint {
  836. position: fixed;
  837. top: 1px;
  838. background-color: rgba(0, 0, 0, 0.5);
  839. padding: 10rpx 20rpx;
  840. border-radius: 10rpx;
  841. margin-top: 20rpx;
  842. z-index: 999;
  843. }
  844. .gesture-hint-text {
  845. color: white;
  846. font-size: 28rpx;
  847. text-align: center;
  848. }
  849. /* 添加切换摄像头按钮样式 */
  850. .switch-camera-btn {
  851. width: 200rpx;
  852. height: 80rpx;
  853. line-height: 80rpx;
  854. background-color: rgba(255, 255, 255, 0.3);
  855. color: #ffffff;
  856. text-align: center;
  857. border-radius: 40rpx;
  858. margin: 20rpx 0;
  859. }
  860. .camera-buttons {
  861. display: flex;
  862. flex-direction: row;
  863. justify-content: center;
  864. width: 100%;
  865. margin-top: 20rpx;
  866. }
  867. .capture-btn {
  868. width: 200rpx;
  869. height: 200rpx;
  870. border-radius: 50%;
  871. background-color: #ffffff;
  872. border: 10rpx solid #0039b3;
  873. margin: 30rpx 0;
  874. }
  875. .switch-camera-btn, .cancel-btn {
  876. width: 200rpx;
  877. height: 80rpx;
  878. line-height: 80rpx;
  879. background-color: rgba(255, 255, 255, 0.3);
  880. color: #ffffff;
  881. text-align: center;
  882. border-radius: 40rpx;
  883. margin: 0 20rpx;
  884. }
  885. /* 添加右上角引导图标样式 */
  886. .guide-icon {
  887. position: absolute;
  888. top: 30rpx;
  889. right: 30rpx;
  890. width: 160rpx;
  891. height: 200rpx;
  892. display: flex;
  893. flex-direction: column;
  894. align-items: center;
  895. justify-content: center;
  896. background-color: rgba(0, 0, 0, 0.5);
  897. border-radius: 10rpx;
  898. z-index: 11;
  899. }
  900. .mp-video-player{
  901. width: 100%;
  902. height: 100%;
  903. border-radius: 10rpx;
  904. }
  905. .guide-image {
  906. width: 160rpx;
  907. height: 200rpx;
  908. }
  909. .guide-text {
  910. color: white;
  911. font-size: 24rpx;
  912. margin-top: 5rpx;
  913. }
  914. </style>