|
|
/**
|
|
|
* Created by qiujun on 2019/7/19.
|
|
|
*/
|
|
|
/* global wx */
|
|
|
let enviroment = '';
|
|
|
|
|
|
let parseUrl = function(url) {
|
|
|
let query = {},
|
|
|
hashs,
|
|
|
hash,
|
|
|
i;
|
|
|
|
|
|
url = (url || '').split('?');
|
|
|
hashs = (url[1] || '').split('&');
|
|
|
|
|
|
if (hashs && hashs.length) {
|
|
|
for (i = 0; i < hashs.length; i++) {
|
|
|
hash = hashs[i].split('=');
|
|
|
query[hash[0]] = hash[1];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return {
|
|
|
path: url[0],
|
|
|
query: query
|
|
|
};
|
|
|
};
|
|
|
|
|
|
let createLinkButton = function(url, id) {
|
|
|
let a = document.createElement('a');
|
|
|
|
|
|
a.style.position = 'fixed';
|
|
|
a.style.top = 0;
|
|
|
a.style.left = 0;
|
|
|
a.style.border = 'none';
|
|
|
a.style.outline = 'none';
|
|
|
a.style.resize = 'none';
|
|
|
a.style.background = 'transparent';
|
|
|
a.style.color = 'transparent';
|
|
|
a.setAttribute('id', id);
|
|
|
a.setAttribute('href', url);
|
|
|
document.body.appendChild(a);
|
|
|
return a;
|
|
|
};
|
|
|
|
|
|
let env = function() {
|
|
|
let envFlag = window.__wxjs_environment;
|
|
|
|
|
|
if (!envFlag && navigator.userAgent.match(/yohobuy/i)) {
|
|
|
enviroment = 'app';
|
|
|
document.addEventListener('deviceready', function() {
|
|
|
});
|
|
|
} else if ((!envFlag && navigator.userAgent.match(/miniProgram/i)) || (envFlag === 'miniprogram')) {
|
|
|
enviroment = 'miniprogram';
|
|
|
} else if (location.origin === 'https://www.yohobuy.com') {
|
|
|
enviroment = 'pc';
|
|
|
} else {
|
|
|
enviroment = 'h5';
|
|
|
}
|
|
|
return enviroment;
|
|
|
};
|
|
|
|
|
|
let loginUrl = function() {
|
|
|
let url, refer = location.href;
|
|
|
|
|
|
if (env === 'app') {
|
|
|
url = 'http://m.yohobuy.com/signin.html?refer=' + encodeURIComponent(refer);
|
|
|
refer = parseUrl(refer);
|
|
|
if (/Android/i.test(navigator.userAgent || '')) {
|
|
|
if (url.indexOf('?') < 0) {
|
|
|
url += '?appLogin=1';
|
|
|
}
|
|
|
if (location.href.indexOf('&openby') >= 0) {
|
|
|
url = url.substring(0, url.indexOf('&openby'));
|
|
|
}
|
|
|
url += '&';
|
|
|
} else {
|
|
|
if (location.href.indexOf('#openby') >= 0) {
|
|
|
url = url.substring(0, url.indexOf('#openby'));
|
|
|
}
|
|
|
url += '#';
|
|
|
}
|
|
|
url += 'openby:yohobuy=' + JSON.stringify({
|
|
|
action: 'go.weblogin',
|
|
|
params: {
|
|
|
priority: 'N',
|
|
|
jumpurl: {
|
|
|
url: refer.path,
|
|
|
param: refer.query
|
|
|
},
|
|
|
needlogout: 'Y'
|
|
|
}
|
|
|
});
|
|
|
} else if (env === 'pc') {
|
|
|
url = 'https://www.yoho/signin.html?refer=' + encodeURIComponent(location.href);
|
|
|
} else if (env === 'miniprogram') {
|
|
|
url = '/pages/userCenter/userCenter';
|
|
|
} else {
|
|
|
if (refer.indexOf('?') < 0) {
|
|
|
refer += '?skcall=1';
|
|
|
} else {
|
|
|
refer += '&skcall=1';
|
|
|
}
|
|
|
url = 'http://m.yohobuy.com/signin.html?refer=' + encodeURIComponent(refer);
|
|
|
}
|
|
|
return url;
|
|
|
};
|
|
|
|
|
|
let Login = function(obj) {
|
|
|
let _this = this;
|
|
|
|
|
|
_this.env = obj.env;
|
|
|
_this.url = obj.loginUrl;
|
|
|
|
|
|
_this.goLogin = function(url) {
|
|
|
url = url || _this.url;
|
|
|
|
|
|
if (_this.env === 'miniprogram') {
|
|
|
wx.miniProgram.switchTab({url: url});
|
|
|
} else if (_this.env === 'app') {
|
|
|
let loginButton = document.getElementById('yosdk-login');
|
|
|
|
|
|
if (!loginButton) {
|
|
|
loginButton = createLinkButton(url, 'yosdk-login');
|
|
|
}
|
|
|
loginButton.click();
|
|
|
} else {
|
|
|
window.location.href = url;
|
|
|
}
|
|
|
return false;
|
|
|
};
|
|
|
|
|
|
_this.auth = function(nodeList, user) {
|
|
|
if (!user.uid) {
|
|
|
[].forEach.call(nodeList, function(value) {
|
|
|
value.onclick = function(event) {
|
|
|
event.preventDefault();
|
|
|
_this.goLogin();
|
|
|
};
|
|
|
});
|
|
|
return true;
|
|
|
}
|
|
|
};
|
|
|
};
|
|
|
|
|
|
let login = new Login({loginUrl: loginUrl(), env: env()});
|
|
|
|
|
|
module.exports = {
|
|
|
goLogin: login.goLogin.bind(login)
|
|
|
}; |
...
|
...
|
|