jinji.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <template>
  2. <view>
  3. <view class="text-white text-center" style="margin-top: 200rpx;">
  4. <view class="datt">严重危害生命财产安全 </view>
  5. <view class="datt margin-top-sm">求助专属通道</view>
  6. <view class="margin-top">平台将为您,保留录音证据,实时位置,通知平台联系人</view>
  7. </view>
  8. <view class="text-center" style="margin-top: 120rpx;" @touchstart="voiceBegin" @touchmove.stop.prevent="voiceIng"
  9. @touchend="voiceEnd" @touchcancel="voiceCancel">
  10. <image src="../../static/images/index/yuyin.png" style="width: 300rpx;height: 300rpx;"></image>
  11. </view>
  12. <!-- 录音UI效果 -->
  13. <view class="record" :class="recording?'':'hidden'">
  14. <view class="ing" :class="willStop?'hidden':''">
  15. <view class="icon luyin2"></view>
  16. </view>
  17. <view class="cancel" :class="willStop?'':'hidden'">
  18. <view class="icon chehui"></view>
  19. </view>
  20. <view class="tis" :class="willStop?'change':''">{{recordTis}}</view>
  21. </view>
  22. <view class="text-center" style="margin-top:200rpx;" @touchstart="voiceBegin" @touchmove.stop.prevent="voiceIng"
  23. @touchend="voiceEnd" @touchcancel="voiceCancel">
  24. <image src="../../static/images/index/sos.png" style="width: 200rpx;height: 200rpx;"></image>
  25. <!-- <view class="voice-mode" :class="[isVoice?'':'hidden',recording?'recording':'']"
  26. @touchstart="voiceBegin" @touchmove.stop.prevent="voiceIng" @touchend="voiceEnd"
  27. @touchcancel="voiceCancel">{{voiceTis}}</view> -->
  28. </view>
  29. </view>
  30. </template>
  31. <script>
  32. export default {
  33. data() {
  34. return {
  35. latitude: '',
  36. longitude: '',
  37. content: '',
  38. //录音相关参数
  39. // #ifndef H5
  40. //H5不能录音
  41. RECORDER: uni.getRecorderManager(),
  42. // #endif
  43. isVoice: false,
  44. voiceTis: '按住 说话',
  45. recordTis: "手指上滑 取消发送",
  46. recording: false,
  47. willStop: false,
  48. initPoint: {
  49. identifier: 0,
  50. Y: 0
  51. },
  52. recordTimer: null,
  53. recordLength: 0,
  54. //播放语音相关参数
  55. AUDIO: uni.createInnerAudioContext(),
  56. playMsgid: null,
  57. VoiceTimer: null,
  58. }
  59. },
  60. onLoad: function(option) {
  61. // #ifndef H5
  62. //录音开始事件
  63. this.RECORDER.onStart((e) => {
  64. this.recordBegin(e);
  65. })
  66. //录音结束事件
  67. this.RECORDER.onStop((e) => {
  68. this.recordEnd(e);
  69. })
  70. // #endif
  71. let that = this;
  72. uni.getLocation({
  73. type: 'wgs84',
  74. success: function(res) {
  75. console.log('当前位置的经度:' + res.longitude);
  76. console.log('当前位置的纬度:' + res.latitude);
  77. that.latitude = res.latitude
  78. that.longitude = res.longitude
  79. }
  80. });
  81. },
  82. onBackPress() {
  83. this.AUDIO.stop();
  84. },
  85. onHide() {
  86. this.AUDIO.stop();
  87. },
  88. methods: {
  89. // 切换语音/文字输入
  90. switchVoice() {
  91. this.isVoice = this.isVoice ? false : true;
  92. },
  93. setChatSave() {
  94. let userId = this.$queue.getData('userId');
  95. let userName = this.$queue.getData('userName');
  96. let data = {
  97. image: this.content,
  98. state: 9,
  99. typeId: this.longitude,
  100. typeName: this.latitude,
  101. userId: userId,
  102. userName: userName
  103. }
  104. this.$Request.postJson('/app/message/insertMessage',data).then(
  105. res => {
  106. if (res.code == 0) {
  107. this.$queue.showToast('上传成功!');
  108. } else {
  109. this.$queue.showToast(res.msg);
  110. }
  111. });
  112. },
  113. // 录音开始
  114. voiceBegin(e) {
  115. if (e.touches.length > 1) {
  116. return;
  117. }
  118. this.initPoint.Y = e.touches[0].clientY;
  119. this.initPoint.identifier = e.touches[0].identifier;
  120. this.RECORDER.start({
  121. format: "mp3"
  122. }); //录音开始,
  123. },
  124. //录音开始UI效果
  125. recordBegin(e) {
  126. this.recording = true;
  127. this.voiceTis = '松开 结束';
  128. this.recordLength = 0;
  129. this.recordTimer = setInterval(() => {
  130. this.recordLength++;
  131. }, 1000)
  132. },
  133. // 录音被打断
  134. voiceCancel() {
  135. this.recording = false;
  136. this.voiceTis = '按住 说话';
  137. this.recordTis = '手指上滑 取消发送'
  138. this.willStop = true; //不发送录音
  139. this.RECORDER.stop(); //录音结束
  140. },
  141. // 录音中(判断是否触发上滑取消发送)
  142. voiceIng(e) {
  143. if (!this.recording) {
  144. return;
  145. }
  146. let touche = e.touches[0];
  147. //上滑一个导航栏的高度触发上滑取消发送
  148. if (this.initPoint.Y - touche.clientY >= uni.upx2px(100)) {
  149. this.willStop = true;
  150. this.recordTis = '松开手指 取消发送'
  151. } else {
  152. this.willStop = false;
  153. this.recordTis = '手指上滑 取消发送'
  154. }
  155. },
  156. // 结束录音
  157. voiceEnd(e) {
  158. if (!this.recording) {
  159. return;
  160. }
  161. this.recording = false;
  162. this.voiceTis = '按住 说话';
  163. this.recordTis = '手指上滑 取消发送'
  164. this.RECORDER.stop(); //录音结束
  165. },
  166. //录音结束(回调文件)
  167. recordEnd(e) {
  168. clearInterval(this.recordTimer);
  169. if (!this.willStop) {
  170. this.$queue.showLoading('发送中...')
  171. console.log("e: " + JSON.stringify(e));
  172. let msg = {
  173. length: 0,
  174. url: e.tempFilePath
  175. }
  176. let min = parseInt(this.recordLength / 60);
  177. let sec = this.recordLength % 60;
  178. min = min < 10 ? '0' + min : min;
  179. sec = sec < 10 ? '0' + sec : sec;
  180. if (this.recordLength % 60 > 1) {
  181. msg.length = min + ':' + sec;
  182. console.log('msg.length___:' + msg.length)
  183. uni.uploadFile({ // 上传接口
  184. url: this.config("APIHOST1") + '/alioss/upload', //真实的接口地址
  185. filePath: e.tempFilePath,
  186. name: 'file',
  187. success: (uploadFileRes) => {
  188. uni.hideLoading();
  189. this.content = JSON.parse(uploadFileRes.data).data;
  190. console.log("语音:" + this.content)
  191. this.setChatSave();
  192. uni.hideLoading();
  193. }
  194. });
  195. } else {
  196. this.$queue.showToast('语音要大于一秒才可以发送!')
  197. }
  198. } else {
  199. console.log('取消发送录音');
  200. }
  201. this.willStop = false;
  202. },
  203. }
  204. }
  205. </script>
  206. <style lang="scss">
  207. @import "../my/css/style.scss";
  208. page {
  209. background: #3FCB46;
  210. }
  211. .datt {
  212. font-size: 42rpx;
  213. font-family: PingFang SC;
  214. font-weight: 800;
  215. }
  216. </style>