Files
console-dev/app/components/instance/InstanceOnDemand.vue
T
2026-11-06 00:04:38 +08:00

798 lines
28 KiB
Vue

<script>
import "~/assets/styles/InstanceOnDemand.okGWwgIJ.css";
import { toDisplayString } from "vue";
import { onBeforeUnmount } from "vue";
import { watch } from "vue";
import { createBlock } from "vue";
import { ref } from "vue";
import { withCtx } from "vue";
import { createCommentVNode } from "vue";
import { computed } from "vue";
import { isRef } from "vue";
import { createElementBlock } from "vue";
import { Fragment } from "vue";
import { createElementVNode } from "vue";
import { createTextVNode } from "vue";
import { unref } from "vue";
import { defineComponent } from "vue";
import { createVNode } from "vue";
import { openBlock } from "vue";
import { useApi } from "~/lib/auth.js";
import dialog from "primevue/dialog";
import button from "primevue/button";
import { t as _plugin_vue_export_helper_t } from "~/lib/_plugin-vue_export-helper.js";
import message from "primevue/message";
import { r as dict_r } from "~/lib/dict.js";
import select from "primevue/select";
import checkbox from "primevue/checkbox";
import skeleton from "primevue/skeleton";
import datepicker from "primevue/datepicker";
var oe = {
class: `on-demand`,
},
se = {
key: 2,
},
ce = {
class: `actions`,
},
le = {
key: 3,
},
x = {
key: 4,
},
S = {
key: 6,
},
ue = {
key: 1,
},
de = {
key: 0,
},
fe = {
class: `confirmation`,
},
C = Object.assign(
_plugin_vue_export_helper_t(
defineComponent({
__name: `InstanceOnDemand`,
props: {
host: {},
hostId: {},
},
emits: [`refresh`, `pay`],
setup(m, { emit: b }) {
let C = m,
w = b,
T = useApi(),
E = computed(() => C.host.billing_cycle === `on_demand`),
D = computed(() => Number(C.host.change_billing_cycle_id) > 0),
O = computed(() => {
if (D.value) return `已设置到期转换计划。`;
if (!E.value && C.host.billing_cycle !== `recurring_prepayment`)
return `当前计费方式不支持到期转为按需计费。`;
let e = E.value
? C.host.product_on_demand?.on_demand_to_duration
: C.host.product_on_demand?.duration_to_on_demand;
return e == null
? `计费转换能力信息未返回,请刷新实例详情。`
: Number(e) === 1
? E.value && Number(C.host.auto_release_time) > 0
? `请先取消自动释放计划,再转为包年包月。`
: (E.value ? [`Active`, `Grace`] : [`Active`]).includes(
C.host.status,
)
? ``
: `当前实例状态不支持计费转换。`
: E.value
? `当前商品未开启按需转包年包月。`
: `当前商品未开启包年包月转按需计费。`;
}),
k = computed(() => {
if (!E.value) return `自动释放设置仅支持按需计费实例。`;
let e = C.host.product_on_demand?.client_auto_delete;
return e == null
? `自动释放能力信息未返回,请刷新实例详情。`
: Number(e) === 1
? C.host.status === `Active`
? ``
: `当前实例状态不支持修改自动释放时间。`
: `当前商品未开启自动释放设置。`;
}),
A = computed(() => !O.value),
j = computed(() => !k.value),
pe = computed(
() =>
(E.value
? C.host.product_on_demand?.on_demand_to_duration
: C.host.product_on_demand?.duration_to_on_demand) == null ||
(E.value &&
C.host.product_on_demand?.client_auto_delete == null),
),
M = computed(
() =>
`/host/${C.hostId}/${E.value ? `on_demand_to_recurring_prepayment` : `recurring_prepayment_to_on_demand`}`,
),
N = ref(!1),
P = ref(!1),
F = ref(!1),
I = ref(``),
L = ref(``),
R = ref([]),
z = ref(null),
B = ref(null),
V = ref(!1),
H = ref(null),
U = ref(!1),
W = ref(!1),
G = 0,
K = !1,
q = () =>
`${C.hostId}:${C.host.billing_cycle}:${C.host.change_billing_cycle_id}:${A.value}:${j.value}`,
J = (e) =>
e != null &&
String(e).trim() !== `` &&
Number.isFinite(Number(e)) &&
Number(e) >= 0,
Y = ``,
X = computed(() => R.value.find((e) => e.id === z.value)),
Z = computed(
() =>
!P.value &&
!F.value &&
A.value &&
Y === q() &&
(E.value ? !!X.value : !!B.value),
);
function Q() {
(G++,
(Y = ``),
(N.value = V.value = W.value = !1),
(P.value = !1),
(R.value = []),
(B.value = null),
(I.value = ``),
(L.value = ``));
}
(watch(q, Q, {
flush: `sync`,
}),
onBeforeUnmount(() => {
((K = !0), Q());
}));
async function $() {
if (!A.value || F.value) return;
((N.value = !0),
(P.value = !0),
(I.value = ``),
(Y = ``),
(R.value = []),
(B.value = null),
(z.value = null));
let e = ++G,
t = q();
try {
let n = await T.get(M.value);
if (K || e !== G || !N.value || t !== q()) return;
if (E.value) {
if (
!Array.isArray(n?.duration) ||
!n.duration.length ||
n.duration.some(
(e) =>
!Number.isSafeInteger(Number(e.id)) ||
Number(e.id) <= 0 ||
!J(e.price),
)
)
throw Error(`未获得可用周期报价,请重试`);
((R.value = n.duration), (z.value = n.duration[0].id));
} else {
if (
!J(n.on_demand_price) ||
(n.keep_time_price != null && !J(n.keep_time_price))
)
throw Error(`按需价格响应无效,请重试`);
B.value = {
...n,
on_demand_price: n.on_demand_price,
};
}
Y = t;
} catch (t) {
e === G &&
!K &&
(I.value = t instanceof Error ? t.message : `计费报价加载失败`);
} finally {
e === G && !K && (P.value = !1);
}
}
async function me() {
if (!Z.value) return;
((F.value = !0), (I.value = ``));
let e = G,
t = E.value,
n = M.value;
try {
let r = await T.post(
n,
t
? {
duration_id: z.value,
}
: void 0,
);
if (K || G !== e) return;
if (t) {
if (
!Number.isSafeInteger(Number(r?.id)) ||
Number(r.id) <= 0 ||
!J(r.amount)
)
throw Error(`转换订单响应无效,请到订单列表核对`);
w(`pay`, {
order_id: r.id,
amount: r.amount,
});
} else L.value = `已申请到期转为按需计费`;
((N.value = !1), (Y = ``), w(`refresh`));
} catch (t) {
e === G &&
!K &&
(I.value = t instanceof Error ? t.message : `转换申请失败`);
} finally {
F.value = !1;
}
}
async function he() {
if (!D.value || F.value || !W.value) return;
((F.value = !0), (I.value = ``));
let e = G;
try {
if (
(await T.del(
`/host/${C.hostId}/recurring_prepayment_to_on_demand`,
),
K || e !== G)
)
return;
((W.value = !1), (L.value = `已取消到期转换计划`), w(`refresh`));
} catch (t) {
!K &&
e === G &&
(I.value = t instanceof Error ? t.message : `取消转换失败`);
} finally {
F.value = !1;
}
}
function ge() {
j.value &&
!F.value &&
((H.value =
Number(C.host.auto_release_time) > 0
? new Date(Number(C.host.auto_release_time) * 1e3)
: null),
(U.value = !1),
(I.value = ``),
(V.value = !0));
}
async function _e() {
if (!j.value || F.value || !V.value || !U.value) return;
let e = H.value ? Math.floor(H.value.getTime() / 1e3) : 0;
if (
H.value &&
(!Number.isSafeInteger(e) || e <= Date.now() / 1e3)
) {
I.value = `请选择将来的释放时间`;
return;
}
((F.value = !0), (I.value = ``));
let t = G;
try {
if (
(await T.put(`/host/${C.hostId}/auto_release_time`, {
auto_release_time: e,
}),
K || t !== G)
)
return;
((V.value = !1),
(L.value = e ? `自动释放时间已保存` : `已取消自动释放`),
w(`refresh`));
} catch (e) {
!K &&
t === G &&
(I.value = e instanceof Error ? e.message : `保存自动释放失败`);
} finally {
F.value = !1;
}
}
return (t, n) => {
let i = message,
s = button,
_ = skeleton,
b = select,
C = dialog,
T = datepicker,
M = checkbox;
return (
openBlock(),
createElementBlock(`section`, oe, [
(n[12] ||= createElementVNode(
`h4`,
null,
`计费转换与自动释放`,
-1,
)),
unref(L)
? (openBlock(),
createBlock(
i,
{
key: 0,
severity: `success`,
closable: !1,
},
{
default: withCtx(() => [
createTextVNode(toDisplayString(unref(L)), 1),
]),
_: 1,
},
))
: createCommentVNode(``, !0),
unref(I)
? (openBlock(),
createBlock(
i,
{
key: 1,
severity: `error`,
closable: !1,
},
{
default: withCtx(() => [
createTextVNode(toDisplayString(unref(I)), 1),
]),
_: 1,
},
))
: createCommentVNode(``, !0),
unref(D)
? (openBlock(),
createElementBlock(
`p`,
se,
`已申请到期转为按需计费,取消转换计划后才可续费或升降级。`,
))
: createCommentVNode(``, !0),
createElementVNode(`div`, ce, [
createVNode(
s,
{
label: unref(E) ? `转为包年包月` : `到期转为按需计费`,
disabled: !unref(A) || unref(F),
outlined: ``,
onClick: $,
},
null,
8,
[`label`, `disabled`],
),
unref(D)
? (openBlock(),
createBlock(
s,
{
key: 0,
label: `取消转换计划`,
disabled: unref(F),
outlined: ``,
onClick: (n[0] ||= (e) => (W.value = !0)),
},
null,
8,
[`disabled`],
))
: createCommentVNode(``, !0),
createVNode(
s,
{
label: `设置自动释放`,
disabled: !unref(j) || unref(F),
outlined: ``,
onClick: ge,
},
null,
8,
[`disabled`],
),
]),
unref(O) && !unref(D)
? (openBlock(),
createElementBlock(
`small`,
le,
toDisplayString(unref(O)),
1,
))
: createCommentVNode(``, !0),
unref(k)
? (openBlock(),
createElementBlock(
`small`,
x,
toDisplayString(unref(k)),
1,
))
: createCommentVNode(``, !0),
unref(pe)
? (openBlock(),
createBlock(
s,
{
key: 5,
label: `刷新实例详情`,
text: ``,
size: `small`,
disabled: unref(F),
onClick: (n[1] ||= (e) => w(`refresh`)),
},
null,
8,
[`disabled`],
))
: createCommentVNode(``, !0),
m.host.auto_release_time
? (openBlock(),
createElementBlock(
`p`,
S,
`当前自动释放时间:` +
toDisplayString(
(`formatTime` in t ? t.formatTime : unref(dict_r))(
m.host.auto_release_time,
),
),
1,
))
: createCommentVNode(``, !0),
createVNode(
C,
{
visible: unref(N),
"onUpdate:visible": (n[3] ||= (e) =>
isRef(N) ? (N.value = e) : null),
header: `确认计费转换`,
modal: ``,
closable: !unref(F),
"close-on-escape": !unref(F),
style: {
width: `min(460px, calc(100vw - 28px))`,
},
},
{
footer: withCtx(() => [
createVNode(
s,
{
label: `重新报价`,
text: ``,
disabled: unref(F) || unref(P),
onClick: $,
},
null,
8,
[`disabled`],
),
createVNode(
s,
{
label: `确认转换`,
loading: unref(F),
disabled: !unref(Z),
onClick: me,
},
null,
8,
[`loading`, `disabled`],
),
]),
default: withCtx(() => [
unref(P)
? (openBlock(),
createBlock(_, {
key: 0,
height: `90px`,
}))
: (openBlock(),
createElementBlock(
Fragment,
{
key: 1,
},
[
unref(E)
? (openBlock(),
createBlock(
b,
{
key: 0,
modelValue: unref(z),
"onUpdate:modelValue": (n[2] ||= (e) =>
isRef(z) ? (z.value = e) : null),
options: unref(R),
"option-label": `name_show`,
"option-value": `id`,
disabled: unref(F),
placeholder: `选择计费周期`,
},
null,
8,
[`modelValue`, `options`, `disabled`],
))
: createCommentVNode(``, !0),
unref(E) && unref(X)
? (openBlock(),
createElementBlock(
`p`,
ue,
`转换费用:¥` +
toDisplayString(unref(X).price),
1,
))
: createCommentVNode(``, !0),
!unref(E) && unref(B)
? (openBlock(),
createElementBlock(
Fragment,
{
key: 2,
},
[
createElementVNode(
`p`,
null,
`到期后按需出账:¥` +
toDisplayString(
unref(B).on_demand_price,
) +
` / ` +
toDisplayString(
unref(B).billing_cycle_name,
),
1,
),
unref(B).keep_time_price
? (openBlock(),
createElementBlock(
`p`,
de,
`保留期价格:¥` +
toDisplayString(
unref(B).keep_time_price,
),
1,
))
: createCommentVNode(``, !0),
],
64,
))
: createCommentVNode(``, !0),
],
64,
)),
unref(I)
? (openBlock(),
createBlock(
i,
{
key: 2,
severity: `error`,
closable: !1,
},
{
default: withCtx(() => [
createTextVNode(toDisplayString(unref(I)), 1),
]),
_: 1,
},
))
: createCommentVNode(``, !0),
]),
_: 1,
},
8,
[`visible`, `closable`, `close-on-escape`],
),
createVNode(
C,
{
visible: unref(W),
"onUpdate:visible": (n[4] ||= (e) =>
isRef(W) ? (W.value = e) : null),
header: `取消转换计划`,
modal: ``,
closable: !unref(F),
style: {
width: `min(420px, calc(100vw - 28px))`,
},
},
{
footer: withCtx(() => [
createVNode(
s,
{
label: `确认取消计划`,
loading: unref(F),
onClick: he,
},
null,
8,
[`loading`],
),
]),
default: withCtx(() => [
(n[9] ||= createElementVNode(
`p`,
null,
`取消后将保留当前计费方式。`,
-1,
)),
unref(I)
? (openBlock(),
createBlock(
i,
{
key: 0,
severity: `error`,
},
{
default: withCtx(() => [
createTextVNode(toDisplayString(unref(I)), 1),
]),
_: 1,
},
))
: createCommentVNode(``, !0),
]),
_: 1,
},
8,
[`visible`, `closable`],
),
createVNode(
C,
{
visible: unref(V),
"onUpdate:visible": (n[8] ||= (e) =>
isRef(V) ? (V.value = e) : null),
header: `自动释放设置`,
modal: ``,
closable: !unref(F),
"close-on-escape": !unref(F),
style: {
width: `min(460px, calc(100vw - 28px))`,
},
},
{
footer: withCtx(() => [
createVNode(
s,
{
label: `取消自动释放`,
text: ``,
disabled: unref(F),
onClick: (n[7] ||= (e) => (H.value = null)),
},
null,
8,
[`disabled`],
),
createVNode(
s,
{
label: `保存设置`,
loading: unref(F),
disabled: !unref(U),
onClick: _e,
},
null,
8,
[`loading`, `disabled`],
),
]),
default: withCtx(() => [
createVNode(
i,
{
severity: `warn`,
closable: !1,
},
{
default: withCtx(() => [
...(n[10] ||= [
createTextVNode(
`达到设定时间后,实例将自动释放,数据无法恢复。请提前备份。`,
-1,
),
]),
]),
_: 1,
},
),
createVNode(
T,
{
modelValue: unref(H),
"onUpdate:modelValue": (n[5] ||= (e) =>
isRef(H) ? (H.value = e) : null),
"show-time": ``,
"hour-format": `24`,
"date-format": `yy-mm-dd`,
disabled: unref(F),
placeholder: `选择释放时间,留空取消计划`,
fluid: ``,
},
null,
8,
[`modelValue`, `disabled`],
),
createElementVNode(`label`, fe, [
createVNode(
M,
{
modelValue: unref(U),
"onUpdate:modelValue": (n[6] ||= (e) =>
isRef(U) ? (U.value = e) : null),
binary: ``,
disabled: unref(F),
},
null,
8,
[`modelValue`, `disabled`],
),
(n[11] ||= createTextVNode(
`我已确认释放时间及数据影响`,
-1,
)),
]),
unref(I)
? (openBlock(),
createBlock(
i,
{
key: 0,
severity: `error`,
},
{
default: withCtx(() => [
createTextVNode(toDisplayString(unref(I)), 1),
]),
_: 1,
},
))
: createCommentVNode(``, !0),
]),
_: 1,
},
8,
[`visible`, `closable`, `close-on-escape`],
),
])
);
};
},
}),
[[`__scopeId`, `data-v-2710106c`]],
),
{
__name: `InstanceOnDemand`,
},
);
export {};
export default C;
</script>