UNPKG

miniapp-web-jsapi

Version:

JSAPI/View adapter for miniprogram running on the web

83 lines 3.22 kB
/// <reference types="mini-types/types/api/open/getLaunchOptionsSync" /> import JsUtils from "../../../utils/js-utils"; import { buildNavigateUrl } from "../.."; import { getAppId, getAppName, getSiteName, isLocalhost } from "../../utils/web-utils"; export function navigateToMiniProgram(options) { var success = options.success, fail = options.fail, complete = options.complete; window.location.href = buildNavigateUrl(options); success === null || success === void 0 ? void 0 : success({}); complete === null || complete === void 0 ? void 0 : complete({}); } /** * 构建 H5 端小程序跳转链接. 如: * * http://page.alipay.net/page/aplus-miniapps/2102000024800001/index.html?site=gcash&app=gcash * http://page.alipay.net/page/aplus-miniapps/2102000024800001/index.html?site=gcash&app=gcash#/pages/main/main * http://page.alipay.net/page/aplus-miniapps/2102000024800001/index.html??site=gcash&app=gcash&a=b&query=encoded(k=v)#/pages/main/main */ export function defaultNavigateToAppUrlBuilder(options) { var appId = options.appId, path = options.path, extraData = options.extraData, parameters = options.parameters; var location = window.location; var siteName = getSiteName(); var appName = getAppName(); var url; if (isLocalhost(location.host)) { url = "".concat(location.origin, "?site=").concat(siteName, "&app=").concat(appName); } else { url = "".concat(location.origin, "/page/aplus-miniapps/").concat(options.appId, "/index.html?site=").concat(siteName, "&app=").concat(appName); } var parametersStr = JsUtils.encodeMap(parameters); if (parametersStr !== null && parametersStr !== void 0 && parametersStr.length) { // k=v url += "&".concat(parametersStr); } var extraDataStr = JsUtils.encodeMap(extraData); if (extraDataStr !== null && extraDataStr !== void 0 && extraDataStr.length) { // query=encoded(k=v) url += "&query=".concat(encodeURIComponent(extraDataStr)); } var currentAppId = getAppId(); var referrerInfo; if (appId !== currentAppId) { // 填入来源 AppId referrerInfo = { appId: currentAppId || '' }; } // referrerInfo var referrerInfoStr = JsUtils.encodeMap(referrerInfo); if (referrerInfoStr !== null && referrerInfoStr !== void 0 && referrerInfoStr.length) { url += "&referrerInfo=".concat(encodeURIComponent(referrerInfoStr)); } if (path) { url += "#".concat(path); } return url; } export function navigateTo(options) { var success = options.success, fail = options.fail, complete = options.complete; window.location.href = options.url; success === null || success === void 0 ? void 0 : success(); complete === null || complete === void 0 ? void 0 : complete(); } export function redirectTo(options) { var success = options.success, fail = options.fail, complete = options.complete; window.location.replace(options.url); success === null || success === void 0 ? void 0 : success(); complete === null || complete === void 0 ? void 0 : complete(); } export function navigateBack(options) { var _ref = options || {}, _ref$delta = _ref.delta, delta = _ref$delta === void 0 ? 1 : _ref$delta; history.go(-delta); }