yubo
2026-03-12 74612d64c6bd2d28dcf80450f2cb94ffbee9970d
src/views/user/inemployees.vue
@@ -347,17 +347,19 @@
    <el-dialog title="基本信息" :visible.sync="dialogShowXzyg" width="50%" class="baseinfo">
      <el-container>
        <el-aside width="200px">
          <el-upload
            class="avatar-uploader"
            action="#"
            :show-file-list="false"
            :on-change="handlePictureCardPreview"
            :auto-upload="false"
          <!-- 头像显示区域 -->
          <div class="avatar-wrapper">
            <img
              v-if="empBaseInfoImageUrl"
              :src="empBaseInfoImageUrl"
              class="avatar"
              @click="openCamera"
          >
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过4MB</div>
            <img v-if="empBaseInfoImageUrl" :src="empBaseInfoImageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
            <div v-else class="avatar-uploader-placeholder" @click="openCamera">
              <i class="el-icon-plus avatar-uploader-icon" />
              <div class="upload-tip">点击拍照上传</div>
            </div>
          </div>
        </el-aside>
        <el-main>
          <el-form
@@ -1000,6 +1002,59 @@
        <el-button type="primary" @click="showDkda(2, 'openArchivesForm')">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 摄像头拍照弹窗 -->
    <el-dialog
      title="拍照上传"
      :visible.sync="cameraDialogVisible"
      width="640px"
      :close-on-click-modal="false"
      @close="closeCamera"
    >
      <div class="camera-container">
        <!-- 视频预览 -->
        <video
          v-show="!capturedImage"
          ref="video"
          class="camera-video"
          autoplay
          playsinline
        />
        <!-- 画布(用于拍照) -->
        <canvas ref="canvas" style="display: none;" />
        <!-- 拍照结果预览 -->
        <img
          v-if="capturedImage"
          :src="capturedImage"
          class="captured-image"
        >
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeCamera">取消</el-button>
        <el-button
          v-if="!capturedImage"
          type="primary"
          @click="takePhoto"
        >
          拍照
        </el-button>
        <el-button
          v-if="capturedImage"
          @click="retakePhoto"
        >
          重拍
        </el-button>
        <el-button
          v-if="capturedImage"
          type="primary"
          @click="confirmPhoto"
        >
          确认使用
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
@@ -1049,6 +1104,10 @@
      },
      baseicInformationForm: {},
      empBaseInfoImageUrl: '',
      // 摄像头相关
      cameraDialogVisible: false,
      capturedImage: '',
      stream: null,
      empBaseInfoForm: {
        imagePath: '',
        empId: '',
@@ -1586,6 +1645,97 @@
        }
      } else {
        this.$message.error('图片大小超过4M,请重新上传')
      }
    },
    // 打开摄像头
    openCamera() {
      this.cameraDialogVisible = true
      this.$nextTick(() => {
        this.initCamera()
      })
    },
    // 初始化摄像头
    async initCamera() {
      try {
        // 请求摄像头权限
        this.stream = await navigator.mediaDevices.getUserMedia({
          video: {
            width: { ideal: 640 },
            height: { ideal: 480 },
            facingMode: 'user' // 前置摄像头
          },
          audio: false
        })
        // 将视频流绑定到 video 元素
        const video = this.$refs.video
        if (video) {
          video.srcObject = this.stream
        }
      } catch (error) {
        this.$message.error('无法访问摄像头,请检查摄像头权限设置')
        console.error('摄像头初始化失败:', error)
      }
    },
    // 拍照
    takePhoto() {
      const video = this.$refs.video
      const canvas = this.$refs.canvas
      if (!video || !canvas) return
      // 设置画布尺寸
      canvas.width = video.videoWidth || 640
      canvas.height = video.videoHeight || 480
      // 绘制视频帧到画布
      const ctx = canvas.getContext('2d')
      ctx.drawImage(video, 0, 0, canvas.width, canvas.height)
      // 转换为图片数据
      this.capturedImage = canvas.toDataURL('image/jpeg', 0.9)
      // 停止摄像头
      this.stopCamera()
    },
    // 重拍
    retakePhoto() {
      this.capturedImage = ''
      this.initCamera()
    },
    // 确认使用照片
    confirmPhoto() {
      if (this.capturedImage) {
        // 设置图片预览
        this.empBaseInfoImageUrl = this.capturedImage
        // 设置表单数据(Base64格式)
        this.empBaseInfoForm.imagePath = this.capturedImage
        // 关闭弹窗
        this.closeCamera()
        this.$message.success('照片已保存')
      }
    },
    // 关闭摄像头
    closeCamera() {
      this.stopCamera()
      this.cameraDialogVisible = false
      this.capturedImage = ''
    },
    // 停止摄像头流
    stopCamera() {
      if (this.stream) {
        this.stream.getTracks().forEach(track => {
          track.stop()
        })
        this.stream = null
      }
      const video = this.$refs.video
      if (video) {
        video.srcObject = null
      }
    },
    cleanEmpBase() {
@@ -2444,4 +2594,75 @@
  cursor: pointer;
  vertical-align: middle;
}
// 头像包装器
.avatar-wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 10px;
  .avatar {
    width: 150px;
    height: 150px;
    border-radius: 4px;
    cursor: pointer;
    object-fit: cover;
    border: 1px dashed #d9d9d9;
    &:hover {
      border-color: #409eff;
    }
  }
  .avatar-uploader-placeholder {
    width: 150px;
    height: 150px;
    border: 1px dashed #d9d9d9;
    border-radius: 4px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    background-color: #fafafa;
    &:hover {
      border-color: #409eff;
      background-color: #f0f9ff;
    }
    .avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      line-height: 1;
      margin-bottom: 8px;
    }
    .upload-tip {
      font-size: 12px;
      color: #8c939d;
    }
  }
}
// 摄像头容器
.camera-container {
  text-align: center;
  .camera-video {
    width: 100%;
    max-width: 600px;
    height: auto;
    border-radius: 4px;
    background: #000;
  }
  .captured-image {
    width: 100%;
    max-width: 600px;
    height: auto;
    border-radius: 4px;
  }
}
</style>