searchone.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. <template>
  2. <view>
  3. <view class="pdl14">
  4. <view class="listtopa flexc">
  5. <image :src="speechimg" @click="getSeep"></image>
  6. <input :placeholder="'请输入'+placeholder+'名称进行搜索'" v-model="xmmc" class="flex1 f16 c34"/>
  7. <view class="btn f16 cf flexcc" @click="getSearch">搜索</view>
  8. </view>
  9. </view>
  10. <view class="bgbox" v-if="voiceflag" @click="getClose"></view>
  11. <view class="voice" v-if="voiceflag">
  12. <image :src="closeimg" class="choseimg" @click="getClose"></image>
  13. <view v-if="isShow">
  14. <view v-if="sendLock" class="tip">
  15. <view class="txt">试试这样说</view>
  16. <view class="txt-bt">科技{{placeholder}}</view>
  17. </view>
  18. <view v-else>
  19. <!-- @click="resultClick" -->
  20. <view class="res-txt" >
  21. <text :style="{
  22. color: (resultText == '正在识别中2...' || resultText == '未检测到语音,请重试') ?
  23. '#919098' :
  24. '#2979ff'}">
  25. {{resultText}}
  26. </text>
  27. <!-- <image v-if="resultText != '正在识别中.1..' && resultText != '未检测到语音,请重试'"
  28. src="/static/img/xiaoshou.png" mode="widthFix"></image> -->
  29. </view>
  30. </view>
  31. </view>
  32. <view v-else class="tip">
  33. <view v-if="!sendLock" class="tipbox">{{voicetext}}</view>
  34. <view v-html="text" class="txt" style="color: #8e8d9a;"></view>
  35. <view v-if="!sendLock" class="prompt-loader">
  36. <view class="em" :style="randomRgb()" v-for="(item,index) in 30" :key="index"></view>
  37. </view>
  38. <view v-else class="prompt-loader"></view>
  39. </view>
  40. <view class="btn" @longpress="handleRecordStart" @touchmove="handleTouchMove" @touchend="handleRecordStop">
  41. <view class="btn-cont">
  42. 长按开始语音搜索
  43. </view>
  44. </view>
  45. </view>
  46. </view>
  47. </template>
  48. <script>
  49. const recorderManager = uni.getRecorderManager();
  50. //播放录音
  51. const innerAudioContext = uni.createInnerAudioContext();
  52. innerAudioContext.autoplay = true;
  53. export default{
  54. props:{
  55. name:{
  56. type: String,
  57. default () {
  58. return ''
  59. }
  60. },
  61. placeholder:{
  62. type: String,
  63. default () {
  64. return '项目'
  65. }
  66. }
  67. },
  68. watch:{
  69. sendLock(newVal, oldVal) {
  70. var that=this;
  71. recorderManager.onStop(res => {
  72. if (newVal) return //上锁不发送
  73. //解锁发送网络请求
  74. setTimeout(function(res){
  75. if(!that.voicetext&&!that.partialResult){
  76. that.resultText='未检测到语音,请重试'
  77. that.sendLock=true;
  78. }else{
  79. that.resultText=that.voicetext||that.partialResult;
  80. // console.log(that.resultText,85)
  81. that.xmmc=that.xmmc+that.resultText;
  82. setTimeout(function(){
  83. that.$emit('getSearch',that.resultText)
  84. that.voiceflag=false;
  85. that.sendLock=true;
  86. },1200)
  87. }
  88. },1000)
  89. // console.log(res.tempFilePath, '获取录音文件')
  90. });
  91. },
  92. },
  93. data(){
  94. return {
  95. speechimg:require('@/static/images/index/speech.png'),
  96. xmmc:'',
  97. closeimg:require('@/static/images/close.png'),
  98. voiceflag:false,
  99. voiceToken: '',
  100. timer: null,
  101. text: '',
  102. resultText: '正在识别中...',
  103. startPoint: {},
  104. sendLock: true,
  105. isShow: true,
  106. adioFileData: '',
  107. adioSize: '',
  108. resContent: '',
  109. luyinStatus: true,
  110. voicetext:'',
  111. options: {}, // 语音转文字的设置
  112. partialResult:'',//临时语音
  113. }
  114. },
  115. mounted() {
  116. if(!this.xmmc){
  117. this.xmmc=this.name
  118. }
  119. var that=this;
  120. // #ifdef APP-PLUS
  121. // 监听语音识别事件
  122. plus.speech.addEventListener('start', this.ontStart, false);
  123. plus.speech.addEventListener('volumeChange', this.onVolumeChange, false);
  124. plus.speech.addEventListener('recognizing', this.onRecognizing, false);
  125. plus.speech.addEventListener('recognition', this.onRecognition, false);
  126. plus.speech.addEventListener('end', this.onEnd, false);
  127. // #endif
  128. recorderManager.onStop(function(res) {
  129. //录音后的回调函数
  130. // console.log('recorder stop' + JSON.stringify(res));
  131. // console.log(res.tempFilePath);
  132. that.voicePath = res.tempFilePath;
  133. // self.voicePath =
  134. });
  135. },
  136. onUnload() {
  137. },
  138. unmounted() {
  139. },
  140. methods:{
  141. getClose(){
  142. this.voiceflag=false;
  143. this.sendLock=true;
  144. },
  145. onRecognizing(e){
  146. this.partialResult=this.partialResult+e.partialResult;
  147. },
  148. // 录音转文字
  149. handleVoice() {
  150. // console.log('语音输入')
  151. let _this = this;
  152. this.options.engine = 'baidu'
  153. // this.options.timeout = 60 * 1000; //超时时间
  154. this.options.continue = true;//语音识别是否采用持续模式
  155. this.options.punctuation = false; // 是否需要标点符号
  156. this.options.userInterface = false; // 是否显示语音界面
  157. plus.speech.startRecognize(this.options, (s) => {
  158. _this.voicetext+=s;
  159. // plus.speech.stopRecognize(); // 关
  160. });
  161. },
  162. //长按录音方法
  163. handleRecordStart(e) {
  164. this.voicetext='';
  165. this.partialResult='';
  166. this.startPoint = e.touches[0]; //记录长按时开始点信息,后面用于计算上划取消时手指滑动的距离。
  167. recorderManager.start({duration: 60000}); //开始录音
  168. this.handleVoice()
  169. this.text = `<text style="color:#333">上划取消搜索</text>`;
  170. this.sendLock = false; //长按时不上锁。
  171. this.isShow = false;
  172. this.resultText = '正在识别中...';
  173. // 按钮
  174. },
  175. //结束录音 (手指松开)时触发
  176. handleRecordStop(e) {
  177. var that=this;
  178. this.isShow = true;
  179. setTimeout(function(){
  180. plus.speech.stopRecognize();
  181. recorderManager.stop(); //结束录音
  182. },1200)
  183. },
  184. //上划取消搜索
  185. handleTouchMove(e) {
  186. let moveLenght = e.touches[e.touches.length - 1].clientY - this.startPoint.clientY;
  187. if (Math.abs(moveLenght) > 50) {
  188. this.text = `松开手指,<text style="color:#333">取消搜索</text>`;
  189. this.sendLock = true; //触发了上滑取消搜索,上锁
  190. this.isShow = false;
  191. } else {
  192. this.text = `<text style="color:#333">上划取消搜索</text>`;
  193. this.sendLock = false; //上划距离不足,可以搜索,不上锁
  194. this.isShow = false;
  195. }
  196. },
  197. //获取录音结果子传父
  198. resultClick() {
  199. if (this.resultText == '正在识别中...' || this.resultText == '未检测到语音,请重试') return;
  200. this.$emit('getSearch',this.resultText)
  201. this.voiceflag=false;
  202. this.sendLock=true;
  203. // this.$emit('voiceResult', this.resultText)
  204. },
  205. //弹窗关闭之后的操作,点击遮罩层或关闭按钮
  206. // afterHide() {
  207. // this.sendLock = true;
  208. // this.$emit('closePopup');
  209. // clearInterval(this.timer);
  210. // this.resultText = '正在识别中...';
  211. // },
  212. randomRgb() {
  213. let R = Math.floor(Math.random() * 130 + 110);
  214. let G = Math.floor(Math.random() * 130 + 110);
  215. let B = Math.floor(Math.random() * 130 + 110);
  216. return {
  217. background: `rgb(${R},${G},${B}, 1)`
  218. };
  219. },
  220. // 语音转文字
  221. getSeep(){
  222. this.voicetext='';
  223. this.partialResult='';
  224. this.voiceflag=true;
  225. },
  226. onEnd() {
  227. // let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
  228. // let curRoute = routes[routes.length - 1].route //获取当前页面路由
  229. // if(curRoute=='pages/work/shprogress'){
  230. // }else{
  231. // if(this.xmmc){
  232. // this.getSearch()
  233. // }
  234. // }
  235. },
  236. getSearch(){
  237. // console.log(this.xmmc,99)
  238. this.$emit('getSearch',this.xmmc)
  239. }
  240. }
  241. }
  242. </script>
  243. <style lang="scss" scoped>
  244. // 搜索
  245. .listtopa{border: 6rpx solid #FD5001;border-radius: 32rpx;height:64rpx;box-sizing: border-box;padding:0 140rpx 0 32rpx ;position: relative;
  246. input{}
  247. image{width: 20rpx;height: 30rpx;margin-right: 16rpx;}
  248. .btn{background: #FA5F03;border-radius: 32rpx;width: 120rpx;position: absolute;right: -2rpx;top: -2rpx;bottom:-2rpx;}
  249. }
  250. .bgbox{z-index: 1900;}
  251. .choseimg{width: 34rpx;height: 34rpx;position: absolute;left: 36rpx;top: 54rpx;}
  252. .voice {
  253. min-height: 500rpx;
  254. padding: 100rpx 60rpx 0 60rpx;
  255. position: relative;
  256. background-color: #fff;
  257. position: fixed;
  258. left:0;right:0;bottom:0;z-index: 2000;
  259. padding-bottom: 180rpx;
  260. .res-txt {
  261. text-align: center;
  262. margin-top: 40rpx;
  263. font-size: 36rpx;
  264. color: #919098;
  265. image {
  266. display: block;
  267. margin: auto;
  268. margin-top: 10rpx;
  269. width: 60rpx;
  270. animation: bounce-down 2.6s linear infinite;
  271. }
  272. }
  273. .tip {
  274. margin-top: 15rpx;
  275. text-align: center;
  276. .txt {
  277. font-size: 36rpx;
  278. color: #151823;
  279. }
  280. .txt-bt {
  281. margin-top: 20rpx;
  282. color: #919098;
  283. }
  284. }
  285. .btn {
  286. width: 50%;
  287. height: 80rpx;
  288. display: flex;
  289. align-items: center;
  290. justify-content: center;
  291. color: #fff;
  292. border-radius: 50rpx;
  293. background: #3484fd;
  294. position: absolute;
  295. bottom: 80rpx;
  296. left: 50%;
  297. transform: translateX(-50%);
  298. .btn-cont {
  299. display: flex;
  300. align-items: center;
  301. }
  302. }
  303. }
  304. @-webkit-keyframes bounce-down {
  305. 25% {
  306. -webkit-transform: translateY(-10px);
  307. }
  308. 50%,
  309. 100% {
  310. -webkit-transform: translateY(0);
  311. }
  312. 75% {
  313. -webkit-transform: translateY(13px);
  314. }
  315. }
  316. .content{background-color: #f5f5f5;position: fixed;left: 0;right: 0;bottom: 0;z-index: 10000;height: 500rpx;}
  317. /* 语音动画 */
  318. .prompt-loader {
  319. width: 100%;
  320. height: 35px;
  321. display: flex;
  322. align-items: center;
  323. justify-content: space-between;
  324. margin: 30rpx auto;
  325. }
  326. .prompt-loader .em {
  327. height: 15%;
  328. width: 2px;
  329. float: left;
  330. display: block;
  331. background: #333333;
  332. }
  333. .prompt-loader .em:last-child {
  334. margin-right: 0px;
  335. }
  336. .prompt-loader .em:nth-child(1) {
  337. animation: load 1.3s 0.4s infinite linear;
  338. }
  339. .prompt-loader .em:nth-child(2) {
  340. animation: load 1.3s 0.2s infinite linear;
  341. }
  342. .prompt-loader .em:nth-child(3) {
  343. animation: load 1.3s 0.6s infinite linear;
  344. }
  345. .prompt-loader .em:nth-child(4) {
  346. animation: load 1.3s 0.8s infinite linear;
  347. }
  348. .prompt-loader .em:nth-child(5) {
  349. animation: load 1.3s 0.6s infinite linear;
  350. }
  351. .prompt-loader .em:nth-child(6) {
  352. animation: load 1.3s 0.4s infinite linear;
  353. }
  354. .prompt-loader .em:nth-child(7) {
  355. animation: load 1.3s 0.2s infinite linear;
  356. }
  357. .prompt-loader .em:nth-child(8) {
  358. animation: load 1.3s 0.6s infinite linear;
  359. }
  360. .prompt-loader .em:nth-child(9) {
  361. animation: load 1.3s 0.2s infinite linear;
  362. }
  363. .prompt-loader .em:nth-child(10) {
  364. animation: load 1.3s 0.4s infinite linear;
  365. }
  366. .prompt-loader .em:nth-child(11) {
  367. animation: load 1.3s 0.6s infinite linear;
  368. }
  369. .prompt-loader .em:nth-child(12) {
  370. animation: load 1.3s 0.8s infinite linear;
  371. }
  372. .prompt-loader .em:nth-child(13) {
  373. animation: load 1.3s 1s infinite linear;
  374. }
  375. .prompt-loader .em:nth-child(14) {
  376. animation: load 1.3s 0.2s infinite linear;
  377. }
  378. .prompt-loader .em:nth-child(15) {
  379. animation: load 1.3s 0.6s infinite linear;
  380. }
  381. .prompt-loader .em:nth-child(16) {
  382. animation: load 1.3s 0.6s infinite linear;
  383. }
  384. .prompt-loader .em:nth-child(17) {
  385. animation: load 1.3s 0.8s infinite linear;
  386. }
  387. .prompt-loader .em:nth-child(18) {
  388. animation: load 1.3s 0.2s infinite linear;
  389. }
  390. .prompt-loader .em:nth-child(19) {
  391. animation: load 1.3s 0.4s infinite linear;
  392. }
  393. .prompt-loader .em:nth-child(20) {
  394. animation: load 1.3s 0.6s infinite linear;
  395. }
  396. .prompt-loader .em:nth-child(21) {
  397. animation: load 1.3s 0.5s infinite linear;
  398. }
  399. .prompt-loader .em:nth-child(22) {
  400. animation: load 1.3s 0.2s infinite linear;
  401. }
  402. .prompt-loader .em:nth-child(23) {
  403. animation: load 1.3s 0.4s infinite linear;
  404. }
  405. .prompt-loader .em:nth-child(24) {
  406. animation: load 1.3s 0.6s infinite linear;
  407. }
  408. .prompt-loader .em:nth-child(25) {
  409. animation: load 1.3s 0.8s infinite linear;
  410. }
  411. .prompt-loader .em:nth-child(26) {
  412. animation: load 1.3s 0.2s infinite linear;
  413. }
  414. .prompt-loader .em:nth-child(27) {
  415. animation: load 1.3s 0.4s infinite linear;
  416. }
  417. .prompt-loader .em:nth-child(28) {
  418. animation: load 1.3s 0.1s infinite linear;
  419. }
  420. .prompt-loader .em:nth-child(29) {
  421. animation: load 1.3s 0.3s infinite linear;
  422. }
  423. .prompt-loader .em:nth-child(30) {
  424. animation: load 1.3s 0.6s infinite linear;
  425. }
  426. @keyframes load {
  427. 0% {
  428. height: 15%;
  429. }
  430. 50% {
  431. height: 100%;
  432. }
  433. 100% {
  434. height: 15%;
  435. }
  436. }
  437. </style>