|
|
import {
|
|
|
$post
|
|
|
} from '@/lu/axios.js'
|
|
|
import $config from '@/config.js'
|
|
|
import {
|
|
|
useStore
|
|
|
} from '@/store'
|
|
|
const app_path = 'App'
|
|
|
let api_map_url = $config.config.api_map_url
|
|
|
let base_assets_url = $config.config.base_assets_url
|
|
|
export const $url = (url_key) => {
|
|
|
const $store = useStore()
|
|
|
if (url_key in $store.api_map) {
|
|
|
return $store.api_map[url_key]
|
|
|
} else {
|
|
|
return ''
|
|
|
}
|
|
|
}
|
|
|
export const $api = async (url_key, data = {}, opt = {}) => {
|
|
|
let loadingTimer = null
|
|
|
|
|
|
try {
|
|
|
const opt_data = {
|
|
|
...$config,
|
|
|
...opt,
|
|
|
}
|
|
|
const $store = useStore()
|
|
|
|
|
|
// 防止loading一直显示的兜底机制(30秒后强制关闭)
|
|
|
loadingTimer = setTimeout(() => {
|
|
|
try {
|
|
|
uni.hideLoading()
|
|
|
console.warn('[api] 强制关闭loading(超时保护)')
|
|
|
} catch (e) {
|
|
|
console.warn('[api] 强制关闭loading失败', e)
|
|
|
}
|
|
|
}, 30000)
|
|
|
|
|
|
if (!(url_key in $store.api_map)) {
|
|
|
const api_map = await $post({
|
|
|
url: opt_data.config.api_map_url
|
|
|
}, opt_data)
|
|
|
if (!api_map.status) {
|
|
|
if (typeof uni.$lu !== 'undefined' && uni.$lu.toast) {
|
|
|
uni.$lu.toast('获取接口失败')
|
|
|
}
|
|
|
if (loadingTimer) clearTimeout(loadingTimer)
|
|
|
return false
|
|
|
}
|
|
|
$store.api_map = api_map.data.list
|
|
|
}
|
|
|
if (!(url_key in $store.api_map)) {
|
|
|
if (typeof uni.$lu !== 'undefined' && uni.$lu.toast) {
|
|
|
uni.$lu.toast(`接口不存在 [${url_key}]`)
|
|
|
}
|
|
|
if (loadingTimer) clearTimeout(loadingTimer)
|
|
|
return false
|
|
|
}
|
|
|
const openid = uni.getStorageSync('OPENID')
|
|
|
if (!!openid) {
|
|
|
data.openid = openid
|
|
|
}
|
|
|
const result = await $post({
|
|
|
url: $store.api_map[url_key],
|
|
|
data
|
|
|
}, opt_data)
|
|
|
|
|
|
// 清除定时器
|
|
|
if (loadingTimer) clearTimeout(loadingTimer)
|
|
|
|
|
|
return result
|
|
|
} catch (e) {
|
|
|
console.error('[api] API调用失败', url_key, e)
|
|
|
|
|
|
// 清除定时器
|
|
|
if (loadingTimer) clearTimeout(loadingTimer)
|
|
|
|
|
|
throw e
|
|
|
}
|
|
|
}
|
|
|
|
|
|
export const $image = (path) => {
|
|
|
const path_ret = ['http://', 'https://', ';base64,']
|
|
|
for (let i = 0; i < path_ret.length; i++) {
|
|
|
if (path.indexOf(path_ret[i]) !== -1) {
|
|
|
return path
|
|
|
}
|
|
|
}
|
|
|
return `${$config.config.base_assets_url}${path}`
|
|
|
}
|
|
|
|
|
|
export const $response = (response, then, opt = {}, error = () => {}) => {
|
|
|
if (response) {
|
|
|
const opt_data = {
|
|
|
...$config,
|
|
|
...opt,
|
|
|
}
|
|
|
if (response.status != opt_data.success_code) {
|
|
|
uni.$lu.toast(response.msg);
|
|
|
error()
|
|
|
return
|
|
|
}
|
|
|
then()
|
|
|
}
|
|
|
} |