Refine chatdoc and base code interface (#2)
This commit is contained in:
1 parent
28da0263dc
commit
09ee60aa6e
21 files changed
+352
-168
No files matched your search
@@ -12,7 +12,46 @@
|
||||
<el-row>
|
||||
<el-col :span="12" :offset="6">
|
||||
<el-skeleton v-if="loading" />
|
||||
<create-order v-else-if="application" :application="application" />
|
||||
<el-form v-else-if="application" label-width="100px">
|
||||
<el-form-item :label="$t('application.field.service')">
|
||||
{{ application?.service?.title }}
|
||||
</el-form-item>
|
||||
<el-form-item :label="$t('application.field.package')">
|
||||
<el-radio-group v-if="application?.service?.packages" v-model="form.packageId">
|
||||
<el-radio-button
|
||||
v-for="(pkg, pkgIndex) in application?.service.packages"
|
||||
:key="pkgIndex"
|
||||
:label="pkg.id"
|
||||
>
|
||||
{{ pkg.amount }} {{ $t(`service.unit.${application?.service?.unit}s`) }}
|
||||
</el-radio-button>
|
||||
</el-radio-group>
|
||||
</el-form-item>
|
||||
<el-form-item :label="$t('service.field.price')">
|
||||
<price :price="package?.price" />
|
||||
</el-form-item>
|
||||
<el-divider border-style="dashed" />
|
||||
<el-form-item :label="$t('application.field.shouldPayPrice')">
|
||||
<span
|
||||
v-if="package"
|
||||
:class="{ price: true, unfree: package?.price > 0, free: package?.price === 0 }"
|
||||
>
|
||||
${{ package?.price?.toFixed(2) }}
|
||||
</span>
|
||||
<span v-else :class="{ price: true, free: true }"> $ 0 </span>
|
||||
</el-form-item>
|
||||
<el-form-item>
|
||||
<el-button
|
||||
type="primary"
|
||||
size="large"
|
||||
class="btn-create"
|
||||
:loading="creating"
|
||||
@click="onCreateOrder"
|
||||
>
|
||||
{{ $t('application.button.createOrder') }}
|
||||
</el-button>
|
||||
</el-form-item>
|
||||
</el-form>
|
||||
</el-col>
|
||||
</el-row>
|
||||
</el-card>
|
||||
@@ -24,51 +63,136 @@
|
||||
|
||||
<script lang="ts">
|
||||
import { defineComponent } from 'vue';
|
||||
import { applicationOperator } from '@/operators';
|
||||
import CreateOrder from '@/components/order/Create.vue';
|
||||
import { ElRow, ElCol, ElCard, ElSkeleton } from 'element-plus';
|
||||
import { IApplication, IApplicationDetailResponse } from '@/models';
|
||||
import {
|
||||
applicationOperator,
|
||||
IApplication,
|
||||
IApplicationDetailResponse,
|
||||
IOrderDetailResponse,
|
||||
orderOperator
|
||||
} from '@/operators';
|
||||
import {
|
||||
ElRow,
|
||||
ElCol,
|
||||
ElCard,
|
||||
ElSkeleton,
|
||||
ElMessage,
|
||||
ElForm,
|
||||
ElFormItem,
|
||||
ElButton,
|
||||
ElInputNumber,
|
||||
ElDivider,
|
||||
ElRadioGroup,
|
||||
ElRadioButton
|
||||
} from 'element-plus';
|
||||
import { ROUTE_CONSOLE_ORDER_DETAIL } from '@/router';
|
||||
import Price from '@/components/common/Price.vue';
|
||||
|
||||
interface IData {
|
||||
application: IApplication | undefined;
|
||||
loading: boolean;
|
||||
form: {
|
||||
amount: number;
|
||||
packageId: string | undefined;
|
||||
};
|
||||
creating: boolean;
|
||||
}
|
||||
|
||||
export default defineComponent({
|
||||
name: 'ConsoleApplicationBuy',
|
||||
components: {
|
||||
CreateOrder,
|
||||
ElSkeleton,
|
||||
ElRow,
|
||||
ElCol,
|
||||
ElCard
|
||||
ElCard,
|
||||
ElForm,
|
||||
ElFormItem,
|
||||
ElInputNumber,
|
||||
ElButton,
|
||||
ElDivider,
|
||||
ElRadioGroup,
|
||||
ElRadioButton,
|
||||
Price
|
||||
},
|
||||
data(): IData {
|
||||
return {
|
||||
application: undefined,
|
||||
loading: false
|
||||
loading: false,
|
||||
form: {
|
||||
amount: 1000,
|
||||
packageId: undefined
|
||||
},
|
||||
creating: false
|
||||
};
|
||||
},
|
||||
computed: {
|
||||
id() {
|
||||
return this.$route.params?.id?.toString();
|
||||
},
|
||||
price() {
|
||||
if (this.application?.service?.price && this.form.amount) {
|
||||
return this.form.amount * this.application.service?.price;
|
||||
}
|
||||
return 0;
|
||||
},
|
||||
package() {
|
||||
if (this.application?.service?.packages && this.form.packageId) {
|
||||
const filterPackages = this.application.service?.packages.filter((item) => item.id === this.form.packageId);
|
||||
if (filterPackages.length > 0) {
|
||||
return filterPackages[0];
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
},
|
||||
mounted() {
|
||||
this.onFetchData();
|
||||
this.onFetchApplication();
|
||||
},
|
||||
methods: {
|
||||
onFetchData() {
|
||||
onFetchApplication() {
|
||||
this.loading = true;
|
||||
applicationOperator
|
||||
.get(this.id)
|
||||
.then(({ data: data }: { data: IApplicationDetailResponse }) => {
|
||||
this.application = data;
|
||||
this.loading = false;
|
||||
// by default select first packageId
|
||||
this.form.packageId = this.application?.service?.packages?.[0]?.id;
|
||||
})
|
||||
.catch(() => {
|
||||
this.loading = false;
|
||||
});
|
||||
},
|
||||
onCreateOrder() {
|
||||
if (!this.application?.id) {
|
||||
return;
|
||||
}
|
||||
this.creating = true;
|
||||
const unit = this.$t(`service.unit.${this.application?.service?.unit}s`);
|
||||
orderOperator
|
||||
.create({
|
||||
application_id: this.application?.id,
|
||||
amount: this.form.amount,
|
||||
...(this.form.packageId !== 'custom' && this.package
|
||||
? {
|
||||
package_id: this.package.id
|
||||
}
|
||||
: {}),
|
||||
description: `${this.application?.service?.title} x ${this.form.amount} ${unit}`
|
||||
})
|
||||
.then(({ data: data }: { data: IOrderDetailResponse }) => {
|
||||
this.creating = false;
|
||||
const order = data;
|
||||
this.$router.push({
|
||||
name: ROUTE_CONSOLE_ORDER_DETAIL,
|
||||
params: {
|
||||
id: order.id
|
||||
}
|
||||
});
|
||||
})
|
||||
.catch(() => {
|
||||
ElMessage.error(this.$t('order.message.createFailed'));
|
||||
this.creating = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -9,24 +9,30 @@
|
||||
<el-row>
|
||||
<el-col :span="24">
|
||||
<el-card shadow="hover">
|
||||
<el-table v-loading="loading" :data="applications" stripe table-layout="fixed">
|
||||
<el-table-column prop="id" :label="$t('application.field.id')" width="300px" class-name="text-center">
|
||||
<el-table
|
||||
v-loading="loading"
|
||||
:data="applications"
|
||||
stripe
|
||||
table-layout="fixed"
|
||||
:empty-text="$t('common.message.noData')"
|
||||
>
|
||||
<el-table-column prop="id" :label="$t('application.field.id')" width="220px" class-name="text-center">
|
||||
<template #default="scope">
|
||||
<span>{{ scope.row.id }}</span>
|
||||
<span class="copy">
|
||||
<copy-to-clipboard :content="scope?.row?.id" />
|
||||
<copy-to-clipboard :content="scope?.row?.id" class="inline-block" />
|
||||
</span>
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column :label="$t('application.field.name')" width="220px">
|
||||
<el-table-column :label="$t('application.field.name')" width="180px">
|
||||
<template #default="scope">
|
||||
<span v-if="scope.row.type === applicationType.API">{{ scope.row?.api?.title }}</span>
|
||||
<span>{{ scope.row?.service?.title }}</span>
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column
|
||||
prop="remaining_amount"
|
||||
:label="$t('application.field.remainingAmount')"
|
||||
width="120px"
|
||||
width="160px"
|
||||
class-name="text-center"
|
||||
>
|
||||
<template #default="scope">
|
||||
@@ -36,22 +42,13 @@
|
||||
<el-table-column
|
||||
prop="used_amount"
|
||||
:label="$t('application.field.usedAmount')"
|
||||
width="120px"
|
||||
width="160px"
|
||||
class-name="text-center"
|
||||
>
|
||||
<template #default="scope">
|
||||
<span>{{ getUsedAmount(scope.row) }}</span>
|
||||
</template>
|
||||
</el-table-column>
|
||||
<el-table-column fixed="right" width="100px">
|
||||
<template #default="scope">
|
||||
<div class="float-right">
|
||||
<el-button type="primary" @click="onBuyMore(scope?.row)">
|
||||
{{ $t('application.button.buyMore') }}
|
||||
</el-button>
|
||||
</div>
|
||||
</template>
|
||||
</el-table-column>
|
||||
</el-table>
|
||||
</el-card>
|
||||
</el-col>
|
||||
@@ -74,7 +71,7 @@ import Pagination from '@/components/common/Pagination.vue';
|
||||
import CopyToClipboard from '@/components/common/CopyToClipboard.vue';
|
||||
import { ElTable, ElRow, ElCol, ElTableColumn, ElButton, ElCard } from 'element-plus';
|
||||
import { ROUTE_CONSOLE_APPLICATION_BUY } from '@/router/constants';
|
||||
import { IApplication, IApplicationListResponse, IApplicationType, ICredentialType } from '@/models';
|
||||
import { IApplication, IApplicationListResponse, IApplicationType, ICredentialType, IService } from '@/models';
|
||||
|
||||
interface IData {
|
||||
applications: IApplication[];
|
||||
@@ -86,9 +83,9 @@ interface IData {
|
||||
amount: number | undefined;
|
||||
};
|
||||
active: {
|
||||
service: IService | undefined;
|
||||
application: IApplication | undefined;
|
||||
};
|
||||
applicationType: typeof IApplicationType;
|
||||
credentialType: typeof ICredentialType;
|
||||
}
|
||||
|
||||
@@ -107,17 +104,16 @@ export default defineComponent({
|
||||
data(): IData {
|
||||
return {
|
||||
credentialType: ICredentialType,
|
||||
applicationType: IApplicationType,
|
||||
applications: [],
|
||||
loading: false,
|
||||
total: undefined,
|
||||
buying: false,
|
||||
// dynamic calculate the limit according to window height
|
||||
limit: Math.floor((window.innerHeight - 300) / 50),
|
||||
limit: 10,
|
||||
form: {
|
||||
amount: 1
|
||||
},
|
||||
active: {
|
||||
service: undefined,
|
||||
application: undefined
|
||||
}
|
||||
};
|
||||
@@ -128,9 +124,6 @@ export default defineComponent({
|
||||
},
|
||||
page() {
|
||||
return parseInt(this.$route.query.page?.toString() || '1');
|
||||
},
|
||||
type() {
|
||||
return (this.$route.query.type?.toString() || IApplicationType.API) as IApplicationType;
|
||||
}
|
||||
},
|
||||
watch: {
|
||||
@@ -149,19 +142,6 @@ export default defineComponent({
|
||||
this.onFetchData();
|
||||
},
|
||||
methods: {
|
||||
onFilterType(type: string) {
|
||||
if (type === this.type) {
|
||||
return;
|
||||
}
|
||||
this.$router.push({
|
||||
...this.$route,
|
||||
query: {
|
||||
...this.$route.query,
|
||||
page: 1,
|
||||
type
|
||||
}
|
||||
});
|
||||
},
|
||||
onBuyMore(application: IApplication) {
|
||||
this.$router.push({
|
||||
name: ROUTE_CONSOLE_APPLICATION_BUY,
|
||||
@@ -186,7 +166,6 @@ export default defineComponent({
|
||||
limit: this.limit,
|
||||
offset: (this.page - 1) * this.limit,
|
||||
user_id: this.$store.getters.user.id,
|
||||
type: this.type,
|
||||
ordering: '-created_at'
|
||||
})
|
||||
.then(({ data: data }: { data: IApplicationListResponse }) => {
|
||||
@@ -199,16 +178,18 @@ export default defineComponent({
|
||||
});
|
||||
},
|
||||
getRemainingAmount(application: IApplication) {
|
||||
if (application.type === IApplicationType.API) {
|
||||
const unit = this.$t(`api.unit.${application?.api?.unit}`);
|
||||
return `${application.remaining_amount}${unit}`;
|
||||
if (application.remaining_amount === undefined || application.remaining_amount === null) {
|
||||
return '';
|
||||
}
|
||||
const unit = this.$t(`service.unit.${application?.service?.unit}s`);
|
||||
return `${application.remaining_amount?.toFixed(6)} ${unit}`;
|
||||
},
|
||||
getUsedAmount(application: IApplication) {
|
||||
if (application.type === IApplicationType.API) {
|
||||
const unit = this.$t(`api.unit.${application?.api?.unit}`);
|
||||
return `${application.used_amount}${unit}`;
|
||||
if (application.used_amount === undefined || application.used_amount === null) {
|
||||
return '';
|
||||
}
|
||||
const unit = this.$t(`service.unit.${application?.service?.unit}s`);
|
||||
return `${application.used_amount?.toFixed(6)} ${unit}`;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user