UNPKG

t-comm

Version:

专业、稳定、纯粹的工具库

136 lines (131 loc) 4.91 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var eBus_init = require('../../e-bus/init.js'); var url_helper = require('../../url/helper.js'); require('../../e-bus/e-bus.js'); require('@babel/runtime/helpers/classCallCheck'); require('@babel/runtime/helpers/createClass'); require('@babel/runtime/helpers/slicedToArray'); function getHeaderIgnoreCase(header, name) { if (!header) { return undefined; } var lowerName = name.toLowerCase(); var key = Object.keys(header).find(function (item) { return item.toLowerCase() === lowerName; }); return key ? header[key] : undefined; } function getLoginCode() { var g = typeof globalThis !== 'undefined' ? globalThis : {}; var wxObj = g.wx; if (!wxObj || typeof wxObj.login !== 'function') { return Promise.resolve({ code: '' }); } return new Promise(function (resolve, reject) { wxObj.login({ success: function success(r) { return resolve({ code: (r === null || r === void 0 ? void 0 : r.code) || '' }); }, fail: reject }); }); } /** * 通过 code 换取小程序登录态。 * * code 来源优先级(高 → 低): * 1. 直接传入的 `code` 参数(如 QQ App 下 `plugin.login()` 拿到的 QQ code) * 2. 自定义 `getCode` 函数(业务可注入任意 code 提供者) * 3. 默认调用 `wx.login()` 拿微信 code * * 配合 `_ltype` 即可同时支持微信登录(`tiploginwxproc`)和 QQ 登录(`tiploginqqproc`)。 * * @example 微信登录(默认行为,向后兼容) * loginMp({ url, appid }); * * @example QQ App 下用 plugin.login() 拿到的 code 走 QQ 登录 * import { qqPluginLogin } from 't-comm/es/qq-mp'; * const { code } = await qqPluginLogin(); * loginMp({ url, appid, _ltype: 'tiploginqqproc', code }); * * @example 注入自定义 code 提供者 * loginMp({ url, appid, _ltype: 'tiploginqqproc', getCode: qqPluginLogin }); */ function loginMp(_ref) { var url = _ref.url, _ref$method = _ref.method, method = _ref$method === void 0 ? 'POST' : _ref$method, _ref$header = _ref.header, header = _ref$header === void 0 ? '' : _ref$header, _ref$body = _ref.body, body = _ref$body === void 0 ? {} : _ref$body, _ref$appid = _ref.appid, appid = _ref$appid === void 0 ? '' : _ref$appid, _ref$_ltype = _ref._ltype, _ltype = _ref$_ltype === void 0 ? 'tiploginwxproc' : _ref$_ltype, _ref$storageKey = _ref.storageKey, storageKey = _ref$storageKey === void 0 ? 'cocos_login_info' : _ref$storageKey, onLoginInfo = _ref.onLoginInfo, storage = _ref.storage, code = _ref.code, getCode = _ref.getCode, qqTicketInfo = _ref.qqTicketInfo; return new Promise(function (resolve, reject) { var codePromise = typeof code === 'string' ? Promise.resolve({ code: code }) : typeof getCode === 'function' ? getCode() : getLoginCode(); codePromise.then(function (loginCode) { var effectiveUrl = url_helper.addUrlParams(url, { appid: appid, _ltype: _ltype, code: loginCode.code }); if (_ltype === 'tiploginqqproc' && qqTicketInfo) { effectiveUrl = url_helper.addUrlParams(url, { openid: "".concat(qqTicketInfo.qqOpenid), appid: "".concat(qqTicketInfo.qqAppid), access_token: "".concat(qqTicketInfo.qqAccessToken), acctype: 'qq' }); } wx.request({ url: effectiveUrl, method: method, header: header, data: body, dataType: 'json', success: function success(res) { var _a; var loginInfo = getHeaderIgnoreCase(res.header, 'logininfo'); console.log('[loginMp] loginInfo', loginInfo); if (loginInfo) { var loginInfoObj = {}; try { loginInfoObj = JSON.parse(loginInfo); } catch (err) {} console.log('[loginMp] loginInfoObj', loginInfoObj); if (loginInfoObj === null || loginInfoObj === void 0 ? void 0 : loginInfoObj.sessionId) { eBus_init.globalEBus === null || eBus_init.globalEBus === void 0 ? void 0 : eBus_init.globalEBus.emit('event-on-cocos-login'); onLoginInfo === null || onLoginInfo === void 0 ? void 0 : onLoginInfo(loginInfoObj); (_a = storage === null || storage === void 0 ? void 0 : storage.set) === null || _a === void 0 ? void 0 : _a.call(storage, storageKey, loginInfo); resolve(); return; } } resolve(); }, fail: function fail(err) { console.log('[loginMp] err: ', err); reject(new Error((err === null || err === void 0 ? void 0 : err.errMsg) || '[loginMp] wx.request failed')); } }); }, reject); }); } exports.getLoginCode = getLoginCode; exports.loginMp = loginMp;