t-comm
Version:
专业、稳定、纯粹的工具库
84 lines (81 loc) • 3.04 kB
JavaScript
import _regeneratorRuntime from '@babel/runtime/regenerator';
import { _ as __awaiter } from '../tslib.es6-848120af.js';
import { getGameletPixuiFrame } from './gamelet.mjs';
/**
* 是否是本地调试
*/
function checkIsDebug() {
var _a, _b, _c, _d;
var isDebug = 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 __awaiter(void 0, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
var mockLoginParams, userInfo, appId, accToken, accType, openId;
return _regeneratorRuntime.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 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);
}));
};
export { getLoginUrlInPixui, loginInPixui };