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