t-comm
Version:
专业、稳定、纯粹的工具库
244 lines (221 loc) • 7.1 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
/*! js-cookie v3.0.5 | MIT */
/* eslint-disable no-var */
function assign (target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i];
for (var key in source) {
target[key] = source[key];
}
}
return target
}
/* eslint-enable no-var */
/* eslint-disable no-var */
var defaultConverter = {
read: function (value) {
if (value[0] === '"') {
value = value.slice(1, -1);
}
return value.replace(/(%[\dA-F]{2})+/gi, decodeURIComponent)
},
write: function (value) {
return encodeURIComponent(value).replace(
/%(2[346BF]|3[AC-F]|40|5[BDE]|60|7[BCD])/g,
decodeURIComponent
)
}
};
/* eslint-enable no-var */
/* eslint-disable no-var */
function init (converter, defaultAttributes) {
function set (name, value, attributes) {
if (typeof document === 'undefined') {
return
}
attributes = assign({}, defaultAttributes, attributes);
if (typeof attributes.expires === 'number') {
attributes.expires = new Date(Date.now() + attributes.expires * 864e5);
}
if (attributes.expires) {
attributes.expires = attributes.expires.toUTCString();
}
name = encodeURIComponent(name)
.replace(/%(2[346B]|5E|60|7C)/g, decodeURIComponent)
.replace(/[()]/g, escape);
var stringifiedAttributes = '';
for (var attributeName in attributes) {
if (!attributes[attributeName]) {
continue
}
stringifiedAttributes += '; ' + attributeName;
if (attributes[attributeName] === true) {
continue
}
// Considers RFC 6265 section 5.2:
// ...
// 3. If the remaining unparsed-attributes contains a %x3B (";")
// character:
// Consume the characters of the unparsed-attributes up to,
// not including, the first %x3B (";") character.
// ...
stringifiedAttributes += '=' + attributes[attributeName].split(';')[0];
}
return (document.cookie =
name + '=' + converter.write(value, name) + stringifiedAttributes)
}
function get (name) {
if (typeof document === 'undefined' || (arguments.length && !name)) {
return
}
// To prevent the for loop in the first place assign an empty array
// in case there are no cookies at all.
var cookies = document.cookie ? document.cookie.split('; ') : [];
var jar = {};
for (var i = 0; i < cookies.length; i++) {
var parts = cookies[i].split('=');
var value = parts.slice(1).join('=');
try {
var found = decodeURIComponent(parts[0]);
jar[found] = converter.read(value, found);
if (name === found) {
break
}
} catch (e) {}
}
return name ? jar[name] : jar
}
return Object.create(
{
set,
get,
remove: function (name, attributes) {
set(
name,
'',
assign({}, attributes, {
expires: -1
})
);
},
withAttributes: function (attributes) {
return init(this.converter, assign({}, this.attributes, attributes))
},
withConverter: function (converter) {
return init(assign({}, this.converter, converter), this.attributes)
}
},
{
attributes: { value: Object.freeze(defaultAttributes) },
converter: { value: Object.freeze(converter) }
}
)
}
var api = init(defaultConverter, { path: '/' });
// 1. 授权登录之后,前端可以拿到 INTLSDK 的 openid、token,
// 把这两个写入 cookie 中,cookie 名称分别为 tip_intl_openid、tip_intl_token。
// 2. url中携带 _ltype=tiploginintl&appid=xxx&channelid=xxx,调用后台1个接口换取登录态,
// 后台校验登录态通过之后,会在 cookie 中写入 session_id。
// 3. 后续调用接口不需要再携带_ltype=tiploginintl&appid=xxx&channelid=xxx,
// 只需要携带cookie中的session_id就可以了。
/**
* 通过 intl 登录
* @param {Options} options 参数
* @example
*
* ```ts
* function loginIntl() {
* const checkLoginAPI = res => new Promise((resolve, reject) => {
* getScheList({
* query: {
* ...INTL_CONFIG.extraQueryObject,
* appid: INTL_CONFIG.gameID,
* channelid: res.channel_info?.channelId,
* },
* }).then((res) => {
* resolve(res);
* })
* .catch((err) => {
* reject(err);
* });
* });
*
* return loginByIntl({
* cookieDomain: COOKIE_DOMAIN,
* env: INTL_CONFIG.env,
* gameID: INTL_CONFIG.gameID,
* appID: INTL_CONFIG.appID,
* webID: INTL_CONFIG.webID,
* checkLoginAPI,
* });
* }
*
* ```
*/
function loginByIntl(options) {
return loginIntl(options);
}
function handleUserInfo(res, options) {
// cookie.set('tip_intl_login_info', JSON.stringify(res), { expires: 7, domain: options.cookieDomain });
api.set('tip_intl_openid', res.openid, {
expires: 7,
domain: options.cookieDomain
});
api.set('tip_intl_token', res.token, {
expires: 7,
domain: options.cookieDomain
});
return new Promise(function (resolve, reject) {
options.checkLoginAPI(res).then(function (res) {
resolve(res);
})["catch"](function (err) {
reject(err);
});
});
}
function loginIntl(options) {
return new Promise(function (resolve, reject) {
var _a;
var pass = new PassFactory.Pass({
env: options.env,
gameID: options.gameID,
appID: options.appID,
webID: options.webID // WEB_ID in Player Network Console
});
var selector = (_a = options.loginDomSelector) !== null && _a !== void 0 ? _a : '#infinite-pass-component';
console.log('[pass]', pass);
// Call `start` to mount the Web widget to the specified DOM node
// After a successful login, the login result is returned in the `onLogin` and `onRegister` events.
pass.start(selector);
// You should listen to the events and obtain the user's authentication
// information when the user completes login or registration.
pass.on('onLogin', function (userInfo) {
// After the user fails to log in, the 'onLoginError event' will be triggered,
// and the game logic after the user successfully logs in, can be processed in the event callback
// For example, redirect to a specific page
console.log('[onLogin userInfo]', userInfo);
handleUserInfo(userInfo, options).then(function (res) {
resolve({
res: res,
pass: pass,
userInfo: userInfo
});
pass.hide();
})["catch"](function (err) {
reject(err);
});
});
pass.on('onRegister', function (userInfo) {
// Logic added after successful registration
// For example, redirect to a specific page
console.log('[onRegister userInfo]', userInfo);
handleUserInfo(userInfo, options).then(function (res) {
resolve(res);
})["catch"](function (err) {
reject(err);
});
});
});
}
exports.loginByIntl = loginByIntl;