// 获取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
};
}
【学习随记】片段
最新推荐文章于 2024-05-13 17:45:01 发布