Blame view

library/cookie.js 718 Bytes
1 2 3 4 5
/**
 * 获取 UID
 * @param  {[object]} req
 * @return {[string]}
 */
姜枫 authored
6
'use strict';
ccbikai authored
7
const sign = require('./sign');
姜枫 authored
8
9
exports.getUid = (req) => {
ccbikai authored
10 11 12
    const cookie = req.cookies._UID;
    let _uid = 0;
    let cookieList;
13 14 15 16 17 18 19 20 21 22 23 24

    if (req.isApp) {
        return req.query.uid || 0;
    }

    if (cookie) {
        cookieList = cookie.split('::');
        if (cookieList[1] && !isNaN(cookieList[1])) {
            _uid = cookieList[1];
        }
    }
ccbikai authored
25 26 27 28 29
    // 校验 cookie 的 uid 有没有被修改
    if (req.cookies._TOKEN !== sign.makeToken(_uid)) {
        _uid = 0;
    }
30 31
    return _uid;
};
姜枫 authored
32 33 34

exports.getShoppingKey = (req) => {
    return req.cookies['_SPK'] ? req.cookies['_SPK'] : ''; // eslint-disable-line
姜枫 authored
35
};