|
@@ -17,18 +17,15 @@ export default withLayout((props) => {
|
17
|
17
|
const { router, person } = props;
|
18
|
18
|
const { friend } = router.params
|
19
|
19
|
|
|
20
|
+ const [submitting, setSubmitting] = useState(false)
|
20
|
21
|
const [text, setText] = useState()
|
21
|
|
- // const [image, setText] = useState()
|
22
|
|
- const typeRef = useRef(im.MESSAGETYPE.TEXT)
|
23
|
|
- const messageRef = useRef()
|
24
|
|
-
|
25
|
22
|
const [receiver, setReceiver] = useState({})
|
26
|
23
|
const [PageList, setPageList] = useState([])
|
27
|
24
|
const [scrollTop, scroll] = useScrollTop('#chat-scroll')
|
28
|
25
|
|
29
|
|
- const submit = (e, msg, type) => {
|
30
|
|
- const message = msg || messageRef.current
|
31
|
|
- const messageType = type || typeRef.current
|
|
26
|
+ const sendMessage = (msg, type = im.MESSAGETYPE.TEXT) => {
|
|
27
|
+ const message = msg
|
|
28
|
+ const messageType = type
|
32
|
29
|
|
33
|
30
|
// 用来回调写入消息列表
|
34
|
31
|
const newMessage = {
|
|
@@ -41,6 +38,7 @@ export default withLayout((props) => {
|
41
|
38
|
}
|
42
|
39
|
|
43
|
40
|
// 发送消息
|
|
41
|
+ setSubmitting(true)
|
44
|
42
|
im.sendMessage(message, messageType).then(() => {
|
45
|
43
|
// 发送成功
|
46
|
44
|
setText()
|
|
@@ -48,7 +46,10 @@ export default withLayout((props) => {
|
48
|
46
|
setPageList(PageList.concat(newMessage))
|
49
|
47
|
// 滚动到底部
|
50
|
48
|
scroll()
|
|
49
|
+ //
|
|
50
|
+ setSubmitting(false)
|
51
|
51
|
}).catch(() => {
|
|
52
|
+ setSubmitting(false)
|
52
|
53
|
Taro.showToast({
|
53
|
54
|
title: '发送失败, 请重试',
|
54
|
55
|
icon: 'none',
|
|
@@ -56,12 +57,21 @@ export default withLayout((props) => {
|
56
|
57
|
})
|
57
|
58
|
}
|
58
|
59
|
|
59
|
|
- const handleMessage = (msg, type = im.MESSAGETYPE.TEXT) => {
|
60
|
|
- typeRef.current = type
|
61
|
|
- messageRef.current = msg
|
62
|
|
- if (type === im.MESSAGETYPE.TEXT) {
|
63
|
|
- setText(msg)
|
64
|
|
- }
|
|
60
|
+ const submitText = () => {
|
|
61
|
+ sendMessage(text)
|
|
62
|
+ }
|
|
63
|
+
|
|
64
|
+ const submitImage = () => {
|
|
65
|
+ Taro.chooseImage({
|
|
66
|
+ count: 1,
|
|
67
|
+ sizeType: ['original', 'compressed'],
|
|
68
|
+ sourceType: ['album', 'camera'],
|
|
69
|
+ success: res => {
|
|
70
|
+ uploadFiles(res.tempFilePaths).then(data => {
|
|
71
|
+ sendMessage(data[0], im.MESSAGETYPE.IMAGE)
|
|
72
|
+ })
|
|
73
|
+ }
|
|
74
|
+ })
|
65
|
75
|
}
|
66
|
76
|
|
67
|
77
|
const getChatHistory = (params) => {
|
|
@@ -94,8 +104,10 @@ export default withLayout((props) => {
|
94
|
104
|
})
|
95
|
105
|
}
|
96
|
106
|
|
|
107
|
+ // 留电话
|
|
108
|
+ // 实际上是发送一条文本记录
|
97
|
109
|
const leavePhone = () => {
|
98
|
|
- submit(undefined, person.phone, im.MESSAGETYPE.TEXT)
|
|
110
|
+ sendMessage(person.phone)
|
99
|
111
|
}
|
100
|
112
|
|
101
|
113
|
useEffect(() => {
|
|
@@ -186,10 +198,10 @@ export default withLayout((props) => {
|
186
|
198
|
</view>
|
187
|
199
|
<view className='SendContent flex-h'>
|
188
|
200
|
<view className='flex-item'>
|
189
|
|
- <Input placeholder='发送消息' value={text} onInput={(e) => handleMessage(e.detail.value)} />
|
|
201
|
+ <Input placeholder='发送消息' value={text} onInput={(e) => setText(e.detail.value)} />
|
190
|
202
|
</view>
|
191
|
|
- <text className='iconfont icon-tianjia'></text>
|
192
|
|
- <text className='Send' onClick={submit}>发送</text>
|
|
203
|
+ <text className='iconfont icon-tianjia' onClick={submitImage}></text>
|
|
204
|
+ <button loading={submitting} className='Send' onClick={submitText}>发送</button>
|
193
|
205
|
</view>
|
194
|
206
|
</view>
|
195
|
207
|
)
|