chat.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <template>
  2. <!-- 微信语音识别 -->
  3. <view class="mask" v-show="isShow">
  4. <view class="weixin-asr">
  5. <view class="title">语音识别</view>
  6. <!-- 动画 -->
  7. <view class="spinner">
  8. <view class="rect rect1"></view>
  9. <view class="rect rect2"></view>
  10. <view class="rect rect3"></view>
  11. <view class="rect rect4"></view>
  12. <view class="rect rect5"></view>
  13. </view>
  14. <view class="tip">说出股票代码</view>
  15. <!-- <view>{{resultText}}</view> -->
  16. <button class="btn" type="default" @click="recordStop">说完了</button>
  17. </view>
  18. </view>
  19. </template>
  20. <script>
  21. const WechatSI = requirePlugin("WechatSI");
  22. const ASRManager = WechatSI.getRecordRecognitionManager();
  23. export default {
  24. data () {
  25. return {
  26. isShow: false,
  27. resultText:''
  28. }
  29. },
  30. onReady () {
  31. // 录音开启成功回调
  32. ASRManager.onStart = function (res) {
  33. _this.isShow = true;
  34. }
  35. const _this = this;
  36. // 识别错误事件
  37. ASRManager.onError = (res) => {
  38. _this.isShow = false;
  39. uni.showToast({
  40. title:'发生错误,请稍后再试',
  41. icon:'none',
  42. duration:2000
  43. })
  44. }
  45. // 录音停止回调
  46. ASRManager.onStop = function (res) {
  47. if (res && res.result) {
  48. // _this.resultText = res.result;
  49. _this.$emit('callback', res.result);
  50. } else {
  51. uni.showToast({
  52. icon: 'none',
  53. title: '抱歉,没听到您的声音哦'
  54. })
  55. }
  56. }
  57. },
  58. methods: {
  59. data () {
  60. return {
  61. isShow: false,
  62. }
  63. },
  64. show () {
  65. const _this = this;
  66. // 获取是否授权信息
  67. uni.getSetting({
  68. success(res) {
  69. if (res && res.authSetting && res.authSetting.hasOwnProperty('scope.record')) {
  70. if (res.authSetting['scope.record']) {
  71. start();
  72. } else { // 拒绝授权,打开授权设置
  73. uni.openSetting({
  74. success() {
  75. start();
  76. }
  77. })
  78. }
  79. } else {
  80. start();
  81. }
  82. }
  83. })
  84. function start () {
  85. ASRManager.start({
  86. lang: "zh_CN"
  87. });
  88. }
  89. },
  90. // 录音停止
  91. recordStop () {
  92. this.isShow = false;
  93. ASRManager.stop();
  94. }
  95. }
  96. }
  97. </script>
  98. <style lang="scss" scoped>
  99. .mask {
  100. position: fixed;
  101. top: 0;
  102. left: 0;
  103. z-index: 300;
  104. width: 100%;
  105. height: 100%;
  106. background: rgba(0, 0, 0, .54);
  107. }
  108. .weixin-asr {
  109. position: absolute;
  110. top: calc(50% - #{477upx / 2});
  111. left: 0;
  112. right: 0;
  113. margin: 0 auto;
  114. width: 560upx;
  115. height: 477upx;
  116. background: #fff;
  117. text-align: center;
  118. transform: .5s ease-out .5s;
  119. .title {
  120. margin-top: 42upx;
  121. color: #000;
  122. font-size: 34upx;
  123. font-weight: 500;
  124. }
  125. .spinner {
  126. margin: 50upx;
  127. height: 100upx;
  128. }
  129. .tip {
  130. color: #787878;
  131. }
  132. .btn {
  133. margin-top: 28upx;
  134. width: 225upx;
  135. height: 82upx;
  136. background: #27BCEF;
  137. color: #fff;
  138. font-size: 34upx;
  139. line-height: 82upx;
  140. border-radius: 82upx;
  141. }
  142. }
  143. .spinner {
  144. text-align: center;
  145. }
  146. .spinner > .rect {
  147. background-color: #EDAA35;
  148. height: 100%;
  149. border-radius: 13upx;
  150. width: 13upx;
  151. display: inline-block;
  152. -webkit-animation: stretchdelay 1.2s infinite ease-in-out;
  153. animation: stretchdelay 1.2s infinite ease-in-out;
  154. & + .rect {
  155. margin-left: 15upx;
  156. }
  157. }
  158. .spinner .rect2 {
  159. -webkit-animation-delay: -1.1s;
  160. animation-delay: -1.1s;
  161. }
  162. .spinner .rect3 {
  163. -webkit-animation-delay: -1.0s;
  164. animation-delay: -1.0s;
  165. }
  166. .spinner .rect4 {
  167. -webkit-animation-delay: -0.9s;
  168. animation-delay: -0.9s;
  169. }
  170. .spinner .rect5 {
  171. -webkit-animation-delay: -0.8s;
  172. animation-delay: -0.8s;
  173. }
  174. @-webkit-keyframes stretchdelay {
  175. 0%, 40%, 100% { -webkit-transform: scaleY(0.4) }
  176. 20% { -webkit-transform: scaleY(1.0) }
  177. }
  178. @keyframes stretchdelay {
  179. 0%, 40%, 100% {
  180. transform: scaleY(0.4);
  181. -webkit-transform: scaleY(0.4);
  182. } 20% {
  183. transform: scaleY(1.0);
  184. -webkit-transform: scaleY(1.0);
  185. }
  186. }
  187. </style>