feat(projects): add alova examples (#647)

* feat: optimistic subpackage `@sa/alova`

* feat: add alova examples
This commit is contained in:
Scott Hu
2024-10-17 17:20:48 +08:00
committed by Soybean
parent 641f3160d6
commit a6545265ac
33 changed files with 1484 additions and 1 deletions

View File

@@ -0,0 +1,43 @@
<script setup lang="ts">
import { actionDelegationMiddleware, useAutoRequest } from '@sa/alova/client';
import { ref } from 'vue';
import { alova } from '@/serviceAlova/request';
const getLastTime = alova.Get<{ time: string }>('/mock/getLastTime', { cacheFor: null });
const isStop = ref(false);
const { loading, data } = useAutoRequest(getLastTime, {
enableVisibility: true,
enableNetwork: false,
enableFocus: false,
initialData: {
time: ''
},
async middleware(_, next) {
await actionDelegationMiddleware('autoRequest:1')(_, () => Promise.resolve());
if (!isStop.value) {
next();
}
}
});
const toggleStop = () => {
isStop.value = !isStop.value;
};
</script>
<template>
<NSpace vertical>
<NAlert type="info">
{{ $t('page.alova.scenes.visibilityRequestTips') }}
</NAlert>
<NButton type="primary" @click="toggleStop">
<icon-carbon-play v-if="isStop" class="mr-2" />
<icon-carbon-stop v-else class="mr-2" />
{{ isStop ? $t('page.alova.scenes.startRequest') : $t('page.alova.scenes.stopRequest') }}
</NButton>
<NSpace align="center">
<span>{{ $t('page.alova.scenes.refreshTime') }}: {{ data.time || '--' }}</span>
<NSpin v-if="loading" :size="12" />
</NSpace>
</NSpace>
</template>

View File

@@ -0,0 +1,71 @@
<script setup lang="ts">
import { computed } from 'vue';
import { actionDelegationMiddleware, useCaptcha, useForm } from '@sa/alova/client';
import { $t } from '@/locales';
import { useFormRules, useNaiveForm } from '@/hooks/common/form';
import { sendCaptcha, verifyCaptcha } from '@/serviceAlova/api';
defineOptions({
name: 'CaptchaVerification'
});
const { loading, send, countdown } = useCaptcha(sendCaptcha, {
middleware: actionDelegationMiddleware('captcha:send')
});
const label = computed(() => {
return countdown.value > 0
? $t('page.login.codeLogin.reGetCode', { time: countdown.value })
: $t('page.login.codeLogin.getCode');
});
const {
form,
loading: submiting,
send: submit
} = useForm(formData => verifyCaptcha(formData.phone, formData.code), {
initialForm: {
phone: '',
code: ''
}
});
const { formRef, validate } = useNaiveForm();
const rules = computed<Record<keyof typeof form.value, App.Global.FormRule[]>>(() => {
const { formRules } = useFormRules();
return {
phone: formRules.phone,
code: formRules.code
};
});
async function handleSubmit() {
await validate();
await submit();
// request
window.$message?.success($t('page.login.common.validateSuccess'));
}
</script>
<template>
<NForm ref="formRef" :model="form" :rules="rules" size="large" :show-label="false" @keyup.enter="handleSubmit">
<NFormItem path="phone">
<NInput v-model:value="form.phone" :placeholder="$t('page.login.common.phonePlaceholder')" :maxlength="11" />
</NFormItem>
<NFormItem path="code">
<div class="w-full flex-y-center gap-16px">
<NInput v-model:value="form.code" :placeholder="$t('page.login.common.codePlaceholder')" />
<NButton size="large" :disabled="countdown > 0" :loading="loading" @click="send(form.phone)">
{{ label }}
</NButton>
</div>
</NFormItem>
<NSpace vertical :size="18" class="w-full">
<NButton type="primary" size="large" round block :loading="submiting" @click="handleSubmit">
{{ $t('common.confirm') }}
</NButton>
</NSpace>
</NForm>
</template>
<style scoped></style>

View File

@@ -0,0 +1,13 @@
<script setup lang="ts">
import { accessAction } from '@sa/alova/client';
const handleAutoRequestSend = async () => {
accessAction(/^autoRequest/, async ({ send }) => {
await send();
});
};
</script>
<template>
<NButton @click="handleAutoRequestSend">{{ $t('page.alova.scenes.triggerAllRequest') }}</NButton>
</template>

View File

@@ -0,0 +1,43 @@
<script setup lang="ts">
import { actionDelegationMiddleware, useAutoRequest } from '@sa/alova/client';
import { ref } from 'vue';
import { alova } from '@/serviceAlova/request';
const getLastTime = alova.Get<{ time: string }>('/mock/getLastTime', { cacheFor: null });
const isStop = ref(false);
const { loading, data } = useAutoRequest(getLastTime, {
enableVisibility: false,
enableNetwork: true,
enableFocus: false,
initialData: {
time: ''
},
async middleware(_, next) {
await actionDelegationMiddleware('autoRequest:2')(_, () => Promise.resolve());
if (!isStop.value) {
next();
}
}
});
const toggleStop = () => {
isStop.value = !isStop.value;
};
</script>
<template>
<NSpace vertical>
<NAlert type="info">
{{ $t('page.alova.scenes.networkRequestTips') }}
</NAlert>
<NButton type="primary" @click="toggleStop">
<icon-carbon-play v-if="isStop" class="mr-2" />
<icon-carbon-stop v-else class="mr-2" />
{{ isStop ? $t('page.alova.scenes.startRequest') : $t('page.alova.scenes.stopRequest') }}
</NButton>
<NSpace align="center">
<span>{{ $t('page.alova.scenes.refreshTime') }}: {{ data.time || '--' }}</span>
<NSpin v-if="loading" :size="12" />
</NSpace>
</NSpace>
</template>

View File

@@ -0,0 +1,41 @@
<script setup lang="ts">
import { actionDelegationMiddleware, useAutoRequest } from '@sa/alova/client';
import { ref } from 'vue';
import { alova } from '@/serviceAlova/request';
const getLastTime = alova.Get<{ time: string }>('/mock/getLastTime', { cacheFor: null });
const isStop = ref(false);
const { loading, data } = useAutoRequest(getLastTime, {
pollingTime: 3000,
initialData: {
time: ''
},
async middleware(_, next) {
await actionDelegationMiddleware('autoRequest:3')(_, () => Promise.resolve());
if (!isStop.value) {
next();
}
}
});
const toggleStop = () => {
isStop.value = !isStop.value;
};
</script>
<template>
<NSpace vertical>
<NAlert type="info">
{{ $t('page.alova.scenes.pollingRequestTips') }}
</NAlert>
<NButton type="primary" @click="toggleStop">
<icon-carbon-play v-if="isStop" class="mr-2" />
<icon-carbon-stop v-else class="mr-2" />
{{ isStop ? $t('page.alova.scenes.startRequest') : $t('page.alova.scenes.stopRequest') }}
</NButton>
<NSpace align="center">
<span>{{ $t('page.alova.scenes.refreshTime') }}: {{ data.time || '--' }}</span>
<NSpin v-if="loading" :size="12" />
</NSpace>
</NSpace>
</template>