张延森 3 years ago
parent
commit
4d2b083e7b

+ 1
- 1
config/dev.js View File

5
   defineConstants: {
5
   defineConstants: {
6
     // HOST: '"https://xlk.njyz.tech"',
6
     // HOST: '"https://xlk.njyz.tech"',
7
     HOST: '"http://127.0.0.1:8081"',
7
     HOST: '"http://127.0.0.1:8081"',
8
-    WSS_HOST: '"wss://xlk.njyz.tech"',
8
+    WSS_HOST: '"ws://127.0.0.1:8081"',
9
     OSS_PATH: '"https://xlk-assets.oss-accelerate.aliyuncs.com/"',
9
     OSS_PATH: '"https://xlk-assets.oss-accelerate.aliyuncs.com/"',
10
     OSS_FAST_PATH: '"https://xlk-assets.oss-accelerate.aliyuncs.com/"',
10
     OSS_FAST_PATH: '"https://xlk-assets.oss-accelerate.aliyuncs.com/"',
11
     ICON_FONT: '"https://yz-websit.oss-cn-hangzhou.aliyuncs.com/xlk/css/iconfont.ttf"',
11
     ICON_FONT: '"https://yz-websit.oss-cn-hangzhou.aliyuncs.com/xlk/css/iconfont.ttf"',

+ 1
- 1
src/components/FixedConsultant/index.jsx View File

17
 
17
 
18
   const handleChat = () => {
18
   const handleChat = () => {
19
     Taro.navigateTo({
19
     Taro.navigateTo({
20
-      url: `/pages/chat/chatDetail/index?targetPerson=${consultantId}}`
20
+      url: `/pages/chat/chatDetail/index?friend=${consultantId}`
21
     })
21
     })
22
   }
22
   }
23
   
23
   

+ 30
- 0
src/pages/chat/chatDetail/Card.jsx View File

1
+import React from 'react'
2
+import Taro from '@tarojs/taro'
3
+import './index.scss'
4
+
5
+export default (props) => {
6
+  const { person, onLeavePhone } = props
7
+
8
+  const callPhone = () => {
9
+    if (person.tel || person.phone) {
10
+      Taro.makePhoneCall({
11
+        phoneNumber: person.tel || person.phone
12
+      })
13
+    }
14
+  }
15
+
16
+  return (
17
+    <view className='SystemModel'>
18
+      <text className='Msg'>{`您好,我是${person.name || person.nickname},有什么可以帮您吗?您可以在这里跟我实时沟通哦~`}</text>
19
+      <text className='Tips'>您还可以通过以下方式快速联系我:</text>
20
+      <view onClick={callPhone}>
21
+        <text className='iconfont icon-dianhua'></text>
22
+        <text>拨打我的电话</text>
23
+      </view>
24
+      <view onClick={onLeavePhone}>
25
+        <text className='iconfont icon-shouji'></text>
26
+        <text>留下您的电话</text>
27
+      </view>
28
+    </view>
29
+  )
30
+}

+ 156
- 57
src/pages/chat/chatDetail/index.jsx View File

1
-import { useState } from 'react'
1
+import { useEffect, useRef, useState } from 'react'
2
+import Taro from '@tarojs/taro'
2
 import { ScrollView, Input, Image } from '@tarojs/components'
3
 import { ScrollView, Input, Image } from '@tarojs/components'
4
+import withLayout from '@/layout'
5
+import { API_QUERY_USERINFO_BYID } from '@/constants/api'
6
+import { ROLE_CODE } from '@/constants/user'
7
+import { uploadFiles, fetch } from '@/utils/request'
8
+import { queryChatHistory, setMessageReaded } from '@/services/chat'
9
+import im from '@/utils/im'
10
+import { getDateForHumans } from '@/utils/chatDate'
3
 import '@/assets/css/iconfont.css'
11
 import '@/assets/css/iconfont.css'
12
+import Card from './Card'
13
+import useScrollTop from './useScrollTop'
4
 import './index.scss'
14
 import './index.scss'
5
 
15
 
6
-export default function MyCollectForActivity () {
16
+export default withLayout((props) => {
17
+  const { router, person } = props;
18
+  const { friend } = router.params
7
 
19
 
8
-  const [PageList, setPageList] = useState([{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}])
20
+  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({})
26
+  const [PageList, setPageList] = useState([])
27
+  const [scrollTop, scroll] = useScrollTop('#chat-scroll')
28
+
29
+  const submit = (e, msg, type) => {
30
+    const message = msg || messageRef.current
31
+    const messageType = type || typeRef.current
32
+
33
+    // 用来回调写入消息列表
34
+    const newMessage = {
35
+      chatId: Math.random().toString(36).substring(2),
36
+      message,
37
+      messageType,
38
+      receivePerson: friend,
39
+      sendPerson: person.personId,
40
+      createDate: new Date(),
41
+    }
42
+
43
+    // 发送消息
44
+    im.sendMessage(message, messageType).then(() => {
45
+      // 发送成功
46
+      setText()
47
+      // 立刻回显刚刚发送内容
48
+      setPageList(PageList.concat(newMessage))
49
+      // 滚动到底部
50
+      scroll()
51
+    }).catch(() => {
52
+      Taro.showToast({
53
+        title: '发送失败, 请重试',
54
+        icon: 'none',
55
+      })
56
+    })
57
+  }
58
+
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
+    }
65
+  }
66
+
67
+  const getChatHistory = (params) => {
68
+    Taro.showLoading()
69
+    queryChatHistory({
70
+      chatWith: friend,
71
+      pageSize: 20,
72
+      ...params || {},
73
+    }).then((res) => {
74
+      const { records } = res;
75
+      Taro.hideLoading()
76
+
77
+      const lst = (records||[]).reverse()
78
+
79
+      // 如果聊天对象是有身份的
80
+      if (receiver.personType !== ROLE_CODE.DRIFT && receiver.personType !== ROLE_CODE.CUSTOMER) {
81
+        const mockMessage = {
82
+          chatId: Math.random().toString(36).substring(2),
83
+          message: '',
84
+          messageType: im.MESSAGETYPE.CARD,
85
+          receivePerson: person.personId,
86
+          sendPerson: friend,
87
+          createDate: new Date(),
88
+        }
89
+        lst.push(mockMessage)
90
+      }
91
+
92
+      setPageList(lst)
93
+      scroll()
94
+    })
95
+  }
96
+
97
+  const leavePhone = () => {
98
+    submit(undefined, person.phone, im.MESSAGETYPE.TEXT)
99
+  }
100
+
101
+  useEffect(() => {
102
+    if (friend) {
103
+      im.bindReceiver(friend)
104
+      fetch({ url: `${API_QUERY_USERINFO_BYID}/${friend}`, spin: true }).then((res) => {
105
+        setReceiver(res)
106
+        getChatHistory({ pageNumber: 1})
107
+      })
108
+    } else {
109
+      Taro.showToast({
110
+        title: '无聊天对象,请退出重试',
111
+        icon: 'none',
112
+      })
113
+    }
114
+  // eslint-disable-next-line react-hooks/exhaustive-deps
115
+  }, [friend])
9
 
116
 
10
   return (
117
   return (
11
     <view className='chatDetail flex-v'>
118
     <view className='chatDetail flex-v'>
12
       <view className='flex-item'>
119
       <view className='flex-item'>
13
         <view>
120
         <view>
14
-          <ScrollView scroll-y>
121
+          <ScrollView  id='chat-scroll' scrollY enhanced scrollTop={scrollTop}>
15
             <view className='PageContent'>
122
             <view className='PageContent'>
16
               {
123
               {
17
-                PageList.map((item, index) => (
18
-                  <view key={`ChatItem-${index}`} className='ChatItem'>
124
+                PageList.map((item) => (
125
+                  <view key={item.chatId} className='ChatItem'>
19
 
126
 
20
                     <view className='Time'>
127
                     <view className='Time'>
21
-                      <text>今天 17:50</text>
128
+                      <text>{getDateForHumans(item.createDate, true)}</text>
22
                     </view>
129
                     </view>
23
 
130
 
24
                     <view className='flex-h'>
131
                     <view className='flex-h'>
25
-
26
-                      {/* 对方头像 */}
27
                       {
132
                       {
28
-                        index % 2 === 0 &&
29
-                        <view className='Icon'>
30
-                          <Image mode='scaleToFill' src={null}></Image>
31
-                        </view>
133
+                        item.sendPerson === friend ? (
134
+                          <>
135
+                            <view className='Icon'>
136
+                              <Image mode='scaleToFill' src={receiver.avatarurl}></Image>
137
+                            </view>
138
+
139
+                            <view className='flex-item'>
140
+                              { /* 如果是文本 */
141
+                                item.messageType === im.MESSAGETYPE.TEXT && (
142
+                                  <view className='Message Left'>
143
+                                    <text>{item.message}</text>
144
+                                  </view>
145
+                                )
146
+                              }
147
+                              { /* 如果是卡片 */
148
+                                item.messageType === im.MESSAGETYPE.CARD && (
149
+                                  <Card person={receiver} onLeavePhone={leavePhone} />
150
+                                )
151
+                              }
152
+                            </view>
153
+                          </>
154
+                        ) : (
155
+                          <>
156
+                            <view className='flex-item'>
157
+                              { /* 如果是文本 */
158
+                                item.messageType === im.MESSAGETYPE.TEXT && (
159
+                                  <view className='Message Right'>
160
+                                    <text>{item.message}</text>
161
+                                  </view>
162
+                                )
163
+                              }
164
+                            </view>
165
+                            <view className='Icon'>
166
+                              <Image mode='scaleToFill' src={person.avatarurl}></Image>
167
+                            </view>
168
+                          </>
169
+                        )
32
                       }
170
                       }
33
 
171
 
34
-                      <view className='flex-item'>
35
 
172
 
36
                         {/* 系统模板消息(对方消息) */}
173
                         {/* 系统模板消息(对方消息) */}
37
-                        {
174
+                        {/* {
38
                           index === 0 &&
175
                           index === 0 &&
39
-                          <view className='SystemModel'>
40
-                            <text className='Msg'>您好,我是陆毅,有什么可以帮您吗?您可以在这里跟我实时沟通哦~</text>
41
-                            <text className='Tips'>您还可以通过以下方式快速联系我:</text>
42
-                            <view>
43
-                              <text className='iconfont icon-dianhua'></text>
44
-                              <text>拨打我的电话</text>
45
-                            </view>
46
-                            <view>
47
-                              <text className='iconfont icon-shouji'></text>
48
-                              <text>留下您的电话</text>
49
-                            </view>
50
-                          </view>
51
-                        }
52
-
53
-                        {/* 对方消息(文本) */}
54
-                        {
55
-                          index % 2 === 0 && !!index &&
56
-                          <view className='Message Left'>
57
-                            <text>知道了</text>
58
-                          </view>
59
-                        }
60
-
61
-                        {/* 我方消息(文本) */}
62
-                        {
63
-                          index % 2 === 1 &&
64
-                          <view className='Message Right'>
65
-                            <text>知道了知道了知道了知道了知道了知道了知道了知道了知道了知道了知道了知道了知道了知道了知道了知道了知道了</text>
66
-                          </view>
67
-                        }
68
-
69
-                      </view>
70
-                      
71
-                      {/* 我方头像 */}
72
-                      {
73
-                        index % 2 === 1 &&
74
-                        <view className='Icon'>
75
-                          <Image mode='scaleToFill' src={null}></Image>
76
-                        </view>
77
-                      }
176
+                        } */}
78
 
177
 
79
                     </view>
178
                     </view>
80
 
179
 
87
       </view>
186
       </view>
88
       <view className='SendContent flex-h'>
187
       <view className='SendContent flex-h'>
89
         <view className='flex-item'>
188
         <view className='flex-item'>
90
-          <Input placeholder='发送消息' />
189
+          <Input placeholder='发送消息' value={text} onInput={(e) => handleMessage(e.detail.value)} />
91
         </view>
190
         </view>
92
         <text className='iconfont icon-tianjia'></text>
191
         <text className='iconfont icon-tianjia'></text>
93
-        <text className='Send'>发送</text>
192
+        <text className='Send' onClick={submit}>发送</text>
94
       </view>
193
       </view>
95
     </view>
194
     </view>
96
   )
195
   )
97
-}
196
+})

+ 21
- 0
src/pages/chat/chatDetail/useScrollTop.js View File

1
+
2
+import { useEffect, useState, useRef } from 'react';
3
+import Taro from '@tarojs/taro'
4
+
5
+export default function useScrollTop() {
6
+  // 设置一个初始高度
7
+  const [scrollTop, setScrollTop] = useState(1000)
8
+
9
+  const scroll = () => {
10
+    // Taro.createSelectorQuery()
11
+    //     .select(selector)
12
+    //     .boundingClientRect((rect) => {
13
+    //       console.log('-------rect------->', rect)
14
+    //   }).exec();
15
+    Taro.nextTick(() => {
16
+      setScrollTop(scrollTop + 500)
17
+    })
18
+  }
19
+
20
+  return [scrollTop, scroll]
21
+}

+ 37
- 9
src/pages/chat/index.jsx View File

1
 import { useState, useEffect } from 'react'
1
 import { useState, useEffect } from 'react'
2
+import Taro from '@tarojs/taro'
2
 import { ScrollView, Image } from '@tarojs/components'
3
 import { ScrollView, Image } from '@tarojs/components'
3
 import withLayout from '@/layout'
4
 import withLayout from '@/layout'
5
+import { queryChatFriends } from '@/services/chat'
6
+import { formatDate } from '@/utils/chatDate'
4
 import './index.scss'
7
 import './index.scss'
5
 
8
 
6
 const defaultRuleImage = 'https://yz-websit.oss-cn-hangzhou.aliyuncs.com/xlk/index-icon18.jpg'
9
 const defaultRuleImage = 'https://yz-websit.oss-cn-hangzhou.aliyuncs.com/xlk/index-icon18.jpg'
7
 
10
 
8
-export default withLayout(() => {
11
+export default withLayout((props) => {
9
 
12
 
10
   // const [PageProps] = useState(props)
13
   // const [PageProps] = useState(props)
11
   const [IsPull, setPull] = useState(false)
14
   const [IsPull, setPull] = useState(false)
12
   const [PullTimer, setPullTimer] = useState(null)
15
   const [PullTimer, setPullTimer] = useState(null)
13
-  const [PageList, setPageList] = useState(['', '', '', '', '', '', '', '', '', ''])
16
+  const [PageList, setPageList] = useState([])
17
+
18
+  const getList = (params) => {
19
+    Taro.showLoading()
20
+    queryChatFriends({
21
+      pageSize: 20,
22
+      ...params || {},
23
+    }).then((res) => {
24
+      Taro.hideLoading()
25
+      const { records } =  res
26
+      setPageList(records || [])
27
+    }).catch((err) => {
28
+      console.error(err)
29
+      Taro.hideLoading()
30
+    })
31
+  }
32
+
33
+  const handleChat = (item) => {
34
+    Taro.navigateTo({
35
+      url: `/pages/chat/chatDetail/index?friend=${item.friendId}`
36
+    })
37
+  }
38
+
39
+  useEffect(() => {
40
+    getList({ pageNumber: 1 })
41
+  }, [])
14
 
42
 
15
   const PageRefresh = () => { // 页面下拉刷新回调
43
   const PageRefresh = () => { // 页面下拉刷新回调
16
     setPull(true)
44
     setPull(true)
28
   return (
56
   return (
29
     <view className='Page Chat'>
57
     <view className='Page Chat'>
30
 
58
 
31
-      <ScrollView scroll-y={true} refresher-enabled={true} refresher-triggered={IsPull} onrefresherrefresh={PageRefresh} refresher-background='#fff'>
59
+      <ScrollView scrollY refresherEnabled refresherTriggered={IsPull} onRefresherrefresh={PageRefresh} refresherBackground='#fff'>
32
         <view className='PageContent'>
60
         <view className='PageContent'>
33
           {
61
           {
34
             PageList.map((item, index) => (
62
             PageList.map((item, index) => (
35
-              <view className='ListItem flex-h' key={`ListItem-${index}`}>
63
+              <view className='ListItem flex-h' key={item.friendId} onClick={() => handleChat(item)}>
36
                 <view className='Icon'>
64
                 <view className='Icon'>
37
                   <view>
65
                   <view>
38
-                    <Image mode='aspectFill' className='centerLabel' src={item.img || defaultRuleImage} />
66
+                    <Image mode='aspectFill' className='centerLabel' src={item.avatar || defaultRuleImage} />
39
                   </view>
67
                   </view>
40
-                  <text className='Num'>99</text>
68
+                  <text className='Num'>{item.unReadNum}</text>
41
                 </view>
69
                 </view>
42
                 <view className='flex-item'>
70
                 <view className='flex-item'>
43
                   <view className='flex-h'>
71
                   <view className='flex-h'>
44
-                    <text className='flex-item'>张三</text>
45
-                    <text>刚刚</text>
72
+                    <text className='flex-item'>{item.name}</text>
73
+                    <text>{formatDate(item.createDate, 'yyyy/M/d')}</text>
46
                   </view>
74
                   </view>
47
                   <view className='Msg'>
75
                   <view className='Msg'>
48
-                    <text>户型很好,很满意</text>
76
+                    <text>{item.messageType === 'text' ? item.message : '[图片]'}</text>
49
                   </view>
77
                   </view>
50
                 </view>
78
                 </view>
51
               </view>
79
               </view>

+ 4
- 18
src/pages/index/activityDetail/index.jsx View File

18
   queryActivityDetail,
18
   queryActivityDetail,
19
 } from "@/services/activity";
19
 } from "@/services/activity";
20
 import { getImgURL } from "@/utils/image";
20
 import { getImgURL } from "@/utils/image";
21
-import getDateFormat from "@/utils/chatDate";
21
+import { formatDate } from "@/utils/chatDate";
22
 import useParams from "@/utils/hooks/useParams";
22
 import useParams from "@/utils/hooks/useParams";
23
 import useShare from "@/utils/hooks/useShare";
23
 import useShare from "@/utils/hooks/useShare";
24
 import useFavor from "@/utils/hooks/useFavor";
24
 import useFavor from "@/utils/hooks/useFavor";
213
         </view>
213
         </view>
214
         <text className='Time'>
214
         <text className='Time'>
215
           报名截止时间:
215
           报名截止时间:
216
-          {getDateFormat(
217
-            new Date(detail.enlistEnd).valueOf(),
218
-            true,
219
-            'yyyy/M/d'
220
-          )}
216
+          {formatDate(detail.enlistEnd, 'yyyy/M/d')}
221
         </text>
217
         </text>
222
         <text className='Name'>{detail.title}</text>
218
         <text className='Name'>{detail.title}</text>
223
         <view className='flex-h Address'>
219
         <view className='flex-h Address'>
230
           <text>时间:</text>
226
           <text>时间:</text>
231
           <view className='flex-item'>
227
           <view className='flex-item'>
232
             <text>
228
             <text>
233
-              {getDateFormat(
234
-                new Date(detail.startDate).valueOf(),
235
-                true,
236
-                'yyyy/M/d'
237
-              )}{' '}
238
-              -{' '}
239
-              {getDateFormat(
240
-                new Date(detail.endDate).valueOf(),
241
-                true,
242
-                'yyyy/M/d'
243
-              )}
229
+              {`${formatDate(detail.startDate, 'yyyy/M/d')} - ${formatDate(detail.endDate, 'yyyy/M/d')}`}
244
             </text>
230
             </text>
245
           </view>
231
           </view>
246
         </view>
232
         </view>
267
             </view>
253
             </view>
268
             <text className='Price'>价格待定</text>
254
             <text className='Price'>价格待定</text>
269
           </view>
255
           </view>
270
-          <text className='Time'>时间:{getDateFormat(new Date(detail.startDate).valueOf(), true, 'yyyy/M/d')} - {getDateFormat(new Date(detail.endDate).valueOf(), true, 'yyyy/M/d')}</text>
256
+          <text className='Time'>时间:{formatDate(detail.startDate, 'yyyy/M/d')} - {formatDate(detail.endDate, 'yyyy/M/d')}</text>
271
         </view>
257
         </view>
272
         <view className='Option'>
258
         <view className='Option'>
273
           <view>
259
           <view>

+ 1
- 1
src/pages/index/buildingDetail/components/DetailBottom/index.jsx View File

29
 
29
 
30
     if (consultant?.id) {
30
     if (consultant?.id) {
31
       Taro.navigateTo({
31
       Taro.navigateTo({
32
-        url: `/pages/chat/chatDetail/index?targetPerson=${consultant.id}}`
32
+        url: `/pages/chat/chatDetail/index?friend=${consultant.id}}`
33
       })
33
       })
34
     } else {
34
     } else {
35
       Taro.navigateTo({
35
       Taro.navigateTo({

+ 1
- 1
src/pages/index/buildingDetail/components/PropertyConsultant/index.jsx View File

27
 
27
 
28
   const handleChat = (item) => {
28
   const handleChat = (item) => {
29
     Taro.navigateTo({
29
     Taro.navigateTo({
30
-      url: `/pages/chat/chatDetail/index?targetPerson=${item.id}}`
30
+      url: `/pages/chat/chatDetail/index?friend=${item.id}`
31
     })
31
     })
32
   }
32
   }
33
   
33
   

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

15
 
15
 
16
   const handleChat = (item) => {
16
   const handleChat = (item) => {
17
     Taro.navigateTo({
17
     Taro.navigateTo({
18
-      url: `/pages/chat/chatDetail/index?targetPerson=${item.id}}`
18
+      url: `/pages/chat/chatDetail/index?friend=${item.id}`
19
     })
19
     })
20
   }
20
   }
21
 
21
 

+ 2
- 2
src/pages/index/components/ActivityListItem/index.jsx View File

3
 import { Image } from '@tarojs/components'
3
 import { Image } from '@tarojs/components'
4
 import Taro from '@tarojs/taro'
4
 import Taro from '@tarojs/taro'
5
 import { getImgURL } from '@/utils/image'
5
 import { getImgURL } from '@/utils/image'
6
-import getDateFormat from '@/utils/chatDate'
6
+import { formatDate } from '@/utils/chatDate'
7
 
7
 
8
 const activityStatusDict={
8
 const activityStatusDict={
9
   0:{
9
   0:{
38
           <text className={activityStatusDict[data.activityStatus]?.className}>{activityStatusDict[data.activityStatus]?.text}</text>
38
           <text className={activityStatusDict[data.activityStatus]?.className}>{activityStatusDict[data.activityStatus]?.text}</text>
39
         </view>
39
         </view>
40
         <view className='Time'>
40
         <view className='Time'>
41
-          <text>活动时间:{getDateFormat(new Date(data.startDate).valueOf(),true,'yyyy/M/d')} - {getDateFormat(new Date(data.endDate).valueOf(),true,'yyyy/M/d')}</text>
41
+          <text>活动时间:{formatDate(data.startDate, 'yyyy/M/d')} - {formatDate(data.endDate, 'yyyy/M/d')}</text>
42
         </view>
42
         </view>
43
       </view>
43
       </view>
44
     </view>
44
     </view>

+ 2
- 2
src/pages/index/components/NewsListItem/index.jsx View File

3
 import { Image } from '@tarojs/components'
3
 import { Image } from '@tarojs/components'
4
 import Taro from '@tarojs/taro'
4
 import Taro from '@tarojs/taro'
5
 import { getImgURL } from "@/utils/image";
5
 import { getImgURL } from "@/utils/image";
6
-import getDateFormat from "@/utils/chatDate";
6
+import { formatDate } from "@/utils/chatDate";
7
 
7
 
8
 export default function NewsListItem (props) {
8
 export default function NewsListItem (props) {
9
   const { data = {} } = props
9
   const { data = {} } = props
15
       </view>
15
       </view>
16
       <view className='flex-item'>
16
       <view className='flex-item'>
17
         <text>{data.newsName}</text>
17
         <text>{data.newsName}</text>
18
-        <text>于{getDateFormat(new Date(data.createDate).valueOf(), true,"yyyy-M-d")}发布</text>
18
+        <text>于{formatDate(data.createDate, "yyyy-M-d")}发布</text>
19
       </view>
19
       </view>
20
     </view>
20
     </view>
21
   )
21
   )

+ 2
- 7
src/pages/index/encyDetail/index.jsx View File

6
 import "@/assets/css/iconfont.css";
6
 import "@/assets/css/iconfont.css";
7
 import { Image, RichText } from "@tarojs/components";
7
 import { Image, RichText } from "@tarojs/components";
8
 import { queryPolicyDetail } from "@/services/policy";
8
 import { queryPolicyDetail } from "@/services/policy";
9
-import getDateFormat from "@/utils/chatDate";
9
+import { formatDate } from "@/utils/chatDate";
10
 
10
 
11
 export default withLayout((props) => {
11
 export default withLayout((props) => {
12
   const { router, shareContent, trackData, person, page } = props;
12
   const { router, shareContent, trackData, person, page } = props;
44
                   <view>
44
                   <view>
45
                   <text>阅读:{data.pvNum}</text>
45
                   <text>阅读:{data.pvNum}</text>
46
                     <text className="Time">
46
                     <text className="Time">
47
-                      {getDateFormat(
48
-                        new Date(data.createDate).valueOf(),
49
-                        true,
50
-                        "yyyy-M-d"
51
-                      )}
47
+                      {formatDate(data.createDate, "yyyy-M-d")}
52
                     </text>
48
                     </text>
53
-                   
54
                   </view>
49
                   </view>
55
                 </view>
50
                 </view>
56
               </view>
51
               </view>

+ 2
- 7
src/pages/index/newsDetail/index.jsx View File

11
   queryNewsDetail,
11
   queryNewsDetail,
12
   cancelFavorNews,
12
   cancelFavorNews,
13
 } from "@/services/news";
13
 } from "@/services/news";
14
-import getDateFormat from "@/utils/chatDate";
14
+import { formatDate } from "@/utils/chatDate";
15
 
15
 
16
 export default withLayout((props) => {
16
 export default withLayout((props) => {
17
   const { id } = props.router.params;
17
   const { id } = props.router.params;
74
                       <text>收藏{data.saveNum || 0}</text>
74
                       <text>收藏{data.saveNum || 0}</text>
75
                     </view>
75
                     </view>
76
                     <text className="Time">
76
                     <text className="Time">
77
-                      时间:
78
-                      {getDateFormat(
79
-                        new Date(data.createDate).valueOf(),
80
-                        true,
81
-                        "yyyy-M-d"
82
-                      )}{" "}
77
+                      {`时间:${formatDate(data.createDate, "yyyy-M-d")} `}
83
                     </text>
78
                     </text>
84
                     <text className="Share">分享好友</text>
79
                     <text className="Share">分享好友</text>
85
                   </view>
80
                   </view>

+ 1
- 3
src/pages/mine/components/UserDetailFollowRecord/index.jsx View File

2
 import { ScrollView } from '@tarojs/components'
2
 import { ScrollView } from '@tarojs/components'
3
 import { fetch } from '@/utils/request'
3
 import { fetch } from '@/utils/request'
4
 import { API_FOLLOW_LIST } from '@/constants/api'
4
 import { API_FOLLOW_LIST } from '@/constants/api'
5
-import getDateFormat from "@/utils/chatDate";
5
+import { formatDate } from "@/utils/chatDate";
6
 import './index.scss'
6
 import './index.scss'
7
 
7
 
8
-const formatDate = (dtStr, formatStr) => getDateFormat((new Date(dtStr)).valueOf(), true, formatStr)
9
-
10
 export default function UserDetailFollowRecord (props) {
8
 export default function UserDetailFollowRecord (props) {
11
   const { CustomerId = null, AddFollow = () => {}, AddFollowCounts = 0 } = props
9
   const { CustomerId = null, AddFollow = () => {}, AddFollowCounts = 0 } = props
12
   const [PageList, setPageList] = useState([])
10
   const [PageList, setPageList] = useState([])

+ 4
- 6
src/pages/video/videoDetail/index.jsx View File

5
 import "./index.scss";
5
 import "./index.scss";
6
 import { fetch } from "@/utils/request";
6
 import { fetch } from "@/utils/request";
7
 import { API_VIDEO_DETAIL } from "@/constants/api";
7
 import { API_VIDEO_DETAIL } from "@/constants/api";
8
-import getDateFormat from "@/utils/chatDate";
8
+import { formatDate } from "@/utils/chatDate";
9
 
9
 
10
 export default withLayout((props) => {
10
 export default withLayout((props) => {
11
   const { id } = props.router.params;
11
   const { id } = props.router.params;
49
           {/* 视频信息 */}
49
           {/* 视频信息 */}
50
           <view className="Info">
50
           <view className="Info">
51
             <text className="Name">{data.name}</text>
51
             <text className="Name">{data.name}</text>
52
-            <text className="Time">时间:{getDateFormat(
53
-            new Date(data.createdTime).valueOf(),
54
-            true,
55
-            'yyyy年M月d日'
56
-          )}</text>
52
+            <text className="Time">
53
+              {`时间:${formatDate(data.createdTime, 'yyyy年M月d日')}`}
54
+            </text>
57
           </view>
55
           </view>
58
 
56
 
59
           {/* 详情 */}
57
           {/* 详情 */}

+ 71
- 71
src/utils/chatDate.js View File

12
 */
12
 */
13
 
13
 
14
 //例子 getDateFormat(new Date().valueOf(),true,'yyyy/M/d')
14
 //例子 getDateFormat(new Date().valueOf(),true,'yyyy/M/d')
15
-var _formatDate = function (date, fmt) {
15
+export function formatDate(dt, fmt) {
16
+  const date = new Date(dt)
17
+
16
   var o = {
18
   var o = {
17
     "M+": date.getMonth() + 1, //月份
19
     "M+": date.getMonth() + 1, //月份
18
     "d+": date.getDate(), //日
20
     "d+": date.getDate(), //日
28
   return fmt;
30
   return fmt;
29
 };
31
 };
30
 
32
 
33
+
31
 /**
34
 /**
32
 * 仿照微信中的消息时间显示逻辑,将时间戳(单位:毫秒)转换为友好的显示格式.
35
 * 仿照微信中的消息时间显示逻辑,将时间戳(单位:毫秒)转换为友好的显示格式.
33
 *
36
 *
34
 * 1)7天之内的日期显示逻辑是:今天、昨天(-1d)、前天(-2d)、星期?(只显示总计7天之内的星期数,即<=-4d);
37
 * 1)7天之内的日期显示逻辑是:今天、昨天(-1d)、前天(-2d)、星期?(只显示总计7天之内的星期数,即<=-4d);
35
 * 2)7天之外(即>7天)的逻辑:直接显示完整日期时间。
38
 * 2)7天之外(即>7天)的逻辑:直接显示完整日期时间。
36
 *
39
 *
37
-* @param  {[long]} timestamp 时间戳(单位:毫秒),形如:1550789954260
40
+* @param  {[string|date]} dt 时间
38
 * @param {boolean} mustIncludeTime true表示输出的格式里一定会包含“时间:分钟”
41
 * @param {boolean} mustIncludeTime true表示输出的格式里一定会包含“时间:分钟”
39
 * ,否则不包含(参考微信,不包含时分的情况,用于首页“消息”中显示时)
42
 * ,否则不包含(参考微信,不包含时分的情况,用于首页“消息”中显示时)
40
 *
43
 *
41
 * @return {string} 输出格式形如:“刚刚”、“10:30”、“昨天 12:04”、“前天 20:51”、“星期二”、“2019/2/21 12:09”等形式
44
 * @return {string} 输出格式形如:“刚刚”、“10:30”、“昨天 12:04”、“前天 20:51”、“星期二”、“2019/2/21 12:09”等形式
42
 * @since 1.1
45
 * @since 1.1
43
 */
46
 */
44
-export default function getDateFormat(timestamp, mustIncludeTime,format='yyyy-M-d') {
47
+export function getDateForHumans(dt, mustIncludeTime, format='yyyy-M-d') {
45
 
48
 
46
   // 当前时间
49
   // 当前时间
47
-  // var currentDate = new Date();
50
+  var currentDate = new Date();
48
   // 目标判断时间
51
   // 目标判断时间
49
-  var srcDate = new Date(parseInt(timestamp));
52
+  var srcDate = new Date(dt);
50
 
53
 
51
-  // var currentYear = currentDate.getFullYear();
52
-  // var currentMonth = (currentDate.getMonth() + 1);
53
-  // var currentDateD = currentDate.getDate();
54
+  var currentYear = currentDate.getFullYear();
55
+  var currentMonth = (currentDate.getMonth() + 1);
56
+  var currentDateD = currentDate.getDate();
54
 
57
 
55
-  // var srcYear = srcDate.getFullYear();
56
-  // var srcMonth = (srcDate.getMonth() + 1);
57
-  // var srcDateD = srcDate.getDate();
58
+  var srcYear = srcDate.getFullYear();
59
+  var srcMonth = (srcDate.getMonth() + 1);
60
+  var srcDateD = srcDate.getDate();
58
 
61
 
59
   var ret = "";
62
   var ret = "";
60
 
63
 
61
   // 要额外显示的时间分钟
64
   // 要额外显示的时间分钟
62
-  var timeExtraStr = (mustIncludeTime ? " " + _formatDate(srcDate, "hh:mm") : "");
65
+  var timeExtraStr = (mustIncludeTime ? " " + formatDate(srcDate, "hh:mm") : "");
63
 
66
 
64
-  // // 当年
65
-  // if (currentYear == srcYear) {
66
-  //   var currentTimestamp = currentDate.getTime();
67
-  //   var srcTimestamp = timestamp;
68
-  //   // 相差时间(单位:毫秒)
69
-  //   var deltaTime = (currentTimestamp - srcTimestamp);
67
+  // 当年
68
+  if (currentYear == srcYear) {
69
+    // 相差时间(单位:毫秒)
70
+    var deltaTime = (currentDate - srcDate);
70
 
71
 
71
-  //   // 当天(月份和日期一致才是)
72
-  //   if (currentMonth == srcMonth && currentDateD == srcDateD) {
73
-  //     // 时间相差60秒以内
74
-  //     if (deltaTime < 60 * 1000)
75
-  //       ret = "刚刚";
76
-  //     // 否则当天其它时间段的,直接显示“时:分”的形式
77
-  //     else
78
-  //       ret = _formatDate(srcDate, "hh:mm");
79
-  //   }
80
-  //   // 当年 && 当天之外的时间(即昨天及以前的时间)
81
-  //   else {
82
-  //     // 昨天(以“现在”的时候为基准-1天)
83
-  //     var yesterdayDate = new Date();
84
-  //     yesterdayDate.setDate(yesterdayDate.getDate() - 1);
72
+    // 当天(月份和日期一致才是)
73
+    if (currentMonth == srcMonth && currentDateD == srcDateD) {
74
+      // 时间相差60秒以内
75
+      if (deltaTime < 60 * 1000)
76
+        ret = "刚刚";
77
+      // 否则当天其它时间段的,直接显示“时:分”的形式
78
+      else
79
+        ret = formatDate(srcDate, "hh:mm");
80
+    }
81
+    // 当年 && 当天之外的时间(即昨天及以前的时间)
82
+    else {
83
+      // 昨天(以“现在”的时候为基准-1天)
84
+      var yesterdayDate = new Date();
85
+      yesterdayDate.setDate(yesterdayDate.getDate() - 1);
85
 
86
 
86
-  //     // 前天(以“现在”的时候为基准-2天)
87
-  //     var beforeYesterdayDate = new Date();
88
-  //     beforeYesterdayDate.setDate(beforeYesterdayDate.getDate() - 2);
87
+      // 前天(以“现在”的时候为基准-2天)
88
+      var beforeYesterdayDate = new Date();
89
+      beforeYesterdayDate.setDate(beforeYesterdayDate.getDate() - 2);
89
 
90
 
90
-  //     // 用目标日期的“月”和“天”跟上方计算出来的“昨天”进行比较,是最为准确的(如果用时间戳差值
91
-  //     // 的形式,是不准确的,比如:现在时刻是2019年02月22日1:00、而srcDate是2019年02月21日23:00,
92
-  //     // 这两者间只相差2小时,直接用“deltaTime/(3600 * 1000)” > 24小时来判断是否昨天,就完全是扯蛋的逻辑了)
93
-  //     if (srcMonth == (yesterdayDate.getMonth() + 1) && srcDateD == yesterdayDate.getDate())
94
-  //       ret = "昨天" + timeExtraStr;// -1d
95
-  //     // “前天”判断逻辑同上
96
-  //     else if (srcMonth == (beforeYesterdayDate.getMonth() + 1) && srcDateD == beforeYesterdayDate.getDate())
97
-  //       ret = "前天" + timeExtraStr;// -2d
98
-  //     else {
99
-  //       // 跟当前时间相差的小时数
100
-  //       var deltaHour = (deltaTime / (3600 * 1000));
91
+      // 用目标日期的“月”和“天”跟上方计算出来的“昨天”进行比较,是最为准确的(如果用时间戳差值
92
+      // 的形式,是不准确的,比如:现在时刻是2019年02月22日1:00、而srcDate是2019年02月21日23:00,
93
+      // 这两者间只相差2小时,直接用“deltaTime/(3600 * 1000)” > 24小时来判断是否昨天,就完全是扯蛋的逻辑了)
94
+      if (srcMonth == (yesterdayDate.getMonth() + 1) && srcDateD == yesterdayDate.getDate())
95
+        ret = "昨天" + timeExtraStr;// -1d
96
+      // “前天”判断逻辑同上
97
+      else if (srcMonth == (beforeYesterdayDate.getMonth() + 1) && srcDateD == beforeYesterdayDate.getDate())
98
+        ret = "前天" + timeExtraStr;// -2d
99
+      else {
100
+        // 跟当前时间相差的小时数
101
+        var deltaHour = (deltaTime / (3600 * 1000));
101
 
102
 
102
-  //       // 如果小于或等 7*24小时就显示星期几
103
-  //       if (deltaHour <= 7 * 24) {
104
-  //         var weekday = new Array(7);
105
-  //         weekday[0] = "星期日";
106
-  //         weekday[1] = "星期一";
107
-  //         weekday[2] = "星期二";
108
-  //         weekday[3] = "星期三";
109
-  //         weekday[4] = "星期四";
110
-  //         weekday[5] = "星期五";
111
-  //         weekday[6] = "星期六";
103
+        // 如果小于或等 7*24小时就显示星期几
104
+        if (deltaHour <= 7 * 24) {
105
+          var weekday = new Array(7);
106
+          weekday[0] = "星期日";
107
+          weekday[1] = "星期一";
108
+          weekday[2] = "星期二";
109
+          weekday[3] = "星期三";
110
+          weekday[4] = "星期四";
111
+          weekday[5] = "星期五";
112
+          weekday[6] = "星期六";
112
 
113
 
113
-  //         // 取出当前是星期几
114
-  //         var weedayDesc = weekday[srcDate.getDay()];
115
-  //         ret = weedayDesc + timeExtraStr;
116
-  //       }
117
-  //       // 否则直接显示完整日期时间
118
-  //       else
119
-  //         ret = _formatDate(srcDate, format) + timeExtraStr;
120
-  //     }
121
-  //   }
122
-  // }
123
-  // // 往年
124
-  // else {
125
-  //   ret = _formatDate(srcDate, format) + timeExtraStr;
126
-  // }
127
-  ret = _formatDate(srcDate, format) + timeExtraStr;
114
+          // 取出当前是星期几
115
+          var weedayDesc = weekday[srcDate.getDay()];
116
+          ret = weedayDesc + timeExtraStr;
117
+        }
118
+        // 否则直接显示完整日期时间
119
+        else
120
+          ret = formatDate(srcDate, format) + timeExtraStr;
121
+      }
122
+    }
123
+  }
124
+  // 往年
125
+  else {
126
+    ret = formatDate(srcDate, format) + timeExtraStr;
127
+  }
128
   return ret;
128
   return ret;
129
 };
129
 };

+ 10
- 17
src/utils/im/index.js View File

13
   const listeners = initListeners()
13
   const listeners = initListeners()
14
 
14
 
15
   let sendPerson;
15
   let sendPerson;
16
+  let receivePerson;
16
   let timer;
17
   let timer;
17
 
18
 
18
   sdk.listen('onMessage', listeners.getListeners)
19
   sdk.listen('onMessage', listeners.getListeners)
21
     MESSAGETYPE,
22
     MESSAGETYPE,
22
     init,
23
     init,
23
     listen: listeners.append,
24
     listen: listeners.append,
25
+    bindReceiver,
24
     connect: sdk.connect,
26
     connect: sdk.connect,
25
-    send,
26
     sendMessage,
27
     sendMessage,
27
   }
28
   }
28
 
29
 
29
-  function bindPerson(person) {
30
-    sendPerson = person
30
+  function bindReceiver(receiver) {
31
+    receivePerson = receiver
31
   }
32
   }
32
 
33
 
33
-  function send(to) {
34
-    return function(type = MESSAGETYPE.TEXT) {
35
-      return function(message) {
36
-        return sendMessage(to, message, type)
37
-      }
38
-    }
39
-  }
40
-
41
-  function sendMessage(to, message, type = MESSAGETYPE.TEXT) {
42
-    return sdk.send(wrapData(to, message, type))
34
+  function sendMessage(message, type = MESSAGETYPE.TEXT) {
35
+    return sdk.send(wrapData(message, type))
43
   }
36
   }
44
 
37
 
45
-  function wrapData(to, data, messageType) {
38
+  function wrapData(message, messageType) {
46
     return JSON.stringify({
39
     return JSON.stringify({
47
       sendPerson,
40
       sendPerson,
48
-      receivePerson: to,
49
-      message: data,
41
+      receivePerson,
42
+      message,
50
       messageType,
43
       messageType,
51
       createDate: new Date().valueOf(),
44
       createDate: new Date().valueOf(),
52
     })
45
     })
56
     // eslint-disable-next-line no-undef
49
     // eslint-disable-next-line no-undef
57
     const url = `${WSS_HOST}/wx/chat/${personId}`  
50
     const url = `${WSS_HOST}/wx/chat/${personId}`  
58
 
51
 
59
-    bindPerson(personId)
52
+    sendPerson = personId
60
 
53
 
61
     if (timer) {
54
     if (timer) {
62
       clearInterval(timer)
55
       clearInterval(timer)