interview.vue 24 KB

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