This commit is contained in:
chc
2022-12-27 14:17:00 +08:00
parent 6baf1534c1
commit 5e38247ce8
15 changed files with 6858 additions and 27 deletions

View File

@@ -85,6 +85,62 @@ export function unixToDate(unix, format) {
return _format;
}
/**
* 人性化显示时间
*
* @param {Object} datetime
*/
export function beautifyTime(datetime = "") {
if (datetime == null || datetime == undefined || !datetime) {
return "";
}
datetime = datetime.replace(/-/g, "/");
let time = new Date();
let outTime = new Date(datetime);
if (/^[1-9]\d*$/.test(datetime)) {
outTime = new Date(parseInt(datetime) * 1000);
}
if (time.getTime() < outTime.getTime()) {
return parseTime(outTime, "{y}/{m}/{d}");
}
if (time.getFullYear() != outTime.getFullYear()) {
return parseTime(outTime, "{y}/{m}/{d}");
}
if (time.getMonth() != outTime.getMonth()) {
return parseTime(outTime, "{m}/{d}");
}
if (time.getDate() != outTime.getDate()) {
let day = outTime.getDate() - time.getDate();
if (day == -1) {
return parseTime(outTime, "昨天 {h}:{i}");
}
if (day == -2) {
return parseTime(outTime, "前天 {h}:{i}");
}
return parseTime(outTime, "{m}-{d}");
}
if (time.getHours() != outTime.getHours()) {
return parseTime(outTime, "{h}:{i}");
}
let minutes = outTime.getMinutes() - time.getMinutes();
if (minutes == 0) {
return "刚刚";
}
minutes = Math.abs(minutes);
return `${minutes}分钟前`;
}
/**
* 13888888888 -> 138****8888
* @param mobile
@@ -98,6 +154,111 @@ export function secrecyMobile(mobile) {
return mobile.replace(/(\d{3})(\d{4})(\d{4})/, "$1****$3");
}
/**
* 人性化时间显示
*
* @param {Object} datetime
*/
export function formatTime(datetime) {
if (datetime == null) return "";
datetime = datetime.replace(/-/g, "/");
let time = new Date();
let outTime = new Date(datetime);
if (/^[1-9]\d*$/.test(datetime)) {
outTime = new Date(parseInt(datetime) * 1000);
}
if (
time.getTime() < outTime.getTime() ||
time.getFullYear() != outTime.getFullYear()
) {
return parseTime(outTime, "{y}-{m}-{d} {h}:{i}");
}
if (time.getMonth() != outTime.getMonth()) {
return parseTime(outTime, "{m}-{d} {h}:{i}");
}
if (time.getDate() != outTime.getDate()) {
let day = outTime.getDate() - time.getDate();
if (day == -1) {
return parseTime(outTime, "昨天 {h}:{i}");
}
if (day == -2) {
return parseTime(outTime, "前天 {h}:{i}");
}
return parseTime(outTime, "{m}-{d} {h}:{i}");
}
if (time.getHours() != outTime.getHours()) {
return parseTime(outTime, "{h}:{i}");
}
let minutes = outTime.getMinutes() - time.getMinutes();
if (minutes == 0) {
return "刚刚";
}
minutes = Math.abs(minutes);
return `${minutes}分钟前`;
}
/**
* 时间格式化方法
*
* @param {(Object|string|number)} time
* @param {String} cFormat
* @returns {String | null}
*/
export function parseTime(time, cFormat) {
if (arguments.length === 0) {
return null;
}
let date;
const format = cFormat || "{y}-{m}-{d} {h}:{i}:{s}";
if (typeof time === "object") {
date = time;
} else {
if (typeof time === "string" && /^[0-9]+$/.test(time)) {
time = parseInt(time);
}
if (typeof time === "number" && time.toString().length === 10) {
time = time * 1000;
console.log("时间判断为number");
}
date = new Date(time.replace(/-/g, "/"));
}
const formatObj = {
y: date.getFullYear(),
m: date.getMonth() + 1,
d: date.getDate(),
h: date.getHours(),
i: date.getMinutes(),
s: date.getSeconds(),
a: date.getDay(),
};
const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
const value = formatObj[key];
// Note: getDay() returns 0 on Sunday
if (key === "a") {
return ["日", "一", "二", "三", "四", "五", "六"][value];
}
return value.toString().padStart(2, "0");
});
return time_str;
}
/**
* 清除逗号
*

86
utils/socket_service.js Normal file
View File

@@ -0,0 +1,86 @@
import store from '@/store/index'
import config from '@/config/config.js'
import storage from './storage';
export default class SocketService {
/**
* 单例
*/
static instance = null;
static get Instance() {
if (!this.instance) {
this.instance = new SocketService();
}
return this.instance;
}
// 和服务端连接的socket对象
ws = null;
// 存储回调函数
callBackMapping = {};
// 标识是否连接成功
connected = false;
// 记录重试的次数
sendRetryCount = 0;
// 重新连接尝试的次数
connectRetryCount = 0;
// 定义连接服务器的方法
connect() {
// 连接服务器
if (!window.WebSocket) {
return console.log("您的浏览器不支持WebSocket");
}
this.ws = new WebSocket(config.BASE_WS_URL+'/'+storage.getAccessToken());
// 连接成功的事件
this.ws.onopen = () => {
console.log("连接服务端成功");
this.connected = true;
// 重置重新连接的次数
this.connectRetryCount = 0;
};
// 1.连接服务端失败
// 2.当连接成功之后, 服务器关闭的情况(连接失败重连)
this.ws.onclose = () => {
console.log("连接服务端失败");
this.connected = false;
this.connectRetryCount++;
setTimeout(() => {
this.connect();
}, 500 * this.connectRetryCount);
};
// 得到服务端发送过来的数据
this.ws.onmessage = (msg) => {
// console.log(msg.data)
this.registerCallBack(msg.data);
};
}
// 回调函数的注册
registerCallBack(callBack) {
// console.log("回调函数的注册", callBack);
this.callBackMapping = callBack;
}
// 取消某一个回调函数
unRegisterCallBack(callBack) {
console.log("取消某一个回调函数", callBack);
this.callBackMapping = null;
}
// 发送数据的方法
send(data) {
// 判断此时此刻有没有连接成功
if (this.connected) {
this.sendRetryCount = 0;
this.ws.send(data);
} else {
this.sendRetryCount++;
setTimeout(() => {
this.send(data);
}, this.sendRetryCount * 500);
}
}
}

View File

@@ -52,6 +52,12 @@ export default {
getUserInfo() {
return uni.getStorageSync(USER_INFO);
},
setTalkToUser(val){
uni.setStorageSync("TALK_TO_USER", val);
},
getTalkToUser(){
return uni.getStorageSync("TALK_TO_USER");
},
// 写入uuid
setUuid(val) {
uni.setStorageSync(UUID, val);