6.17周六随写

class F {
            constructor() {

                // 获取新闻消息
                this.newsCb = null;    // 用户的回调
                this.aNews = [];    // 接口数据
                this.getNewsTimer = null;    // 获取新闻消息定时器
                this.intermissionShowNewsTimer = null;    // 轮播新闻消息定时器
                this.aNewsIndex = 0;    // 新闻索引
                this.option = {   
                    getNewstime: 3,  // 单位h
                    intermissionShowNewsTime: 3000,  //单位 ms,
                    num: 5,    // 取几条数据
                    url: 'https://news.xiaohulu.com/api.php?mod=js&bid=80'
                };
            }

            /**
             * [getNewsInfo description]
             * @param  {[Function]} newsCb [回调, err, res2个参数, err是错误信息, res是结果]
             * @param  {[Object]} option [description]
             * /*f.getNewsInfo(function(err, res) {
                    console.log(res);
                }, {   
                    num: 5,    // 取几条新闻轮播
                    getNewstime: 3,  // 单位h
                    intermissionShowNewsTime: 1000,  //单位 ms
                    url: 'https://news.xiaohulu.com/api.php?mod=js&bid=80'
                });

                setTimeout(()=>{
                    f.stop();
                }, 10000)
            */
            getNewsInfo(newsCb, option) {
                if (this._isFunction(newsCb)) this.newsCb = newsCb;
                this.option = Object.assign(this.option, option);
                let self = this,
                    url = this.option.url;
                let p = fetch(url).then((res)=> {
                    return res.text();
                }).then((res)=> {
                    self._getInfoFromStr(res);
                }).catch((err)=> {
                    self.newsCb(err);
                });

                return this;
            }

            stop() {
                clearInterval(this.getNewsTimer);
                clearInterval(this.intermissionShowNewsTimer);
                this.getNewsTimer = null;    // 获取新闻消息定时器
                this.intermissionShowNewsTimer = null;    // 轮播新闻消息定时器
            }

            _isFunction(fn) {
                return Object.prototype.toString.call(fn) === '[object Function]';
            }

            _getInfoFromStr(txt) {
                
                let patt1 = /<a([^>]*?)href=['"]([^'"]*?)['"]([^>]*?)>(.*?)<\/a>/ig;
                let res = txt.match(patt1);
                let hrefArr = this._getHrefFromA(res);
                let TitleArr = this._getContFromTxt(txt);
                let result = [];
                hrefArr.forEach(function(item, index) {
                    let obj = {};
                    obj.href = item,
                    obj.title = TitleArr[index];
                    result.push(obj);
                });
                this.aNews = result;
                // 获取数据后就开启3小时再次获取数据
                this._startUpGetNews();
                this._startShowNews();
                // 先搞起数据
                ++ self.aNewsIndex;
                self.newsCb && self.newsCb(null, self._changeData());
            }

            // 返回一个title数组
            _getContFromTxt(txt) {
                let patt1 = /(?:[^_]+(?=\.html))|[^>]+(?=<\/a>)/g;
                let res = txt.match(patt1);
                return res;
            }

            // 返回一个href数组
            _getHrefFromA(arr) {
                let result = [];
                result = arr.map(function(a, index) {
                    a = a || "<a href='https://news.xiaohulu.com/portal.php?mod=view&aid=589' title='主播进阶篇-敢想敢做,做一个“敢”的主播' target='_blank'>主播进阶篇-敢想敢做,做一个“敢”的主播</a>";
                    let href = "";
                    let hStart = "<a href=",
                        hEnd = " title=",
                        startIndex = a.indexOf(hStart),
                        entIndex = a.indexOf(hEnd);
                    if (startIndex !== -1 && entIndex != -1) {
                        href = a.substring(hStart.length + 1, entIndex - 1);
                    };
                    return href;
                });
                return result;
            }

            // 开启3小时获取一次新闻消息
            _startUpGetNews() {
                clearInterval(this.getNewsTimer);
                let h = this.option.getNewstime;
                let self = this;
                this.getNewsTimer = setInterval(()=>{
                    self.getInfo('', '');
                }, h*60*60*1000);
            }

            // 开启每几秒轮播一下新闻
            _startShowNews() {
                let self = this;
                clearInterval(this.intermissionShowNewsTimer);

                this.intermissionShowNewsTimer = setInterval(function() {
                    ++ self.aNewsIndex;
                    self.newsCb && self.newsCb(null, self._changeData());
                }, this.option.intermissionShowNewsTime);
            }

            // 换数据
            _changeData() {
                // let length = this.aNews.length - 1;
                let length = this.option.num - 1;
                if (this.aNewsIndex > length) {
                    this.aNewsIndex = 0;
                };
                return this.aNews[this.aNewsIndex];
            }
        } 

        let f = new F();
        f.getNewsInfo(function(err, res) {
            console.log(res);
        }, {   
            getNewstime: 3,  // 单位h
            num: 2,
            intermissionShowNewsTime: 1000,  //单位 ms
            url: 'https://news.xiaohulu.com/api.php?mod=js&bid=80'
        });

        setTimeout(()=>{
            f.stop();
        }, 10000)

 

posted @ 2017-06-18 15:58  Sorrow.X  阅读(202)  评论(0编辑  收藏  举报