Commit 5129117a by 李伟康

图片增加缓存

1 parent 3cf2b0b1
...@@ -9,8 +9,13 @@ Component({ ...@@ -9,8 +9,13 @@ Component({
item: { item: {
type: Object, type: Object,
observer(n) { observer(n) {
console.log(n, '=================') this.check(n).then(res => {
n.posterUrl && this.getImageInfo(n) if (res) this.setData({
poster: res,
show: true
})
else this.begin()
}).catch(() => this.begin())
} }
} }
}, },
...@@ -24,6 +29,45 @@ Component({ ...@@ -24,6 +29,45 @@ Component({
* 组件的方法列表 * 组件的方法列表
*/ */
methods: { methods: {
begin() {
let {
item
} = this.data
item.posterUrl && this.getImageInfo(item)
},
check(n) {
const fileList = wx.$storage.get('fileList') || [],
fn = wx.getFileSystemManager()
return new Promise((resolve, reject) => {
if (fileList.length) {
fn.readdir({
dirPath: wx.env.USER_DATA_PATH,
success({
files
}) {
console.log(files,'文件列表')
if (files.length < 21) {
let item = fileList.find(e => e.activityId === n.activityId)
if (item) {
const poster = files.find(s => s.includes(item.fileId))
wx.hideLoading()
if (poster) resolve(`${wx.env.USER_DATA_PATH}/${poster}`)
else reject()
} else reject()
} else {
wx.$storage.rm('fileList')
files.map(e =>{
fn.unlink({
filePath: `${wx.env.USER_DATA_PATH}/${e}`
})
})
reject()
}
}
})
} else reject()
})
},
getImageInfo(n, width, height) { getImageInfo(n, width, height) {
wx.$api.showLoading('处理中') wx.$api.showLoading('处理中')
let that = this let that = this
...@@ -46,19 +90,19 @@ Component({ ...@@ -46,19 +90,19 @@ Component({
let actualHeight = ImgHeight / proportion let actualHeight = ImgHeight / proportion
console.log(actualHeight) console.log(actualHeight)
n.activityName = n.activityName.length > 12 ? `${n.activityName.substring(0, 12)}...` : n.activityName n.activityName = n.activityName.length > 12 ? `${n.activityName.substring(0, 12)}...` : n.activityName
const drawfun = new Draw( const drawfun = new Draw({
{
width: 279, width: 279,
// height: 554 // height: 554
height: 82 + actualHeight height: 82 + actualHeight
}, },
this this
), ),
{ userInfo } = wx.$storage.get(), {
merchantName = userInfo.merchantName.length > 12 ? `${userInfo.merchantName.substring(0, 12)}...` : userInfo.merchantName, userInfo
} = wx.$storage.get(),
merchantName = userInfo.merchantName.length > 12 ? `${userInfo.merchantName.substring(0, 12)}...` : userInfo.merchantName,
res = await drawfun.begin({ res = await drawfun.begin({
imageList: [ imageList: [{
{
x: 0, x: 0,
y: 0, y: 0,
width: 279, width: 279,
...@@ -73,8 +117,7 @@ Component({ ...@@ -73,8 +117,7 @@ Component({
url: n.posterUrl url: n.posterUrl
} }
], ],
textList: [ textList: [{
{
x: 12, x: 12,
y: 14, y: 14,
fontSize: 14, fontSize: 14,
...@@ -114,6 +157,13 @@ Component({ ...@@ -114,6 +157,13 @@ Component({
poster: res.path, poster: res.path,
show: true show: true
}) })
let fileList = wx.$storage.get('fileList') || []
fileList.push({
fileId: res.fileId,
path: res.path,
activityId: n.activityId
})
wx.$storage.set('fileList', fileList)
}, },
download() { download() {
const filePath = this.data.poster, const filePath = this.data.poster,
...@@ -142,4 +192,4 @@ Component({ ...@@ -142,4 +192,4 @@ Component({
}) })
} }
} }
}) })
\ No newline at end of file \ No newline at end of file
// packageOutput/pages/summary/components/outputCrowdfund/outputCrowdfund.js // packageOutput/pages/summary/components/outputCrowdfund/outputCrowdfund.js
import wxqrcode from '~/utils/lib/wxqrcode' import wxqrcode from '~/utils/lib/wxqrcode'
import { IMG_URL } from '~/utils/constantData' import {
IMG_URL
} from '~/utils/constantData'
import outputApi from '~/api/outputApi/index' import outputApi from '~/api/outputApi/index'
const IMAGES = Object.freeze({ const IMAGES = Object.freeze({
download: `${IMG_URL}/output/download.webp` download: `${IMG_URL}/output/download.webp`
...@@ -30,8 +32,7 @@ Component({ ...@@ -30,8 +32,7 @@ Component({
IMGURL: IMG_URL, IMGURL: IMG_URL,
IMAGES, IMAGES,
isShowTip: true, isShowTip: true,
orderFormList: [ orderFormList: [{
{
label: '总场次', label: '总场次',
name: 'totalNum' name: 'totalNum'
}, },
...@@ -45,8 +46,7 @@ Component({ ...@@ -45,8 +46,7 @@ Component({
prefix: '¥' prefix: '¥'
} }
], ],
merchantFormList: [ merchantFormList: [{
{
label: '活动时间', label: '活动时间',
name: 'time' name: 'time'
}, },
...@@ -66,8 +66,7 @@ Component({ ...@@ -66,8 +66,7 @@ Component({
prefix: '¥' prefix: '¥'
} }
], ],
tableHeadList: [ tableHeadList: [{
{
label: '', label: '',
width: '', width: '',
width: '28rpx' width: '28rpx'
...@@ -115,7 +114,9 @@ Component({ ...@@ -115,7 +114,9 @@ Component({
}) })
}, },
handleMore(e) { handleMore(e) {
var { idx } = e.currentTarget.dataset var {
idx
} = e.currentTarget.dataset
var list = this.data.orderObj var list = this.data.orderObj
var item = list.activityList[idx] var item = list.activityList[idx]
list.activityList[idx].showStaffList = list.activityList[idx].showStaffList.concat(item.staffList.length > 10 ? item.staffList.slice(0, 10) : item.staffList) list.activityList[idx].showStaffList = list.activityList[idx].showStaffList.concat(item.staffList.length > 10 ? item.staffList.slice(0, 10) : item.staffList)
...@@ -125,7 +126,9 @@ Component({ ...@@ -125,7 +126,9 @@ Component({
}) })
}, },
handleCopy(e) { handleCopy(e) {
var { idx } = e.currentTarget.dataset var {
idx
} = e.currentTarget.dataset
var currentItem = this.data.orderObj.activityList[idx] var currentItem = this.data.orderObj.activityList[idx]
var qr = wxqrcode.createQrCodeImg(currentItem.activityUrl) var qr = wxqrcode.createQrCodeImg(currentItem.activityUrl)
const base64src = function (base64data) { const base64src = function (base64data) {
...@@ -163,16 +166,31 @@ Component({ ...@@ -163,16 +166,31 @@ Component({
// const { item } = e.currentTarget.dataset // const { item } = e.currentTarget.dataset
// wx.$router.jumpH5(item.activityUrl) // wx.$router.jumpH5(item.activityUrl)
}, },
download(e) { async download(e) {
const { item } = e.currentTarget.dataset const {
wx.$api.showLoading('加载中...') item
outputApi.getPosterUrl({ id: item.activityId }).then(res => { } = e.currentTarget.dataset,
item.posterUrl = res posterObj = this.selectComponent('#poster')
this.setData({ try {
posterItem: item, let flag = await posterObj.check(item)
isShowTip: false posterObj.setData({
poster: flag,
show: true
}) })
}) } catch (error) {
wx.$api.showLoading('加载中...')
outputApi.getPosterUrl({
id: item.activityId
}).then(res => {
item.posterUrl = res
this.setData({
posterItem: item,
isShowTip: false
})
}).catch(()=>{
wx.$api.showToast('下载海报失败')
})
}
} }
} }
}) })
\ No newline at end of file \ No newline at end of file
...@@ -115,5 +115,5 @@ ...@@ -115,5 +115,5 @@
</view> </view>
</view> </view>
</view> </view>
<crowdfund-poster item="{{posterItem}}" /> <crowdfund-poster item="{{posterItem}}" id="poster" />
...@@ -34,7 +34,14 @@ class Draw { ...@@ -34,7 +34,14 @@ class Draw {
async drawImages(imageParams, callback) { async drawImages(imageParams, callback) {
this[_ctx_].beginPath() this[_ctx_].beginPath()
this[_ctx_].save() this[_ctx_].save()
let { borderRadius, height, width, url, x, y } = imageParams let {
borderRadius,
height,
width,
url,
x,
y
} = imageParams
height = this.toRpx(height) height = this.toRpx(height)
width = this.toRpx(width) width = this.toRpx(width)
x = this.toRpx(x) x = this.toRpx(x)
...@@ -76,7 +83,13 @@ class Draw { ...@@ -76,7 +83,13 @@ class Draw {
return this[_ctx_].measureText(text).width return this[_ctx_].measureText(text).width
} }
// 画块形状 // 画块形状
drawFill({ x, y, width, height, color }) { drawFill({
x,
y,
width,
height,
color
}) {
height = this.toRpx(height) height = this.toRpx(height)
width = this.toRpx(width) width = this.toRpx(width)
x = this.toRpx(x) x = this.toRpx(x)
...@@ -89,8 +102,25 @@ class Draw { ...@@ -89,8 +102,25 @@ class Draw {
this[_ctx_].save() this[_ctx_].save()
} }
drawText(textParams) { drawText(textParams) {
let { textDecoration, textBaseline, lineHeight, fontWeight, textAlign, textWidth, maxWidth, fontSize, lastFont, opacity, lineNum, width, color, text, x, y } = textParams, let {
font = this[_ctx_].font.split('px')[1] textDecoration,
textBaseline,
lineHeight,
fontWeight,
textAlign,
textWidth,
maxWidth,
fontSize,
lastFont,
opacity,
lineNum,
width,
color,
text,
x,
y
} = textParams,
font = this[_ctx_].font.split('px')[1]
textAlign = textAlign || 'left' textAlign = textAlign || 'left'
textBaseline = textBaseline || 'normal' textBaseline = textBaseline || 'normal'
opacity = typeof opacity === 'number' ? opacity : 1 opacity = typeof opacity === 'number' ? opacity : 1
...@@ -189,9 +219,7 @@ class Draw { ...@@ -189,9 +219,7 @@ class Draw {
}) })
} }
}) })
} else { } else drawOneLineText()
drawOneLineText()
}
} else drawOneLineText() } else drawOneLineText()
if (textParams.border) if (textParams.border)
this.drawBorder({ this.drawBorder({
...@@ -205,8 +233,18 @@ class Draw { ...@@ -205,8 +233,18 @@ class Draw {
this[_ctx_].stroke() this[_ctx_].stroke()
} }
drawBorder(params) { drawBorder(params) {
let { x, y, text, color, width, radius, opacity, padding, fontSize } = params, let {
paddingX = 0, x,
y,
text,
color,
width,
radius,
opacity,
padding,
fontSize
} = params,
paddingX = 0,
paddingY = 0 paddingY = 0
if (padding && padding.length) { if (padding && padding.length) {
paddingX = this.toRpx(padding[0]) paddingX = this.toRpx(padding[0])
...@@ -217,8 +255,7 @@ class Draw { ...@@ -217,8 +255,7 @@ class Draw {
endX = this.toRpx(x) + this.getTextWidth(text) + paddingX, endX = this.toRpx(x) + this.getTextWidth(text) + paddingX,
startY = this.toRpx(y) - paddingY, startY = this.toRpx(y) - paddingY,
endY = this.toRpx(y + fontSize) + paddingY / 2, endY = this.toRpx(y + fontSize) + paddingY / 2,
arr = [ arr = [{
{
x: startX, x: startX,
y: startY, y: startY,
width: endX width: endX
...@@ -245,8 +282,7 @@ class Draw { ...@@ -245,8 +282,7 @@ class Draw {
this[_ctx_].lineWidth = width || this.toRpx(1) this[_ctx_].lineWidth = width || this.toRpx(1)
if (radius) { if (radius) {
radius = this.toRpx(radius) radius = this.toRpx(radius)
let arcArr = [ let arcArr = [{
{
moveTo: [startX, startY + radius], moveTo: [startX, startY + radius],
arcTo: [startX, startY, startX + radius, startY] arcTo: [startX, startY, startX + radius, startY]
}, },
...@@ -291,7 +327,16 @@ class Draw { ...@@ -291,7 +327,16 @@ class Draw {
} }
drawLine(lineParams, callback) { drawLine(lineParams, callback) {
this[_ctx_].beginPath() this[_ctx_].beginPath()
let { noTransition, opacity, startX, startY, width, color, endX, endY } = lineParams let {
noTransition,
opacity,
startX,
startY,
width,
color,
endX,
endY
} = lineParams
if (!noTransition) { if (!noTransition) {
startX = this.toRpx(startX) startX = this.toRpx(startX)
startY = this.toRpx(startY) startY = this.toRpx(startY)
...@@ -310,7 +355,12 @@ class Draw { ...@@ -310,7 +355,12 @@ class Draw {
if (callback) callback() if (callback) callback()
} }
setLineY = params => { setLineY = params => {
let { textDecoration, textBaseline, fontSize, y } = params let {
textDecoration,
textBaseline,
fontSize,
y
} = params
let type = { let type = {
underline: { underline: {
top: y + fontSize, top: y + fontSize,
...@@ -333,7 +383,11 @@ class Draw { ...@@ -333,7 +383,11 @@ class Draw {
} }
return type[textDecoration][textBaseline] return type[textDecoration][textBaseline]
} }
setLineX({ textAlign, text, x }) { setLineX({
textAlign,
text,
x
}) {
let textWidth = this.getTextWidth(text) let textWidth = this.getTextWidth(text)
let type = { let type = {
left: x, left: x,
...@@ -342,7 +396,13 @@ class Draw { ...@@ -342,7 +396,13 @@ class Draw {
} }
return type[textAlign] return type[textAlign]
} }
drawRadiusRect({ borderRadius, height, width, x, y }, callBack) { drawRadiusRect({
borderRadius,
height,
width,
x,
y
}, callBack) {
let radius = typeof borderRadius === 'string' ? ((Number(borderRadius.split('%')[0]) / 100) * width) / 2 : borderRadius / 2 let radius = typeof borderRadius === 'string' ? ((Number(borderRadius.split('%')[0]) / 100) * width) / 2 : borderRadius / 2
radius = radius || 0 radius = radius || 0
this[_ctx_].beginPath() this[_ctx_].beginPath()
...@@ -353,7 +413,12 @@ class Draw { ...@@ -353,7 +413,12 @@ class Draw {
} }
if (callBack) callBack() if (callBack) callBack()
} }
getFontWidth({ textAlign, fontSize, text, x }) { getFontWidth({
textAlign,
fontSize,
text,
x
}) {
let type = { let type = {
left: x, left: x,
center: x - (fontSize * text.length) / 2, center: x - (fontSize * text.length) / 2,
...@@ -363,7 +428,12 @@ class Draw { ...@@ -363,7 +428,12 @@ class Draw {
} }
drawDecoration(params) { drawDecoration(params) {
let { newText, opacity, y, x } = params let {
newText,
opacity,
y,
x
} = params
let lineX = x let lineX = x
let lineY = y let lineY = y
lineX = this.setLineX(params) lineX = this.setLineX(params)
...@@ -376,16 +446,20 @@ class Draw { ...@@ -376,16 +446,20 @@ class Draw {
let url = this[_canvas_].toDataURL('image/png') let url = this[_canvas_].toDataURL('image/png')
const base64src = function (base64data) { const base64src = function (base64data) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const [a, format, bodyData] = /data:image\/(\w+);base64,(.*)/.exec(base64data) || [] const [, format, bodyData] = /data:image\/(\w+);base64,(.*)/.exec(base64data) || []
if (!format) reject(new Error('ERROR_BASE64SRC_PARSE')) if (!format) reject(new Error('ERROR_BASE64SRC_PARSE'))
const filePath = `${wx.env.USER_DATA_PATH}/tmp_base64src${Date.now()}.${format}` const fileId = Date.now()
const filePath = `${wx.env.USER_DATA_PATH}/tmp_base64src_${fileId}.${format}`
const buffer = wx.base64ToArrayBuffer(bodyData) const buffer = wx.base64ToArrayBuffer(bodyData)
wx.getFileSystemManager().writeFile({ wx.getFileSystemManager().writeFile({
filePath, filePath,
data: buffer, data: buffer,
encoding: 'binary', encoding: 'binary',
success() { success() {
resolve(filePath) resolve({
filePath,
fileId
})
}, },
fail() { fail() {
reject(new Error('ERROR_BASE64SRC_WRITE')) reject(new Error('ERROR_BASE64SRC_WRITE'))
...@@ -431,11 +505,17 @@ const checkTextDecoration = textDecoration => { ...@@ -431,11 +505,17 @@ const checkTextDecoration = textDecoration => {
} }
/**网络图片转成本地资源 */ /**网络图片转成本地资源 */
const createLocalImg = (url, callBack) => { const createLocalImg = ({
filePath,
fileId
}, callBack) => {
wx.getImageInfo({ wx.getImageInfo({
src: url, src: filePath,
success(res) { success(res) {
callBack(res) callBack({
...res,
fileId
})
}, },
fail() { fail() {
wx.showToast({ wx.showToast({
...@@ -447,4 +527,4 @@ const createLocalImg = (url, callBack) => { ...@@ -447,4 +527,4 @@ const createLocalImg = (url, callBack) => {
} }
}) })
} }
export default Draw export default Draw
\ No newline at end of file \ No newline at end of file
...@@ -16,10 +16,11 @@ export const STORAGE_OBJECTkEY = { ...@@ -16,10 +16,11 @@ export const STORAGE_OBJECTkEY = {
firstLogin: 'firstLogin', firstLogin: 'firstLogin',
authphone: 'authphone', authphone: 'authphone',
posterTimer: 'posterTimer', posterTimer: 'posterTimer',
disLikeRecordFlag: 'disLikeRecordFlag' disLikeRecordFlag: 'disLikeRecordFlag',
fileList: 'fileList'
} }
export const IMG_VERSION = () => { export const IMG_VERSION = () => {
const accountInfo = wx.getAccountInfoSync() const accountInfo = wx.getAccountInfoSync()
return accountInfo.miniProgram.version || '1.0.0' return accountInfo.miniProgram.version || '1.0.0'
} }
\ No newline at end of file \ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!