【学习随记】片段


// 获取URL中某个参数
function getParams(sName) {
    let reg = new RegExp("(^|&)" + sName + "=([^&]*)(&|$)", "i");
    let r = window.location.search.substr(1).match(reg);
    if (r != null)
        return decodeURIComponent(r[2]); // (r[2]);
    return "";
}

//设置、获取cookie
function Cookie(key, value, options) {
    if (arguments.length > 1 && (!/Object/.test(Object.prototype.toString.call(value)) || value === null || value === undefined)) {
        options = options || {};
        if (value === null || value === undefined) {
            options.expires = -1;
        }
        if (typeof options.expires === 'number') {
            let days = options.expires,
                t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }
        value = String(value);
        document.cookie = [encodeURIComponent(key), '=', options.raw ? value : encodeURIComponent(value), options.expires ? '; expires=' + options.expires.toUTCString() : '', options.path ? '; path=' + options.path : '', options.domain ? '; domain=' + options.domain : '', options.secure ? '; secure' : ''].join('');
        return;
    }
    options = value || {};
    let decode = options.raw ? function (s) {
        return s;
    } : decodeURIComponent;
    let pairs = document.cookie.split('; ');
    for (let i = 0, pair; pair = pairs[i] && pairs[i].split('='); i++) {
        if (decode(pair[0]) === key)
            return decode(pair[1] || '');
    }
}

// 获取微信授权以后返回的参数
function getURIParams() {
    let nickname = window.atob(decodeURIComponent(getParams("nickname"))) || Cookie('nickname') || '';
    let nonce = getParams('nonce') || Cookie('nonce') || '';
    let partnerid = getParams('partnerid') || Cookie('partnerid') || '';
    let wxname =  decodeURIComponent(getParams('wxname')) || Cookie('wxname') || '';
    let wxno = getParams('wxno') || Cookie('wxno') || '';
    let openid = getParams('openid') || Cookie('openid') || '';
    let timestamp = getParams('timestamp') || Cookie('timestamp') || '';
    let headimgurl = getParams('headimgurl') || Cookie('headimgurl') || '';
    let subscribe = getParams('subscribe') || Cookie('subscribe') || '';
    let signature = getParams('signature') || Cookie('signature') || '';
    let unionid = getParams('unionid') || Cookie('unionid') || '';

    if (nickname != null && nickname !== '') {
        Cookie('nickname', nickname, { path: "/" });
    }
    if (nonce != null && nonce !== '') {
        Cookie('nonce', nonce, { path: "/" });
    }
    if (partnerid != null && partnerid !== '') {
        Cookie('partnerid', partnerid, { path: "/" });
    }
    if (wxname != null && wxname !== '') {
        Cookie('wxname', wxname, { path: "/" });
    }
    if (wxno != null && wxno !== '') {
        Cookie('wxno', wxno, { path: "/" });
    }
    if (openid != null && openid !== '') {
        Cookie('openid', openid, { path: "/" });
    }
    if (timestamp != null && timestamp !== '') {
        Cookie('timestamp', timestamp, { path: "/" });
    }
    if (headimgurl != null && headimgurl !== '') {
        Cookie('headimgurl', headimgurl, { path: "/" });
    }
    if (subscribe != null && subscribe !== '') {
        Cookie('subscribe', subscribe, { path: "/" });
    }
    if (signature != null && signature !== '') {
        Cookie('signature', signature, { path: "/" });
    }
    if (unionid != null && unionid !== '') {
        Cookie('unionid', unionid, { path: "/" });
    }

    return {
        nickname: nickname,
        nonce: nonce,
        partnerid: partnerid,
        wxname: wxname,
        wxno: wxno,
        openid: openid,
        timestamp: timestamp,
        headimgurl: headimgurl,
        subscribe: subscribe,
        signature: signature,
        unionid: unionid
    };
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值