import * as React from 'react'; import { Props, State } from './type'; import { Container, Divider } from './index.style'; import { jsonp } from '../utils/request'; import { getLoginInstance } from 'snail-login' import { isSnail } from 'snail-helpers'; import { handlePathQuery } from '../utils/snailTargetHandler' export class SnailTopic extends React.Component { public static defaultProps = new Props(); public state = new State(); static getDerivedStateFromProps(props){ if(props.isEdit){ return { title: props.title } } } public componentDidUpdate(prevProps) { const { topicId } = this.props let { timerId } = this.state if(prevProps.topicId !== topicId) { if(timerId) { clearTimeout(timerId) } timerId = setTimeout(() => { this.getTopicData(topicId) }, 500) this.setState({ timerId }) } } public async componentDidMount() { const { topicId } = this.props if(topicId) { if(isSnail()) { const login = getLoginInstance() // 客户端内同步登陆获取话题投票信息 await login.isLogined() } this.getTopicData(topicId) } } public async getTopicData(topicId: string) { try { let res = await jsonp(`https://du.163.com/api/topic/detail.jsonp`, { topicId: topicId }) if(res.code === 0) { let totalCount = 0 if(res.topicWrapper.votable) { // 填充热门投票用户 res.topicWrapper.voteOptions = res.topicWrapper.voteOptions.map(item => { if(!item.hotVoteUsers.length) { item.hotVoteUsers = [null,null,null] } totalCount += item.count return item }) } this.setState({ topicData: res.topicWrapper, totalCount }) } else { this.setState({ tips: '话题id输入有误' }) } } catch { this.setState({ tips: '话题id输入有误' }) } } public clickHanler(topicId) { const { pos, isEdit } = this.props handlePathQuery({ path: 'topicDetail', query: { topicId }, logPointId: 'acttemplate1-7', logQuery: { category: 'acttemplate', topicId: topicId, pos }, isEdit }) } public render() { const { isEdit, style, banner, topicId, title } = this.props const { topicData, tips, totalCount } = this.state return ( { title ?

{title}

: null } { topicData ?
{ !topicData.votable && banner ? : null }
{topicData.topic.title}
{topicData.topic.feedCount}篇内容 {topicData.topic.followCount}人关注
{ topicData.votable ?
{ topicData.voteOptions.map((item, index) => { return ( {item.option.optionText} { topicData.votedOption && topicData.votedOption.option.optionId === item.option.optionId ? icon : null }
{ item.hotVoteUsers.map((user, index) => { return { user ? avatar : } }) }
{ item.count } 人支持
) }) }
{ topicData.votedOption ?
{ topicData.voteOptions.map((item, index) => { let percent = Math.floor((item.count / totalCount) * 100) return { percent >= 10 ? `${Math.round((item.count / totalCount) * 100)}%` : '' } }) }
:
选择观点后查看百分比
}
:
{topicData.topic.description}
}
: {tips} } { !tips && !title && !topicId && isEdit &&
蜗牛话题
} { topicData && }
) } }