video.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta name="viewport"
  5. content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
  6. <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
  7. <title>视频认证</title>
  8. <style>
  9. body {
  10. margin: 0;
  11. background: #F5F6FA;
  12. }
  13. .content {
  14. background: #F5F6FA;
  15. height: 100vh;
  16. position: relative;
  17. }
  18. .row {
  19. display: flex;
  20. justify-content: center;
  21. padding-top: 50px;
  22. }
  23. .img-video {
  24. position: relative;
  25. border-radius: 280px;
  26. width: 250px;
  27. height: 250px;
  28. overflow: hidden;
  29. }
  30. .row2 {
  31. display: flex;
  32. font-size: 20px;
  33. justify-content: center;
  34. margin-top: 20px;
  35. }
  36. .verify-btn {
  37. background: #F5BA3C;
  38. font-size: 20px;
  39. padding: 10px 0;
  40. width: 70%;
  41. display: flex;
  42. justify-content: center;
  43. align-items: center;
  44. border-radius: 50px;
  45. color: white;
  46. }
  47. .btn {
  48. position: absolute;
  49. bottom: 0;
  50. background: white;
  51. width: 100%;
  52. display: flex;
  53. justify-content: center;
  54. padding: 40px 0;
  55. }
  56. .img {
  57. position: absolute;
  58. top: 0;
  59. bottom: 0;
  60. width: 100%;
  61. width: 564px;
  62. height: 564px;
  63. right: 0;
  64. left: 0;
  65. margin: auto;
  66. }
  67. .video-content {
  68. position: absolute;
  69. top: 0;
  70. bottom: 0;
  71. width: 100%;
  72. right: 0;
  73. left: 0;
  74. margin: auto;
  75. }
  76. .video {
  77. width: calc(100%);
  78. background: #F5F6FA;
  79. }
  80. .video-btn {
  81. width: 100%;
  82. display: flex;
  83. justify-content: center;
  84. }
  85. .progressBar {
  86. width: 50%;
  87. height: 25px;
  88. display: block;
  89. top: 0;
  90. left: 0;
  91. bottom: 0;
  92. right: 0;
  93. margin: auto;
  94. padding: 10px 10px 10px 10px;
  95. position: absolute;
  96. z-index: 2001;
  97. background: rgb(102, 102, 102);
  98. color: white;
  99. border-radius: 5px;
  100. }
  101. .text{
  102. padding: 20px;
  103. }
  104. </style>
  105. </head>
  106. <body>
  107. <div id='app'>
  108. <div class='content'>
  109. <!-- <div id="progressBar" class="progressBar">地图数据加载中...</div> -->
  110. <div class='row'>
  111. <div class='img-video'>
  112. <!-- <img src="./img/shipinrenzheng.png" alt="" class='img'> -->
  113. <div class="video-content">
  114. <!-- 人脸检测-->
  115. <div class="video-cover"></div>
  116. <video :src="url" ref="videoRef" autoplay playsinline x5-video-player-type="h5"
  117. class="video"></video>
  118. </div>
  119. </div>
  120. </div>
  121. <!-- <div class='row2' v-show="!showVideo">
  122. 请打开摄像头 并阅读提示文字
  123. </div> -->
  124. <div class='row2'>
  125. 请将头像放于圆圈内,并阅读以下文字
  126. </div>
  127. <div class='row2 text'>
  128. 网络客服过段时间浪费多少级范德萨快逻辑范德萨拉丝机多亏了房价按时付款了金坷垃所肩负的看离开静安寺了发动机as在考虑家双方都会计分录卡死范德萨拉使肌肤抵抗力
  129. </div>
  130. <div class='btn'>
  131. <!-- <div class="verify-btn" @click="getCamera" v-show="!showVideo">打开摄像头</div> -->
  132. <div class='video-btn'>
  133. <div class="verify-btn" @click="saveVideo" v-if='!isAlreadyRecord'>开始录制</div>
  134. <div class="verify-btn" @click="saveVideo" v-if='isAlreadyRecord' data-action="navigateTo">结束录制
  135. </div>
  136. <!-- <button class="video-close" @click="closeVideo">×</button>
  137. <button class="video-save" @click="saveVideo">
  138. {{ isAlreadyRecord ? '结束录制(' + count + 's)' : '开始录制' }}</button> -->
  139. </div>
  140. </div>
  141. <!-- <video ref="videob" controls="" name="media" width="100%" height="100"></video> -->
  142. </div>
  143. </div>
  144. </body>
  145. <script type="text/javascript" src="vue.min.js"></script>
  146. <script type="text/javascript" src="https://taohaoliang.oss-cn-beijing.aliyuncs.com/app/jquery.min.js"></script>
  147. <script type="text/javascript" src="base64.js"></script>
  148. <script type="text/javascript" src="crypto.js"></script>
  149. <script type="text/javascript" src="index.js"></script>
  150. <script type="text/javascript">
  151. document.addEventListener('UniAppJSBridgeReady', function() {
  152. uni.webView.getEnv(function(res) {
  153. console.log('当前环境:' + JSON.stringify(res));
  154. });
  155. //
  156. new Vue({
  157. el: '#app',
  158. data: {
  159. url: '',
  160. // showVideo: true,
  161. mediaRecorder: null,
  162. MediaStreamTrack: null,
  163. isAlreadyRecord: false,
  164. count: 8,
  165. countTimer: null,
  166. recordedBlobs: [],
  167. compId: ''
  168. },
  169. beforeDestroy() {
  170. this.MediaStreamTrack && this.MediaStreamTrack.stop()
  171. this.countTimer && clearTimeout(this.countTimer)
  172. },
  173. computed: {},
  174. mounted() {
  175. this.getCamera()
  176. },
  177. watch: {},
  178. methods: {
  179. getPolicyBase64() {
  180. let date = new Date();
  181. date.setHours(date.getHours() + 87600);
  182. let srcT = date.toISOString();
  183. const policyText = {
  184. "expiration": srcT, //设置该Policy的失效时间,超过这个失效时间之后,就没有办法通过这个policy上传文件了
  185. "conditions": [
  186. ["content-length-range", 0, 100 * 1024 * 1024] // 设置上传文件的大小限制,5mb
  187. ]
  188. };
  189. const policyBase64 = Window.base64.encode(JSON.stringify(policyText));
  190. console.log(policyBase64);
  191. return policyBase64;
  192. },
  193. getSignature(policyBase64) {
  194. const accesskey = 'FpClTp4OVrRRtHEfi3lBOWUoLxKieW';
  195. // console.log('video.js')
  196. // console.log(Crypto)
  197. const bytes = Window.Crypto.HMAC(Window.Crypto.SHA1, policyBase64, accesskey, {
  198. asBytes: true
  199. })
  200. const signature = Window.Crypto.util.bytesToBase64(bytes);
  201. // console.log(signature);
  202. return signature;
  203. },
  204. async uploadFile(file) {
  205. var formdata = new FormData()
  206. const policyBase64 = this.getPolicyBase64();
  207. const signature = this.getSignature(policyBase64); //获取签名
  208. const urlStr = "https://taohaoliang.oss-cn-beijing.aliyuncs.com/"
  209. const fileName = "appData/video" + new Date().getTime() + Math.floor(Math.random() *
  210. 150) + '.mp4'
  211. formdata.append("key", fileName)
  212. formdata.append("policy", policyBase64)
  213. formdata.append("OSSAccessKeyId", 'LTAI4G9c14PgKvM23WZ9zrpc')
  214. formdata.append("signature", signature)
  215. formdata.append("success_action_status", '200')
  216. formdata.append("file", file)
  217. console.log('file.name')
  218. $.ajax({
  219. type: "POST",
  220. data: formdata,
  221. contentType: false,
  222. processData: false,
  223. url: urlStr,
  224. success: function(result) {
  225. console.log(urlStr + fileName)
  226. uni.webView.navigateTo({
  227. url: '/pages/mine/company/addcompany?videoSrc=' +
  228. urlStr + fileName
  229. })
  230. },
  231. //请求失败,包含具体的错误信息
  232. error: function(e) {
  233. console.log(e);
  234. console.log(e.status);
  235. console.log(e.responseText);
  236. }
  237. });
  238. },
  239. // 调用摄像头 开始录制
  240. getCamera() {
  241. // 注意本例需要在HTTPS协议网站中运行,新版本Chrome中getUserMedia接口在http下不再支持。
  242. let constraints = {
  243. audio: true,
  244. video: {
  245. facingMode: 'user' // 优先调前置摄像头
  246. }
  247. }
  248. console.log('--------------')
  249. console.log(navigator)
  250. // 老的浏览器可能根本没有实现 mediaDevices,所以我们可以先设置一个空的对象
  251. if (navigator.mediaDevices === undefined) {
  252. navigator.mediaDevices = {}
  253. }
  254. // 一些浏览器部分支持 mediaDevices。我们不能直接给对象设置 getUserMedia
  255. // 因为这样可能会覆盖已有的属性。这里我们只会在没有getUserMedia属性的时候添加它。
  256. if (navigator.mediaDevices.getUserMedia === undefined) {
  257. navigator.mediaDevices.getUserMedia = function(constraints) {
  258. // 首先,如果有getUserMedia的话,就获得它
  259. // var getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia
  260. var getUserMedia = navigator.getUserMedia ||
  261. navigator.webkitGetUserMedia ||
  262. navigator.mozGetUserMedia
  263. // 一些浏览器根本没实现它 - 那么就返回一个error到promise的reject来保持一个统一的接口
  264. if (!getUserMedia) {
  265. this.$messageBox.alert('该浏览器不支持getUserMedia,请使用其他浏览器')
  266. return Promise.reject(new Error(
  267. 'getUserMedia is not implemented in this browser'))
  268. }
  269. // 否则,为老的navigator.getUserMedia方法包裹一个Promise
  270. return new Promise(function(resolve, reject) {
  271. getUserMedia.call(navigator, constraints, resolve, reject)
  272. })
  273. }
  274. }
  275. navigator.mediaDevices.getUserMedia(constraints)
  276. .then((stream) => {
  277. this.MediaStreamTrack = typeof stream.stop === 'function' ? stream :
  278. stream
  279. .getTracks()[0]
  280. console.log(stream)
  281. console.log(this.MediaStreamTrack)
  282. // 显示录制框
  283. this.showVideo = true
  284. this.isAlreadyRecord = false
  285. let winURL = window.URL || window.webkitURL
  286. if ('srcObject' in this.$refs.videoRef) {
  287. this.$refs.videoRef.srcObject = stream
  288. } else {
  289. this.$refs.videoRef.src = winURL.createObjectURL(stream)
  290. }
  291. console.log(this.$refs.videoRef)
  292. this.$refs.videoRef.onloadedmetadata = e => {
  293. // 播放视频
  294. this.$refs.videoRef.play()
  295. }
  296. let options = {
  297. videoBitsPerSecond: 2500000
  298. }
  299. this.mediaRecorder = new MediaRecorder(stream, options)
  300. })
  301. .catch((err) => {
  302. console.log(err)
  303. console.log('摄像头开启失败,请检查摄像头是否授权或是否可用!')
  304. })
  305. $("#progressBar").hide();
  306. },
  307. // 关闭活体检测
  308. closeVideo() {
  309. this.recordedBlobs = ''
  310. this.isAlreadyRecord = false
  311. this.MediaStreamTrack && this.MediaStreamTrack.stop()
  312. this.countTimer && clearTimeout(this.countTimer)
  313. this.$router.go(0)
  314. },
  315. // 录制倒计时
  316. countDown() {
  317. let that = this
  318. let sendTime = Math.round(+new Date() / 1000)
  319. return function walk() {
  320. that.countTimer = setTimeout(function() {
  321. that.countTimer && clearTimeout(that.countTimer)
  322. let diff = sendTime + 8 - Math.round(+new Date() / 1000)
  323. if (diff > 0) {
  324. that.count = diff
  325. walk()
  326. } else {
  327. console.log('倒计时结束')
  328. // this.showVideo = false
  329. console.log(this.url)
  330. console.log(this.MediaStreamTrack)
  331. }
  332. }, 1000)
  333. }
  334. },
  335. dataURLtoBlob(dataurl) {
  336. var arr = dataurl.split(","),
  337. mime = arr[0].match(/:(.*?);/)[1],
  338. bstr = atob(arr[1]),
  339. n = bstr.length,
  340. u8arr = new Uint8Array(n);
  341. while (n--) {
  342. u8arr[n] = bstr.charCodeAt(n);
  343. }
  344. return this.blobToFile(new Blob([u8arr], {
  345. type: mime
  346. }), {
  347. type: mime
  348. });
  349. },
  350. blobToFile(theBlob, type) {
  351. theBlob.lastModifiedDate = new Date();
  352. return new File([theBlob], `${new Date().getTime()}.mp4`, type);
  353. },
  354. // 保存录制视频
  355. saveVideo() {
  356. let that = this
  357. if (this.isAlreadyRecord) {
  358. this.countTimer && clearTimeout(this.countTimer)
  359. // this.showVideo = false
  360. //当录制的数据可用时
  361. this.mediaRecorder.ondataavailable = (e) => {
  362. if (e.data && e.data.size > 0) {
  363. this.recordedBlobs.push(e.data)
  364. }
  365. }
  366. this.mediaRecorder.stop()
  367. setTimeout(() => {
  368. console.log("this.recordedBlobs-------------------------")
  369. var blob = new Blob(this.recordedBlobs, {
  370. type: 'video/mp4'
  371. })
  372. this.isAlreadyRecord = false
  373. this.MediaStreamTrack && this.MediaStreamTrack.stop()
  374. var reader = new FileReader();
  375. reader.readAsDataURL(blob)
  376. reader.onload = (e) => {
  377. // this.$refs.videob.src = reader.result
  378. let a = this.dataURLtoBlob(reader.result)
  379. console.log(a)
  380. this.uploadFile(a)
  381. }
  382. }, 100)
  383. } else {
  384. this.count = 8
  385. this.isAlreadyRecord = true
  386. this.mediaRecorder.start(8000)
  387. this.countDown()()
  388. }
  389. },
  390. changeVideo(e) {
  391. let file = this.$refs.videoFile.files
  392. console.log(file)
  393. }
  394. }
  395. })
  396. });
  397. </script>
  398. </html>