张延森 3 years ago
parent
commit
fa3513f255
2 changed files with 30 additions and 18 deletions
  1. 29
    17
      src/pages/chat/chatDetail/index.jsx
  2. 1
    1
      src/pages/chat/index.jsx

+ 29
- 17
src/pages/chat/chatDetail/index.jsx View File

17
   const { router, person } = props;
17
   const { router, person } = props;
18
   const { friend } = router.params
18
   const { friend } = router.params
19
 
19
 
20
+  const [submitting, setSubmitting] = useState(false)
20
   const [text, setText] = useState()
21
   const [text, setText] = useState()
21
-  // const [image, setText] = useState()
22
-  const typeRef = useRef(im.MESSAGETYPE.TEXT)
23
-  const messageRef = useRef()
24
-
25
   const [receiver, setReceiver] = useState({})
22
   const [receiver, setReceiver] = useState({})
26
   const [PageList, setPageList] = useState([])
23
   const [PageList, setPageList] = useState([])
27
   const [scrollTop, scroll] = useScrollTop('#chat-scroll')
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
     const newMessage = {
31
     const newMessage = {
41
     }
38
     }
42
 
39
 
43
     // 发送消息
40
     // 发送消息
41
+    setSubmitting(true)
44
     im.sendMessage(message, messageType).then(() => {
42
     im.sendMessage(message, messageType).then(() => {
45
       // 发送成功
43
       // 发送成功
46
       setText()
44
       setText()
48
       setPageList(PageList.concat(newMessage))
46
       setPageList(PageList.concat(newMessage))
49
       // 滚动到底部
47
       // 滚动到底部
50
       scroll()
48
       scroll()
49
+      //
50
+      setSubmitting(false)
51
     }).catch(() => {
51
     }).catch(() => {
52
+      setSubmitting(false)
52
       Taro.showToast({
53
       Taro.showToast({
53
         title: '发送失败, 请重试',
54
         title: '发送失败, 请重试',
54
         icon: 'none',
55
         icon: 'none',
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
   const getChatHistory = (params) => {
77
   const getChatHistory = (params) => {
94
     })
104
     })
95
   }
105
   }
96
 
106
 
107
+  // 留电话
108
+  // 实际上是发送一条文本记录
97
   const leavePhone = () => {
109
   const leavePhone = () => {
98
-    submit(undefined, person.phone, im.MESSAGETYPE.TEXT)
110
+    sendMessage(person.phone)
99
   }
111
   }
100
 
112
 
101
   useEffect(() => {
113
   useEffect(() => {
186
       </view>
198
       </view>
187
       <view className='SendContent flex-h'>
199
       <view className='SendContent flex-h'>
188
         <view className='flex-item'>
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
         </view>
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
       </view>
205
       </view>
194
     </view>
206
     </view>
195
   )
207
   )

+ 1
- 1
src/pages/chat/index.jsx View File

65
                   <view>
65
                   <view>
66
                     <Image mode='aspectFill' className='centerLabel' src={item.avatar || defaultRuleImage} />
66
                     <Image mode='aspectFill' className='centerLabel' src={item.avatar || defaultRuleImage} />
67
                   </view>
67
                   </view>
68
-                  <text className='Num'>{item.unReadNum}</text>
68
+                  {!!item.unReadNum && <text className='Num'>{item.unReadNum}</text>}
69
                 </view>
69
                 </view>
70
                 <view className='flex-item'>
70
                 <view className='flex-item'>
71
                   <view className='flex-h'>
71
                   <view className='flex-h'>