kidArtExpo/src/store/auth/index.js

154 lines
4.5 KiB
JavaScript
Raw Normal View History

2024-08-08 09:07:42 +00:00
import {ref,computed} from 'vue'
2024-08-07 12:00:54 +00:00
import {createGlobalState,useStorage} from '@vueuse/core'
2024-08-09 05:40:02 +00:00
import {competitionApply, competitionWorks, loginRegister, sendCode} from '@/api/auth/index.js'
2024-08-07 12:00:54 +00:00
import {message} from "@/utils/message.js"
2024-08-08 09:07:42 +00:00
import { useRouter } from 'vue-router';
2024-08-07 12:00:54 +00:00
export const useAuth=createGlobalState(()=>{
2024-08-08 09:07:42 +00:00
const router = useRouter();
2024-08-07 12:00:54 +00:00
const token = useStorage('token', '', localStorage)
2024-08-09 05:40:02 +00:00
const telNum =useStorage('telNum', '', localStorage)
2024-08-07 12:00:54 +00:00
const code=ref('')
2024-08-09 05:40:02 +00:00
const countdown = ref(0)
const isCountingDown = ref(false)
2024-08-08 09:07:42 +00:00
const showTextCode=computed(()=>{
return isCountingDown.value ? `${countdown.value}s` : '获取验证码'
})
const genderOptions=ref([
{text:'男',value:'男'},
{text:'女',value:'女'}
])
2024-08-09 05:40:02 +00:00
// 验证 formData 中的字段
function validateFormData() {
// 验证基本信息
const baseFields = ['name', 'age', 'gender'];
if (baseFields.some(field => !Boolean(formData.value[field]))) {
return false;
}
// 验证 works 数组中的每一个作品
if (formData.value.works.some(work =>
!Boolean(work.picUrl) ||
!Boolean(work.workName) ||
!Boolean(work.length) ||
!Boolean(work.wide))) {
return false;
}
return true;
}
const detailData=useStorage('detailData', {}, localStorage)
const formData=useStorage('formData',{
2024-08-08 09:07:42 +00:00
name:'',
age:'',
gender:'',
works:[
{
2024-08-09 05:40:02 +00:00
imgList:[],
2024-08-08 09:07:42 +00:00
picUrl: "", //作品图片url
workName: "", //作品名称
length: undefined, //长度
wide:undefined//宽度
}
]
2024-08-09 05:40:02 +00:00
},localStorage)
2024-08-08 09:07:42 +00:00
const clickAddWorks=()=>{
formData.value.works.push({
picUrl: "", //作品图片url
workName: "", //作品名称
length: undefined, //长度
wide:undefined//宽度
})
}
const removeWorks=(index)=>{
formData.value.works.splice(index,1)
}
const clickApply=async ()=>{
2024-08-09 05:40:02 +00:00
const isValid = validateFormData();
if (!isValid){
message.warning('作品信息不全,请补充')
return
}
2024-08-08 09:07:42 +00:00
const data={
...formData.value
}
const res=await competitionApply(data)
if(res.status===0){
message.success('报名成功')
2024-08-09 05:40:02 +00:00
router.push('/confirm')
2024-08-08 09:07:42 +00:00
}
}
let timer = null;
2024-08-09 05:40:02 +00:00
const getDetail=async ()=>{
if (!telNum.value){
message.error('获取手机号失败')
return
}
const res=await competitionWorks({telNum:telNum.value})
if (res.status===0){
console.log(res.data,'getDetail')
detailData.value=res.data
}
}
2024-08-08 09:07:42 +00:00
const clickLogin=async ()=>{
const data={
telNum:telNum.value,
code:code.value
}
const res=await loginRegister(data)
if(res.status===0){
2024-08-09 05:40:02 +00:00
if (res.data.status===1){
message.warning('您已经报名')
await getDetail()
router.push('/details')
}else {
message.success('登录成功')
token.value=res.data.token
router.push('/signup')
}
2024-08-08 09:07:42 +00:00
}
}
2024-08-07 12:00:54 +00:00
const clickSendCode=async ()=>{
2024-08-08 09:07:42 +00:00
if (isCountingDown.value) return;
if (!code.value){
message.warning('请输入验证码')
return
}
2024-08-07 12:00:54 +00:00
if (!telNum.value){
message.warning('请输入手机号')
return
}
const data={
telNum:telNum.value
}
const res=await sendCode(data)
if (res.status===0){
2024-08-08 09:07:42 +00:00
countdown.value = 60;
isCountingDown.value = true;
2024-08-07 12:00:54 +00:00
message.success('发送成功')
2024-08-08 09:07:42 +00:00
timer = setInterval(() => {
if (countdown.value > 0) {
countdown.value -= 1;
} else {
clearInterval(timer);
isCountingDown.value = false;
}
}, 1000);
2024-08-07 12:00:54 +00:00
}
}
return {
2024-08-09 05:40:02 +00:00
detailData,
2024-08-08 09:07:42 +00:00
removeWorks,
clickAddWorks,
genderOptions,
formData,
clickApply,
clickLogin,
showTextCode,
2024-08-07 12:00:54 +00:00
code,
clickSendCode,
telNum,
token
}
})