interview.vue 25 KB

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