HomeList.js 14 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
/**
 * Created by Cassie on 2018/05/14
 * 公共的图文列表页面
 */
import React, {Component} from "react";
import {ActivityIndicator, Image,DeviceEventEmitter,Text, FlatList, StyleSheet, View} from "react-native";
import ItemImageText from "../component/ItemImageText";
import {xnToast} from "../../utils/utils";
import AppService from "../../service/AppService";
import QuestionHomeListItem from "../questions/QuestionHomeListItem";
import AtlasItem from "../atlas/AtlasItem";
import {width, zoomH, zoomW} from "../../utils/getSize";
import EmptyView from '../component/EmptyView';
import ItemCourse from "../component/ItemCourse";
const defaultIcon = require("../../img/defaultIcon.png");

const pageSize = 20;
{
    var arr_keyValue;
}

export default class HomeList extends Component {
    constructor(props) {
        super(props);
        this.state = {
            data: [],
            showMoreText: [],
            loadMore: false,
            loading: false,
            totalCount:0,
            refreshing: false,
            showEmptyView:false,//是否显示暂无数据
            firstPageIds :[],//推荐第一页数据
        };

    }

    componentWillMount() {
        let arr_keyValue = this.props.arr_keyValue;
        for (var key in arr_keyValue) {
            var obj = arr_keyValue[key];
            for (var key1 in obj) {
                if (key1 == 'tabIndex'){
                    this.refreshAll = DeviceEventEmitter.addListener(obj[key1], () => {
                        this.setState({
                            data: [],
                            loadMore: false
                        }, () => {
                            this.getList();
                        });
                    });
                }
            }
        }
        this.getList();
    }

    componentDidMount() {
        let _this = this;
        //刷新列表 refreshThreadList
        this.refreshHomeList = DeviceEventEmitter.addListener(
            "refreshHomeList",
            function() {
                {
                    _this.setState(
                        {
                            data: [],
                            loadMore: false
                        },
                        function() {
                            _this.getList();
                        }
                    );
                }
            }
        );
        this.refreshThreadList = DeviceEventEmitter.addListener(
            "refreshThreadList",
            function() {
                _this.getList();
            }
        )
    }

    componentWillUnmount() {
        if (this.refreshHomeList != null) {
            this.refreshHomeList.remove();
        }
        if (this.refreshAll != null ){
            this.refreshAll.remove();
        }
    }

    //获取板块列表信息
    getList(from) {
        let that = this;
        if (!global.isConnected) {
            DeviceEventEmitter.emit('getTopicList',{});
            xnToast("暂无网络连接,请稍后重试!");
            return;
        }
        if (this.state.loading){
            return;
        }
        let length = this.state.data.length;
        let arr_keyValue = this.props.arr_keyValue;
        if (length % pageSize == 0 && !this.state.loadMore) {
            let params = {
                // threadType: this.props.threadType || 0,
                sourceFrom:'APP',
                tenantId: global.config.tenantId,
                forumId :global.forumId,
                searchType:"HAS_PUBLISH",
                // cityCode: this.props.cityCode,
                isActive: true,
                pageNumber: length / pageSize + 1,
                pageSize: pageSize
                // keyword:this.props.triData,
            };
            var isRecommend  =false;//是否是推荐
            // 添加前页面自定义的参数
            for (var key in arr_keyValue) {
                var obj = arr_keyValue[key];
                for (var key in obj) {
                    params[key] = obj[key];
                }
                if(key == 'isRecommend' && obj){
                    isRecommend = true;
                }
            }
            if (isRecommend && params.pageNumber >1 ){
                //推荐第二页的时候把第一页的id 传过去
                params["firstPageIds"] = JSON.stringify(this.state.firstPageIds);
            }
            this.setState({
                loadMore: true,
                loading:true
            });
            DeviceEventEmitter.emit('loadShow',{show:true});
            AppService.queryList(params)
                .then(data => {
                    that.setState({
                        loading: false,
                        loadMore: false,
                    });
                    console.log('getList',params,data);
                    DeviceEventEmitter.emit('loadShow',{show:false});
                    DeviceEventEmitter.emit('getTopicList',{});
                    console.log(data);
                    if (data.message) {
                        xnToast(data.message);
                        return;
                    }
                    if (data.errors.length > 0) {
                        xnToast(data.errors[0].message);
                    } else {
                        if (length + data.result.length > data.totalCount) {
                            return;
                        }
                        if(isRecommend && params.pageNumber ==1){
                            var ids = [] ;
                            for(let i = 0;i< data.result.length;i++){
                                ids.push(data.result[i].id);
                            }
                            that.setState({
                                firstPageIds:ids
                            });

                        }
                        that.setState({
                            data: that.state.data.concat(data.result),
                            totalCount:data.totalCount,
                            showEmptyView:data.totalCount>0 ? false:true,
                        });
                    }
                })
                .catch(error => {
                    that.setState({
                        loading: false,
                        loadMore: false,
                    });
                    xnToast(error);
                });
        }
    }

    //举报后数据处理
    reportData(item,index){
        try{
        item.isReported = true;
        let data = this.state.data;
        //所有改用户的帖子都删除
        let userId = item.threadUserId;
        for (let i=0; i< data.length;i++){
            if (userId == data[i].threadUserId){
                data[i].isReported = true;
            }
        }
        global.lastReportId = userId;
        this.setState({
            data:data
        })
        //接口请求拉黑用户
        if (!global.isConnected) {
            xnToast("暂无网络连接,请稍后重试");
            return;
        }

        let params = {
            forumId: global.forumId,
            type: 1,// 操作类型(0:用户拉黑 1:系统拉黑)
            targetUserId: item.threadUserId,//拉黑对象用户ID
            userId: global.userId,
            isActive: true
        };
        AppService.userBlockListRequest(params)
            .then(data => {
                console.log(params,data);
                if (data.message) {
                    xnToast(data.message);
                    return;
                }
                if (data.errors.length > 0) {
                    xnToast(data.errors[0].message);
                    return;
                }
            }).catch(error => {
            xnToast(error);
        });
        }catch (e){
        }

    }
    //渲染item
    keyExtractor = (item, index) => index;
    renderItem({ item, index }) {
        //先判断是否被举报
        let isReported = item.isReported || false;
        if (isReported){
            return null;
        }
        if(global.lastReportId  == item.threadUserId){
            return null;
        }
        //是否是推荐板块的帖子
        let isRecommend = this.props.isRecommend || false;
        //帖子类型
        switch (item.threadType) {
            case 0: //图文
                return <ItemImageText
                    itemList={item}
                    nav={this.props.navigation}
                    showLocation = {true}
                    showType = {true}
                    hideTop = {isRecommend}//隐藏置顶标签
                    hideTime = {isRecommend}//隐藏时间
                    showReport = {true}//显示举报按钮
                    reportCallback = {() =>{//举报
                        this.reportData(item,index);
                    }}
                />;
                break;
            case 1: //图集
                return <AtlasItem
                    item={item}
                    nav={this.props.navigation}
                    showType = {true}
                    hideTop = {isRecommend}//隐藏置顶标签
                    hideTime = {isRecommend}//隐藏时间
                    showReport = {true}//显示举报按钮
                    reportCallback = {() =>{//举报
                        this.reportData(item,index);
                    }}
                />;
                break;
            case 2: //视频
                return (
                    <ItemImageText
                        itemList={item}
                        nav={this.props.navigation}
                        isVideo={true}
                        showType = {true}
                        hideTop = {isRecommend}//隐藏置顶标签
                        hideTime = {isRecommend}//隐藏时间
                        showReport = {true}//显示举报按钮
                        reportCallback = {() =>{//举报
                            this.reportData(item,index);
                        }}
                    />
                );
                break;
            case 3: //提问
                return <QuestionHomeListItem
                    item={item}
                    nav={this.props.navigation}
                    showType = {true}
                    hideTop = {isRecommend}//隐藏置顶标签
                    hideTime = {isRecommend}//隐藏时间
                    showReport = {true}//显示举报按钮
                    reportCallback = {() =>{//举报
                        this.reportData(item,index);
                    }}
                />;
                break;
            case 4: //文章
                return <ItemImageText
                    itemList={item}
                    nav={this.props.navigation}
                    showType = {true}
                    hideTop = {isRecommend}//隐藏置顶标签
                    hideTime = {isRecommend}//隐藏时间
                    showReport = {true}//显示举报按钮
                    reportCallback = {() =>{//举报
                        this.reportData(item,index);
                    }}
                />;
                break;
            case 5: //课程
                return <ItemCourse
                    item={item}
                    nav={this.props.navigation}
                    showType = {true}
                    hideTop = {isRecommend}//隐藏置顶标签
                    hideTime = {isRecommend}//隐藏时间
                    showReport = {true}//显示举报按钮
                    reportCallback = {() =>{//举报
                        this.reportData(item,index);
                    }}
                />;
                break;
            default:
                //其他
                return <ItemImageText
                    itemList={item}
                    nav={this.props.navigation}
                    showType = {true}
                    hideTop = {isRecommend}//隐藏置顶标签
                    hideTime = {isRecommend}//隐藏时间
                    showReport = {true}//显示举报按钮
                    reportCallback = {() =>{//举报
                        this.reportData(item,index);
                    }}
                />;
                break;
        }
    }

    _separator ({ item, index })  {
        // //先判断是否被举报
        // let isReported = item.isReported || false;
        // if (isReported){
        //     return null;
        // }
        return <View style={{ height: 6/zoomH, backgroundColor: "#F3F5F6" }} />;
    };

    render() {
        return (
            <View style={styles.background}>
                {this.state.data && (
                    <FlatList
                        style={{ flex: 1, display: "flex" }}
                        onEndReachedThreshold={0.01}
                        onEndReached={() => {
                            this.getList()
                        }}
                        keyExtractor={this.keyExtractor}
                        data={this.state.data}
                        renderItem={this.renderItem.bind(this)}
                        refreshing={this.state.refreshing}
                        onRefresh={() => {
                            let _this = this;
                            this.setState({
                                refreshing: true
                            }, () => {
                                const timer = setTimeout(() => {
                                    clearTimeout(timer);
                                    _this.setState({
                                        refreshing: false,
                                        data: []
                                    }, () => {
                                        _this.getList();
                                    });
                                }, 1000);
                            })}}
                    />
                )}
                {this.state.data.length > pageSize && this.state.loadMore && (
                    <ActivityIndicator />
                )}
                {this.state.showEmptyView &&
                <EmptyView />}


            </View>
        );
    }
}

const styles = StyleSheet.create({
    background: {
        flex: 1,
        backgroundColor: "#F3F5F6",
        display: "flex"
    },

});