Blame view

public/js/guang/info-common.js 6.91 KB
1 2 3 4 5 6
/**
 * 资讯相关API
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2015/10/10
 */
陈峰 authored
7
let $ = require('yoho-jquery'),
8
    Hammer = require('common/toy-hammer'),
9
    lazyLoad = require('yoho-jquery-lazyload'),
10
    Swiper = require('yoho-swiper2');
11
12 13
let tip = require('plugin/tip');
let loading = require('plugin/loading');
14
陈峰 authored
15 16
let $loadMoreInfo = $('#load-more-info');
let $loading = $(''),
17 18 19
    $noMore = $(''),
    $swiper = $('');
陈峰 authored
20 21
let searching = false;
let mySwiper = {};
22
陈峰 authored
23
let isLoading = false;
24 25 26 27 28 29 30 31 32 33

if ($loadMoreInfo.length > 0) {
    $loading = $loadMoreInfo.children('.loading');
    $noMore = $loadMoreInfo.children('.no-more');
}

// 获取url中的参数
function getUrlParam(name) {

    // 构造一个含有目标参数的正则表达式对象
陈峰 authored
34
    let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
35 36

    // 匹配目标参数
陈峰 authored
37
    let r = window.location.search.substr(1).match(reg);
38 39

    // 返回参数值
陈峰 authored
40
    if (r !== null) {
41 42 43 44 45 46 47 48
        return r[2];
    }

    return null;
}

// 初始化swiper
function initSwiper(typeId) {
陈峰 authored
49
    if (!typeId) {
50 51
        return;
    }
52 53 54 55 56 57 58 59 60

    if (!mySwiper[typeId]) {
        mySwiper[typeId] = new Swiper('.swiper-cont-' + typeId, {
            lazyLoading: true,
            wrapperClass: 'swiper-wrap-' + typeId,
            pagination: '.swiper-pagi-' + typeId,
            autoplay: 3000
        });
    }
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
}

/**
 * 设置指定资讯项的Lazyload和文字截取
 * @params $infos 资讯项
 */

function setLazyLoadAndMellipsis($infos) {
    lazyLoad($infos.find('img.lazy'));
}

/**
 * 初始化资讯列表事件绑定
 * @params $container 逛资讯列表容器
 */
function initInfosEvt($container) {
陈峰 authored
77
    let cHammer;
78 79 80 81 82 83 84 85 86 87 88

    if (typeof $container === 'undefined') {
        return;
    }
    if (typeof $container[0] === 'undefined') {
        return;
    }
    cHammer = new Hammer($container[0]);

    // 点赞或者收藏事件
    cHammer.on('tap', function(e) {
陈峰 authored
89
        let $this = $(e.target),
90 91 92 93 94
            opt = 'ok',
            $btn,
            $info,
            yhChannel;
郝肖肖 authored
95
        e.preventDefault && e.preventDefault();
96 97 98 99

        // 点赞
        $btn = $this.closest('.like-btn');
        if ($btn.length > 0 && !isLoading) {
郝肖肖 authored
100
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
            if ($btn.hasClass('like')) {
                opt = 'cancel';
            }

            $info = $this.closest('.guang-info');

            isLoading = true;

            $.ajax({
                type: 'POST',
                url: '/guang/opt/praiseArticle',
                data: {
                    id: $info.data('id'),
                    opt: opt
                },
                success: function(data) {
陈峰 authored
117
                    let code = data.code;
118 119 120


                    if (code === 200) {
张孝茹 authored
121
                        $btn.prev('.like-count').text(data.data);
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

                        // 切换点赞状态
                        $btn.toggleClass('like');
                    }
                },
                error: function() {
                    tip.show('网络断开连接了~');
                },
                complete: function() {
                    isLoading = false;
                }
            });
            return;
        }

        // APP收藏
        $btn = $this.closest('.collect-btn');
        if ($btn.length > 0) {
陈峰 authored
140
            let uid = getUrlParam('uid');
ccbikai(👎🏻🍜) authored
141
沈志敏 authored
142 143 144 145
            if (uid) {
                if ($btn.hasClass('collected')) {
                    opt = 'cancel';
                }
146
沈志敏 authored
147
                $info = $this.closest('.guang-info');
148
沈志敏 authored
149 150 151
                if (getUrlParam('yh_channel')) {
                    yhChannel = getUrlParam('yh_channel');
                }
152
沈志敏 authored
153 154
                $.ajax({
                    type: 'POST',
155
                    url: location.protocol + '//m.yohobuy.com/guang/opt/collectArticle',
沈志敏 authored
156 157 158 159 160 161
                    data: {
                        id: $info.data('id'),
                        opt: opt,
                        yh_channel: yhChannel,
                        uid: uid
                    },
162 163 164
                    xhrFields: {
                        withCredentials: true
                    },
沈志敏 authored
165 166 167 168 169 170 171 172 173 174 175
                    success: function(data) {
                        if (data.code && data.code === 200) {

                            // 切换收藏状态
                            $btn.toggleClass('collected');
                        } else {
                            tip.show(data.message);
                        }
                    },
                    error: function() {
                        tip.show('网络断开连接了~');
176
                    }
沈志敏 authored
177 178
                });
            }
179 180 181 182 183 184 185 186 187 188 189 190 191
        }
    });

    setLazyLoadAndMellipsis($container.find('.guang-info'));
}

/**
 * 资讯LoadMore
 * @param $container 资讯容器 jqyeryObject
 * @param opt 请求参数
 * @param url[可选], 扩展请求的url而不使用默认值
 */
function loadMore($container, opt, url) {
陈峰 authored
192
    let num;
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207

    if (searching) {
        return;
    }

    if (opt.end) {
        return;
    }

    if (opt.page === 1) {

        // 显示loading
        loading.showLoadingMask();
    }
208
    opt.app_version = window.queryString.app_version || '';
209
210 211 212 213 214 215 216
    num = $container.find('.guang-info').length;
    searching = true;
    $.ajax({
        type: 'GET',
        url: url ? url : '/guang/index/page', // 对于指定url的使用指定url(存在不同的控制器)
        data: opt,
        success: function(data) {
陈峰 authored
217
            let $newItems;
218
lijing authored
219
            if (data === '') {
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
                opt.end = true;
                searching = false;

                //
                $loading.addClass('hide');
                $noMore.removeClass('hide');

                return;
            }

            $container.append(data);

            $swiper = $container.find('.swiper-container');
            if ($swiper.length) {
                $swiper.addClass('swiper-cont-' + opt.type);
                $swiper.children('.swiper-wrapper').addClass('swiper-wrap-' + opt.type);
                $swiper.children('.swiper-pagination').addClass('swiper-pagi-' + opt.type);
                initSwiper(opt.type);
            }

            if (num > 0) {
                $newItems = $container.find('.guang-info:gt(' + (num - 1) + ')');
            } else {
                $newItems = $container.find('.guang-info');
            }

            setLazyLoadAndMellipsis($newItems);

            if (opt.page === 1) {
                loading.hideLoadingMask();
沈志敏 authored
251
                $loading.removeClass('hide'); // 显示空屏加载时hide的隐藏
252
沈志敏 authored
253
                window.rePosFooter(); // 插入内容后重新计算底部位置
254 255 256 257 258 259 260 261
            }

            opt.page++;

            searching = false;
            delete opt.isTab;
        },
        error: function() {
262
            console.log('error');
263 264 265 266 267 268 269 270 271 272 273 274
            tip.show('网络断开连接了~');
            searching = false;
            delete opt.isTab;
        }
    });
}


exports.mySwiper = mySwiper;
exports.initSwiper = initSwiper;
exports.initInfosEvt = initInfosEvt;
exports.setLazyLoadAndMellipsis = setLazyLoadAndMellipsis;
275
exports.loadMore = loadMore;