合并makeServerUrl 和 make_ipfs_url 到 make_server_url
This commit is contained in:
parent
0086915515
commit
bc5c0a5eb4
73
unitool.js
73
unitool.js
@ -109,31 +109,39 @@ export default {
|
||||
}
|
||||
},
|
||||
|
||||
makeServerUrl (route = '') {
|
||||
make_server_url (route) {
|
||||
if (typeof route === 'string') route = route.replace('\\', '/')
|
||||
else if (typeof route === 'object') {
|
||||
const { apiVersion = 'api', apiWho, apiTodo } = route
|
||||
route = `${apiVersion}/${apiWho}/${apiTodo}`
|
||||
} else {
|
||||
// 防止 route 为 null, undefined 等由于后台数据库默认值而造成的异常。
|
||||
route = ''
|
||||
}
|
||||
// 已是完整url
|
||||
if (/^https?:\/\//.test(route)) {
|
||||
return route
|
||||
}
|
||||
|
||||
if (typeof route !== 'string') route = '' // 防止 route 为 null, undefined 等由于后台数据库默认值而造成的异常。
|
||||
route = route.replace('\\', '/').replace(/^\//, '')
|
||||
|
||||
// 本地图片
|
||||
if (/^\/static\//.test(route)) {
|
||||
return route
|
||||
}
|
||||
// cid
|
||||
if (/^[\da-zA-Z]+$/.test(route)) {
|
||||
return (wo?.ss?.envarRemote?.ipfsGateway || '') + route
|
||||
}
|
||||
// 需要组装url
|
||||
const envar = this.envar || wo?.envar || {}
|
||||
|
||||
route = route.replace(/^\//, '')
|
||||
// 已有现成后端服务域名
|
||||
if (envar.servUrl) {
|
||||
return `${envar.servUrl}/${route}`
|
||||
} else {
|
||||
const hostname = envar.servHostname /*|| globalThis.window?.location?.hostname*/ || 'localhost'
|
||||
const port = envar.servPort /*|| globalThis.window?.location?.port*/ || ''
|
||||
const protocol = hostname === 'localhost' ? 'http' : envar.servProtocol || (process.env.NODE_ENV === 'production' ? 'https' : 'http')
|
||||
return `${protocol}://${hostname}${port ? ':' : ''}${port}/${route}`
|
||||
}
|
||||
},
|
||||
|
||||
makeBgUrl (path) {
|
||||
if (path) {
|
||||
return `url(${this.makeServerUrl(path)})`
|
||||
}
|
||||
return ''
|
||||
// 需要组装后端服务域名
|
||||
const hostname = envar.servHostname /*|| globalThis.window?.location?.hostname*/ || 'localhost'
|
||||
const port = envar.servPort /*|| globalThis.window?.location?.port*/ || ''
|
||||
const protocol = hostname === 'localhost' ? 'http' : envar.servProtocol || (process.env.NODE_ENV === 'production' ? 'https' : 'http')
|
||||
return `${protocol}://${hostname}${port ? ':' : ''}${port}/${route}`
|
||||
},
|
||||
|
||||
/** 统一 uni.request 和 uniCloud.callFunction 的调用方法,提供统一、透明的后台调用
|
||||
@ -191,7 +199,7 @@ export default {
|
||||
apiWhat[key] = JSON.stringify(apiWhat[key])
|
||||
}
|
||||
}
|
||||
url = this.makeServerUrl(`${apiVersion}/${apiWho}/${apiTodo}`)
|
||||
url = this.make_server_url(`${apiVersion}/${apiWho}/${apiTodo}`)
|
||||
let [error, { statusCode, header, errMsg, data: resultServer = {} } = {}] = await uni.request({
|
||||
method: httpMethod,
|
||||
url: url,
|
||||
@ -271,7 +279,7 @@ export default {
|
||||
formData['_passtoken'] = uni.getStorageSync('_passtoken') // 20230527 加回这一句,让后台可以根据验证用户来决定怎样处理文件。
|
||||
|
||||
uni.showLoading()
|
||||
let [errorUpload, { data, statusCode } = {}] = await uni.uploadFile({ url: this.makeServerUrl(url), filePath, name, header, formData })
|
||||
let [errorUpload, { data, statusCode } = {}] = await uni.uploadFile({ url: this.make_server_url(url), filePath, name, header, formData })
|
||||
// 后台 Multer 处理 req.file = { destination, filename, originalname, path, mimetype, size }, 其中 path 包括了 destination 和 filename 的文件相对路径。
|
||||
// url 所在方法进一步处理后,通过 uni.uploadFile 存在 data 里返回结果
|
||||
uni.hideLoading()
|
||||
@ -286,7 +294,7 @@ export default {
|
||||
}
|
||||
|
||||
if (data?._state === 'SUCCESS' && data?.path) {
|
||||
return { _state: 'SUCCESS', fileUrl: this.makeServerUrl(data.path), filePath: data.path, ...data }
|
||||
return { _state: 'SUCCESS', fileUrl: this.make_server_url(data.path), filePath: data.path, ...data }
|
||||
} else {
|
||||
return { _state: 'CLIENT_FAIL_UPLOAD_FILE', error: errorUpload }
|
||||
}
|
||||
@ -452,12 +460,33 @@ export default {
|
||||
}
|
||||
// #ifdef APP-PLUS
|
||||
uni.showToast({ icon: 'none', title, duration, ...rest })
|
||||
// plus.nativeUI.toast( title, { align: center/left/right, verticalAlign: bottom/center/top, duration:long/short, icon, iconWidth, iconHeight, style: block/inline }) 对应 plus.nativeUI.closeToast()
|
||||
// #endif
|
||||
// #ifdef H5
|
||||
// #ifndef APP-PLUS
|
||||
uni.showToast({ icon: 'none', image, title, duration, ...rest })
|
||||
// #endif
|
||||
},
|
||||
|
||||
showLoading ({ title, mask }) {
|
||||
// #ifndef APP-PLUS
|
||||
uni.showLoading({ title, mask })
|
||||
// #endif
|
||||
// #ifdef APP-PLUS
|
||||
// 在安卓应用里,uni.showLoading() 重复调用,导致不断闪烁跳动。
|
||||
// plus.nativeUI.showWaiting() 调用多了则导致死机。
|
||||
// 还好,showWaiting() 返回 waiting 对象,可以 waiting.setTitle()
|
||||
return plus.nativeUI.showWaiting(title, { modal: mask })
|
||||
// #endif
|
||||
},
|
||||
hideLoading () {
|
||||
// #ifndef APP-PLUS
|
||||
uni.hideLoading()
|
||||
// #endif
|
||||
// #ifdef APP-PLUS
|
||||
plus.nativeUI.closeWaiting()
|
||||
// #endif
|
||||
},
|
||||
|
||||
// precision 要有默认值,以防无法连接后台时,这个方法会导致 part-header.vue 出错。
|
||||
formatMoney (amount, { precision = 2 } = {}) {
|
||||
// parseInt(NaN/undefined/false/null/'') 都返回 NaN,而 Number(false/null/'')===0,因此用 parseInt 来过滤无效输入。
|
||||
|
Loading…
Reference in New Issue
Block a user