You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
4.5 KiB

3 years ago
<template>
<div class="login flex-column flex-justify-center flex-align-center m0 b0">
<a-image
style="width: 17vw; height: 10vw; margin-bottom: 50px"
3 years ago
:src="djiLogo"
3 years ago
/>
<p class="logo fz35 pb50">Pilot Cloud API Demo</p>
<a-form
layout="inline"
:model="formState"
class="flex-row flex-justify-center flex-align-center"
>
<a-form-item>
3 years ago
<a-input v-model:value="formState.username" placeholder="Username">
3 years ago
<template #prefix
><UserOutlined style="color: rgba(0, 0, 0, 0.25)"
/></template>
</a-input>
</a-form-item>
<a-form-item>
<a-input
v-model:value="formState.password"
type="password"
placeholder="Password"
>
<template #prefix
><LockOutlined style="color: rgba(0, 0, 0, 0.25)"
/></template>
</a-input>
</a-form-item>
<a-form-item>
<a-button
class="m0"
type="primary"
html-type="submit"
:disabled="formState.user === '' || formState.password === ''"
@click="onSubmit"
>
Login
</a-button>
</a-form-item>
</a-form>
</div>
</template>
<script lang="ts" setup>
import { message } from 'ant-design-vue'
3 years ago
import { onMounted, reactive, ref, UnwrapRef } from 'vue'
3 years ago
import { CURRENT_CONFIG } from '/@/api/http/config'
3 years ago
import { login, LoginBody, refreshToken } from '/@/api/manage'
3 years ago
import apiPilot from '/@/api/pilot-bridge'
import { getRoot } from '/@/root'
3 years ago
import router from '/@/router'
import { EComponentName, ELocalStorageKey, ERouterName, EUserType } from '/@/types'
import { UserOutlined, LockOutlined } from '@ant-design/icons-vue'
import djiLogo from '/@/assets/icons/dji_logo.png'
3 years ago
const root = getRoot()
3 years ago
const formState: UnwrapRef<LoginBody> = reactive({
username: 'pilot',
password: 'pilot123',
flag: EUserType.Pilot,
3 years ago
})
3 years ago
const isVerified = ref<boolean>(false)
3 years ago
onMounted(async () => {
3 years ago
verifyLicense()
if (!isVerified.value) {
3 years ago
return
}
3 years ago
3 years ago
apiPilot.setPlatformMessage('Cloud Api Platform', '', '')
3 years ago
const token = localStorage.getItem(ELocalStorageKey.Token)
if (token) {
3 years ago
await refreshToken({})
.then(res => {
3 years ago
apiPilot.setComponentParam(EComponentName.Api, {
3 years ago
host: CURRENT_CONFIG.baseURL,
token: res.data.access_token
})
3 years ago
const jsres = apiPilot.loadComponent(EComponentName.Api, apiPilot.getComponentParam(EComponentName.Api))
if (!jsres) {
message.error('Failed to load api module.')
return
}
3 years ago
apiPilot.setToken(res.data.access_token)
3 years ago
localStorage.setItem(ELocalStorageKey.Token, res.data.access_token)
root.$router.push(ERouterName.PILOT_HOME)
3 years ago
})
.catch(err => {
3 years ago
message.error(err)
3 years ago
})
}
})
const onSubmit = async (e: any) => {
3 years ago
await login(formState)
3 years ago
.then(res => {
3 years ago
if (!isVerified.value) {
3 years ago
message.error('Please verify the license firstly.')
return
}
console.log('login res:', res)
if (res.code === 0) {
3 years ago
apiPilot.setComponentParam(EComponentName.Api, {
3 years ago
host: CURRENT_CONFIG.baseURL,
token: res.data.access_token
})
const jsres = apiPilot.loadComponent(
3 years ago
EComponentName.Api,
apiPilot.getComponentParam(EComponentName.Api)
3 years ago
)
console.log('load api module res:', jsres)
apiPilot.setToken(res.data.access_token)
3 years ago
localStorage.setItem(ELocalStorageKey.Token, res.data.access_token)
localStorage.setItem(ELocalStorageKey.WorkspaceId, res.data.workspace_id)
localStorage.setItem(ELocalStorageKey.UserId, res.data.user_id)
localStorage.setItem(ELocalStorageKey.Username, res.data.username)
localStorage.setItem(ELocalStorageKey.Flag, EUserType.Pilot.toString())
3 years ago
message.success('Login Success')
3 years ago
root.$router.push(ERouterName.PILOT_HOME)
3 years ago
}
})
.catch(err => {
3 years ago
message.error(err)
3 years ago
})
}
3 years ago
function verifyLicense () {
isVerified.value = apiPilot.platformVerifyLicense(CURRENT_CONFIG.appId, CURRENT_CONFIG.appKey, CURRENT_CONFIG.appLicense) &&
apiPilot.isPlatformVerifySuccess()
if (isVerified.value) {
message.success('The license verification is successful.')
} else {
message.error('Filed to verify the license. Please check license whether the license is correct, or apply again.')
}
}
3 years ago
</script>
<style lang="scss" scoped>
@import '/@/styles/index.scss';
.login {
// background-color: $dark-highlight;
height: 100vh;
}
.logo {
color: $primary;
}
</style>