seep.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. <template>
  2. <view class="content">
  3. <view class="bgbox"></view>
  4. <view class="voice">
  5. <view v-if="isShow">
  6. <view v-if="sendLock" class="tip">
  7. <view class="txt">试试这样说</view>
  8. <view class="txt-bt">马大云银行、马大云、马大云发展有限公司</view>
  9. </view>
  10. <view v-else>
  11. <view class="res-txt" @click="resultClick">
  12. <text :style="{
  13. color: (resultText == '正在识别中2...' || resultText == '未检测到语音,请重试') ?
  14. '#919098' :
  15. '#2979ff'}">
  16. {{resultText}}
  17. </text>
  18. <image v-if="resultText != '正在识别中.1..' && resultText != '未检测到语音,请重试'"
  19. src="/static/img/xiaoshou.png" mode="widthFix"></image>
  20. </view>
  21. </view>
  22. </view>
  23. <view v-else class="tip">
  24. <view v-html="text" class="txt" style="color: #8e8d9a;"></view>
  25. <view v-if="!sendLock" class="prompt-loader">
  26. <view class="em" :style="randomRgb()" v-for="(item,index) in 30" :key="index"></view>
  27. </view>
  28. <view v-else class="prompt-loader"></view>
  29. </view>
  30. <view class="btn" @longpress="handleRecordStart" @touchmove="handleTouchMove"
  31. @touchend="handleRecordStop">
  32. <view class="btn-cont">
  33. 长按开始语音搜索
  34. </view>
  35. </view>
  36. </view>
  37. </view>
  38. </template>
  39. <script>
  40. export default {
  41. components: {
  42. // popup
  43. },
  44. data() {
  45. return {
  46. recorderManager: uni.getRecorderManager(),
  47. voiceToken: '',
  48. timer: null,
  49. text: '',
  50. resultText: '正在识别中...',
  51. startPoint: {},
  52. sendLock: true,
  53. isShow: true,
  54. adioFileData: '',
  55. adioSize: '',
  56. resContent: '',
  57. luyinStatus: true,
  58. voicetext:'',
  59. options: {}, // 语音转文字的设置
  60. }
  61. },
  62. watch: {
  63. sendLock(newVal, oldVal) {
  64. console.log(newVal)
  65. var that=this;
  66. this.recorderManager.onStop(res => {
  67. if (newVal) return //上锁不发送
  68. //解锁发送网络请求
  69. console.log(that.resultText,7)
  70. setTimeout(function(res){
  71. if(!that.voicetext){
  72. that.resultText='未检测到语音,请重试'
  73. }else{
  74. that.resultText=that.voicetext
  75. }
  76. console.log(that.resultText,9)
  77. },1200)
  78. // this.uploadAudio(res.tempFilePath)
  79. console.log(res.tempFilePath, '获取录音文件')
  80. // that.Audio2dataURL(res.tempFilePath)
  81. });
  82. },
  83. },
  84. mounted() {
  85. console.log(1)
  86. },
  87. methods: {
  88. // 录音转文字
  89. handleVoice() {
  90. console.log('语音输入')
  91. let _this = this;
  92. this.options.engine = 'baidu'
  93. this.options.punctuation = false; // 是否需要标点符号
  94. this.options.timeout = 10 * 1000; //超时时间
  95. this.options.userInterface = false; // 是否显示语音界面
  96. plus.speech.startRecognize(this.options, (s) => {
  97. _this.voicetext = s
  98. _this.zwz = true
  99. plus.speech.stopRecognize(); // 关闭
  100. });
  101. },
  102. //长按录音方法
  103. handleRecordStart(e) {
  104. this.startPoint = e.touches[0]; //记录长按时开始点信息,后面用于计算上划取消时手指滑动的距离。
  105. this.recorderManager.start(); //开始录音
  106. this.handleVoice()
  107. this.text = `<text style="color:#333">上划取消搜索</text>`;
  108. this.sendLock = false; //长按时不上锁。
  109. this.isShow = false;
  110. this.resultText = '正在识别中...';
  111. // 按钮
  112. console.log(67)
  113. },
  114. //结束录音 (手指松开)时触发
  115. handleRecordStop(e) {
  116. console.log(56)
  117. this.isShow = true;
  118. console.log(this.resultText,777)
  119. this.recorderManager.stop(); //结束录音
  120. },
  121. //上划取消搜索
  122. handleTouchMove(e) {
  123. console.log(555)
  124. let moveLenght = e.touches[e.touches.length - 1].clientY - this.startPoint.clientY;
  125. if (Math.abs(moveLenght) > 50) {
  126. this.text = `松开手指,<text style="color:#333">取消搜索</text>`;
  127. this.sendLock = true; //触发了上滑取消搜索,上锁
  128. this.isShow = false;
  129. } else {
  130. this.text = `<text style="color:#333">上划取消搜索</text>`;
  131. this.sendLock = false; //上划距离不足,可以搜索,不上锁
  132. this.isShow = false;
  133. }
  134. },
  135. //上传录音文件
  136. uploadAudio(tempFilePath) {
  137. // uni.uploadFile({
  138. // url: `${env.test.baseURL}/asr/v2/simpleRequest/`,
  139. // filePath: tempFilePath,
  140. // name: 'file',
  141. // formData: { //接口上传参数
  142. // api_key: '3yo',
  143. // api_secret: 'zT2jXm7wCYth',
  144. // },
  145. // header: {
  146. // 'content-type': 'multipart/form-data',
  147. // 'Authorization': 'Basic cYmVyX3NlY3JldA',
  148. // 'Blade-Auth': uni.getStorageSync('Blade-Auth')
  149. // },
  150. // success: (res) => {
  151. // if (res.statusCode != 200) {
  152. // this.resultText = "未检测到语音,请重试";
  153. // return;
  154. // }
  155. // let index = res.data.indexOf('{')
  156. // let id = JSON.parse(res.data.substr(index)).job_id
  157. // let params = {
  158. // job_id: id,
  159. // token: this.voiceToken,
  160. // }
  161. // this.getResult(params)
  162. // console.log(params, 'params')
  163. // console.log(res, 'resss')
  164. // },
  165. // fail: (err) => {
  166. // this.resultText = "未检测到语音,请重试";
  167. // console.log('---上传失败---' + JSON.stringify(err))
  168. // }
  169. // })
  170. },
  171. // 录音之后的结果
  172. //获取录音结果
  173. //获取录音结果子传父
  174. resultClick() {
  175. console.log(78)
  176. if (this.resultText == '正在识别中...' || this.resultText == '未检测到语音,请重试') return;
  177. this.$emit('voiceResult', this.resultText)
  178. },
  179. //录音token
  180. show() {
  181. this.$refs.popup.show() // 显示
  182. this.voiceTokenMethod()
  183. },
  184. hide() {
  185. this.$refs.popup.hide() // 隐藏
  186. },
  187. //弹窗关闭之后的操作,点击遮罩层或关闭按钮
  188. afterHide() {
  189. console.log(77)
  190. this.sendLock = true;
  191. this.$emit('closePopup');
  192. clearInterval(this.timer);
  193. this.resultText = '正在识别中...';
  194. },
  195. randomRgb() {
  196. let R = Math.floor(Math.random() * 130 + 110);
  197. let G = Math.floor(Math.random() * 130 + 110);
  198. let B = Math.floor(Math.random() * 130 + 110);
  199. return {
  200. background: `rgb(${R},${G},${B}, 1)`
  201. };
  202. },
  203. }
  204. }
  205. </script>
  206. <style lang="scss" scoped>
  207. .voice {
  208. height: 500rpx;
  209. padding: 100rpx 60rpx 0 60rpx;
  210. position: relative;
  211. background-color: #fff;
  212. position: fixed;
  213. left:0;right:0;bottom:0;z-index: 20;
  214. .res-txt {
  215. text-align: center;
  216. margin-top: 40rpx;
  217. font-size: 36rpx;
  218. color: #919098;
  219. image {
  220. display: block;
  221. margin: auto;
  222. margin-top: 10rpx;
  223. width: 60rpx;
  224. animation: bounce-down 2.6s linear infinite;
  225. }
  226. }
  227. .tip {
  228. margin-top: 15rpx;
  229. text-align: center;
  230. .txt {
  231. font-size: 36rpx;
  232. color: #151823;
  233. }
  234. .txt-bt {
  235. margin-top: 20rpx;
  236. color: #919098;
  237. }
  238. }
  239. .btn {
  240. width: 50%;
  241. height: 80rpx;
  242. display: flex;
  243. align-items: center;
  244. justify-content: center;
  245. color: #fff;
  246. border-radius: 50rpx;
  247. background: #3484fd;
  248. position: absolute;
  249. bottom: 50px;
  250. left: 50%;
  251. transform: translateX(-50%);
  252. .btn-cont {
  253. display: flex;
  254. align-items: center;
  255. }
  256. }
  257. }
  258. @-webkit-keyframes bounce-down {
  259. 25% {
  260. -webkit-transform: translateY(-10px);
  261. }
  262. 50%,
  263. 100% {
  264. -webkit-transform: translateY(0);
  265. }
  266. 75% {
  267. -webkit-transform: translateY(13px);
  268. }
  269. }
  270. </style>
  271. <style scoped>
  272. .content{background-color: #f5f5f5;position: fixed;left: 0;right: 0;bottom: 0;z-index: 10000;height: 500rpx;}
  273. /* 语音动画 */
  274. .prompt-loader {
  275. width: 100%;
  276. height: 35px;
  277. display: flex;
  278. align-items: center;
  279. justify-content: space-between;
  280. margin: 30rpx auto;
  281. }
  282. .prompt-loader .em {
  283. height: 15%;
  284. width: 2px;
  285. float: left;
  286. display: block;
  287. background: #333333;
  288. }
  289. .prompt-loader .em:last-child {
  290. margin-right: 0px;
  291. }
  292. .prompt-loader .em:nth-child(1) {
  293. animation: load 1.3s 0.4s infinite linear;
  294. }
  295. .prompt-loader .em:nth-child(2) {
  296. animation: load 1.3s 0.2s infinite linear;
  297. }
  298. .prompt-loader .em:nth-child(3) {
  299. animation: load 1.3s 0.6s infinite linear;
  300. }
  301. .prompt-loader .em:nth-child(4) {
  302. animation: load 1.3s 0.8s infinite linear;
  303. }
  304. .prompt-loader .em:nth-child(5) {
  305. animation: load 1.3s 0.6s infinite linear;
  306. }
  307. .prompt-loader .em:nth-child(6) {
  308. animation: load 1.3s 0.4s infinite linear;
  309. }
  310. .prompt-loader .em:nth-child(7) {
  311. animation: load 1.3s 0.2s infinite linear;
  312. }
  313. .prompt-loader .em:nth-child(8) {
  314. animation: load 1.3s 0.6s infinite linear;
  315. }
  316. .prompt-loader .em:nth-child(9) {
  317. animation: load 1.3s 0.2s infinite linear;
  318. }
  319. .prompt-loader .em:nth-child(10) {
  320. animation: load 1.3s 0.4s infinite linear;
  321. }
  322. .prompt-loader .em:nth-child(11) {
  323. animation: load 1.3s 0.6s infinite linear;
  324. }
  325. .prompt-loader .em:nth-child(12) {
  326. animation: load 1.3s 0.8s infinite linear;
  327. }
  328. .prompt-loader .em:nth-child(13) {
  329. animation: load 1.3s 1s infinite linear;
  330. }
  331. .prompt-loader .em:nth-child(14) {
  332. animation: load 1.3s 0.2s infinite linear;
  333. }
  334. .prompt-loader .em:nth-child(15) {
  335. animation: load 1.3s 0.6s infinite linear;
  336. }
  337. .prompt-loader .em:nth-child(16) {
  338. animation: load 1.3s 0.6s infinite linear;
  339. }
  340. .prompt-loader .em:nth-child(17) {
  341. animation: load 1.3s 0.8s infinite linear;
  342. }
  343. .prompt-loader .em:nth-child(18) {
  344. animation: load 1.3s 0.2s infinite linear;
  345. }
  346. .prompt-loader .em:nth-child(19) {
  347. animation: load 1.3s 0.4s infinite linear;
  348. }
  349. .prompt-loader .em:nth-child(20) {
  350. animation: load 1.3s 0.6s infinite linear;
  351. }
  352. .prompt-loader .em:nth-child(21) {
  353. animation: load 1.3s 0.5s infinite linear;
  354. }
  355. .prompt-loader .em:nth-child(22) {
  356. animation: load 1.3s 0.2s infinite linear;
  357. }
  358. .prompt-loader .em:nth-child(23) {
  359. animation: load 1.3s 0.4s infinite linear;
  360. }
  361. .prompt-loader .em:nth-child(24) {
  362. animation: load 1.3s 0.6s infinite linear;
  363. }
  364. .prompt-loader .em:nth-child(25) {
  365. animation: load 1.3s 0.8s infinite linear;
  366. }
  367. .prompt-loader .em:nth-child(26) {
  368. animation: load 1.3s 0.2s infinite linear;
  369. }
  370. .prompt-loader .em:nth-child(27) {
  371. animation: load 1.3s 0.4s infinite linear;
  372. }
  373. .prompt-loader .em:nth-child(28) {
  374. animation: load 1.3s 0.1s infinite linear;
  375. }
  376. .prompt-loader .em:nth-child(29) {
  377. animation: load 1.3s 0.3s infinite linear;
  378. }
  379. .prompt-loader .em:nth-child(30) {
  380. animation: load 1.3s 0.6s infinite linear;
  381. }
  382. @keyframes load {
  383. 0% {
  384. height: 15%;
  385. }
  386. 50% {
  387. height: 100%;
  388. }
  389. 100% {
  390. height: 15%;
  391. }
  392. }
  393. </style>