diff --git a/src/pages/order/index.vue b/src/pages/order/index.vue index f011ffc..2b34cd6 100644 --- a/src/pages/order/index.vue +++ b/src/pages/order/index.vue @@ -137,11 +137,35 @@ - - + + {{ cameraLoading ? '摄像头启动中...' : cameraError || '暂无摄像头画面' }} + + + + + + {{ Math.round(cameraZoom * 100) }}% + + + + @@ -238,6 +262,13 @@ export default { cameraInitializing: false, cameraInitToken: 0, cameraStartTimer: null, + cameraPaused: false, + cameraZoom: 1, + cameraZoomMin: 1, + cameraZoomMax: 3, + cameraZoomStep: 0.1, + cameraFullscreen: false, + cameraFrameAspectRatio: '16 / 9', } }, created() { @@ -247,6 +278,7 @@ export default { }, mounted() { this.requestCameraPreview() + this.bindFullscreenEvents() }, onReady() { this.requestCameraPreview() @@ -255,12 +287,15 @@ export default { this.requestCameraPreview() }, beforeUnmount() { + this.unbindFullscreenEvents() this.stopCameraPreview() }, beforeDestroy() { + this.unbindFullscreenEvents() this.stopCameraPreview() }, onUnload() { + this.unbindFullscreenEvents() this.stopCameraPreview() }, methods: { @@ -287,6 +322,119 @@ export default { icon: 'success', }) }, + toggleCameraPause() { + const videoElement = this.cameraVideoElement + if (!videoElement) { + return + } + + if (this.cameraPaused) { + const playPromise = videoElement.play ? videoElement.play() : null + if (playPromise && typeof playPromise.then === 'function') { + playPromise.catch(() => { + this.cameraError = '恢复播放失败' + }) + } + this.cameraPaused = false + return + } + + if (typeof videoElement.pause === 'function') { + videoElement.pause() + } + this.cameraPaused = true + }, + handleZoomOut() { + const nextZoom = Math.max(this.cameraZoomMin, Number((this.cameraZoom - this.cameraZoomStep).toFixed(2))) + this.cameraZoom = nextZoom + this.applyCameraViewState() + }, + handleZoomIn() { + const nextZoom = Math.min(this.cameraZoomMax, Number((this.cameraZoom + this.cameraZoomStep).toFixed(2))) + this.cameraZoom = nextZoom + this.applyCameraViewState() + }, + toggleFullscreen() { + const frameRef = this.$refs.cameraPreviewFrame + const frameTarget = Array.isArray(frameRef) ? frameRef[0] : frameRef + const frameElement = + (frameTarget && typeof frameTarget.requestFullscreen === 'function' && frameTarget) || + (frameTarget?.$el && typeof frameTarget.$el.requestFullscreen === 'function' && frameTarget.$el) || + (typeof document !== 'undefined' ? document.getElementById('camera-preview-frame') : null) + + if (!frameElement) { + uni.showToast({ title: '全屏不可用', icon: 'none' }) + return + } + + const fullscreenElement = this.getFullscreenElement() + if (fullscreenElement) { + this.exitFullscreen() + return + } + + const requestFullscreen = + frameElement.requestFullscreen || + frameElement.webkitRequestFullscreen || + frameElement.mozRequestFullScreen || + frameElement.msRequestFullscreen + + if (typeof requestFullscreen === 'function') { + requestFullscreen.call(frameElement) + } + }, + bindFullscreenEvents() { + if (typeof document === 'undefined') { + return + } + + if (!this._onFullscreenChange) { + this._onFullscreenChange = () => { + this.cameraFullscreen = !!this.getFullscreenElement() + } + } + + document.addEventListener('fullscreenchange', this._onFullscreenChange) + document.addEventListener('webkitfullscreenchange', this._onFullscreenChange) + document.addEventListener('mozfullscreenchange', this._onFullscreenChange) + document.addEventListener('MSFullscreenChange', this._onFullscreenChange) + }, + unbindFullscreenEvents() { + if (typeof document === 'undefined' || !this._onFullscreenChange) { + return + } + + document.removeEventListener('fullscreenchange', this._onFullscreenChange) + document.removeEventListener('webkitfullscreenchange', this._onFullscreenChange) + document.removeEventListener('mozfullscreenchange', this._onFullscreenChange) + document.removeEventListener('MSFullscreenChange', this._onFullscreenChange) + }, + getFullscreenElement() { + if (typeof document === 'undefined') { + return null + } + + return document.fullscreenElement || document.webkitFullscreenElement || document.mozFullScreenElement || document.msFullscreenElement || null + }, + exitFullscreen() { + if (typeof document === 'undefined') { + return + } + + const exitHandler = document.exitFullscreen || document.webkitExitFullscreen || document.mozCancelFullScreen || document.msExitFullscreen + if (typeof exitHandler === 'function') { + exitHandler.call(document) + } + }, + applyCameraViewState() { + const videoElement = this.cameraVideoElement + if (!videoElement) { + return + } + + videoElement.style.transform = `scale(${this.cameraZoom})` + videoElement.style.transformOrigin = 'center center' + }, async initCameraPreview() { if (typeof navigator === 'undefined' || !navigator?.mediaDevices?.getUserMedia) { this.cameraError = '当前环境不支持摄像头预览' @@ -298,10 +446,9 @@ export default { return } + await this.stopCameraPreview() const currentToken = ++this.cameraInitToken this.cameraInitializing = true - - await this.stopCameraPreview() await new Promise((resolve) => setTimeout(resolve, 200)) this.cameraLoading = true @@ -336,7 +483,7 @@ export default { await this.$nextTick() const hostRef = this.$refs.cameraHost - const hostElement = Array.isArray(hostRef) ? hostRef[0] : hostRef + const hostElement = this.resolveCameraHostElement(hostRef) const videoElement = this.ensureCameraVideoElement(hostElement) if (videoElement) { @@ -350,11 +497,17 @@ export default { if (playPromise && typeof playPromise.then === 'function') { await playPromise } + + if (videoElement.readyState >= 2) { + this.markCameraReady() + } + + this.applyCameraViewState() } else { throw new Error('VIDEO_ELEMENT_NOT_FOUND') } - this.cameraReady = true + this.markCameraReady() } catch (error) { if (error?.name === 'NotReadableError') { try { @@ -375,7 +528,7 @@ export default { await this.$nextTick() const hostRef = this.$refs.cameraHost - const hostElement = Array.isArray(hostRef) ? hostRef[0] : hostRef + const hostElement = this.resolveCameraHostElement(hostRef) const videoElement = this.ensureCameraVideoElement(hostElement) if (!videoElement || !('srcObject' in videoElement)) { @@ -388,6 +541,8 @@ export default { await playPromise } + this.applyCameraViewState() + this.cameraReady = true this.cameraError = '' return @@ -443,6 +598,43 @@ export default { this.cameraLoading = false this.cameraInitializing = false this.cameraStartTimer = null + this.cameraPaused = false + this.cameraFullscreen = false + }, + resolveCameraHostElement(hostRef) { + const target = Array.isArray(hostRef) ? hostRef[0] : hostRef + + if (!target && typeof document !== 'undefined') { + return document.getElementById('camera-preview-host') + } + + if (target && typeof target.appendChild === 'function') { + return target + } + + if (target?.$el && typeof target.$el.appendChild === 'function') { + return target.$el + } + + if (target?.$el?.querySelector) { + const innerElement = target.$el.querySelector('.camera-preview-host') + if (innerElement && typeof innerElement.appendChild === 'function') { + return innerElement + } + } + + if (target?.querySelector) { + const innerElement = target.querySelector('.camera-preview-host') + if (innerElement && typeof innerElement.appendChild === 'function') { + return innerElement + } + } + + if (typeof document !== 'undefined') { + return document.getElementById('camera-preview-host') + } + + return null }, ensureCameraVideoElement(hostElement) { if (typeof document === 'undefined' || !hostElement) { @@ -450,6 +642,7 @@ export default { } if (this.cameraVideoElement && hostElement.contains(this.cameraVideoElement)) { + this.bindCameraVideoEvents(this.cameraVideoElement) return this.cameraVideoElement } @@ -462,11 +655,60 @@ export default { videoElement.playsInline = true videoElement.setAttribute('playsinline', 'true') videoElement.setAttribute('webkit-playsinline', 'true') + videoElement.style.width = '100%' + videoElement.style.height = '100%' + videoElement.style.objectFit = 'contain' + videoElement.style.objectPosition = 'center' + this.bindCameraVideoEvents(videoElement) hostElement.appendChild(videoElement) this.cameraVideoElement = videoElement return videoElement }, + bindCameraVideoEvents(videoElement) { + if (!videoElement) { + return + } + + videoElement.onplaying = () => { + this.markCameraReady() + this.cameraPaused = false + } + + videoElement.onpause = () => { + if (this.cameraReady) { + this.cameraPaused = true + } + } + + videoElement.onloadedmetadata = () => { + if (videoElement.videoWidth > 0 && videoElement.videoHeight > 0) { + this.syncCameraAspectRatio(videoElement) + this.markCameraReady() + } + } + + videoElement.onerror = () => { + if (!this.cameraReady && !this.cameraLoading) { + this.cameraError = '摄像头画面加载失败' + } + } + }, + markCameraReady() { + this.cameraReady = true + this.cameraLoading = false + this.cameraError = '' + this.cameraPaused = false + }, + syncCameraAspectRatio(videoElement) { + const width = Number(videoElement?.videoWidth || 0) + const height = Number(videoElement?.videoHeight || 0) + if (!width || !height) { + return + } + + this.cameraFrameAspectRatio = `${width} / ${height}` + }, getRowValue(row, keys = []) { for (const key of keys) { const value = row?.[key] @@ -708,7 +950,10 @@ export default { } .camera-preview-panel { + width: 50%; margin-bottom: 14rpx; + margin-left: auto; + margin-right: auto; border: 1rpx solid #e6ebf5; border-radius: 8rpx; background-color: #fff; @@ -718,24 +963,104 @@ export default { .camera-preview-frame { position: relative; - height: 240rpx; - border: 1rpx dashed #d9dde8; + width: 100%; + min-height: 120rpx; + aspect-ratio: 16 / 9; border-radius: 8rpx; overflow: hidden; - background-color: #f9fbff; + background-color: #000; } .camera-preview-video { width: 100%; height: 100%; display: block; - object-fit: cover; - background-color: #000; + object-fit: contain; + object-position: center; + background-color: transparent; } .camera-preview-host { width: 100%; height: 100%; + display: flex; + align-items: center; + justify-content: center; +} + +.camera-controls { + margin-top: 12rpx; + display: flex; + align-items: center; + gap: 10rpx; +} + +.camera-controls--inside { + position: absolute; + left: 8rpx; + right: 8rpx; + bottom: 8rpx; + z-index: 3; + margin-top: 0; + padding: 8rpx; + border-radius: 8rpx; +} + +.camera-controls--inside .camera-zoom-text { + color: #fff; +} + +.camera-control-btn { + min-width: 116rpx; + height: 64rpx; + line-height: 64rpx; + font-size: 28rpx; + margin: 0; + padding: 0 16rpx; + border: none; + border-radius: 0; + background-color: transparent; + color: #ffffff; +} + +.camera-control-btn::after { + border: none; +} + +.camera-controls .camera-control-btn:first-child { + min-width: 64rpx; + width: 64rpx; + padding: 0; +} + +.camera-control-icon { + font-size: 30rpx; + line-height: 1; + color: #ffffff; +} + +.camera-control-btn--small { + min-width: 64rpx; + width: 64rpx; + padding: 0; +} + +.camera-pause-progress { + flex: 1; + margin: 0; +} + +.camera-zoom-controls { + display: flex; + align-items: center; + gap: 8rpx; +} + +.camera-zoom-text { + width: 88rpx; + text-align: center; + font-size: 26rpx; + color: #333; } .camera-preview-placeholder {