|
|
@@ -1,16 +1,20 @@
|
|
|
-const MIN_VOICE_TIME = 1, MAX_VOICE_TIME = 60, START_TIME_DOWN = 54, status = {
|
|
|
- START: 1,
|
|
|
- SUCCESS: 2,
|
|
|
- CANCEL: 3,
|
|
|
- SHORT: 4,
|
|
|
- FAIL: 5,
|
|
|
- UNAUTH: 6
|
|
|
-}, EVENT = {
|
|
|
- EXTRA_CLICK: 'extraClickEvent',
|
|
|
- EXTRA_ITEM_CLICK: 'extraItemClickEvent',
|
|
|
- VOICE_RECORD: 'voiceRecordEvent',
|
|
|
- SEND_MESSAGE: 'sendMessageEvent'
|
|
|
-};
|
|
|
+const MIN_VOICE_TIME = 1,
|
|
|
+ MAX_VOICE_TIME = 60,
|
|
|
+ START_TIME_DOWN = 54,
|
|
|
+ status = {
|
|
|
+ START: 1,
|
|
|
+ SUCCESS: 2,
|
|
|
+ CANCEL: 3,
|
|
|
+ SHORT: 4,
|
|
|
+ FAIL: 5,
|
|
|
+ UNAUTH: 6
|
|
|
+ },
|
|
|
+ EVENT = {
|
|
|
+ EXTRA_CLICK: 'extraClickEvent',
|
|
|
+ EXTRA_ITEM_CLICK: 'extraItemClickEvent',
|
|
|
+ VOICE_RECORD: 'voiceRecordEvent',
|
|
|
+ SEND_MESSAGE: 'sendMessageEvent'
|
|
|
+ };
|
|
|
Component({
|
|
|
properties: {
|
|
|
minVoiceTime: {
|
|
|
@@ -46,7 +50,9 @@ Component({
|
|
|
timer: -1,
|
|
|
singleVoiceTimeCount: 0,
|
|
|
textMessage: '',
|
|
|
- voiceObj: {moveToCancel: false},
|
|
|
+ voiceObj: {
|
|
|
+ moveToCancel: false
|
|
|
+ },
|
|
|
extraObj: {
|
|
|
chatInputShowExtra: false,
|
|
|
chatInputExtraArr: []
|
|
|
@@ -67,7 +73,9 @@ Component({
|
|
|
},
|
|
|
methods: {
|
|
|
getRecordStatus() {
|
|
|
- return {...status};
|
|
|
+ return {
|
|
|
+ ...status
|
|
|
+ };
|
|
|
},
|
|
|
closeExtraView() {
|
|
|
this.setData({
|
|
|
@@ -79,7 +87,9 @@ Component({
|
|
|
this.setData({
|
|
|
'extraObj.chatInputShowExtra': isShow
|
|
|
}, () => {
|
|
|
- this.triggerEvent(EVENT.EXTRA_CLICK, {isShow}, {});
|
|
|
+ this.triggerEvent(EVENT.EXTRA_CLICK, {
|
|
|
+ isShow
|
|
|
+ }, {});
|
|
|
});
|
|
|
},
|
|
|
_change$input$way$event() {
|
|
|
@@ -88,23 +98,37 @@ Component({
|
|
|
'extraObj.chatInputShowExtra': false
|
|
|
});
|
|
|
},
|
|
|
- _triggerVoiceRecordEvent({status, dataset}) {
|
|
|
- this.triggerEvent(EVENT.VOICE_RECORD, {recordStatus: status, ...dataset}, {});
|
|
|
+ _triggerVoiceRecordEvent({
|
|
|
+ status,
|
|
|
+ dataset
|
|
|
+ }) {
|
|
|
+ this.triggerEvent(EVENT.VOICE_RECORD, {
|
|
|
+ recordStatus: status,
|
|
|
+ ...dataset
|
|
|
+ }, {});
|
|
|
},
|
|
|
_long$click$voice$btn(e) {
|
|
|
- if ('send$voice$btn' === e.currentTarget.id) {//长按时需要打开录音功能,开始录音
|
|
|
+ if ('send$voice$btn' === e.currentTarget.id) { //长按时需要打开录音功能,开始录音
|
|
|
this._checkRecordAuth(() => {
|
|
|
- const {maxVoiceTime, singleVoiceTimeCount} = this.data;
|
|
|
- this.setData({//调出取消弹窗
|
|
|
+ const {
|
|
|
+ maxVoiceTime,
|
|
|
+ singleVoiceTimeCount
|
|
|
+ } = this.data;
|
|
|
+ this.setData({ //调出取消弹窗
|
|
|
'voiceObj.showCancelSendVoicePart': true,
|
|
|
'voiceObj.timeDownNum': maxVoiceTime - singleVoiceTimeCount,
|
|
|
'voiceObj.status': 'start',
|
|
|
'voiceObj.startStatus': 1,
|
|
|
'voiceObj.moveToCancel': false
|
|
|
}, () => {
|
|
|
- this._triggerVoiceRecordEvent({status: status.START});
|
|
|
+ this._triggerVoiceRecordEvent({
|
|
|
+ status: status.START
|
|
|
+ });
|
|
|
+ });
|
|
|
+ this.recorderManager.start({
|
|
|
+ duration: 60000,
|
|
|
+ format: this.data.format
|
|
|
});
|
|
|
- this.recorderManager.start({duration: 60000, format: this.data.format});
|
|
|
}, (res) => {
|
|
|
//录音失败
|
|
|
console.error('录音拒绝授权');
|
|
|
@@ -114,7 +138,9 @@ Component({
|
|
|
'voiceObj.status': 'end',
|
|
|
'voiceObj.showCancelSendVoicePart': false
|
|
|
});
|
|
|
- this._triggerVoiceRecordEvent({status: status.UNAUTH});
|
|
|
+ this._triggerVoiceRecordEvent({
|
|
|
+ status: status.UNAUTH
|
|
|
+ });
|
|
|
|
|
|
wx.showModal({
|
|
|
title: '您未授权语音功能',
|
|
|
@@ -145,7 +171,10 @@ Component({
|
|
|
_dealVoiceLongClickEventWithHighVersion() {
|
|
|
this.recorderManager.onStart(() => {
|
|
|
this.data.singleVoiceTimeCount = 0;
|
|
|
- const {_startTimeDown, maxVoiceTime} = this.data;
|
|
|
+ const {
|
|
|
+ _startTimeDown,
|
|
|
+ maxVoiceTime
|
|
|
+ } = this.data;
|
|
|
//设置定时器计时60秒
|
|
|
this.data.timer = setInterval(() => {
|
|
|
const voiceTimeCount = ++this.data.singleVoiceTimeCount;
|
|
|
@@ -167,7 +196,12 @@ Component({
|
|
|
},
|
|
|
_send$voice$move$event(e) {
|
|
|
if ('send$voice$btn' === e.currentTarget.id) {
|
|
|
- const {windowHeight, voiceObj, tabBarHeight, cancelLineYPosition} = this.data,
|
|
|
+ const {
|
|
|
+ windowHeight,
|
|
|
+ voiceObj,
|
|
|
+ tabBarHeight,
|
|
|
+ cancelLineYPosition
|
|
|
+ } = this.data,
|
|
|
y = windowHeight + tabBarHeight - e.touches[0].clientY;
|
|
|
if (y > cancelLineYPosition) {
|
|
|
if (!voiceObj.moveToCancel) {
|
|
|
@@ -176,7 +210,7 @@ Component({
|
|
|
});
|
|
|
}
|
|
|
} else {
|
|
|
- if (voiceObj.moveToCancel) {//如果移出了该区域
|
|
|
+ if (voiceObj.moveToCancel) { //如果移出了该区域
|
|
|
this.setData({
|
|
|
'voiceObj.moveToCancel': false
|
|
|
})
|
|
|
@@ -187,13 +221,17 @@ Component({
|
|
|
},
|
|
|
_send$voice$move$end$event(e) {
|
|
|
if ('send$voice$btn' === e.currentTarget.id) {
|
|
|
- const {singleVoiceTimeCount, minVoiceTime, timer} = this.data;
|
|
|
- if (singleVoiceTimeCount < minVoiceTime) {//语音时间太短
|
|
|
+ const {
|
|
|
+ singleVoiceTimeCount,
|
|
|
+ minVoiceTime,
|
|
|
+ timer
|
|
|
+ } = this.data;
|
|
|
+ if (singleVoiceTimeCount < minVoiceTime) { //语音时间太短
|
|
|
this.setData({
|
|
|
'voiceObj.status': 'short'
|
|
|
});
|
|
|
this._delayDismissCancelView();
|
|
|
- } else {//语音时间正常
|
|
|
+ } else { //语音时间正常
|
|
|
this.setData({
|
|
|
'voiceObj.showCancelSendVoicePart': false,
|
|
|
'voiceObj.status': 'end'
|
|
|
@@ -204,7 +242,10 @@ Component({
|
|
|
}
|
|
|
},
|
|
|
_initVoiceData() {
|
|
|
- const {windowWidth, windowHeight} = this.data, width = windowWidth / 2.6;
|
|
|
+ const {
|
|
|
+ windowWidth,
|
|
|
+ windowHeight
|
|
|
+ } = this.data, width = windowWidth / 2.6;
|
|
|
this.setData({
|
|
|
'inputStatus': 'text',
|
|
|
'windowHeight': windowHeight,
|
|
|
@@ -241,12 +282,16 @@ Component({
|
|
|
})
|
|
|
},
|
|
|
_chatInput$send$text$message(e) {
|
|
|
+
|
|
|
this.setData({
|
|
|
textMessage: ''
|
|
|
}, () => {
|
|
|
- this.triggerEvent(EVENT.SEND_MESSAGE, {value: e.detail.value});
|
|
|
+ this.triggerEvent(EVENT.SEND_MESSAGE, {
|
|
|
+ value: e.detail.value
|
|
|
+ });
|
|
|
this.data.inputValueEventTemp = '';
|
|
|
});
|
|
|
+
|
|
|
},
|
|
|
_chatInput$bind$blur$event() {
|
|
|
setTimeout(() => {
|
|
|
@@ -260,43 +305,74 @@ Component({
|
|
|
});
|
|
|
},
|
|
|
_chatInput$send$text$message02() {
|
|
|
+
|
|
|
this.setData({
|
|
|
textMessage: '',
|
|
|
'inputType': 'none'
|
|
|
}, () => {
|
|
|
+
|
|
|
if (!!this.data.inputValueEventTemp) {
|
|
|
- this.triggerEvent(EVENT.SEND_MESSAGE, {value: this.data.inputValueEventTemp});
|
|
|
- this.data.inputValueEventTemp = '';
|
|
|
+
|
|
|
+ this.triggerEvent(EVENT.SEND_MESSAGE, {
|
|
|
+ value: this.data.inputValueEventTemp
|
|
|
+ });
|
|
|
+ this.setData({
|
|
|
+ inputValueEventTemp: '',
|
|
|
+ textMessage: '',
|
|
|
+ })
|
|
|
+ // this.data.inputValueEventTemp = '';
|
|
|
}
|
|
|
});
|
|
|
+
|
|
|
},
|
|
|
_chatInput$getValue$event(e) {
|
|
|
- const {detail: {value: textMessage}} = e;
|
|
|
+
|
|
|
+ const {
|
|
|
+ detail: {
|
|
|
+ value: textMessage
|
|
|
+ }
|
|
|
+ } = e;
|
|
|
this.data.inputValueEventTemp = textMessage;
|
|
|
this.setData({
|
|
|
textMessage
|
|
|
})
|
|
|
},
|
|
|
_chatInput$extra$item$click$event(e) {
|
|
|
- const {currentTarget: {dataset}} = e;
|
|
|
- this.triggerEvent(EVENT.EXTRA_ITEM_CLICK, {...dataset}, {});
|
|
|
+ const {
|
|
|
+ currentTarget: {
|
|
|
+ dataset
|
|
|
+ }
|
|
|
+ } = e;
|
|
|
+ this.triggerEvent(EVENT.EXTRA_ITEM_CLICK, {
|
|
|
+ ...dataset
|
|
|
+ }, {});
|
|
|
},
|
|
|
|
|
|
_setVoiceListener() {
|
|
|
this.recorderManager.onStop((res) => {
|
|
|
console.log(res, this.data.voiceObj.status);
|
|
|
- if (this.data.voiceObj.status === 'short') {//录音时间太短或者移动到了取消录音区域, 则取消录音
|
|
|
- this._triggerVoiceRecordEvent({status: status.SHORT});
|
|
|
+ if (this.data.voiceObj.status === 'short') { //录音时间太短或者移动到了取消录音区域, 则取消录音
|
|
|
+ this._triggerVoiceRecordEvent({
|
|
|
+ status: status.SHORT
|
|
|
+ });
|
|
|
return;
|
|
|
} else if (this.data.voiceObj.moveToCancel) {
|
|
|
- this._triggerVoiceRecordEvent({status: status.CANCEL});
|
|
|
+ this._triggerVoiceRecordEvent({
|
|
|
+ status: status.CANCEL
|
|
|
+ });
|
|
|
return;
|
|
|
}
|
|
|
console.log('录音成功');
|
|
|
- this._triggerVoiceRecordEvent({status: status.SUCCESS, dataset: res});
|
|
|
+ this._triggerVoiceRecordEvent({
|
|
|
+ status: status.SUCCESS,
|
|
|
+ dataset: res
|
|
|
+ });
|
|
|
});
|
|
|
this.recorderManager.onError((res) => {
|
|
|
- this._triggerVoiceRecordEvent({status: status.FAIL, dataset: res});
|
|
|
+ this._triggerVoiceRecordEvent({
|
|
|
+ status: status.FAIL,
|
|
|
+ dataset: res
|
|
|
+ });
|
|
|
});
|
|
|
},
|
|
|
|
|
|
@@ -309,7 +385,8 @@ Component({
|
|
|
success: (res) => {
|
|
|
// 用户已经同意小程序使用录音功能,后续调用 wx.startRecord 接口不会弹窗询问
|
|
|
console.log('同意', res);
|
|
|
- }, fail: res => {
|
|
|
+ },
|
|
|
+ fail: res => {
|
|
|
console.log('拒绝', res);
|
|
|
cbError && cbError();
|
|
|
}
|
|
|
@@ -324,7 +401,10 @@ Component({
|
|
|
lifetimes: {
|
|
|
created() {
|
|
|
this.recorderManager = wx.getRecorderManager();
|
|
|
- const {windowHeight, windowWidth} = wx.getSystemInfoSync();
|
|
|
+ const {
|
|
|
+ windowHeight,
|
|
|
+ windowWidth
|
|
|
+ } = wx.getSystemInfoSync();
|
|
|
if (!windowHeight || !windowWidth) {
|
|
|
console.error('没有获取到手机的屏幕尺寸:windowWidth', windowWidth, 'windowHeight', windowHeight);
|
|
|
return;
|
|
|
@@ -352,4 +432,4 @@ Component({
|
|
|
// setTextMessageListener: setTextMessageListener,
|
|
|
// setExtraButtonClickListener: setExtraButtonClickListener,
|
|
|
// VRStatus: status
|
|
|
-// };
|
|
|
+// };
|