update apply
This commit is contained in:
1 parent
94a3a0604d
commit
17905a21e5
4 files changed
+173
-29
No files matched your search
@@ -1,37 +1,87 @@
|
||||
<template>
|
||||
<el-input
|
||||
v-model="value"
|
||||
:placeholder="$t('conversation.message.newMessagePlaceholder')"
|
||||
@keyup.enter="$emit('send')"
|
||||
>
|
||||
<template #suffix>
|
||||
<font-awesome-icon icon="fa-regular fa-paper-plane" class="icon-send" @click="$emit('send')" />
|
||||
</template>
|
||||
</el-input>
|
||||
<div class="new-message-box">
|
||||
<el-input
|
||||
v-model="value"
|
||||
:disabled="!application"
|
||||
class="new-message-input"
|
||||
:placeholder="$t('conversation.message.newMessagePlaceholder')"
|
||||
@keyup.enter="$emit('send')"
|
||||
>
|
||||
<template #suffix>
|
||||
<font-awesome-icon icon="fa-regular fa-paper-plane" class="icon-send" @click="$emit('send')" />
|
||||
</template>
|
||||
</el-input>
|
||||
<div v-if="!application" class="new-message-apply">
|
||||
<div v-if="initializing">
|
||||
<span class="mr-2">{{ $t('conversation.message.apiInfo') }}</span>
|
||||
<span>
|
||||
<el-button type="primary" size="small" @click="confirming = true">
|
||||
{{ $t('common.button.apply') }}
|
||||
</el-button>
|
||||
</span>
|
||||
<span class="ml-1">{{ $t('conversation.message.tryForFree') }}</span>
|
||||
</div>
|
||||
<div v-else>
|
||||
<span class="ml-1">{{ $t('conversation.message.initializing') }}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<application-confirm
|
||||
v-if="api"
|
||||
v-model.visible="confirming"
|
||||
:object="api"
|
||||
:type="applicationType.API"
|
||||
@apply="$emit('apply')"
|
||||
/>
|
||||
</template>
|
||||
|
||||
<script lang="ts">
|
||||
import { defineComponent } from 'vue';
|
||||
import { ElInput, ElButton, ElIcon } from 'element-plus';
|
||||
import { ElInput, ElButton } from 'element-plus';
|
||||
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome';
|
||||
import { IApi, IApplication } from '@/operators';
|
||||
import { IApplicationType } from '@/operators';
|
||||
import ApplicationConfirm from '@/components/application/Confirm.vue';
|
||||
|
||||
interface IData {
|
||||
applicationType: typeof IApplicationType;
|
||||
value: string;
|
||||
confirming: boolean;
|
||||
}
|
||||
|
||||
export default defineComponent({
|
||||
name: 'NewMessageBox',
|
||||
components: {
|
||||
ElInput,
|
||||
ElIcon,
|
||||
FontAwesomeIcon
|
||||
ElButton,
|
||||
FontAwesomeIcon,
|
||||
ApplicationConfirm
|
||||
},
|
||||
props: {
|
||||
initializing: {
|
||||
type: Boolean,
|
||||
required: false,
|
||||
default: false
|
||||
},
|
||||
modelValue: {
|
||||
type: String,
|
||||
required: true
|
||||
},
|
||||
api: {
|
||||
type: Object as () => IApi | undefined,
|
||||
required: true
|
||||
},
|
||||
application: {
|
||||
type: Object as () => IApplication | undefined,
|
||||
required: true
|
||||
}
|
||||
},
|
||||
emits: ['send', 'update:modelValue'],
|
||||
data() {
|
||||
emits: ['send', 'update:modelValue', 'apply'],
|
||||
data(): IData {
|
||||
return {
|
||||
value: this.modelValue
|
||||
applicationType: IApplicationType,
|
||||
value: this.modelValue,
|
||||
confirming: false
|
||||
};
|
||||
},
|
||||
computed: {
|
||||
@@ -41,19 +91,38 @@ export default defineComponent({
|
||||
},
|
||||
watch: {
|
||||
value(val) {
|
||||
this.$emit('update:modelValue', this.value);
|
||||
this.$emit('update:modelValue', val);
|
||||
},
|
||||
modelValue(val) {
|
||||
if (val !== this.value) {
|
||||
this.value = val;
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
methods: {}
|
||||
});
|
||||
</script>
|
||||
|
||||
<style lang="scss" scoped>
|
||||
.icon-send {
|
||||
cursor: pointer;
|
||||
.new-message-box {
|
||||
position: relative;
|
||||
margin-bottom: 30px;
|
||||
.new-message-input {
|
||||
height: 50px;
|
||||
}
|
||||
.icon-send {
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.new-message-apply {
|
||||
position: absolute;
|
||||
top: 50%;
|
||||
left: 50%;
|
||||
width: 100%;
|
||||
text-align: center;
|
||||
transform: translate(-50%, -50%);
|
||||
font-size: 12px;
|
||||
color: #666;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@@ -1,5 +1,5 @@
|
||||
export default {
|
||||
alreadyApplied: '已经申请过该服务,请到控制台查看',
|
||||
alreadyApplied: '已经申请过该服务',
|
||||
applySuccessfully: '申请成功',
|
||||
notApplied: '你尚未申请过该服务,请先申请该服务之后使用',
|
||||
yourApplication: '你的申请',
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
export default {
|
||||
noToken: '请先申请对应服务',
|
||||
newMessagePlaceholder: '请输入你的问题...',
|
||||
noInput: '问题不能为空'
|
||||
noInput: '问题不能为空',
|
||||
apiInfo: '本服务由「知数云」提供技术支持',
|
||||
tryForFree: '免费使用',
|
||||
initializing: '初始化中,请稍后...'
|
||||
};
|
||||
@@ -20,7 +20,15 @@
|
||||
<div class="footer">
|
||||
<el-row>
|
||||
<el-col :span="12" :offset="6">
|
||||
<new-message-box v-model="input" class="new-message" @send="onSend" />
|
||||
<new-message-box
|
||||
v-model="input"
|
||||
:initializing="loading"
|
||||
class="new-message"
|
||||
:api="api"
|
||||
:application="application"
|
||||
@send="onSend"
|
||||
@apply="onApply"
|
||||
/>
|
||||
</el-col>
|
||||
</el-row>
|
||||
</div>
|
||||
@@ -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'
|
||||
)} <a class="underline" href="${getVerificationUrl()}">${this.$t('application.message.goVerify')}</a>`,
|
||||
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) {
|
||||
|
||||
Reference in new issue
Block a user