|
| 1 | +<template> |
| 2 | + <div id="de2-lark-qr" :class="{ 'de2-lark-qr': !isBind }" /> |
| 3 | +</template> |
| 4 | + |
| 5 | +<script lang="ts" setup> |
| 6 | +import { loadScript } from '@/utils/RemoteJs' |
| 7 | +import { propTypes } from '@/utils/propTypes' |
| 8 | +import { queryClientInfo } from './platformUtils' |
| 9 | +import { getSQLBotAddr } from '@/utils/utils' |
| 10 | +import { ref } from 'vue' |
| 11 | +interface LarkQrInfo { |
| 12 | + client_id?: string |
| 13 | + state?: string |
| 14 | + redirect_uri?: string |
| 15 | +} |
| 16 | +
|
| 17 | +const props = defineProps({ |
| 18 | + isBind: propTypes.bool.def(false), |
| 19 | +}) |
| 20 | +const origin = ref(8) |
| 21 | +const remoteJsUrl = |
| 22 | + 'https://lf-package-cn.feishucdn.com/obj/feishu-static/lark/passport/qrcode/LarkSSOSDKWebQRCode-1.0.3.js' |
| 23 | +const jsId = 'de-lark-qr-id' |
| 24 | +const init = () => { |
| 25 | + loadScript(remoteJsUrl, jsId).then(() => { |
| 26 | + queryClientInfo(origin.value).then((res: any) => { |
| 27 | + const data = formatQrResult(res) as any |
| 28 | + loadQr(data.client_id, data.state, data.redirect_uri) |
| 29 | + }) |
| 30 | + }) |
| 31 | +} |
| 32 | +
|
| 33 | +const formatQrResult = (data: any): LarkQrInfo => { |
| 34 | + const result = { client_id: null, state: null, redirect_uri: null } as unknown as LarkQrInfo |
| 35 | + result.client_id = data.client_id |
| 36 | + result.state = 'fit2cloud-lark-qr' |
| 37 | + result.redirect_uri = data.redirect_uri || getSQLBotAddr() |
| 38 | + if (props.isBind) { |
| 39 | + result.state += '_de_bind' |
| 40 | + } |
| 41 | + return result |
| 42 | +} |
| 43 | +
|
| 44 | +const loadQr = (CLIENT_ID: string, STATE: string, REDIRECT_URI: string) => { |
| 45 | + let url = `https://passport.feishu.cn/suite/passport/oauth/authorize?client_id=${CLIENT_ID}&response_type=code&state=${STATE}&redirect_uri=${REDIRECT_URI}` |
| 46 | + // eslint-disable-next-line |
| 47 | + // @ts-ignore |
| 48 | + const QRLoginObj = window['QRLogin']({ |
| 49 | + id: 'de2-lark-qr', |
| 50 | + goto: url, |
| 51 | + style: 'border:none;background-color:#FFFFFF;width: 266px;height: 266px;', |
| 52 | + }) |
| 53 | + const handleMessage = function (event: any) { |
| 54 | + const origin = event.origin |
| 55 | + if (QRLoginObj.matchOrigin(origin)) { |
| 56 | + const loginTmpCode = event.data |
| 57 | + url += '&tmp_code=' + loginTmpCode.tmp_code |
| 58 | + window.location.href = url |
| 59 | + } |
| 60 | + } |
| 61 | + if (typeof window.addEventListener != 'undefined') { |
| 62 | + window.addEventListener('message', handleMessage, false) |
| 63 | + // eslint-disable-next-line |
| 64 | + // @ts-ignore |
| 65 | + } else if (typeof window['attachEvent'] != 'undefined') { |
| 66 | + // eslint-disable-next-line |
| 67 | + // @ts-ignore |
| 68 | + window['attachEvent']('onmessage', handleMessage) |
| 69 | + } |
| 70 | +} |
| 71 | +init() |
| 72 | +</script> |
| 73 | +<style lang="less" scoped> |
| 74 | +.de2-lark-qr { |
| 75 | + margin-top: -15px; |
| 76 | +} |
| 77 | +</style> |
0 commit comments