From 91c6792c5c3b14c9eb6ef73667ed272f3273aa50 Mon Sep 17 00:00:00 2001 From: AceDataCloud Date: Sat, 11 Feb 2023 12:18:47 +0800 Subject: [PATCH] add verification check and support --- src/App.vue | 24 +++++++++++++++++++----- src/components/api/Form.vue | 26 +++++++++++++++++++------- src/components/api/Usage.vue | 6 +++++- src/constants/errorCode.ts | 2 +- src/i18n/zh/application/message.ts | 4 +++- src/operators/auth/models.ts | 6 ++++++ src/operators/auth/operator.ts | 11 +++++++++++ src/pages/service/Detail.vue | 14 +++++++++++++- src/utils/index.ts | 1 + src/utils/verification.ts | 12 ++++++++++++ 10 files changed, 90 insertions(+), 16 deletions(-) create mode 100644 src/operators/auth/models.ts create mode 100644 src/operators/auth/operator.ts create mode 100644 src/utils/verification.ts diff --git a/src/App.vue b/src/App.vue index c2742979..1f0bfd69 100644 --- a/src/App.vue +++ b/src/App.vue @@ -8,6 +8,7 @@ import { defineComponent } from 'vue'; import { ElConfigProvider } from 'element-plus'; import zhCn from 'element-plus/dist/locale/zh-cn.mjs'; +import { authOperator } from './operators/auth/operator'; export default defineComponent({ components: { @@ -18,11 +19,24 @@ export default defineComponent({ locale: zhCn }; }, - mounted() { - // if access token is already set, try to get user info - const accessToken = this.$store.getters.accessToken; - if (accessToken) { - this.$store.dispatch('getMe'); + async mounted() { + // if refresh token is already set, try to refresh it and get user info + const currentRefreshToken = this.$store.getters.refreshToken; + const currentAccessToken = this.$store.getters.accessToken; + if (!currentAccessToken) { + console.log('user not login'); + } + if (currentRefreshToken) { + const { + data: { access: accessToken, refresh: refreshToken } + } = await authOperator.refreshToken({ + refresh: currentRefreshToken + }); + await this.$store.dispatch('setAccessToken', accessToken); + await this.$store.dispatch('setRefreshToken', refreshToken); + await this.$store.dispatch('getMe'); + } else { + console.error('refresh token does not exist'); } } }); diff --git a/src/components/api/Form.vue b/src/components/api/Form.vue index bd1a7cec..37d8c0a0 100644 --- a/src/components/api/Form.vue +++ b/src/components/api/Form.vue @@ -13,8 +13,8 @@
-
- +
+

{{ $t('application.message.notApplied') }}

@@ -26,7 +26,6 @@ v-model="value[itemKey?.toString()]" :placeholder="$t('common.title.placeholderOfInput')" class="inline-block w-4/5" - @focus="$emit('refresh-applications')" />
@@ -81,7 +80,8 @@ import { ISchema } from '@/operators/api/models'; import { applicationOperator, IApi, IApplication, IApplicationType } from '@/operators'; import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'; import { ElPopover, ElInput, ElSelect, ElButton, ElTooltip, ElOption, ElMessage } from 'element-plus'; -import { ERROR_CODE_DUPLICATION } from '@/constants/errorCode'; +import { ERROR_CODE_DUPLICATION, ERROR_CODE_UNVERIFIED } from '@/constants/errorCode'; +import { getVerificationUrl } from '@/utils'; interface IData { value: { @@ -112,6 +112,10 @@ export default defineComponent({ applications: { type: Array as () => IApplication[], required: true + }, + applied: { + type: Boolean, + required: false } }, emits: ['update:form', 'refresh-applications'], @@ -121,9 +125,6 @@ export default defineComponent({ }; }, computed: { - isApplied() { - return this.applications.filter((application) => application.api?.id === this.api?.id).length > 0; - }, appliedApplication(): IApplication | undefined { const applications = this.applications.filter((application) => application.api?.id === this.api?.id); if (applications.length > 0) { @@ -160,6 +161,17 @@ export default defineComponent({ if (error?.response?.data?.code === ERROR_CODE_DUPLICATION) { ElMessage.error(this.$t('application.message.alreadyApplied')); } + if (error?.response?.data?.code === ERROR_CODE_UNVERIFIED) { + ElMessage({ + dangerouslyUseHTMLString: true, + duration: 0, + showClose: true, + message: `${this.$t( + 'application.message.unverified' + )} ${this.$t('application.message.goVerify')}`, + type: 'warning' + }); + } }); } } diff --git a/src/components/api/Usage.vue b/src/components/api/Usage.vue index 81bea298..b1980b2c 100644 --- a/src/components/api/Usage.vue +++ b/src/components/api/Usage.vue @@ -8,6 +8,7 @@ v-model:form="formValue.queries" :schema="api?.request?.queries" :api="api" + :applied="applied" :applications="applications" @refresh-applications="getApplications" /> @@ -45,6 +46,7 @@ import ApiInfo from './Info.vue'; export interface IData { formValue: IForm; applications: IApplication[]; + applied: boolean | undefined; } export default defineComponent({ @@ -69,7 +71,8 @@ export default defineComponent({ data(): IData { return { formValue: this.form || {}, - applications: [] + applications: [], + applied: undefined }; }, watch: { @@ -89,6 +92,7 @@ export default defineComponent({ }) .then(({ data: data }: { data: IApplicationListResponse }) => { this.applications = data.items; + this.applied = this.applications.filter((item) => item?.api?.id === this.api.id).length > 0; }) .catch((error) => {}); } diff --git a/src/constants/errorCode.ts b/src/constants/errorCode.ts index 37ef5a28..98940a8b 100644 --- a/src/constants/errorCode.ts +++ b/src/constants/errorCode.ts @@ -1,2 +1,2 @@ -export const ERROR_CODE_UNVERIFIED = 'unverifed'; +export const ERROR_CODE_UNVERIFIED = 'unverified'; export const ERROR_CODE_DUPLICATION = 'duplication'; diff --git a/src/i18n/zh/application/message.ts b/src/i18n/zh/application/message.ts index 6437637f..5467f5a0 100644 --- a/src/i18n/zh/application/message.ts +++ b/src/i18n/zh/application/message.ts @@ -2,5 +2,7 @@ export default { alreadyApplied: '已经申请过该服务,请到控制台查看', applySuccessfully: '申请成功,即将跳转到控制台', notApplied: '你尚未申请过该服务,请先申请该服务之后使用', - yourApplication: '你的申请' + yourApplication: '你的申请', + unverified: '您需要完成实名认证之后方可申请该服务', + goVerify: '去实名认证' }; diff --git a/src/operators/auth/models.ts b/src/operators/auth/models.ts new file mode 100644 index 00000000..8fc7d02c --- /dev/null +++ b/src/operators/auth/models.ts @@ -0,0 +1,6 @@ +export interface IToken { + access?: string; + refresh?: string; +} + +export interface ITokenResponse extends IToken {} diff --git a/src/operators/auth/operator.ts b/src/operators/auth/operator.ts new file mode 100644 index 00000000..deb25c00 --- /dev/null +++ b/src/operators/auth/operator.ts @@ -0,0 +1,11 @@ +import { AxiosResponse } from 'axios'; +import { httpClient } from '../instance'; +import { ITokenResponse, IToken } from './models'; + +class AuthOperator { + async refreshToken(payload: IToken): Promise> { + return httpClient.post('/token/refresh/', payload); + } +} + +export const authOperator = new AuthOperator(); diff --git a/src/pages/service/Detail.vue b/src/pages/service/Detail.vue index b7a50e33..8a53b5e0 100644 --- a/src/pages/service/Detail.vue +++ b/src/pages/service/Detail.vue @@ -85,7 +85,7 @@ import { serviceOperator } from '@/operators'; import { defineComponent } from 'vue'; -import { ERROR_CODE_DUPLICATION } from '@/constants'; +import { ERROR_CODE_DUPLICATION, ERROR_CODE_UNVERIFIED } from '@/constants'; import { ElMessage } from 'element-plus'; import { apiOperator } from '@/operators/api/operator'; import { IApi, IApiListResponse } from '@/operators/api/models'; @@ -94,6 +94,7 @@ import MarkdownRenderer from '@/components/common/MarkdownRenderer.vue'; import { ROUTE_CONSOLE_APPLICATION_LIST } from '@/router'; import { ElCol, ElRow, ElButton, ElCard, ElDivider, ElSkeleton, ElSkeletonItem } from 'element-plus'; import ApiInfo from '@/components/api/Info.vue'; +import { getVerificationUrl } from '@/utils'; interface IData { service: IService | undefined; @@ -191,6 +192,17 @@ export default defineComponent({ }); }, 2000); } + if (error?.response?.data?.code === ERROR_CODE_UNVERIFIED) { + ElMessage({ + dangerouslyUseHTMLString: true, + duration: 0, + showClose: true, + message: `${this.$t( + 'application.message.unverified' + )} ${this.$t('application.message.goVerify')}`, + type: 'warning' + }); + } }); } } diff --git a/src/utils/index.ts b/src/utils/index.ts index 50d82f52..972cbe8b 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,3 +1,4 @@ export * from './baseUrl'; export * from './env'; export * from './mode'; +export * from './verification'; diff --git a/src/utils/verification.ts b/src/utils/verification.ts new file mode 100644 index 00000000..7a114e3f --- /dev/null +++ b/src/utils/verification.ts @@ -0,0 +1,12 @@ +/** + * Get verification url + * @returns + */ + +import { getAuthBaseUrl } from './baseUrl'; + +export const getVerificationUrl = () => { + const baseUrl = getAuthBaseUrl(); + const targetUrl = `${baseUrl}/user/verify?redirect=` + window.location.href; + return targetUrl; +};