promotion.js 13.3 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 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
/* global wx */
// 1. 优惠券领取
// 2. YOHO币领取

import $ from 'jquery';
import utils from './utils';
import user from './user';
import cookies from './cookies';
import jsonp from './jsonp';
import captcha from './captcha';

const miniAppEntrance = '/pages/common/webback';

let _getMiniappLoginUrl = function(url) {
    if (url.indexOf('openby:yohobuy') > 0) {
        url = url.substring(0, url.indexOf('openby:yohobuy'));
    }
    let h5Url = `http://m.yohobuy.com/signin.html?openby:yohobuy={"action":"go.miniapp_login","params":{"h5back":"${encodeURIComponent(url)}"}}`;

    return `${miniAppEntrance}?url=${encodeURIComponent(h5Url)}`;
};

const gotoLogin = function() {
    if (window.__wxjs_environment === 'miniprogram' && typeof wx !== 'undefined' && wx.miniProgram) {
        wx.miniProgram.navigateTo({
            url: _getMiniappLoginUrl(location.href)
        });
        return;
    }

    if ($('#intimacy-link').length <= 0) {
        $('body').append('<a href=\'' + user.noLoginUrl() + '\' style="display:none;" id="intimacy-link"><span class="intimacy-link"></span></a>');
    }
    $('.intimacy-link').click();
};

let isSendCoin = false;
let _getCoin = function(data) {
    data.app = utils.queryString();
    data.app.uid = data.uid;

    if (isSendCoin) {
        return;
    }

    isSendCoin = true;
    jsonp({
        url: '//m.yohobuy.com/activity/coin/sendCoin?callback=?',
        timeout: 5000, // 5s
        data: data
    }).then(function(res) {
        isSendCoin = false;
        if (res.code === 200 && res.data) {
            if (res.data.code === 200) {
                utils.showCoinTip({
                    title: '恭喜您,成功领取有货币!',
                    content: '特殊情况下到账有延时<br>请耐心等待',
                    close: true,
                    coin: true,
                    img: res.data.popupImg
                });
            } else if (res.data.code === 501) {
                utils.showCoinTip({
                    title: '领取失败',
                    content: '哎呀,你来的有点早,活动还没开始呢<br/>稍后再来哦',
                    close: true,
                    img: res.data.popupImg
                });
            } else if (res.data.code === 502) {
                utils.showCoinTip({
                    title: '领取失败',
                    content: '抱歉,活动已结束了,下次要快哟~~',
                    close: true,
                    img: res.data.popupImg
                });
            } else if (res.data.code === 503) {
                utils.showCoinTip({
                    title: '已经领取',
                    content: '贪心会长胖,你已经领取过了啦~~',
                    close: true,
                    img: res.data.popupImg
                });
            } else if (res.data.code === 504) {
                utils.showCoinTip({
                    title: '领取失败',
                    content: '哎呀,你来晚了,有货币已经领完了,<br/>下次早点来哦',
                    close: true,
                    img: res.data.popupImg
                });
            } else if (res.data.msg) {
                utils.showCoinTip({
                    title: '领取失败',
                    content: '抱歉,系统错误,有货君正奋力解决中...请稍后再来',
                    close: true,
                    img: res.data.popupImg
                });
            }
        } else if (res.code === 401 && res.auth) {
            utils.showCoinTip({
                title: '领取失败',
                content: '登录信息失效,请重新登录',
                close: true,
                img: res.data && res.data.popupImg
            });
            setTimeout(() => {
                gotoLogin();
            }, 1000);
        } else if (res.message) {
            utils.showCoinTip({
                title: '领取失败',
                content: '抱歉,系统错误,有货君正奋力解决中...请稍后再来',
                close: true,
                img: res.data && res.data.popupImg
            });
        }
    }, function(){
        isSendCoin = false;
        utils.showCoinTip({
            title: '领取失败',
            content: '抱歉,系统错误,有货君正奋力解决中...请稍后再来',
            close: true
        });
    });
};

let isSendCoupon = false;
let _getCoupon = function(data){
    const qs = utils.queryString();

    data.app = {
        uid: qs.uid || cookies.cookie('app_uid'),
        app_version: qs.app_version || cookies.cookie('app_version'),
        client_type: qs.client_type || cookies.cookie('app_client_type'),
        session_key: qs.session_key || cookies.cookie('app_session_key')
    };

    if (isSendCoupon) {
        return;
    }

    let options = {
        bottom: '48%',
        paddingX: 20,
        paddingY: 20,
        radius: 6
    };

    isSendCoupon = true;
    jsonp({
        url: '//m.yohobuy.com/activity/feature/couponSend?callback=?',
        timeout: 5000, // 5s
        data: data
    }).then(function(res) {
        isSendCoupon = false;

        if (res.code === 200) {
            utils.toast('领取成功', options);
            let $img = $(`#${data.token}`);
            if($img) {
                let statusImgs = $img.data('status-imgs');
                if(statusImgs && statusImgs.received) {
                    $img.attr('src', statusImgs.received);
                }
            }
        } else if (res.code === 401) {
            if(res.auth) {
                utils.toast('登录信息失效,请重新登录', options);
                setTimeout(() => {
                    gotoLogin();
                }, 1000);
            }
            if(res.message === '优惠券已经领取') {
                utils.toast(res.message, options);
                let $img = $(`#${data.token}`);
                if($img) {
                    let statusImgs = $img.data('status-imgs');
                    if(statusImgs && statusImgs.received) {
                        $img.attr('src', statusImgs.received);
                    }
                }
            }
        } else if (res.message) {
            utils.toast(res.message, options);
        } else {
            utils.toast('领取失败', options);
        }
    }, function(){
        isSendCoupon = false;
        utils.toast('领取失败', options);
    });
};

let isCheckCoupon = false;
let _getCouponCheck = function(data) {
    const qs = utils.queryString();

    data.app = {
        uid: qs.uid || cookies.cookie('app_uid'),
        app_version: qs.app_version || cookies.cookie('app_version'),
        client_type: qs.client_type || cookies.cookie('app_client_type'),
        session_key: qs.session_key || cookies.cookie('app_session_key')
    };

    if (isCheckCoupon) {
        return;
    }

    isCheckCoupon = true;
    return jsonp({
        url: '//m.yohobuy.com/activity/feature/couponCheck?callback=?',
        timeout: 5000, // 5s
        data: data
    }).then(function(res) {
        isCheckCoupon = false;
        return res;
    }, function(){
        isSendCoupon = false;
        return {
            code: 400,
            message: '出错了~'
        };
    });
};

let _getCaptchaCoupon = function(data) {
    let options = {
        bottom: '48%',
        paddingX: 20,
        paddingY: 20,
        radius: 6
    };

    if (data.couponType === 'storeTrial') {
        _getCoupon({
            token: data.token,
            couponType: data.couponType,
            uid: data.uid,
            webSession: data.webSession
        });
        return;
    }

    _getCouponCheck(data).then(result => {
        if (result.code !== 200) {
            return utils.toast(result.message, options);
        }

        if (result.data.checkRequired) {
            let captchaInstance = captcha.getInstance((result) => {
                if (result.ret !== 0) {
                    return utils.toast('出错了~', options);
                }

                _getCoupon({
                    token: data.token,
                    couponType: data.couponType,
                    uid: data.uid,
                    ticket: result.ticket,
                    randstr: result.randstr,
                    webSession: data.webSession
                });
            });

            captchaInstance.show();
        } else {
            _getCoupon({
                token: data.token,
                couponType: data.couponType,
                uid: data.uid,
                webSession: data.webSession
            });
        }
    });
};


let isSendRedEnevlope = false;
let _getRedEnevlope = function(data){
    data.app = utils.queryString();

    data.app.uid = data.app.uid || cookies.cookie('app_uid');
    data.app.app_version = data.app.app_version || cookies.cookie('app_version');
    data.app.client_type = data.app.client_type || cookies.cookie('app_client_type');
    data.app.session_key = data.app.session_key || cookies.cookie('app_session_key');

    if (isSendRedEnevlope) {
        return;
    }

    isSendRedEnevlope = true;
    jsonp({
        url: '//m.yohobuy.com/activity/feature/redenvelope?callback=?',
        timeout: 5000, // 5s
        data: data
    }).then(function(res) {
        isSendRedEnevlope = false;
        if (res.code === 200 && res.data) {
            if (res.data.addMsg === '领取成功') {
                utils.showTip({
                    title: '领取成功',
                    content: `成功领取${res.data.amount}元红包!`,
                    close: true
                });
            } else {
                utils.showTip({
                    title: '已领取过',
                    content: '今日领取机会已用完',
                    close: true
                });
            }
        } else if (res.code === 401 && res.auth) {
            utils.showTip({
                title: '领取失败',
                content: '登录信息失效,请重新登录',
                close: false
            });
            setTimeout(() => {
                gotoLogin();
            }, 1000);
        } else if (res.message) {
            utils.showTip({
                title: '领取失败',
                content: res.message,
                close: false
            });
        } else {
            utils.showTip({
                title: '领取失败',
                content: '请刷新重新领取,若多次领取失败,<br>可能网络故障或活动已结束。',
                close: false
            });
        }
    }, function(){
        isSendRedEnevlope = false;
        utils.showTip({
            title: '领取失败<br>请刷新重新领取',
            content: '如多次领取失败,请联系客服人员<br>带来不便敬请谅解',
            close: false
        });
    });
};

let _initCoin = function(uid){
    if (uid) {
        let cointoken = cookies.cookie('yoho-coin-token');
        if (cointoken) {
            _getCoin({
                token: cointoken,
                uid: uid
            });

            cookies.setCookie('yoho-coin-token', '');
        }
    }

    $('body').on('click', '.yoho-coin', function() {
        let token = $(this).data('token');
        if (user.uid) {
            _getCoin({
                token: token,
                uid: user.uid
            });
        } else {
            cookies.setCookie('yoho-coin-token', token);

            gotoLogin();
            return;
        }
    });
};

let _initCoupon = function(uid) {
    if (uid) {
        let token = cookies.cookie('yoho-coupon-token');
        let couponType = cookies.cookie('yoho-coupon-type');

        if (token) {
            _getCaptchaCoupon({
                token,
                couponType,
                uid
            });

            cookies.setCookie('yoho-coupon-token', '');
            cookies.setCookie('yoho-coupon-type', '');
        }
    }

    $('body').on('click', '.yoho-conpon', function() {
        let token = $(this).data('token');
        let type = $(this).data('type');
        let webSession = +$(this).data('websession') > 0;

        if (user.uid || webSession) {
            _getCaptchaCoupon({
                token,
                couponType: type,
                uid: user.uid,
                webSession: webSession
            });
        } else {
            cookies.setCookie('yoho-coupon-token', token);
            cookies.setCookie('yoho-coupon-type', type);

            gotoLogin();
            return;
        }
    });
};

let _initRedEnvelope = function(uid) {
    if (uid) {
        let redevenlopetoken = cookies.cookie('yoho-redenvelope-token');
        if (redevenlopetoken) {
            _getRedEnevlope({
                token: redevenlopetoken,
                uid: uid
            });

            cookies.setCookie('yoho-redenvelope-token', '');
        }
    }

    $('body').on('click', '.yoho-redenvelope', function() {
        let token = $(this).data('token');
        if (user.uid) {
            _getRedEnevlope({
                token: token,
                uid: user.uid
            });
        } else {
            cookies.setCookie('yoho-redenvelope-token', token);

            gotoLogin();
            return;
        }
    });
};

export default {
    init(uid) {
        _initCoin(uid);  // 初始化有货币
        _initCoupon(uid); // 初始化优惠券
        _initRedEnvelope(uid); // 初始化红包
    }
};