index.xml¶
<view class="panel" bind:tap="goDetail">
<!-- 封面 -->
<image wx:if="{{panel.cover}}" class="panel__cover" src="{{panel.cover.image}}">
<view class="panel__tag {{panel.tagClass}}" wx:if="{{panel.tagClass}}">{{panel.tag}}</view>
</image>
<!-- 标题 -->
<view class="panel__title" wx:if="{{panel.title}}">{{panel.title}}</view>
<!-- footer -->
<view class="panel__footer" wx:if="{{panel.showFooter}}">
<image class="panel__footer-icon" wx:if="{{panel.user.icon}}" src="{{panel.user.icon}}"></image>
<text class="panel__footer-name" wx:if="{{panel.user.nickname}}">{{panel.user.nickname}}</text>
<text class="panel__footer-comment" wx:if="{{panel.commentCount}}">{{panel.commentCount}}评论</text>
</view>
</view>
index.js¶
import { getPanelData } from "./tool";
Component({
properties: {
item: Object,
},
attached() {
this.setData({
panel: getPanelData(this.data.item),
});
},
});
tool.js¶
import { CONFIG, DEFAULT } from "./CONFIG";
// 获取瀑布流item数据
export const getPanelData = (item) => {
const getConfigByTypeAndLayout = () => {
let config = CONFIG.find((i) => i.type == item.type);
if (config && config.layouts) {
config = config.layouts.find((i) => i.layout === item.layout_type);
}
return config || DEFAULT;
};
const getPanelDataByConfig = (
c,
_item = c.itemWrap ? c.itemWrap(item) : item
) => {
const panel = {};
Object.keys(c).forEach((key) => {
if (typeof c[key] === "function") {
panel[key] = c[key](_item);
} else {
panel[key] = c[key];
}
});
return panel;
};
// 根据item的类型、样式获取配置
const config = getConfigByTypeAndLayout(item);
// 根据配置获取瀑布流item信息
return getPanelDataByConfig(config);
};
CONFIG.js¶
import { Layout, NewsType, Redirect } from 'src/constant';
import { formatImage, formatUser } from './tool';
/**
* 配置项
* @param {String} title 标题
* @param {String} cover 封面
* @param {String} tag 标签
* @param {Object} user 用户信息
* @param {Boolean} showFooter 是否显示footer
* @param {Boolean} isAd 是否广告
* @param {Function} itemWrap 兼容接口数据函数,数据可能以ref_xxx下发,比如帖子:ref_post
* ......
*/
<!-- 默认配置项 -->
export const DEFAULT = {
title: ({ title = '' }) => title,
cover: ({ image_list = [], article_images = [] }) =>
formatImage(image_list[0]) || formatImage(article_images[0]),
showFooter: true,
user: ({ user, user_account, article_source_tx = '' }) =>
user
? formatUser(user)
: user_account
? formatUser(user_account)
: {
icon: '',
nickname: article_source_tx,
},
};
export const CONFIG = [
{
type: NewsType.NEWS,
...DEFAULT,
tag: '资讯',
tagClass: 'news',
},
{
type: NewsType.VIDEO,
...DEFAULT,
tag: '视频',
video: ({ video_url = '', tencent_vid = '', image_gif_list = [] }) => ({
hasVideo: true,
src: tencent_vid || video_url,
gifCover: formatImage(image_gif_list[0]),
}),
},
{
type: Redirect.EVAL_DETAIL,
layouts: [
{
layout: Layout.EVALUATION,
...DEFAULT,
tag: '口碑',
},
{
layout: Layout.PRAISE_COMPONENT,
...DEFAULT,
itemWrap: ({ ref_car_score = {} }) => ref_car_score,
cover: ({ images = [], recommend_images = [] }) =>
formatImage(images[0]) ||
formatImage(getCoverFromRecommendImages(recommend_images)),
},
],
},
......
];