prefers.vue 1.58 KB
<template>
    <prefer-list :title="title" :list="list"></prefer-list>
</template>

<script>
    const $ = require('jquery');
    const _ = require('lodash');
    const preferList = require('component/product/preferList.vue');

    module.exports = {
        data() {
            return {
                list: [],
                title: '你可能喜欢'
            };
        },
        components: {
            preferList
        },
        methods: {
            imgerror() {
                // 图片报错时,给个默认透明图片
                this.headIco = 'data:image/png;base64,R0lGODlhFAAUAIAAAP///wAAACH5BAEAAAAALAAAAAAUABQAAAIRhI+py+0Po5y02ouz3rz7rxUAOw==';
            },
            reload() {
                $.ajax({
                    url: '/me/preferlist'
                }).then(result => {
                    if (result[0]) {
                        let list = [];

                        for (let i in result) {
                            list = _.union(list, result[i].list);
                        }
                        for (let i = 0; i < list.length; i++) {
                            list[i].default_images = list[i].default_images.replace(/(\{width\}x\{\height})/g, '373x497');
                        }
                        this.list = list;
                    }

                });
            }
        },
        ready() {
            this.reload();

            document.addEventListener('visibilitychange', () => {
                if (!document.hidden) {
                    this.reload();
                }
            });
        }
    };

</script>