t-comm
Version:
专业、稳定、纯粹的工具库
93 lines (86 loc) • 3.44 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var _regeneratorRuntime = require('@babel/runtime/regenerator');
var tslib_es6 = require('../tslib.es6-0d92ef81.js');
var pixui_gamelet = require('./gamelet.js');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var _regeneratorRuntime__default = /*#__PURE__*/_interopDefaultLegacy(_regeneratorRuntime);
/**
* 是否是本地调试
*/
function checkIsDebug() {
var _a, _b, _c, _d;
var isDebug = pixui_gamelet.getGameletPixuiFrame().GameletAPI.getRuntimeEnv() === 'PxIDE' || ((_b = (_a = window === null || window === void 0 ? void 0 : window.location) === null || _a === void 0 ? void 0 : _a.host) === null || _b === void 0 ? void 0 : _b.indexOf('127.0.0.1')) > -1 || ((_d = (_c = window === null || window === void 0 ? void 0 : window.location) === null || _c === void 0 ? void 0 : _c.host) === null || _d === void 0 ? void 0 : _d.indexOf('localhost')) > -1;
return isDebug;
}
var loginParams = '';
function getUrl(_ref) {
var openId = _ref.openId,
_ref$accType = _ref.accType,
accType = _ref$accType === void 0 ? 'wx' : _ref$accType,
accToken = _ref.accToken,
appId = _ref.appId;
var obj = {
appid: appId,
openid: openId,
access_token: accToken,
acctype: accType
};
return Object.keys(obj).map(function (item) {
return "".concat(item, "=").concat(obj[item]);
}).join('&');
}
function getLoginUrlInPixui(baseUrl) {
if (loginParams.length === 0) {
throw new Error('请先调用 login 方法,再进行网络请求');
}
var symbol = baseUrl.includes('?') ? '&' : '?';
return "".concat(baseUrl).concat(symbol).concat(loginParams);
}
/**
* 用户登录(初始化游戏accToken等字段)
* @example
* ```ts
* import { loginInPixui, getLoginUrlInPixui } from 't-comm/es/pixui';
*
* await loginInPixui();
*
* // 拼接登录态参数
* const url = getLoginUrlInPixui('https://api.example.com/foo');
* // => https://api.example.com/foo?appid=xxx&openid=xxx&access_token=xxx&acctype=wx
* ```
*/
var loginInPixui = function loginInPixui() {
return tslib_es6.__awaiter(void 0, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee() {
var mockLoginParams, userInfo, appId, accToken, accType, openId;
return _regeneratorRuntime__default["default"].wrap(function (_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
mockLoginParams = process.env.MOCK_LOGIN_PARAMS || '';
if (!(checkIsDebug() && mockLoginParams)) {
_context.next = 1;
break;
}
loginParams = mockLoginParams;
return _context.abrupt("return");
case 1:
_context.next = 2;
return pixui_gamelet.getGameletPixuiFrame().GameletAPI.getUserData();
case 2:
userInfo = _context.sent;
appId = userInfo.sAppId, accToken = userInfo.sAccessToken, accType = userInfo.sAcountType, openId = userInfo.sOpenId;
loginParams = getUrl({
openId: openId,
accType: accType,
accToken: accToken,
appId: appId
});
case 3:
case "end":
return _context.stop();
}
}, _callee);
}));
};
exports.getLoginUrlInPixui = getLoginUrlInPixui;
exports.loginInPixui = loginInPixui;