UNPKG

t-comm

Version:

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

56 lines (53 loc) 1.65 kB
import { TENCENT_DOC_URL_PREFIX, TENCENT_DOC_MP_APP_ID, TENCENT_DOC_MP_DETAIL_PATH } from './config.mjs'; /** * 判断是否为腾讯文档链接 * @param url 待判断的链接 * * @example * ```ts * isTencentDocLink('https://docs.qq.com/doc/xxx'); // true * isTencentDocLink('https://example.com'); // false * ``` */ function isTencentDocLink() { var url = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; return !!url && url.startsWith(TENCENT_DOC_URL_PREFIX); } /** * 打开腾讯文档链接 * * - H5 环境下:直接 `window.location.href` 跳转 * - 小程序环境下:跳转到腾讯文档小程序详情页 * * @param jumpUrl 腾讯文档链接(必须以 `https://docs.qq.com/doc` 开头) * @returns 是否为腾讯文档链接(仅当链接合法时执行跳转并返回 true) * * @example * ```ts * openTencentDocLink('https://docs.qq.com/doc/xxx'); * ``` */ function openTencentDocLink() { var jumpUrl = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ''; if (!isTencentDocLink(jumpUrl)) { return false; } // 小程序环境 if (typeof wx !== 'undefined' && typeof wx.navigateToMiniProgram === 'function') { wx.navigateToMiniProgram({ appId: TENCENT_DOC_MP_APP_ID, path: "".concat(TENCENT_DOC_MP_DETAIL_PATH, "?url=").concat(encodeURIComponent(jumpUrl)), success: function success() { // 打开成功 } }); return true; } // H5 环境 if (typeof window !== 'undefined') { window.location.href = jumpUrl; return true; } return false; } export { isTencentDocLink, openTencentDocLink };