mtl-js-sdk
Version:
ynf-fw-mtl-api
106 lines (98 loc) • 3.19 kB
JavaScript
/*
* @Author: wangyingliang@yonyou.com
* @Date: 2023-11-15 20:56:18
* @LastEditors: wangyingliang wangyingliang@yonyou.com
* @LastEditTime: 2025-04-07 09:38:52
* @FilePath: /mtl-api-project/src/platforms/tt/platform.js
* @Description: 飞书入口文件
* Copyright (c) 2023 by Yonyou, All Rights Reserved.
*/
import methods from './methods.js'
import proxy from './tt.proxy.js'
const SUCCESS_CODE = 200
const FAIL_CODE = -1004
const { configPermission } = proxy
let platform = {
id: "tt",
bootstrap(onready) {
// 初始化 tt jssdk
onready()
initConfig()
},
}
/**
* mtl 初始化接口
* @param {string} url getTicket 接口地址 非必传
* @param {string} jsSdkPath 飞书SDK地址 非必传
* @param {string} appId 默认显示的图片地址 非必传
* @param {string} secret 秘钥 非必传
* @param {Function} success 成功回调
* @param {Function} fail 失败回调
*/
function initConfig(object) {
if (!mtl.isReady) {
window.mtl$Auth = object
}
let jsSdkPath = object?.jsSdkPath || object?.authSdkUrl || "https://lf1-cdn-tos.bytegoofy.com/goofy/lark/op/h5-js-sdk-1.5.37.js"
loadScript(jsSdkPath, function (res) {
// 授权
configPermission({
...object,
success: function (res) {
console.log("TT authorization successful")
const backRes = {
code: SUCCESS_CODE,
data: res,
}
object?.success && object?.success(res)
object?.complete && object?.complete(backRes)
},
fail: function (err) {
let newErr = err || {}
console.log("TT authorization fail:" + JSON.stringify(err))
const res = {
code: FAIL_CODE,
message: "TT authorization fail",
...newErr,
}
object?.fail && object?.fail(err)
object?.complete && object?.complete(res)
},
})
}, function (err) {
console.log("TT SDK load fail.")
})
}
/**
* @description: 加载远程js文件
* @param {String} url 远程地址
* @param {Function} callback 加载成功的回调函数
* @param {Function} errorCallback 加载失败的回调函数
*/
function loadScript(url = "", callback = "", errorCallback = "", defer) {
if (url && !isLoadedJsScript(url) && !window?.tt) {
let script = document.createElement("script")
script.type = "text/javascript"
script.src = url
script.onload = function (e) {
callback && callback()
}
script.defer = defer
script.onerror = function () {
errorCallback && errorCallback()
}
window.document.head.appendChild(script)
} else {
callback && callback()
}
}
function isLoadedJsScript(url) {
for (var i = 0; i < document.scripts.length; i++) {
if (document.scripts[i].src == url) {
return true;
}
}
return false;
}
platform.exports = { ...methods, initConfig }
export default platform