pilot-index.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <template>
  2. <div class="login flex-column flex-justify-center flex-align-center m0 b0">
  3. <a-image style="width: 17vw; height: 10vw; margin-bottom: 50px" :src="djiLogo" />
  4. <p class="logo fz35 pb50">上云无人机管理平台</p>
  5. <a-form layout="inline" :model="formState" class="flex-row flex-justify-center flex-align-center">
  6. <a-form-item>
  7. <a-input v-model:value="formState.username" placeholder="账号">
  8. <template #prefix>
  9. <UserOutlined style="color: rgba(0, 0, 0, 0.25)" />
  10. </template>
  11. </a-input>
  12. </a-form-item>
  13. <a-form-item>
  14. <a-input v-model:value="formState.password" type="password" placeholder="密码">
  15. <template #prefix>
  16. <LockOutlined style="color: rgba(0, 0, 0, 0.25)" />
  17. </template>
  18. </a-input>
  19. </a-form-item>
  20. <a-form-item>
  21. <a-button class="m0" type="primary" html-type="submit"
  22. :disabled="formState.user === '' || formState.password === ''" @click="onSubmit">
  23. 登录
  24. </a-button>
  25. </a-form-item>
  26. </a-form>
  27. </div>
  28. </template>
  29. <script lang="ts" setup>
  30. import { message } from 'ant-design-vue'
  31. import { onMounted, reactive, ref, UnwrapRef } from 'vue'
  32. import { CURRENT_CONFIG } from '/@/api/http/config'
  33. import { login, LoginBody, refreshToken } from '/@/api/manage'
  34. import apiPilot from '/@/api/pilot-bridge'
  35. import { getRoot } from '/@/root'
  36. import { EComponentName, ELocalStorageKey, ERouterName, EUserType } from '/@/types'
  37. import { UserOutlined, LockOutlined } from '@ant-design/icons-vue'
  38. import djiLogo from '/@/assets/icons/dji_logo.png'
  39. const root = getRoot()
  40. const formState: UnwrapRef<LoginBody> = reactive({
  41. username: 'pilot',
  42. password: 'pilot123',
  43. flag: EUserType.Pilot,
  44. gateway_sn: apiPilot.getRemoteControllerSN()
  45. })
  46. const isVerified = ref<boolean>(false)
  47. onMounted(async () => {
  48. verifyLicense()
  49. if (!isVerified.value) {
  50. return
  51. }
  52. apiPilot.setPlatformMessage('Cloud Api Platform', '', '')
  53. const token = localStorage.getItem(ELocalStorageKey.Token)
  54. if (token) {
  55. await refreshToken({})
  56. .then(res => {
  57. apiPilot.setComponentParam(EComponentName.Api, {
  58. host: import.meta.env.VITE_APP_API_URL,
  59. token: res.data.access_token
  60. })
  61. const jsres = apiPilot.loadComponent(EComponentName.Api, apiPilot.getComponentParam(EComponentName.Api))
  62. if (!jsres) {
  63. message.error('加载api模块失败')
  64. return
  65. }
  66. apiPilot.setToken(res.data.access_token)
  67. localStorage.setItem(ELocalStorageKey.Token, res.data.access_token)
  68. root.$router.push(ERouterName.PILOT_HOME)
  69. })
  70. .catch(err => {
  71. message.error(err)
  72. })
  73. }
  74. })
  75. const onSubmit = async (e: any) => {
  76. await login(formState)
  77. .then(res => {
  78. if (!isVerified.value) {
  79. message.error('请先核实license')
  80. return
  81. }
  82. console.log('login res:', res)
  83. if (res.code === 0) {
  84. apiPilot.setComponentParam(EComponentName.Api, {
  85. host: import.meta.env.VITE_APP_API_URL,
  86. token: res.data.access_token
  87. })
  88. const jsres = apiPilot.loadComponent(
  89. EComponentName.Api,
  90. apiPilot.getComponentParam(EComponentName.Api)
  91. )
  92. console.log('load api module res:', jsres)
  93. apiPilot.setToken(res.data.access_token)
  94. localStorage.setItem(ELocalStorageKey.Token, res.data.access_token)
  95. localStorage.setItem(ELocalStorageKey.WorkspaceId, res.data.workspace_id)
  96. localStorage.setItem(ELocalStorageKey.UserId, res.data.user_id)
  97. localStorage.setItem(ELocalStorageKey.Username, res.data.username)
  98. localStorage.setItem(ELocalStorageKey.Flag, EUserType.Pilot.toString())
  99. message.success('登录成功')
  100. root.$router.push(ERouterName.PILOT_HOME)
  101. }
  102. })
  103. .catch(err => {
  104. message.error(err)
  105. })
  106. }
  107. function verifyLicense() {
  108. isVerified.value = apiPilot.platformVerifyLicense(CURRENT_CONFIG.appId, CURRENT_CONFIG.appKey, CURRENT_CONFIG.appLicense) &&
  109. apiPilot.isPlatformVerifySuccess()
  110. if (isVerified.value) {
  111. message.success('license认证成功')
  112. } else {
  113. message.error('申请license验证。请检查license是否正确,或者重新申请')
  114. }
  115. }
  116. </script>
  117. <style lang="scss" scoped>
  118. @import '/@/styles/index.scss';
  119. .login {
  120. // background-color: $dark-highlight;
  121. height: 100vh;
  122. }
  123. .logo {
  124. color: $primary;
  125. }
  126. </style>