import * as React from 'react' import { BaseComponent,handleComponentClickLog } from '../base-component' import { showMessage } from '../common/toast/toast'; import { get, post } from '../utils/request' import { callHandler, support } from 'nejsbridge/dist/bridge.lofter.es.js'; import { openAppLofter } from 'nw-app-lofter'; import { isLofter } from 'nw-detect'; import { Props, State, BlogInfo, DataInfo } from './type' import { onBlogInfoRequest } from '../utils/one-request' import { fetchBlogInfo } from './fetch' import { rem } from "../style/function.style"; import * as Styled from './index.style' import log from '../utils/lofter-log' export class Focus extends React.Component { static defaultProps = new Props() state = new State() getSelfBlogId = () => { onBlogInfoRequest.oneFetch() .then(res => { if (res.meta.status === 200) { this.setState({ selfBlogId: res.response.blogId }) } }).catch(err => { console.log('blog info err', err) }) } handleFollow = (blogId: number, item: DataInfo, index: number) => { // 这里只能从未关注到关注 if (!isLofter()) { openAppLofter({ path: 'webview', query: { url: location.href } }) return; } post('//www.lofter.com/spread/common/follow', { blogId: blogId }).then(res => { if (res.code === 200) { // 关注打点 log.capture(`cms-1`, { category: 'cms', action: 101, scene: 'cms', v: '1.0.0', followers: this.state.selfBlogId, creator: blogId }) handleComponentClickLog(this.props.instanceKey) // 关注点击打点 showMessage({ text: '关注成功' }); let temp: any temp = this.state.data; temp[index].follow = true; this.setState({ data: temp }) } else { if (res.code === 401) { callHandler('njb_login', () => { this.getSelfBlogId() this.getUserInfo(this.props.blogNames).then( (data) => { this.setState({ data: data }) } ) }) } else { showMessage({ text: res.msg }); } } }) } getUserInfo = async(blogNames: string) => { let res = await fetchBlogInfo( blogNames ) return res ? ( Array.isArray(res) ? res : [res] ) : [] } componentDidMount() { this.getSelfBlogId() this.getUserInfo(this.props.blogNames).then( (data) => { this.setState({ data: data }) } ) } componentDidUpdate(preProps: any) { if (this.props.blogNames !== preProps.blogNames) { if (this.props.blogNames.indexOf(',') > 0) { showMessage({ text: '不能出现中文逗号' }); } else { this.getUserInfo(this.props.blogNames).then( (data) => { data = data.filter((a: any) => a.blogInfo !== null) this.setState({ data: data }) } ) } } } openPage(blogName:string) { if (isLofter()) { window.location.href = `https://${blogName}.lofter.com/` } else { window.location.href = `https://www.lofter.com/front/blog/home-page/${blogName}` } } render() { const { style: oriStyle, buttonName, type, buttonColor, } = this.props const style = { ...oriStyle } console.log(style) const YellowIcon = () => { return ( ) }; const GreenIcon = () => { return ( ) }; const YellowL = () => { return ( ) } const computedLogo = (isAuth: boolean, isVerify: boolean, signAuth: boolean) => { // 权限signAuth>isVerify>isAuth if (signAuth) return if (isVerify) return if (isAuth) return if (!isVerify && !isAuth && !signAuth) return null; } // !传入 BaseComponent 不要包含 style! const baseComponentProps = { ...this.props, } delete baseComponentProps.style return (
{ this.state?.data?.length > 0 && this.state.data?.map((item: DataInfo, index: number) => ( this.openPage(item.blogInfo.blogName)}>
{item.blogInfo.blogNickName} { !item.follow &&
this.handleFollow(item.blogInfo.blogId, item, index)} > {(buttonName || '关注')}
} {item.follow &&
已关注
}
{ computedLogo(item.blogInfo.isAuth, item.blogInfo.isVerify, item.blogInfo.signAuth) }
)) }
) } }