diff --git a/src/components/conversation/NewMessageBox.vue b/src/components/conversation/NewMessageBox.vue
index d5479798..bbee1885 100644
--- a/src/components/conversation/NewMessageBox.vue
+++ b/src/components/conversation/NewMessageBox.vue
@@ -1,37 +1,87 @@
-
-
-
-
-
+
+
+
+
+
+
+
+
+ {{ $t('conversation.message.apiInfo') }}
+
+
+ {{ $t('common.button.apply') }}
+
+
+ {{ $t('conversation.message.tryForFree') }}
+
+
+ {{ $t('conversation.message.initializing') }}
+
+
+
+
diff --git a/src/i18n/zh/application/message.ts b/src/i18n/zh/application/message.ts
index fdbe9e96..d139a9f8 100644
--- a/src/i18n/zh/application/message.ts
+++ b/src/i18n/zh/application/message.ts
@@ -1,5 +1,5 @@
export default {
- alreadyApplied: '已经申请过该服务,请到控制台查看',
+ alreadyApplied: '已经申请过该服务',
applySuccessfully: '申请成功',
notApplied: '你尚未申请过该服务,请先申请该服务之后使用',
yourApplication: '你的申请',
diff --git a/src/i18n/zh/conversation/message.ts b/src/i18n/zh/conversation/message.ts
index 66ca1678..02d12ee4 100644
--- a/src/i18n/zh/conversation/message.ts
+++ b/src/i18n/zh/conversation/message.ts
@@ -1,5 +1,8 @@
export default {
noToken: '请先申请对应服务',
newMessagePlaceholder: '请输入你的问题...',
- noInput: '问题不能为空'
+ noInput: '问题不能为空',
+ apiInfo: '本服务由「知数云」提供技术支持',
+ tryForFree: '免费使用',
+ initializing: '初始化中,请稍后...'
};
diff --git a/src/pages/conversation/Main.vue b/src/pages/conversation/Main.vue
index fd69c4cc..b787734f 100644
--- a/src/pages/conversation/Main.vue
+++ b/src/pages/conversation/Main.vue
@@ -20,7 +20,15 @@
@@ -35,14 +43,28 @@ import Message from '@/components/conversation/Message.vue';
import { chatgptOperator } from '@/operators/api/chatgpt/operator';
import NewMessageBox from '@/components/conversation/NewMessageBox.vue';
import { chatgptAvatar, userAvatar } from '@/constants/image';
-import { applicationOperator, IApplicationListResponse } from '@/operators';
+import {
+ apiOperator,
+ applicationOperator,
+ IApi,
+ IApiDetailResponse,
+ IApplication,
+ IApplicationDetailResponse,
+ IApplicationListResponse,
+ IApplicationType
+} from '@/operators';
import { ROUTE_AUTH_LOGIN, ROUTE_CONVERSATION_DETAIL } from '@/router/constants';
+import { ERROR_CODE_DUPLICATION, ERROR_CODE_UNVERIFIED } from '@/constants';
+import { getVerificationUrl } from '@/utils';
export interface IData {
input: string;
messages: IMessage[];
loading: boolean;
+ answering: boolean;
token: string | undefined;
+ api: IApi | undefined;
+ application: IApplication | undefined;
}
export default defineComponent({
@@ -57,8 +79,11 @@ export default defineComponent({
return {
input: '',
loading: false,
+ answering: false,
messages: [],
- token: undefined
+ token: undefined,
+ api: undefined,
+ application: undefined
};
},
computed: {
@@ -69,29 +94,45 @@ export default defineComponent({
return this.$store.getters.user;
}
},
- mounted() {
+ async mounted() {
if (!this.$store.getters.authenticated) {
this.$router.push({
name: ROUTE_AUTH_LOGIN,
query: { redirect: this.$route.fullPath }
});
} else {
- this.onCheckApplication();
+ await this.onGetApiInfo();
+ await this.onCheckApplication();
}
},
methods: {
- onCheckApplication() {
+ async onGetApiInfo() {
+ return new Promise((resolve, reject) => {
+ const id = '1d58971c-e3cd-4713-a3ce-854a731adb14';
+ apiOperator
+ .get(id)
+ .then(({ data: data }: { data: IApiDetailResponse }) => {
+ this.api = data;
+ resolve(data);
+ })
+ .catch((error) => {
+ reject(error);
+ });
+ });
+ },
+ async onCheckApplication() {
if (!this.user) {
return;
}
applicationOperator
.getAll({
user_id: this.user.id,
- api_id: '1d58971c-e3cd-4713-a3ce-854a731adb14'
+ api_id: this.api?.id
})
.then(({ data: data }: { data: IApplicationListResponse }) => {
if (data.items?.length > 0) {
const application = data.items[0];
+ this.application = application;
const token = application.credential?.token;
if (token) {
this.token = token;
@@ -99,6 +140,33 @@ export default defineComponent({
}
});
},
+ onApply() {
+ applicationOperator
+ .create({
+ type: IApplicationType.API,
+ api_id: this.api?.id
+ })
+ .then(({ data: data }: { data: IApplicationDetailResponse }) => {
+ this.application = data;
+ ElMessage.success(this.$t('application.message.applySuccessfully'));
+ })
+ .catch((error) => {
+ 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'
+ });
+ }
+ });
+ },
onSend() {
if (!this.input) {
ElMessage.error(this.$t('conversation.message.noInput'));
@@ -129,7 +197,7 @@ export default defineComponent({
avatar: chatgptAvatar
}
});
- this.loading = true;
+ this.answering = true;
this.input = '';
chatgptOperator
.post(
@@ -172,7 +240,11 @@ export default defineComponent({
}
}
)
+ .then(() => {
+ this.answering = false;
+ })
.catch((error) => {
+ this.answering = false;
if (error?.response?.data) {
const data = error?.response?.data;
if (this.messages && this.messages.length > 0) {