cookie.js 718 Bytes
/**
 * 获取 UID
 * @param  {[object]} req
 * @return {[string]}
 */
'use strict';
const sign = require('./sign');

exports.getUid = (req) => {
    const cookie = req.cookies._UID;
    let _uid = 0;
    let cookieList;

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

    if (cookie) {
        cookieList = cookie.split('::');
        if (cookieList[1] && !isNaN(cookieList[1])) {
            _uid = cookieList[1];
        }
    }

    // 校验 cookie 的 uid 有没有被修改
    if (req.cookies._TOKEN !== sign.makeToken(_uid)) {
        _uid = 0;
    }

    return _uid;
};

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