feat: add virtual resource payments and optimize images
This commit is contained in:
69
utils/image.js
Normal file
69
utils/image.js
Normal file
@@ -0,0 +1,69 @@
|
||||
const CDN_HOST = 'img.yidaima.cn'
|
||||
|
||||
// JPEG is used deliberately here. Some older iOS/WeChat image decoders cannot
|
||||
// display the WebP thumbnails returned by Qiniu.
|
||||
const ARTICLE_CARD = 'imageView2/1/w/600/h/375/format/jpg/q/75/ignore-error/1'
|
||||
const RESOURCE_CARD = 'imageView2/1/w/500/h/400/format/jpg/q/75/ignore-error/1'
|
||||
|
||||
function original(url) {
|
||||
if (!url || typeof url !== 'string') {
|
||||
return url || ''
|
||||
}
|
||||
|
||||
const value = url.trim()
|
||||
const httpPrefix = `http://${CDN_HOST}/`
|
||||
if (value.indexOf(httpPrefix) === 0) {
|
||||
return `https://${value.substring('http://'.length)}`
|
||||
}
|
||||
if (value.indexOf(`//${CDN_HOST}/`) === 0) {
|
||||
return `https:${value}`
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
function qiniuThumbnail(url, operation) {
|
||||
const normalized = original(url)
|
||||
if (!normalized || normalized.indexOf(`https://${CDN_HOST}/`) !== 0) {
|
||||
return normalized
|
||||
}
|
||||
// Keep signed or already processed URLs intact.
|
||||
if (normalized.indexOf('?') >= 0) {
|
||||
return normalized
|
||||
}
|
||||
return `${normalized}?${operation}`
|
||||
}
|
||||
|
||||
function compatibleThumbnail(url) {
|
||||
const normalized = original(url)
|
||||
if (!normalized) {
|
||||
return ''
|
||||
}
|
||||
|
||||
// The API may still return a cached showImgThumb containing format/webp.
|
||||
// Rewrite that Qiniu operation on the client while the backend rolls over.
|
||||
if (normalized.indexOf(`https://${CDN_HOST}/`) === 0) {
|
||||
return normalized.replace(/\/format\/webp(?=\/|$)/i, '/format/jpg')
|
||||
}
|
||||
return normalized
|
||||
}
|
||||
|
||||
function articleThumbnail(image) {
|
||||
if (image && typeof image === 'object') {
|
||||
return compatibleThumbnail(image.showImgThumb) || qiniuThumbnail(image.showImg, ARTICLE_CARD)
|
||||
}
|
||||
return qiniuThumbnail(image, ARTICLE_CARD)
|
||||
}
|
||||
|
||||
function resourceThumbnail(image) {
|
||||
if (image && typeof image === 'object') {
|
||||
return compatibleThumbnail(image.showImgThumb) || qiniuThumbnail(image.showImg, RESOURCE_CARD)
|
||||
}
|
||||
return qiniuThumbnail(image, RESOURCE_CARD)
|
||||
}
|
||||
|
||||
export default {
|
||||
original,
|
||||
compatibleThumbnail,
|
||||
articleThumbnail,
|
||||
resourceThumbnail
|
||||
}
|
||||
@@ -35,6 +35,7 @@ const request = config => {
|
||||
let [error, res] = response
|
||||
if (error) {
|
||||
toast('请返回重试')
|
||||
uni.hideLoading()
|
||||
reject('请返回重试')
|
||||
return
|
||||
}
|
||||
@@ -48,21 +49,27 @@ const request = config => {
|
||||
})
|
||||
}
|
||||
})
|
||||
uni.hideLoading()
|
||||
reject('无效的会话,或者会话已过期,请重新登录。')
|
||||
return
|
||||
} else if (code === 500) {
|
||||
setTimeout(()=>{
|
||||
toast(msg)
|
||||
},200);
|
||||
uni.hideLoading()
|
||||
reject('500')
|
||||
return
|
||||
} else if (code !== 200) {
|
||||
toast(msg)
|
||||
uni.hideLoading()
|
||||
reject(code)
|
||||
return
|
||||
}
|
||||
resolve(res.data)
|
||||
uni.hideLoading()
|
||||
})
|
||||
.catch(error => {
|
||||
let { message } = error
|
||||
let message = error && error.message ? error.message : '请求失败'
|
||||
if (message === 'Network Error') {
|
||||
message = '后端接口连接异常'
|
||||
} else if (message.includes('timeout')) {
|
||||
|
||||
Reference in New Issue
Block a user