Skip to content

Commit

Permalink
消息时间格式化
Browse files Browse the repository at this point in the history
  • Loading branch information
moohng committed Sep 13, 2017
1 parent ed4548c commit b0e8027
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 81 deletions.
4 changes: 2 additions & 2 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ export default {
height: 100vh;
// 字体
font-family: "Microsoft YaHei", Helvetica, Arial, sans-serif;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
background: #000;
// background: #000;
// 防止某些浏览器 切换界面时出现滚动条
overflow: hidden;
Expand Down
51 changes: 37 additions & 14 deletions src/components/message/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,52 @@
<div class="message">
<search :autoFixed="false"></search>
<group :gutter="0">
<cell v-for="session in sessionList" :title="session.title"
@click.native.stop="$router.push({name: 'chat'})">
<img slot="icon" src="" alt="" class="cell-hd__icon">
<span slot="inline-desc" class="cell-desc">这段文字很长很长很长很长很长很长很长很长很长很长很长很长</span>
<span slot="default" class="cell-time">14:20</span>
<cell v-for="session in sessionList"
:title="session.name"
@click.native.stop="$router.push({name: 'chat'})">
<img slot="icon" src="" class="cell-hd__icon">
<span slot="inline-desc" class="cell-desc">{{ session | lastMessage | messageText }}</span>
<span slot="default" class="cell-time">{{ session | lastMessage | messageTime }}</span>
</cell>
</group>
</div>
</template>

<script>
import { Search, Group, Cell } from 'vux'
import { mapState } from 'vuex'
import { format } from '../../utils'
export default {
name: 'message',
computed: mapState(['sessionList']),
filters: {
lastMessage (session) {
const len = session.messages.length
return session.messages[len - 1]
},
messageText: message => message.content.text,
messageTime (message) {
const date = new Date(message.date)
const messageDay = format(date, 'dd')
const nowDay = format(new Date(), 'dd')
let time
switch (nowDay - messageDay) {
case 0:
time: format(date, 'hh:mm')
break
case 1:
time: '昨天'
break
case 2:
time: '前天'
break
default:
time: format(date, 'MM月dd日')
}
return time
}
},
components: {
Group,
Search,
Expand All @@ -33,18 +61,13 @@ export default {
.message {
.cell-hd__icon {
width: 48px;
height: 48px;
width: 48px; height: 48px;
border: 1px solid #000;
}
.cell-desc {
@include textHidden
}
.cell-desc { @include textHidden }
.cell-time {
position: absolute;
right: 15px;
top: 15px;
right: 15px; top: 15px;
font-size: 12px;
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/plugins/WRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ const ax = axios.create({
// window.ax = ax

const request = options => {
return new Promise(async (resolve, reject) => {
return new Promise(async (resolve) => {
const loading = options.loading
loading && Vue.$loading(typeof loading === 'string' ? loading : null)
try {
const loading = options.loading
loading && Vue.$loading(typeof loading === 'string' ? loading : '')
const res = await ax.request(options)
loading && Vue.$hide()
const data = res.data
Expand All @@ -33,13 +33,13 @@ const request = options => {
name: 'login',
query: { mode: 'dismiss' }
})
reject('未登录')
Vue.$toast(data.message || data)
} else {
resolve(data)
}
} catch (err) {
loading && Vue.$hide()
reject(err)
Vue.$toast(err)
}
})
}
Expand Down
12 changes: 7 additions & 5 deletions src/plugins/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ const plugin = {
// 注册
Vue.use(LoadingPlugin)
Vue.use(ToastPlugin)

// toast
Vue.prototype.$toast = Vue.$toast = options => {
if (options && options.text) {
Vue.$vux.toast.show(options)
} else {
Vue.$vux.toast.text(options, 'bottom')
if (!options.text) {
options = {
type: 'text',
text: options,
position: 'bottom'
}
}
Vue.$vux.toast.show(options)
}
// loading
Vue.prototype.$loading = Vue.$loading = options => {
Expand Down
16 changes: 4 additions & 12 deletions src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,10 @@ router.beforeEach(async (to, from, next) => {
return next()
}
if (!from.name || from.path === '/') {
// 检查是否登录
try {
// 检查登陆态
await Vue.$check()
// 连接socket
await Vue.$connect()
next()
} catch (err) {
Vue.$toast(err)
router.replace({ name: 'login', query: { mode: 'dismiss' } })
}
} else {
// 检查登陆态
await Vue.$check()
// 连接socket
await Vue.$connect()
next()
}
})
Expand Down
18 changes: 18 additions & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export function format(date, fmt) {
const obj = {
'y{1,4}': date.getFullYear(),
'M+': date.getMonth() + 1,
'd+': date.getDate(),
'h+': date.getHours(),
'm+': date.getMinutes(),
's+': date.getSeconds()
}
for (const exp in obj) {
if (new RegExp(`(${exp})`).test(fmt)) {
const res = obj[exp].toString()
const $1 = RegExp.$1
fmt = fmt.replace($1, `0000${res}`.substr(res.length + 4 - $1.length))
}
}
return fmt
}
59 changes: 29 additions & 30 deletions src/vuex/state/index.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,44 @@

// 会话列表
let sessionList = [
{
title: '聊天室',
messages: [
{
from: '小三',
to: '聊天室',
content: {
text: '现在方便吗?'
},
time: '12:00'
},
{
from: '老王',
to: '聊天室',
content: {
text: '你不要来烦我你不要来烦我你不要来烦我你不要来烦我你不要来烦我你不要来烦我'
},
time: '12:04'
}
]
}
{
id: '12345',
name: '聊天室',
members: ['moohng', 'qqqqqq'],
messages: [
{
from: { username: 'moohng' },
content: {
text: '现在方便吗?'
},
date: 8786785656
},
{
from: { username: 'qqqqqq' },
content: {
text: '你不要来烦我你不要来烦我你不要来烦我你不要来烦我你不要来烦我你不要来烦我'
},
date: 232132131
}
]
}
]
// 好友列表
let friendList = [
'moohng',
'b',
'bbbbbb'
'moohng',
'b',
'bbbbbb'
]


let state = {
user: {},
// 聊天记录
sessionList,
user: {},
// 聊天记录
sessionList,

friendList,

newFriendList: [],
friendList,

newFriendList: [],
}

export default state
18 changes: 9 additions & 9 deletions src/vuex/state/message.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"from": {
"username": "moohng"
},
"to": {
"username": "1"
},
"info": {
"text": "Hey! Nice a day!"
}
"from": {
"username": "moohng"
},
"to": {
"username": "1"
},
"info": {
"text": "Hey! Nice a day!"
}
}
8 changes: 4 additions & 4 deletions src/vuex/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import getters from './getters'
Vue.use(Vuex)

export default new Vuex.Store({
state,
getters,
actions,
mutations
state,
getters,
actions,
mutations
})

0 comments on commit b0e8027

Please sign in to comment.