You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
167 lines
4.4 KiB
167 lines
4.4 KiB
import { WX_API, H5_API, httpPrefix } from '@/config/site.config.js';
|
|
var _url = H5_API+ WX_API + httpPrefix
|
|
|
|
import store from '@/store/index.js'
|
|
|
|
export function checkToken(vm) {
|
|
let expiresTime = vm.$store.state.user.vuex_loginInfo.expiresTime
|
|
let nowTime = new Date() * 1
|
|
console.log('超时了')
|
|
console.log(expiresTime)
|
|
if (nowTime > expiresTime * 1) {
|
|
vm.$store.commit('goLogin')
|
|
// 如果小于20分钟就刷新一下token &&
|
|
} else if ((expiresTime * 1 - nowTime) / 60000 < 10) {
|
|
vm.$store.dispatch('refreshToken')
|
|
}
|
|
}
|
|
|
|
function dateRangeFn(dateRange) {
|
|
let tmp = []
|
|
let dateArr = []
|
|
dateRange.forEach((date) => {
|
|
let dateStr = date.toISOString().split('T')[0]
|
|
let dd = dateStr.split('-')[2]
|
|
const daysOfWeek = ['日', '一', '二', '三', '四', '五', '六'];
|
|
const dayOfWeek = date.getDay();
|
|
const weekName = daysOfWeek[dayOfWeek];
|
|
// console.log(dateStr)
|
|
// console.log(weekName)
|
|
if (tmp.length == 0) {
|
|
dateArr.push(tmp)
|
|
}
|
|
let obj = {
|
|
week: weekName,
|
|
num: dd,
|
|
date: dateStr
|
|
}
|
|
tmp.push(obj)
|
|
if (tmp.length == 5) {
|
|
tmp = []
|
|
}
|
|
});
|
|
console.log(dateArr)
|
|
return dateArr
|
|
}
|
|
|
|
// 获取两个时间段的所有日期
|
|
export function getDates(startDate, endDate) {
|
|
const dates = [];
|
|
let currentDate = new Date(startDate);
|
|
|
|
while (currentDate <= endDate) {
|
|
dates.push(new Date(currentDate));
|
|
currentDate.setDate(currentDate.getDate() + 1);
|
|
}
|
|
return dateRangeFn(dates)
|
|
}
|
|
|
|
// 获取两个时间段的所有月份
|
|
export function getMonthsBetweenDates(startDate, endDate) {
|
|
let months = [];
|
|
let currentDate = new Date(startDate);
|
|
|
|
while (currentDate <= endDate) {
|
|
const year = currentDate.getFullYear();
|
|
const month = currentDate.getMonth() + 1; // 月份从0开始,所以要加1
|
|
months.push(`${year}-${month.toString().padStart(2, '0')}`);
|
|
|
|
// 将当前日期设置为下一个月的第一天
|
|
currentDate.setMonth(currentDate.getMonth() + 1);
|
|
currentDate.setDate(1);
|
|
}
|
|
|
|
return months;
|
|
}
|
|
|
|
|
|
//选择图片
|
|
export function chooseImages(num=0) {
|
|
let imgNum = 3 - num
|
|
let tempArr = []
|
|
return new Promise((reslove, reject)=>{
|
|
uni.chooseImage({
|
|
count: imgNum, //允许选择的数量
|
|
sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
|
|
sourceType: ['album', 'camera'], //从相册选择
|
|
success: res => {
|
|
uni.showLoading({
|
|
title: '图片上传中...'
|
|
});
|
|
console.log(res)
|
|
res.tempFiles.forEach( async (item,index)=>{
|
|
let dataImg = await uploadImgApi(item.path, index)
|
|
if(dataImg) tempArr.push(dataImg)
|
|
if(index==res.tempFiles.length-1) {
|
|
reslove(tempArr)
|
|
}
|
|
})
|
|
}
|
|
})
|
|
})
|
|
}
|
|
export function uploadImgApi(filePath, imgName) {
|
|
console.log(filePath)
|
|
let token = 'Bearer '+ store.state.user.vuex_loginInfo.accessToken
|
|
let timer = new Date() * 1
|
|
return new Promise((reslove, reject)=>{
|
|
// 上传图片到服务器
|
|
uni.uploadFile({
|
|
url: _url + 'infra/file/upload',//接口
|
|
filePath: filePath,//要上传的图片的本地路径
|
|
name: 'file',
|
|
|
|
formData: {
|
|
path: 'complain/'+ uni.$u.date(timer, 'yyyy-mm-dd')+'/'+timer + '-'+ imgName,
|
|
type: 1,
|
|
fileSuffix: "png"
|
|
},
|
|
header: {
|
|
Authorization: token,
|
|
// 'tenant-id': '1704459882232553474'
|
|
},
|
|
success(res) {
|
|
console.log('上传成功')
|
|
let res2 = JSON.parse(res.data)
|
|
reslove(res2.data)
|
|
|
|
},
|
|
fail() {
|
|
reject(0)
|
|
},
|
|
complete: (err)=> {
|
|
uni.hideLoading();
|
|
}
|
|
})
|
|
})
|
|
}
|
|
let apiOk =true
|
|
let refreshTokenFn = null
|
|
export function refreshToken() {
|
|
return new Promise(async(resolve, reject)=>{
|
|
if(!apiOk) {
|
|
return refreshTokenFn
|
|
}
|
|
apiOk = false
|
|
refreshTokenFn = null
|
|
const http = uni.$u.http
|
|
let config = { header: {'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'}, custom: { noToken: true } }
|
|
let refreshToken = store.state.user.vuex_loginInfo.refreshToken
|
|
refreshTokenFn = await http.post('member/auth/refresh-token',{refreshToken}, config)
|
|
if( refreshTokenFn.data|| refreshTokenFn.data.accessToken) {
|
|
apiOk = true
|
|
store.commit('update_vuex_loginInfo', refreshTokenFn.data)
|
|
uni.$u.http.setConfig((config) => {
|
|
config.header.Authorization = 'Bearer ' + refreshTokenFn.data.accessToken
|
|
return config
|
|
})
|
|
resolve(refreshTokenFn)
|
|
}else {
|
|
apiOk = true
|
|
store.commit('goLogin')
|
|
reject('刷新token失败了')
|
|
}
|
|
})
|
|
}
|
|
|
|
|