2023-01-11 16:33:13 +08:00
|
|
|
|
import request from './request'
|
2023-02-09 08:50:01 +08:00
|
|
|
|
import cache from './cacheKey.js'
|
2023-08-11 11:03:31 +08:00
|
|
|
|
import { getImageUrl } from '@/common/api/basic'
|
2023-08-17 09:24:59 +08:00
|
|
|
|
import { apiUpdatePush } from '@/common/api/user'
|
2023-07-24 08:47:20 +08:00
|
|
|
|
import { queryDictDataCache } from '../api/dictionary.js'
|
|
|
|
|
|
import cacheKey from './cacheKey.js'
|
2023-01-11 16:33:13 +08:00
|
|
|
|
const toast = (title, duration = 1500, call, mask = false, icon = 'none') => {
|
|
|
|
|
|
if (Boolean(title) === false) {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
uni.showToast({
|
|
|
|
|
|
title,
|
|
|
|
|
|
duration,
|
|
|
|
|
|
mask,
|
|
|
|
|
|
icon,
|
|
|
|
|
|
})
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
|
call && call()
|
2023-05-25 10:10:22 +08:00
|
|
|
|
}, duration)
|
2023-01-11 16:33:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @description 格式化时间
|
|
|
|
|
|
* @param time
|
|
|
|
|
|
* @param cFormat
|
|
|
|
|
|
* @returns {string|null}
|
|
|
|
|
|
*/
|
2023-05-25 10:10:22 +08:00
|
|
|
|
function parseTime(time, cFormat) {
|
2023-01-11 16:33:13 +08:00
|
|
|
|
if (arguments.length === 0) {
|
|
|
|
|
|
return null
|
|
|
|
|
|
}
|
|
|
|
|
|
const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
|
|
|
|
|
|
let date
|
|
|
|
|
|
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
|
|
|
|
|
|
}
|
|
|
|
|
|
date = new Date(time)
|
|
|
|
|
|
}
|
|
|
|
|
|
const formatObj = {
|
|
|
|
|
|
y: date.getFullYear(),
|
|
|
|
|
|
m: date.getMonth() + 1,
|
|
|
|
|
|
d: date.getDate(),
|
|
|
|
|
|
h: date.getHours(),
|
|
|
|
|
|
i: date.getMinutes(),
|
|
|
|
|
|
s: date.getSeconds(),
|
2023-05-25 10:10:22 +08:00
|
|
|
|
a: date.getDay(),
|
2023-01-11 16:33:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
return format.replace(/{([ymdhisa])+}/g, (result, key) => {
|
|
|
|
|
|
let value = formatObj[key]
|
|
|
|
|
|
if (key === 'a') {
|
|
|
|
|
|
return ['日', '一', '二', '三', '四', '五', '六'][value]
|
|
|
|
|
|
}
|
|
|
|
|
|
if (result.length > 0 && value < 10) {
|
|
|
|
|
|
value = '0' + value
|
|
|
|
|
|
}
|
|
|
|
|
|
return value || 0
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @description 格式化时间
|
|
|
|
|
|
* @param time
|
|
|
|
|
|
* @param option
|
|
|
|
|
|
* @returns {string}
|
|
|
|
|
|
*/
|
2023-05-25 10:10:22 +08:00
|
|
|
|
function formatTime(time, option) {
|
2023-01-11 16:33:13 +08:00
|
|
|
|
if (('' + time).length === 10) {
|
|
|
|
|
|
time = parseInt(time) * 1000
|
|
|
|
|
|
} else {
|
|
|
|
|
|
time = +time
|
|
|
|
|
|
}
|
|
|
|
|
|
if (option) {
|
|
|
|
|
|
return parseTime(time, option)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
const d = new Date(time)
|
|
|
|
|
|
const now = Date.now()
|
|
|
|
|
|
const diff = (now - d) / 1000
|
|
|
|
|
|
if (diff < 30) {
|
|
|
|
|
|
return '刚刚'
|
|
|
|
|
|
} else if (diff < 3600) {
|
|
|
|
|
|
// less 1 hour
|
|
|
|
|
|
return Math.ceil(diff / 60) + '分钟前'
|
|
|
|
|
|
} else if (diff < 3600 * 24) {
|
|
|
|
|
|
return Math.ceil(diff / 3600) + '小时前'
|
|
|
|
|
|
} else if (diff < 3600 * 24 * 2) {
|
|
|
|
|
|
return '1天前'
|
|
|
|
|
|
}
|
|
|
|
|
|
return (
|
2023-05-25 10:10:22 +08:00
|
|
|
|
d.getMonth() + 1 + '月' + d.getDate() + '日'
|
2023-01-11 16:33:13 +08:00
|
|
|
|
// +
|
|
|
|
|
|
// d.getHours() +
|
|
|
|
|
|
// '时'
|
|
|
|
|
|
// +
|
|
|
|
|
|
// d.getMinutes() +
|
|
|
|
|
|
// '分'
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
const h5Helper = {
|
|
|
|
|
|
isAndroid: function () {
|
|
|
|
|
|
return window.navigator.appVersion.toLowerCase().indexOf('android') != -1
|
|
|
|
|
|
},
|
|
|
|
|
|
isIOS: function () {
|
|
|
|
|
|
return window.navigator.appVersion.toLowerCase().indexOf('iphone') != -1
|
|
|
|
|
|
},
|
|
|
|
|
|
isWeiXinWeb: function () {
|
|
|
|
|
|
var ua = window.navigator.userAgent.toLowerCase()
|
|
|
|
|
|
return ua.match(/MicroMessenger/i) == 'micromessenger'
|
|
|
|
|
|
},
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 验证手机号格式
|
|
|
|
|
|
const validatePhoneNumber = (phone) => {
|
|
|
|
|
|
if (!phone) return false
|
|
|
|
|
|
var testReg = /^1[23456789]\d{9}$/
|
|
|
|
|
|
return testReg.test(phone)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const getUserLocation = (call) => {
|
|
|
|
|
|
uni.getLocation({
|
|
|
|
|
|
type: 'wgs84',
|
|
|
|
|
|
success: function (address) {
|
|
|
|
|
|
call(address)
|
|
|
|
|
|
},
|
2023-05-25 10:10:22 +08:00
|
|
|
|
fail: (err) => {
|
2023-01-11 16:33:13 +08:00
|
|
|
|
uni.showModal({
|
|
|
|
|
|
title: '提示',
|
|
|
|
|
|
content: '定位失败,请打开定位权限',
|
|
|
|
|
|
success: function (res) {
|
|
|
|
|
|
if (res.confirm) {
|
|
|
|
|
|
uni.openSetting({
|
2023-05-25 10:10:22 +08:00
|
|
|
|
success: (resSett) => {
|
2023-01-11 16:33:13 +08:00
|
|
|
|
if (resSett.authSetting['scope.userLocation']) {
|
|
|
|
|
|
uni.getLocation({
|
2023-05-25 10:10:22 +08:00
|
|
|
|
success: (address) => {
|
2023-01-11 16:33:13 +08:00
|
|
|
|
call && call(address)
|
|
|
|
|
|
},
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
2023-05-25 10:10:22 +08:00
|
|
|
|
},
|
2023-01-11 16:33:13 +08:00
|
|
|
|
})
|
|
|
|
|
|
},
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 加载用户配置
|
|
|
|
|
|
var globalConfigIsLoading = false,
|
|
|
|
|
|
global_config = null,
|
2023-05-25 10:10:22 +08:00
|
|
|
|
globalConfigCallbacks = []
|
2023-01-11 16:33:13 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 加载用户配置
|
|
|
|
|
|
* @param call 加载成功后的回调
|
|
|
|
|
|
* @param need_fresh 是否及时刷新用户配置
|
|
|
|
|
|
*/
|
|
|
|
|
|
const loadConfig = (call, need_fresh = false) => {
|
|
|
|
|
|
if (call && global_config && !need_fresh) {
|
2023-05-25 10:10:22 +08:00
|
|
|
|
call(global_config)
|
|
|
|
|
|
return
|
2023-01-11 16:33:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
if (globalConfigIsLoading) {
|
2023-05-25 10:10:22 +08:00
|
|
|
|
globalConfigCallbacks.push(call)
|
|
|
|
|
|
return
|
2023-01-11 16:33:13 +08:00
|
|
|
|
}
|
2023-05-25 10:10:22 +08:00
|
|
|
|
globalConfigIsLoading = true
|
2023-01-11 16:33:13 +08:00
|
|
|
|
request({
|
|
|
|
|
|
url: '/org/userResource/userMsg',
|
|
|
|
|
|
})
|
2023-05-25 10:10:22 +08:00
|
|
|
|
.then((rs) => {
|
|
|
|
|
|
globalConfigIsLoading = false
|
|
|
|
|
|
global_config = rs.data
|
|
|
|
|
|
uni.setStorage({
|
|
|
|
|
|
key: 'userInfo',
|
|
|
|
|
|
data: global_config,
|
|
|
|
|
|
})
|
|
|
|
|
|
if (call) {
|
|
|
|
|
|
call(global_config)
|
|
|
|
|
|
}
|
|
|
|
|
|
for (var i = 0; i < globalConfigCallbacks.length; i++) {
|
|
|
|
|
|
globalConfigCallbacks[i](global_config)
|
|
|
|
|
|
}
|
|
|
|
|
|
globalConfigCallbacks = []
|
|
|
|
|
|
})
|
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
|
globalConfigIsLoading = false
|
|
|
|
|
|
console.warn(err)
|
|
|
|
|
|
// uni.reLaunch({ url: '/pages/user/login' })
|
|
|
|
|
|
})
|
2023-01-11 16:33:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-05-25 10:10:22 +08:00
|
|
|
|
const prePage = () => {
|
|
|
|
|
|
let pages = getCurrentPages()
|
|
|
|
|
|
let prePage = pages[pages.length - 2]
|
|
|
|
|
|
return prePage
|
|
|
|
|
|
}
|
2023-03-16 15:26:25 +08:00
|
|
|
|
|
2023-08-17 09:24:59 +08:00
|
|
|
|
const loginSuccess = (data, jump = true) => {
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
|
console.log(data)
|
|
|
|
|
|
uni.setStorageSync('access_token', data.token_type + ' ' + data.access_token)
|
|
|
|
|
|
uni.setStorageSync('refresh_token', data.refresh_token)
|
|
|
|
|
|
let strings = data.access_token.split('.') //截取token,获取载体
|
|
|
|
|
|
console.log(escape, atob)
|
|
|
|
|
|
var userInfo = JSON.parse(decodeURIComponent(escape(atob(strings[1].replace(/-/g, '+').replace(/_/g, '/')))))
|
|
|
|
|
|
userInfo.authorities = userInfo.authorities[0]
|
|
|
|
|
|
if (userInfo.headSculpture) {
|
|
|
|
|
|
getImageUrl(userInfo.headSculpture).then((res) => {
|
|
|
|
|
|
userInfo.avatar = res.data
|
|
|
|
|
|
uni.setStorageSync(cache.userInfo, userInfo)
|
|
|
|
|
|
apiUpdatePush()
|
|
|
|
|
|
resolve(userInfo)
|
|
|
|
|
|
})
|
|
|
|
|
|
} else {
|
2023-08-11 11:03:31 +08:00
|
|
|
|
uni.setStorageSync(cache.userInfo, userInfo)
|
2023-08-17 09:24:59 +08:00
|
|
|
|
apiUpdatePush()
|
|
|
|
|
|
resolve(userInfo)
|
|
|
|
|
|
}
|
|
|
|
|
|
console.log('reLaunch')
|
|
|
|
|
|
if (jump) {
|
|
|
|
|
|
queryDictDataCache().then((res) => {
|
|
|
|
|
|
uni.setStorageSync(cacheKey.dictData, res.data)
|
|
|
|
|
|
uni.reLaunch({
|
|
|
|
|
|
url: '/pages/index/index',
|
|
|
|
|
|
fail: (err) => {
|
|
|
|
|
|
console.log(err)
|
|
|
|
|
|
},
|
|
|
|
|
|
})
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
2023-07-03 09:16:54 +08:00
|
|
|
|
})
|
|
|
|
|
|
}
|
2023-07-31 09:00:30 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 只针对列表页的刷新
|
|
|
|
|
|
* @param {*} number
|
|
|
|
|
|
* @param {*} time
|
|
|
|
|
|
*/
|
|
|
|
|
|
const refreshPrePage = (number = 1, time = 1500) => {
|
|
|
|
|
|
let pages = getCurrentPages()
|
|
|
|
|
|
let prePage = pages[pages.length - number - 1]
|
|
|
|
|
|
if (prePage && time) {
|
|
|
|
|
|
prePage.$vm.store.reload()
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
|
uni.navigateBack({
|
|
|
|
|
|
delta: number,
|
|
|
|
|
|
})
|
|
|
|
|
|
}, time)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-07-03 09:16:54 +08:00
|
|
|
|
|
2023-01-11 16:33:13 +08:00
|
|
|
|
export default {
|
|
|
|
|
|
validatePhoneNumber,
|
|
|
|
|
|
toast,
|
|
|
|
|
|
formatTime,
|
|
|
|
|
|
parseTime,
|
|
|
|
|
|
h5Helper,
|
|
|
|
|
|
getUserLocation,
|
|
|
|
|
|
loadConfig,
|
2023-05-25 10:10:22 +08:00
|
|
|
|
prePage,
|
2023-07-03 09:16:54 +08:00
|
|
|
|
loginSuccess,
|
2023-07-31 09:00:30 +08:00
|
|
|
|
refreshPrePage,
|
2023-01-11 16:33:13 +08:00
|
|
|
|
}
|