first commit
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
export default {
|
||||
|
||||
baseUrl:"http://127.0.0.1:8989",
|
||||
version: "/v1",
|
||||
|
||||
}
|
||||
91
l-im-app-imooc-master/l-im-app-imooc/common/lib/request.js
Normal file
91
l-im-app-imooc-master/l-im-app-imooc/common/lib/request.js
Normal file
@@ -0,0 +1,91 @@
|
||||
import $C from './config.js';
|
||||
import $store from '@/store/index.js';
|
||||
export default {
|
||||
// 全局配置
|
||||
common: {
|
||||
baseUrl: $C.baseUrl + $C.version,
|
||||
header: {
|
||||
'Content-Type': 'application/json;charset=UTF-8',
|
||||
},
|
||||
data: {},
|
||||
method: 'GET',
|
||||
dataType: 'json',
|
||||
token: true,
|
||||
},
|
||||
|
||||
// get请求
|
||||
get(url, data = {}, options = {}) {
|
||||
options.url = url
|
||||
options.data = data
|
||||
options.method = 'GET'
|
||||
return this.request(options)
|
||||
},
|
||||
// post请求
|
||||
post(url, data = {}, options = {}) {
|
||||
options.url = url
|
||||
options.data = data
|
||||
options.method = 'POST'
|
||||
return this.request(options)
|
||||
},
|
||||
// delete请求
|
||||
del(url, data = {}, options = {}) {
|
||||
options.url = url
|
||||
options.data = data
|
||||
options.method = 'DELETE'
|
||||
return this.request(options)
|
||||
},
|
||||
|
||||
// 请求 返回promise
|
||||
request(options = {}) {
|
||||
// 组织参数
|
||||
options.url = this.common.baseUrl + options.url
|
||||
options.header = options.header || this.common.header
|
||||
options.data = options.data || this.common.data
|
||||
options.method = options.method || this.common.method
|
||||
options.dataType = options.dataType || this.common.dataType
|
||||
options.token = options.token === false ? false : this.common.token
|
||||
|
||||
// 请求
|
||||
return new Promise((res, rej) => {
|
||||
// 请求中...
|
||||
uni.request({
|
||||
...options,
|
||||
success: (result) => {
|
||||
// 返回原始数据
|
||||
if (options.native) {
|
||||
return res(result)
|
||||
}
|
||||
// 服务端失败
|
||||
if (result.statusCode !== 200) {
|
||||
if (options.toast !== false) {
|
||||
uni.showToast({
|
||||
title: result.data.data || '服务器失败',
|
||||
icon: 'none'
|
||||
});
|
||||
}
|
||||
// token不合法,直接退出登录
|
||||
if (result.data.code === 401) {
|
||||
return uni.reLaunch({
|
||||
url: '/pages/common/login/login',
|
||||
})
|
||||
}
|
||||
return rej(result.data)
|
||||
}
|
||||
// 成功
|
||||
let data = result.data
|
||||
|
||||
res(data)
|
||||
},
|
||||
fail: (error) => {
|
||||
uni.showToast({
|
||||
title: '请求失败',
|
||||
icon: 'none'
|
||||
});
|
||||
return rej(error)
|
||||
}
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
65
l-im-app-imooc-master/l-im-app-imooc/common/lib/time.js
Normal file
65
l-im-app-imooc-master/l-im-app-imooc/common/lib/time.js
Normal file
@@ -0,0 +1,65 @@
|
||||
export default{
|
||||
|
||||
// 获取聊天时间(相差300s内的信息不会显示时间)
|
||||
getChatTime(v1,v2){
|
||||
v1=v1.toString().length<13 ? v1*1000 : v1;
|
||||
v2=v2.toString().length<13 ? v2*1000 : v2;
|
||||
if(((parseInt(v1)-parseInt(v2))/1000) > 300){
|
||||
return this.gettime(v1);
|
||||
}
|
||||
},
|
||||
// 人性化时间格式
|
||||
gettime(shorttime){
|
||||
shorttime=shorttime.toString().length<13 ? shorttime*1000 : shorttime;
|
||||
let now = (new Date()).getTime();
|
||||
let cha = (now-parseInt(shorttime))/1000;
|
||||
|
||||
if (cha < 43200) {
|
||||
// 当天
|
||||
return this.dateFormat(new Date(shorttime),"{A} {t}:{ii}");
|
||||
} else if(cha < 518400){
|
||||
// 隔天 显示日期+时间
|
||||
return this.dateFormat(new Date(shorttime),"{Mon}月{DD}日 {A} {t}:{ii}");
|
||||
} else {
|
||||
// 隔年 显示完整日期+时间
|
||||
return this.dateFormat(new Date(shorttime),"{Y}-{MM}-{DD} {A} {t}:{ii}");
|
||||
}
|
||||
},
|
||||
|
||||
parseNumber(num) {
|
||||
return num < 10 ? "0" + num : num;
|
||||
},
|
||||
|
||||
dateFormat(date, formatStr) {
|
||||
let dateObj = {},
|
||||
rStr = /\{([^}]+)\}/,
|
||||
mons = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'];
|
||||
|
||||
dateObj["Y"] = date.getFullYear();
|
||||
dateObj["M"] = date.getMonth() + 1;
|
||||
dateObj["MM"] = this.parseNumber(dateObj["M"]);
|
||||
dateObj["Mon"] = mons[dateObj['M'] - 1];
|
||||
dateObj["D"] = date.getDate();
|
||||
dateObj["DD"] = this.parseNumber(dateObj["D"]);
|
||||
dateObj["h"] = date.getHours();
|
||||
dateObj["hh"] = this.parseNumber(dateObj["h"]);
|
||||
dateObj["t"] = dateObj["h"] > 12 ? dateObj["h"] - 12 : dateObj["h"];
|
||||
dateObj["tt"] = this.parseNumber(dateObj["t"]);
|
||||
dateObj["A"] = dateObj["h"] > 12 ? '下午' : '上午';
|
||||
dateObj["i"] = date.getMinutes();
|
||||
dateObj["ii"] = this.parseNumber(dateObj["i"]);
|
||||
dateObj["s"] = date.getSeconds();
|
||||
dateObj["ss"] = this.parseNumber(dateObj["s"]);
|
||||
|
||||
while(rStr.test(formatStr)) {
|
||||
formatStr = formatStr.replace(rStr, dateObj[RegExp.$1]);
|
||||
}
|
||||
return formatStr;
|
||||
},
|
||||
// 获取年龄
|
||||
getAgeByBirthday(data){
|
||||
let birthday=new Date(data.replace(/-/g, "\/"));
|
||||
let d=new Date();
|
||||
return d.getFullYear()-birthday.getFullYear()-((d.getMonth()<birthday.getMonth()|| d.getMonth()==birthday.getMonth() && d.getDate()<birthday.getDate())?1:0);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user