interview.vue 25 KB

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