CrowdfundPoster.js 11.4 KB
// packageOutput/pages/summary/components/CrowdfundPoster/CrowdfundPoster.js
import Draw from '~/utils/Draw'

Component({
    /**
     * 组件的属性列表
     */
    properties: {
        // 默认母链海报 不是须样式须单独处理 通过item.type 区分类型
        item: {
            type: Object,
            observer(n) {
                // 如果是喜报和战报就跳过图片生成
                console.log(n)
                if (n.type == 'warReport') {
                    // n.posterUrl
                    wx.$api.showLoading('图片加载中')
                    return this.setData({
                        poster: n.posterUrl,
                        show: true
                    })
                }

                n.posterUrl &&
                this.checkUrl()
                    .then(result => {
                        result &&
                        this.setData({
                            poster: result,
                            show: true
                        })
                    })
                    .catch(err => {
                        console.log(err)
                        this.begin()
                    })
            }
        },
        scrollImg: {
            type: Boolean,
            value: true
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        _item_: null,
        findKey: 'posterUrl',
        height: '68%',
        userInfo: {}
        // poster: 'https://wx.glsx.com.cn/upload/biz/599036445419769856.png'
    },

    /**
     * 组件的方法列表
     */
    methods: {
        successOnload() {
            // debugger
            // console.log('a123456')
            wx.$api.hideLoading()
            // this.setData({
            //   show: true
            // })
        },
        // 图片缓存 有取小程序缓存图片
        checkUrl() {
            console.log('checkUrl')
            let {item: postObj} = this.data
            const fileList = wx.$storage.get('fileList') || [],
                fn = wx.getFileSystemManager(),
                {findKey} = this.data

            return new Promise((resolve, reject) => {
                // return reject()
                if (fileList.length) {
                    fn.readdir({
                        dirPath: wx.env.USER_DATA_PATH,
                        success({files}) {
                            let obj = fileList.find(e => e[findKey] === postObj[findKey])
                            const poster = files.find(s => obj?.path.includes(s))
                            wx.hideLoading()
                            // console.log(poster)
                            // 处理图片文件缓存太多的问题
                            let filesLength = files.length
                            console.log(files)
                            if (filesLength > 21) {
                                files.map((e, index) => {
                                    // debugger
                                    // console.log(filesLength - index > 21)
                                    filesLength - index > 21 &&
                                    index != 0 &&
                                    fn.unlink({
                                        filePath: `${wx.env.USER_DATA_PATH}/${e}`,
                                        fail(res) {
                                            console.error(res)
                                        }
                                    })
                                })
                            }
                            // console.log(poster)
                            if (poster) {
                                resolve(`${wx.env.USER_DATA_PATH}/${poster}`)
                            } else {
                                reject()
                            }
                        }
                    })
                } else reject()
            })
        },
        begin() {
            let {item} = this.data
            item.posterUrl && this.getImageInfo(item)
        },
        check(n) {
            this.setData({
                _item_: n
            })
        },
        getImageInfo(n) {
            // console.log(n)
            wx.$api.showLoading('处理中')
            let that = this
            wx.getImageInfo({
                src: `${n.posterUrl}?v=${Date.now()}`,
                success: function (res) {
                    // console.log(res)
                    // w = res.width
                    // h = res.height
                    that.init(n, res.width, res.height)
                },
                fail: function (res) {
                    // console.log(res)
                    wx.$api.showToast('生成失败')
                }
            })
        },
        async init(n, ImgWidth, ImgHeight) {
            const {findKey} = this.data
            // debugger
            // const {pixelRatio} = wx.getWindowInfo()
            let actualWidth = 247 //默认宽度
            let canvasHeight = 1959 //最大高度
            let proportion = ImgWidth / actualWidth //图片实例化对应的比例
            let actualHeight = ImgHeight / proportion //实际高度

            // 解决 createOffscreenCanvas 最大高度不能超过4096的问题
            // 这个 pixelRatio 不写是默认两倍 需转换计算
            if (actualHeight > canvasHeight) {
                actualWidth = actualWidth / (actualHeight / canvasHeight)
                actualHeight = canvasHeight
            }

            // console.log(actualHeight)

            // 默认母链海报 不是须单独处理
            this.setData({
                width: actualWidth + 32,
                height: actualHeight + 82
            })
            const drawfun = new Draw( // 大画布
                    {
                        // height: 554
                        width: actualWidth + 32,
                        height: actualHeight + 82
                        // pixelRatio
                    },
                    this
                ),


                // { userInfo } = wx.$storage.get(),
                userInfo = wx.$util.getUserInfo()
            let merchantName = userInfo.merchantName.length > 12 ? `${userInfo.merchantName.substring(0, 12)}...` : userInfo.merchantName

            // console.log(n.activityName)
            let imageList = [],
                textList = []
            // debugger

            if (n.type == 'warReport') {
                imageList = [
                    {
                        x: 0,
                        y: 0,
                        width: actualWidth + 32,
                        height: actualHeight + 82,
                        url: n.posterUrl
                    }
                ]
                textList = []
            } else {
                imageList = [
                    {
                        x: 0,
                        y: 0,
                        width: actualWidth + 32,
                        height: actualHeight + 82,
                        url: n.posterUrl
                    }
                ]
                textList = []
                // n.activityName = n.activityName.length > 12 ? `${n.activityName.substring(0, 12)}...` : n.activityName
                // imageList = [
                //     {
                //         x: 0,
                //         y: 0,
                //         width: 279,
                //         height: 66,
                //         url: 'https://img02.glsx.com.cn/weapp/resource/saas/business-assistant/output/poster-bg.png'
                //     },
                //     {
                //         x: 16,
                //         y: 70,
                //         width: actualWidth,
                //         height: actualHeight,
                //         url: n.posterUrl
                //     }
                // ]
                // textList = [
                //     {
                //         x: 12,
                //         y: 14,
                //         fontSize: 14,
                //         color: '#2D2D33',
                //         // text: merchantName,
                //         text: '',
                //         textBaseline: 'top'
                //     },
                //     {
                //         x: 12,
                //         y: 36,
                //         fontSize: 14,
                //         color: '#1B233D',
                //         // text: n.activityName,
                //         text: '',
                //         textBaseline: 'top',
                //         fontWeight: 500
                //     },
                //     {
                //         x: 14 + drawfun.getTextWidth(n.activityName, 14) + 9,
                //         y: 37,
                //         fontSize: 10,
                //         color: '#4466F0',
                //         text: '众筹',
                //         textBaseline: 'top',
                //         border: {
                //             width: 1,
                //             color: '#4466F0',
                //             radius: 2,
                //             padding: [2, 5],
                //             opacity: 0.5
                //         }
                //     }
                // ]
            }

            let res = await drawfun.begin({
                imageList,
                textList
            })
            // console.log(res)
            wx.hideLoading()
            this.setData({
                poster: res.path,
                show: true
            })
            let fileList = wx.$storage.get('fileList') || []
            // console.log([findKey])
            // this.setData({
            //   [findKey]: n[findKey]
            // })
            // 去重
            // debugger
            // let fileLength = fileList.length
            let obj = {
                fileId: res.fileId,
                path: res.path,
                [findKey]: n[findKey]
            }
            // console.log(fileLength)
            // if (fileLength) {
            //   fileList.forEach((item, index) => {
            //     // console.log(index)
            //     console.log(condition.posterUrl == n.posterUrl)
            //     if (condition.posterUrl == n.posterUrl) {
            //       item = obj
            //     } else if (index == fileLength) {
            //       fileList.unshift(obj)
            //     }
            //   })
            // } else {
            //   fileList.unshift(obj)
            // }

            fileList.unshift(obj)
            // console.log(fileList)
            // fileList = [...fileList, ...fileList]
            fileList = fileList.slice(0, 21)
            // console.log(fileList)
            wx.$storage.set('fileList', fileList)
        },
        download() {
            const filePath = this.data.poster,
                _this = this
            wx.saveImageToPhotosAlbum({
                filePath,
                success() {
                    wx.$api.showToast('保存成功')
                },
                fail(err) {
                    const auth = _this.selectComponent('#auth')
                    auth.check()
                }
            })
        },
        hide() {
            console.log('hide')
            this.setData({
                show: false,
                poster: ''
            })
        },
        noScroll() {
        },
        authFail(e) {
            this.setData({
                showAuth: true
            })
        },
        longpress() {
            let {item} = this.data
            this.triggerEvent('longtouch', item)
        }
    }
})