voiceBroadcast.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. <!-- 语音广播 -->
  2. <template>
  3. <view class="voiceBroadcast">
  4. <view class="null-box" @click="backPage()"></view>
  5. <!-- 语音广播-执行疏散 -->
  6. <view class="broadcast">
  7. <view class="broadcast_t">语音广播<label>选择喇叭位置</label></view>
  8. <!-- 按钮部分 -->
  9. <view class="trumpet-max-box">
  10. <view @click="trumpetClick(index)" class="trumpet-for-box"
  11. :class="item.type?'trumpet-color-a':'trumpet-color-b'" v-for="(item,index) in trumpetList"
  12. :key="index">
  13. <img src="@/images/basicsModules/icon_sskz_zc.png" v-if="!item.type">
  14. <img src="@/images/basicsModules/icon_sskz_xz.png" v-if="item.type">
  15. {{item.name}}
  16. </view>
  17. </view>
  18. <view class="broadcast_m">
  19. <view class="broadcast_m_t"
  20. :class="liveType?'broadcast_m_t_back_a':'broadcast_m_t_back_b'"
  21. @longpress.stop="recordButton"
  22. @touchmove.stop="cancelButton"
  23. @touchend.stop="sendButton"
  24. >
  25. {{liveType?'松开发送':'按住说话'}}
  26. </view>
  27. <view class="broadcast_m_b" v-if="!liveType">按住说话,录入广播内容</view>
  28. <view class="broadcast_m_b" v-if="liveType">松开发送,向上滑动取消发送</view>
  29. </view>
  30. </view>
  31. </view>
  32. </template>
  33. <script>
  34. import {} from '@/api/basicsModules/index.js'
  35. import { config } from '@/api/request/config.js'
  36. export default {
  37. name: "voiceBroadcast",
  38. props: {
  39. subjectData: {},
  40. },
  41. data() {
  42. return {
  43. baseUrl:config.base_url,
  44. //喇叭数据
  45. trumpetList: [],
  46. //广播相关
  47. liveType: false,
  48. sendLock: true, //发送锁,当为true时上锁,false时解锁发送
  49. recorderManager: wx.getRecorderManager(),
  50. isEvacuate: true, //疏散按钮控制,当为true时候执行疏散
  51. //滑动记录
  52. startPoint: {},
  53. }
  54. },
  55. created() {
  56. },
  57. mounted() {
  58. },
  59. methods: {
  60. // 返回按钮
  61. backPage(){
  62. this.$parent.buttonClick('broadcastClose','');
  63. },
  64. //获取喇叭列表
  65. async getDeviceListBySub() {
  66. let _this=this;
  67. let obj = {
  68. subId: _this.subId,
  69. floorId: _this.itemData.subAddrr.floorId,
  70. page: 1,
  71. pageSize: 100,
  72. };
  73. const {data} = await getDeviceListBySub(obj)
  74. if (data.code == 200) {
  75. for (let i = 0; i < data.data.length; i++) {
  76. data.data[i].type = false;
  77. }
  78. this.$set(this, 'trumpetList', data.data)
  79. }
  80. },
  81. //点击选择喇叭
  82. trumpetClick(index) {
  83. this.trumpetList[index].type = !this.trumpetList[index].type;
  84. },
  85. //录制
  86. recordButton(e) {
  87. console.log("按下")
  88. let self = this;
  89. let num = 0;
  90. for (let i = 0; i < self.trumpetList.length; i++) {
  91. if (self.trumpetList[i].type) {
  92. num++
  93. }
  94. }
  95. if (num == 0) {
  96. uni.showToast({
  97. title: '请选择喇叭',
  98. icon: "none",
  99. mask: true,
  100. duration: 2000
  101. });
  102. return
  103. }
  104. this.liveType = true;
  105. console.log('录制', e)
  106. this.startPoint = e.touches[0]; //记录长按时开始点信息,后面用于计算上划取消时手指滑动的距离。
  107. const options = {
  108. duration: 10000,
  109. sampleRate: 16000,
  110. numberOfChannels: 1,
  111. encodeBitRate: 48000,
  112. format: 'mp3',
  113. frameSize: 50
  114. }
  115. this.recorderManager.start(options); //开始录音
  116. this.recorderManager.onStart(() => {
  117. console.log('recorder start')
  118. })
  119. this.recorderManager.onError((res) => {
  120. console.log(res);
  121. })
  122. wx.showToast({
  123. title: "正在录音,上划取消发送",
  124. icon: "none",
  125. duration: 60000 //先定义个60秒,后面可以手动调用wx.hideToast()隐藏
  126. });
  127. this.sendLock = false; //长按时是不上锁的。
  128. },
  129. //取消
  130. cancelButton(e) {
  131. console.log("移动")
  132. let self = this;
  133. let num = 0;
  134. for (let i = 0; i < self.trumpetList.length; i++) {
  135. if (self.trumpetList[i].type) {
  136. num++
  137. }
  138. }
  139. if (num == 0) {
  140. return
  141. }
  142. this.liveType = false;
  143. console.log('取消', e)
  144. let moveLenght = e.touches[e.touches.length - 1].clientY - this.startPoint.clientY; //移动距离
  145. if (Math.abs(moveLenght) > 50) {
  146. wx.showToast({
  147. title: "松开手指,取消发送",
  148. icon: "none",
  149. duration: 60000
  150. });
  151. this.sendLock = true; //触发了上滑取消发送,上锁
  152. } else {
  153. wx.showToast({
  154. title: "正在录音,上划取消发送",
  155. icon: "none",
  156. duration: 60000
  157. });
  158. this.sendLock = false; //上划距离不足,依然可以发送,不上锁
  159. }
  160. },
  161. //发送
  162. sendButton(e) {
  163. console.log("松开")
  164. let self = this;
  165. let num = 0;
  166. for (let i = 0; i < self.trumpetList.length; i++) {
  167. if (self.trumpetList[i].type) {
  168. num++
  169. }
  170. }
  171. if (num == 0) {
  172. return
  173. }
  174. this.liveType = false;
  175. console.log('发送', e)
  176. wx.hideToast(); //结束录音、隐藏Toast提示框
  177. this.recorderManager.stop(); //结束录音
  178. this.recorderManager.onStop((res) => {
  179. if (!this.sendLock) {
  180. console.log('1', this.recorderManager)
  181. this.uploadImg(res.tempFilePath);
  182. }
  183. console.log('停止录音', res.tempFilePath)
  184. console.log("sendLock", this.sendLock);
  185. })
  186. },
  187. //上传MP3
  188. async uploadImg(tempFilePaths) {
  189. var self = this;
  190. uni.uploadFile({
  191. url: config.base_url + '/base/file/upload', //仅为示例,非真实的接口地址
  192. header: {
  193. 'Authorization': uni.getStorageSync('token')
  194. },
  195. filePath: tempFilePaths,
  196. name: 'file',
  197. formData: {
  198. 'user': 'test'
  199. },
  200. success: (uploadFileRes) => {
  201. let res = JSON.parse(uploadFileRes.data);
  202. if (res.code == 200) {
  203. console.log("上传成功", res)
  204. self.textParseUrlIps(config.base_url + '/' + res.data.url);
  205. } else {
  206. uni.showToast({
  207. title: res.msg,
  208. icon: "none",
  209. mask: true,
  210. duration: 2000
  211. });
  212. }
  213. },
  214. fail: err => {
  215. uni.hideLoading()
  216. },
  217. complete: () => {}
  218. });
  219. },
  220. //发送语音
  221. async textParseUrlIps(text) {
  222. let self = this;
  223. let newList = [];
  224. for (let i = 0; i < self.trumpetList.length; i++) {
  225. if (self.trumpetList[i].type) {
  226. let obj = {
  227. sn: self.trumpetList[i].deviceSn,
  228. port: self.trumpetList[i].port,
  229. deviceIp: self.trumpetList[i].deviceIp,
  230. type: "",
  231. name: "",
  232. speed: "",
  233. params: {
  234. tid: "",
  235. vol: self.trumpetList[i].deviceVol,
  236. urls: []
  237. }
  238. };
  239. newList.push(obj);
  240. }
  241. }
  242. const {
  243. data
  244. } = await textParseUrlIps(newList, text)
  245. if (data.code == 200) {
  246. uni.showToast({
  247. title: '发送成功',
  248. icon: "none",
  249. mask: true,
  250. duration: 2000
  251. });
  252. }
  253. },
  254. },
  255. }
  256. </script>
  257. <style lang="stylus" scoped>
  258. .voiceBroadcast{
  259. height: 100%;
  260. width: 100%;
  261. position: fixed;
  262. top: 0;
  263. display: flex;
  264. flex-direction: column;
  265. z-index: 10;
  266. background: rgba(0, 0, 0, 0.2);
  267. .null-box {
  268. flex: 1;
  269. }
  270. /* 语音广播-执行疏散 */
  271. .broadcast {
  272. width: 100%;
  273. background: #FFFFFF;
  274. border-top-left-radius: 20rpx;
  275. border-top-right-radius: 20rpx;
  276. padding:22rpx 30rpx 30rpx;
  277. box-sizing: border-box;
  278. margin-top: 20rpx;
  279. position: absolute;
  280. bottom: 0;
  281. .broadcast_t {
  282. font-size: 30rpx;
  283. font-family: PingFang SC;
  284. font-weight: 500;
  285. color: #333333;
  286. line-height: 30rpx;
  287. >label {
  288. font-size: 24rpx;
  289. font-family: PingFang SC;
  290. font-weight: 500;
  291. color: #999999;
  292. line-height: 30rpx;
  293. margin-left: 16rpx;
  294. }
  295. }
  296. .trumpet-max-box {
  297. display: flex;
  298. justify-content: flex-start;
  299. margin-top: 22rpx;
  300. flex-wrap: wrap;
  301. .trumpet-for-box {
  302. display: inline-block;
  303. width: auto;
  304. height: 60rpx;
  305. line-height: 60rpx;
  306. font-size: 24rpx;
  307. text-align: center;
  308. cursor: pointer;
  309. overflow: hidden;
  310. border: 1rpx solid #E0E0E0;
  311. border-radius: 10rpx;
  312. color: #E0E0E0;
  313. display: flex;
  314. justify-content: center;
  315. margin-right: 20rpx;
  316. margin-bottom: 10rpx;
  317. padding: 0 12rpx;
  318. box-sizing: border-box;
  319. >img {
  320. width: 36rpx;
  321. height: 34rpx;
  322. margin: 12rpx 20rpx 0 25rpx;
  323. }
  324. }
  325. .trumpet-color-a {
  326. border: 1px solid #0183FA;
  327. color: #0183FA;
  328. }
  329. .trumpet-color-b {
  330. border: 1px solid #CCCCCC;
  331. color: #999;
  332. }
  333. }
  334. .broadcast_m {
  335. width: 100%;
  336. .broadcast_m_t {
  337. width: 142rpx;
  338. height: 142rpx;
  339. margin: 30rpx 0 0 258rpx;
  340. position: relative;
  341. font-size: 24rpx;
  342. font-family: PingFang SC;
  343. font-weight: 500;
  344. line-height: 170rpx;
  345. text-align: center;
  346. >img {
  347. width: 142rpx;
  348. height: 142rpx;
  349. position: absolute;
  350. }
  351. >label {
  352. width: 100%;
  353. font-size: 24rpx;
  354. font-family: PingFang SC;
  355. font-weight: 500;
  356. color: #0183FA;
  357. line-height: 24rpx;
  358. display: inline-block;
  359. text-align: center;
  360. position: absolute;
  361. top: 76rpx;
  362. }
  363. /* 按下 */
  364. .press_color {
  365. color: #FFFFFF;
  366. }
  367. /* 松开 */
  368. .slip_color {
  369. color: #0183FA;
  370. }
  371. }
  372. .broadcast_m_t_back_a {
  373. background: url(@/images/basicsModules/icon_sskz_skfs.png);
  374. background-size: 100%;
  375. color: #FFFFFF;
  376. }
  377. .broadcast_m_t_back_b {
  378. background: url(@/images/basicsModules/icon_sskz_azsh.png);
  379. background-size: 100%;
  380. color: #0183FA;
  381. }
  382. .broadcast_m_b {
  383. font-size: 24rpx;
  384. font-family: PingFang SC;
  385. font-weight: 500;
  386. color: #999999;
  387. line-height: 24rpx;
  388. text-align: center;
  389. margin-top: 14rpx;
  390. }
  391. }
  392. /* 疏散按钮 */
  393. .evacuation-button-box {
  394. width: 650rpx;
  395. height: 100rpx;
  396. background: #0183FA;
  397. color: #fff;
  398. text-align center;
  399. line-height: 100rpx;
  400. font-size: 28rpx;
  401. margin: 88rpx auto 0;
  402. border-radius: 20rpx;
  403. }
  404. }
  405. }
  406. </style>