前言
废话不多说,前端开发调用摄像头,获取地址位置时需要https的问题,在本地运行时可以参考这篇文章操作
我这里用的mkcert
如果你使用的vite,那么可以直接使用vitejs/plugin-basic-ssl
一、通过mkcert创建本地证书并加载
1. 安装mkcert
npm install -g mkcert
2. 生成证书
mkcert create-ca
mkcert create-cert
这时,你应该能看到生成了四个文件
ca.crt
ca.key
cert.crt
cert.key
3. 加载证书
- mac
双击ca.cert,在弹出的界面选择Test CA,选择“信任”,然后选择“始终信任”,最后输入密码确认即可 - windows
双击ca.crt,在弹出的界面中选择安装证书,选择“当前用户”或“本地计算机”均可,关键的是下一步,选择“将所有的证书都放入下列存储”,并且选择为“受信任的根证书颁发机构”,然后一路确定即可
二、在vue或uniapp中使用刚刚的证书
1.vue2
将刚刚生成的cert.crt和cert.key两个拷贝到项目的src/ssl文件夹中
// vue.config.js
const path = require('path')
const fs = require('fs')
module.exports = {
devServer: {
open: true,
https: {
cert: fs.readFileSync(path.join(__dirname, 'src/ssl/cert.crt')),
key: fs.readFileSync(path.join(__dirname, 'src/ssl/cert.key'))
}
}
}
2.vue3
将刚刚生成的cert.crt和cert.key两个拷贝到项目的src/ssl文件夹中
// vite.config.js
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
const fs = require('fs')
const path = require('path')
export default defineConfig({
plugins: [vue()],
resolve: {
alias: {
'@': path.join(__dirname, 'src')
}
},
server: {
open: true,
https: {
// 主要是下面两行的配置文件,不要忘记引入 fs 和 path 两个对象
cert: fs.readFileSync(path.join(__dirname, 'src/ssl/cert.crt')),
key: fs.readFileSync(path.join(__dirname, 'src/ssl/cert.key'))
}
}
})
3.uniapp
uniapp则有点不一样,需要手动复制cert.crt和cert.key两个文件的内容到manifest.json中文章来源:https://www.toymoban.com/news/detail-755798.html
// manifest.json
"h5" : {
"devServer" : {
"https" : {
"cert" : "-----BEGIN CERTIFICATE-----\nMIIDTDCCAjSgAwIBAgIFNzg2OTEwDQYJKoZIhvcNAQELBQAwXjEQMA4GA1UEAx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tx/6rPhutP2SUt/30sbh3Xa\nMBcoMWJMQaZlWmQmNUrtuyT1Qowsgv88jcDfYGYPaFePNE6ZS5mYlOapAoGBAOie\nZYfHM3RN3ULFEMOBnAjakdLclicJ33u9dIhdN91goG7IaCL+iBy8M/ctUTACpFfa\ni7lXVhccBnpX5W663DjAkpWa4ecwE04Mi/5Pe8nWsPKI+NgYg/zH3D5gYROKOXst\nIwi0Ku0wiskI8KShuJ8x9/RG+xSyix+Nuj0uDTa5AoGBANIvKtVsz5Lu5mg6P+e/\nprQozy6JHUIZkYLRZa92qPOtYgchZKu7L3GIBj0WU0vCswcuNlBwDcHHtzhrX5MI\nelnGr3/Rl98dPcShtQ/ECyuWUttpoWu/Gsr9J6Td2ezQqK5x4OHjuEzBSdR0ucRz\njft/jNnAYxsTTjy6mIwtaPdJAoGAI3SJGyHkmNSJZfTtci8eHkdaNJ6EkFyz4INm\n8KXsruRcb6ZCIRtUmbSh4wnYNw4gCHf6FfzW1VzjK+Cbyr3WdRv8+6qIxhkQyDOn\nXtK0lnYPsFfqPd22uF+qFSPRiA3oZvrhPSFg7ZgZfzzKmP1BS5ptJaFwAZ6ZD+0l\nNs//GZkCgYEA0NdSF/XomDS7NLh1B2vWW/tcCoOmv+FQ3/mtnnx4JNs9KEDKazSb\nA6jUyuzmQfwHJrHTLbSoVcvPwSmdU+4mvd4ktomg4CTp/DjgHAjPveMsWiv/++v/\n8VtcXkc/M/c0AMrdZBv9J2jXngbz8rvOrK/QZpsaoohlOAsHjFl4lWg=\n-----END RSA PRIVATE KEY-----\n"
},
"proxy" : {}
},
"async" : {
"delay" : "10000"
},
"router" : {
"base" : "./"
}
},
总结
到此,证书生成安装结束了,项目跑起来就ok了文章来源地址https://www.toymoban.com/news/detail-755798.html
到了这里,关于vue项目,uniapp项目本地调试开启https,加载本地证书的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!