1. 时间处理
在安卓上使用2021-5-1 12:10:54这类字符串转时间
在字符串后添加.replace(/-/g,’/’)就可以解决了
这里是处理分解;今天、昨天、之前的
//用computed是可以在循环中进行运算
computed: {
timestampFormat() {
return function(time) {
//可以直接截取,然后补零,比当前方法更有效率
let newTime = new Date(formatDate(new Date(), 'yyyy-MM-dd')).getTime();
let nowTime = new Date(time.replace(/-/g, '/')).getTime();
let cutTime = (nowTime - newTime) / 3600 / 1000 + 8;
if (typeof(time) != 'undefined') {
console.log("11111111")
if (-24 > cutTime) {
return time.substr(0, 10)
} else if (0 > cutTime&&cutTime >= -24) {
console.log(time.substr(11, 5))
return "昨天 " + time.substr(11, 5)
} else if (cutTime >= 0) {
return time.substr(11, 5)
}
}
return "";
}
},
},
时间处理,可使用formatDate(new date(),“yyyy-MM-dd hh:mm:ss”)
export function formatDate(date, fmt) {
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
}
let o = {
'M+': date.getMonth() + 1,
'd+': date.getDate(),
'h+': date.getHours(),
'm+': date.getMinutes(),
's+': date.getSeconds()
};
for (let k in o) {
if (new RegExp(`(${k})`).test(fmt)) {
let str = o[k] + '';
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
}
}
return fmt;
}
function padLeftZero(str) {
return ('00' + str).substr(str.length);
}
2. 键盘弹出控制高度
在聊天界面,如果弹出键盘,则最上方的消息会被顶上去而看不见,这时候进行监听键盘高度,并将键盘的高度转换成padding-top,这样就可以实现消息不被顶上去
<template>
<view class="input-box-flex-grow">
<scroll-view class="content-box" :scroll-y="true" :scroll-top="scrollTop" :style="{'padding-top':inputBottom }">
<view class="message" v-for="(item, index) in messageList" :key="index" :id="`msg-${item.hasBeenSentId}`">
<view class="message-item " :class="item.isItMe ? 'right' : 'left'">
<image class="img" :src="item.fromUserHeadImg" mode="" @tap="linkToBusinessCard(item.fromUserId)"></image>
<view class="content">{{ item.content }}</view>
</view>
</view>
</scroll-view>
<input
type="text"
class="content"
id="input"
v-model="formData.content"
@focus="focusTextarea"
@blur="blurTextarea"
/>
</view>
</template>
<script>
export default {
data() {
return {
scrollTop: 1000,
inputBottom:'',
}
}
methods: {
focusTextarea(e) {
this.inputBottom = e.detail.height +"px";
console.log( e.detail.height)
this.scrollTop += 1; //滚到底部
},
blurTextarea(e) {
this.inputBottom = "0px" ;
this.scrollTop += 1; //滚到底部
},
}
}
</script>