Files
gao-customer-h5/src/pages/user/component/UserAgreement.vue
T

363 lines
7.2 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<div class="user-agreement">
<t-checkbox
:model-value="props.modelValue"
class="checkbox"
borderless
@change="handleCheckboxChange"
>
<span>我已阅读并同意用户协议</span>
</t-checkbox>
</div>
<t-dialog
class="user-agreement-dialog"
v-model:visible="dialogVisible"
title="用户协议"
cancel-btn="拒绝"
confirm-btn="同意"
:close-btn="false"
:close-on-overlay-click="false"
destroy-on-close
@cancel="rejectAgreement"
@confirm="agreeAgreement"
>
<div class="dialog-content">
<t-tabs
v-model="activeAgreementId"
class="tabs"
theme="line"
bottom-line-mode="full"
:split="false"
:space-evenly="false"
:swipeable="true"
>
<t-tab-panel
v-for="item in agreementList"
:key="item.id"
:value="item.id"
:label="item.title"
lazy
>
<div class="panel">
<t-loading v-if="item.loading" class="loading" text="正在加载协议" />
<article-view
v-else-if="item.article"
:article="item.article"
empty-text="暂无协议正文"
:resolve-attachment-url="resolveAttachmentUrl"
/>
<p v-else class="error" v-text="item.errorMessage || '协议加载失败,请拒绝后重试。'" />
</div>
</t-tab-panel>
</t-tabs>
<div class="tips">
<span>请阅读以上全部协议</span>
<br />
<span>阅读完成后点击“同意”继续</span>
</div>
</div>
</t-dialog>
</template>
<script setup lang="ts">
import { ArticleAPI, AttachmentAPI, SettingAPI, type Article } from "timi-web-api";
import { ArticleView } from "timi-tdesign-mobile";
import { getErrorMessage } from "@/utils/error";
type AgreementItem = {
id: string;
title: string;
article?: Article;
errorMessage?: string;
loading: boolean;
};
defineOptions({
name: "UserAgreement"
});
const props = withDefaults(defineProps<{
modelValue?: boolean;
}>(), {
modelValue: false
});
const emit = defineEmits<{
"update:modelValue": [value: boolean];
agree: [];
reject: [];
}>();
const dialogVisible = ref(false);
const agreementList = ref<AgreementItem[]>([]);
const activeAgreementId = ref("");
function normalizeAgreementItem(value: unknown): Pick<AgreementItem, "id" | "title"> | undefined {
if (!value || typeof value !== "object") {
return undefined;
}
const source = value as Record<string, unknown>;
const id = typeof source.id === "string" ? source.id.trim() : "";
const title = typeof source.title === "string" ? source.title.trim() : "";
if (!id || !title) {
return undefined;
}
return { id, title };
}
function parseAgreementList(value?: string): Array<Pick<AgreementItem, "id" | "title">> {
if (!value?.trim()) {
return [];
}
try {
const sourceList: unknown = JSON.parse(value);
if (!Array.isArray(sourceList)) {
return [];
}
return sourceList
.map(item => normalizeAgreementItem(item))
.filter((item): item is Pick<AgreementItem, "id" | "title"> => !!item);
} catch {
return [];
}
}
function createAgreementList(list: Array<Pick<AgreementItem, "id" | "title">>): AgreementItem[] {
return list.map(item => ({
...item,
loading: false
}));
}
async function handleCheckboxChange(checked: boolean): Promise<void> {
if (!checked) {
emit("update:modelValue", false);
return;
}
await open();
}
async function open(): Promise<void> {
emit("update:modelValue", false);
dialogVisible.value = true;
activeAgreementId.value = "";
await loadAgreements();
}
async function loadAgreements(): Promise<void> {
try {
const settingMap = await SettingAPI.map("GAO_SYSTEM", undefined, ["AGREEMENT"]);
const configuredAgreementList = parseAgreementList(settingMap.AGREEMENT?.value);
agreementList.value = createAgreementList(configuredAgreementList).map(item => ({
...item,
loading: true
}));
activeAgreementId.value = configuredAgreementList[0]?.id || "";
agreementList.value = await Promise.all(agreementList.value.map(async item => {
const articleId = item.id;
if (!articleId) {
return {
...item,
loading: false,
errorMessage: `${item.title}暂未配置`
};
}
try {
return {
...item,
article: await ArticleAPI.detail(articleId),
loading: false
};
} catch (error) {
return {
...item,
loading: false,
errorMessage: getErrorMessage(error, `${item.title}加载失败`)
};
}
}));
} catch (error) {
const errorMessage = getErrorMessage(error, "协议配置加载失败");
agreementList.value = [{
id: "AGREEMENT_ERROR",
title: "用户协议",
loading: false,
errorMessage
}];
activeAgreementId.value = "AGREEMENT_ERROR";
}
}
function resolveAttachmentUrl(attachmentId: string): string {
return AttachmentAPI.getReadURL(attachmentId);
}
defineExpose({ open });
function rejectAgreement(): void {
emit("update:modelValue", false);
emit("reject");
}
function agreeAgreement(): void {
emit("update:modelValue", true);
emit("agree");
}
</script>
<style scoped lang="less">
.user-agreement {
display: flex;
margin-top: .75rem;
justify-content: center;
.checkbox {
color: #52607a;
font-size: .8125rem;
}
}
.user-agreement-dialog {
:global(.user-agreement-dialog.t-dialog .t-dialog__content) {
height: min(62vh, 36rem);
max-height: min(62vh, 36rem);
overflow: hidden;
}
:global(.user-agreement-dialog.t-dialog .t-dialog__body) {
flex: 1 1 auto;
display: flex;
overflow: hidden;
min-height: 0;
text-align: unset;
}
:global(.user-agreement-dialog.t-dialog .t-dialog__body-text) {
width: 100%;
min-width: 0;
}
.dialog-content {
--td-spacer-3: 10px;
gap: .5rem;
width: 100%;
min-width: 0;
height: 100%;
flex: 1 1 auto;
display: flex;
min-height: 0;
flex-direction: column;
.tabs {
--td-tab-item-height: 2rem;
flex: 1 1 auto;
width: 100%;
min-width: 0;
display: flex;
overflow: hidden;
min-height: 0;
flex-direction: column;
:deep(.t-tabs__nav) {
border-bottom: 1px solid var(--td-component-border);
}
:deep(.t-tabs__content) {
flex: 1 1 auto;
height: 0;
min-height: 0;
overflow-y: auto;
}
:deep(.t-tabs__item) {
padding: 0 .5rem;
font-size: .75rem;
}
:deep(.t-tabs__item--active) {
font-weight: unset;
}
}
.panel {
.loading {
padding: 1.5rem 0;
}
.error {
margin: 0;
color: #b42318;
font-size: .8125rem;
line-height: 1.6;
}
:deep(.content) {
border: none;
padding: 0;
}
}
.tips {
flex: 0 0 auto;
margin: 0;
color: #52607a;
line-height: 1.5;
font-size: .75rem;
}
}
}
@media (orientation: landscape) and (min-width: 48rem) {
:global(.user-agreement-dialog.t-dialog) {
--td-dialog-width: min(80vw, 48rem);
}
.user-agreement-dialog {
:global(.user-agreement-dialog.t-dialog .t-dialog__content) {
height: min(76vh, 36rem);
max-height: min(76vh, 36rem);
}
.dialog-content {
.tips {
br {
display: none;
}
span + span::before {
content: ",";
}
}
}
}
}
:global(html.theme-dark) {
.user-agreement {
.checkbox {
color: #94a3b8;
}
}
.user-agreement-dialog {
.dialog-content {
.tips {
color: #94a3b8;
}
.agreement-item {
.error {
color: #fda29b;
}
}
}
}
}
</style>