UNPKG

mtl-js-sdk

Version:

ynf-fw-mtl-api

94 lines (82 loc) 3.32 kB
/* * @Author: wangyingliang@yonyou.com * @Date: 2023-11-10 11:46:25 * @LastEditors: wangyingliang wangyingliang@yonyou.com * @LastEditTime: 2025-09-02 20:30:41 * @FilePath: /mtl-api-project/src/platforms/tt/tt.proxy.js * @Description: 鉴权接口 * Copyright (c) 2023 by Yonyou, All Rights Reserved. */ import permission from '../../common/permission' const FAIL_CODE = -1 const ttGetticketUrl = `${location.origin}/iuap-yonbuilder-mobile/rest/v2/mobile/feishu/signature` /** * 飞书 jssdk 鉴权接口 * @param {string} url getTicket 接口地址 非必传 * @param {string} appId 默认显示的图片地址 非必传 * @param {string} secret 秘钥 非必传 * @param {Function} success 成功回调 * @param {Function} fail 失败回调 */ function configPermission(params) { console.log("start apiAuth") // 增加从 localstorage 和 url 中获取鉴权参数的逻辑 const localParams = permission.getConfigParams() // 获取 ticket 地址 let serverUrl = params?.url || localParams?.url || ttGetticketUrl // 调用config接口的当前网页url let url = encodeURIComponent(window.location.href.split("#")[0]) if ((params?.pageUrl && params?.pageUrl !== "") || (localParams?.pageUrl && localParams?.pageUrl !== "")) { url = encodeURIComponent(params?.pageUrl || localParams?.pageUrl) } const appId = params?.appId || params?.appid || params?.aKey || localParams?.appId || localParams?.appid || localParams?.aKey if (!appId || appId === "") { params?.fail && params?.fail({ code: FAIL_CODE, msg: "appId is null!" }) return } const sKey = params?.secret || params?.sKey || localParams?.secret || localParams?.sKey // 处理参数 serverUrl = `${serverUrl}?url=${url}&appId=${appId}&appSecret=${sKey}` // 第三方平台私有化场景下, 需要指定第三方平台部署域名 const domain = params?.domain || localParams?.domain if (domain && domain !== "") { serverUrl = `${serverUrl}&domain=${domain}` } // 获取 ticket fetch(serverUrl).then((response) => response.json().then((res) => { console.log("ticket res:", res) // 通过error接口处理API验证失败后的回调 window.h5sdk.error((err) => { console.log("h5sdk error:", JSON.stringify(err)) params?.fail && params?.fail(err) }) if (res.errorCode === 400) { return } // 调用config接口进行鉴权 window.h5sdk.config({ appId: res?.result?.appid || appId, timestamp: res?.result?.timestamp, nonceStr: res?.result?.noncestr, signature: res?.result?.signature, jsApiList: [], // 鉴权成功回调 onSuccess: (res) => { console.log("config success: ", res) params?.success && params?.success(res) }, // 鉴权失败回调 onFail: (err) => { console.log("config failed: ", err) params?.fail && params?.fail(err) } }) })).catch((e) => { console.log("config failed: ", e) params?.fail && params?.fail(e) }) } export default { configPermission }