feat: 完善登录流程+完善请求封装
This commit is contained in:
parent
64404ce7b9
commit
55144a4ea1
@ -41,9 +41,10 @@
|
|||||||
import { codeLoginApi, CodeLoginRequest } from '@/service/user'
|
import { codeLoginApi, CodeLoginRequest } from '@/service/user'
|
||||||
import { PlatId } from '@/typings'
|
import { PlatId } from '@/typings'
|
||||||
import { Result } from '@/constants/result'
|
import { Result } from '@/constants/result'
|
||||||
import { useBasicStore } from '@/store'
|
import { useBasicStore, useUserStore } from '@/store'
|
||||||
|
|
||||||
const $basic = useBasicStore()
|
const $basic = useBasicStore()
|
||||||
|
const $user = useUserStore()
|
||||||
|
|
||||||
const phone = ref<string>('')
|
const phone = ref<string>('')
|
||||||
|
|
||||||
@ -89,10 +90,8 @@
|
|||||||
})
|
})
|
||||||
uni.hideLoading()
|
uni.hideLoading()
|
||||||
if (result.errorCode === Result.Success.code) {
|
if (result.errorCode === Result.Success.code) {
|
||||||
uni.setStorageSync({
|
uni.setStorageSync('token', result.data.token)
|
||||||
key: 'token',
|
$user.getUserInfo()
|
||||||
data: result.data.token
|
|
||||||
})
|
|
||||||
await uni.switchTab({
|
await uni.switchTab({
|
||||||
url: '/pages/home/home'
|
url: '/pages/home/home'
|
||||||
})
|
})
|
||||||
|
|||||||
@ -8,11 +8,12 @@
|
|||||||
</route>
|
</route>
|
||||||
<template>
|
<template>
|
||||||
<view class="custom-bg-default">
|
<view class="custom-bg-default">
|
||||||
<TopNavigation :title="appName"></TopNavigation>
|
<view v-if="$user.loginStatus">
|
||||||
<button class="mt-12" @click="toLogin">去登录</button>
|
<CustomTab class="mt-10" :list="featuresList" @clickItem="clickItem"></CustomTab>
|
||||||
<CustomTab class="mt-10" :list="featuresList" @clickItem="clickItem"></CustomTab>
|
<button class="mt-12" @click="$user.logout">退出登录</button>
|
||||||
<view class="pt-10">
|
</view>
|
||||||
<CustomTabBar :list="list" :default-index="1" @change="change"></CustomTabBar>
|
<view v-else>
|
||||||
|
<button class="mt-12" @click="toLogin">去登录</button>
|
||||||
</view>
|
</view>
|
||||||
</view>
|
</view>
|
||||||
</template>
|
</template>
|
||||||
@ -22,8 +23,6 @@
|
|||||||
import { useUserStore } from '@/store'
|
import { useUserStore } from '@/store'
|
||||||
import { Result } from '@/constants/result'
|
import { Result } from '@/constants/result'
|
||||||
|
|
||||||
const appName = import.meta.env.VITE_APP_TITLE
|
|
||||||
|
|
||||||
const $user = useUserStore()
|
const $user = useUserStore()
|
||||||
|
|
||||||
const list = ref<Array<TabBarItem>>([
|
const list = ref<Array<TabBarItem>>([
|
||||||
@ -237,13 +236,6 @@
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
const change = data => {
|
|
||||||
console.log(data.value)
|
|
||||||
if (data.value === 3) {
|
|
||||||
list.value[data.value].value = 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const toLogin = () => {
|
const toLogin = () => {
|
||||||
uni.navigateTo({
|
uni.navigateTo({
|
||||||
url: '/pages/login/login'
|
url: '/pages/login/login'
|
||||||
|
|||||||
@ -146,7 +146,6 @@
|
|||||||
</text>
|
</text>
|
||||||
</Modal>
|
</Modal>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import { useBasicStore } from '@/store'
|
import { useBasicStore } from '@/store'
|
||||||
import { passwordRegExp, phoneRegExp } from '@/constants/regular-expressions'
|
import { passwordRegExp, phoneRegExp } from '@/constants/regular-expressions'
|
||||||
@ -209,7 +208,6 @@
|
|||||||
deviceModel: systemInfo.value?.deviceModel
|
deviceModel: systemInfo.value?.deviceModel
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
console.log(111111, result)
|
|
||||||
uni.hideLoading()
|
uni.hideLoading()
|
||||||
if (result.errorCode === Result.Success.code) {
|
if (result.errorCode === Result.Success.code) {
|
||||||
await uni.switchTab({
|
await uni.switchTab({
|
||||||
|
|||||||
@ -7,19 +7,20 @@ export const useUserStore = defineStore(
|
|||||||
'user',
|
'user',
|
||||||
() => {
|
() => {
|
||||||
const userInfo = ref<UserInfo>(null)
|
const userInfo = ref<UserInfo>(null)
|
||||||
const loginStatus = ref(false)
|
const loginStatus = ref<boolean>(false)
|
||||||
|
|
||||||
const getUserInfo = async () => {
|
const getUserInfo = async () => {
|
||||||
const result = await getUserInfoApi()
|
try {
|
||||||
if (result.errorCode === Result.Success.code) {
|
const result = await getUserInfoApi()
|
||||||
userInfo.value = result.data
|
if (result.errorCode === Result.Success.code) {
|
||||||
loginStatus.value = true
|
userInfo.value = result.data
|
||||||
uni.setStorageSync({
|
loginStatus.value = true
|
||||||
key: 'userInfo',
|
uni.setStorageSync('userInfo', userInfo.value)
|
||||||
data: userInfo.value
|
return new Result(result.errorCode, userInfo.value, result.errorMsg)
|
||||||
})
|
}
|
||||||
return new Result(result.errorCode, userInfo.value, result.errorMsg)
|
loginStatus.value = false
|
||||||
} else {
|
return Result.Fail
|
||||||
|
} catch (error) {
|
||||||
const userInfoStorage = uni.getStorageSync('userInfo')
|
const userInfoStorage = uni.getStorageSync('userInfo')
|
||||||
if (userInfoStorage) {
|
if (userInfoStorage) {
|
||||||
userInfo.value = userInfoStorage
|
userInfo.value = userInfoStorage
|
||||||
@ -32,8 +33,17 @@ export const useUserStore = defineStore(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const logout = () => {
|
||||||
|
userInfo.value = null
|
||||||
|
loginStatus.value = false
|
||||||
|
uni.removeStorageSync('userInfo')
|
||||||
|
uni.removeStorageSync('token')
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
userInfo,
|
userInfo,
|
||||||
|
loginStatus,
|
||||||
|
logout,
|
||||||
getUserInfo
|
getUserInfo
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
import { CustomRequestOptions } from '@/interceptors/request'
|
import { CustomRequestOptions } from '@/interceptors/request'
|
||||||
|
import { useUserStore } from '@/store'
|
||||||
|
|
||||||
export const http = <T>(options: CustomRequestOptions) => {
|
export const http = <T>(options: CustomRequestOptions) => {
|
||||||
return new Promise<IResData<T>>((resolve, reject) => {
|
return new Promise<IResData<T>>((resolve, reject) => {
|
||||||
@ -14,8 +15,15 @@ export const http = <T>(options: CustomRequestOptions) => {
|
|||||||
success(res) {
|
success(res) {
|
||||||
if (res.statusCode >= 200 && res.statusCode < 300) {
|
if (res.statusCode >= 200 && res.statusCode < 300) {
|
||||||
if (res.data.errorCode === 403) {
|
if (res.data.errorCode === 403) {
|
||||||
// userStore.clearUserInfo()
|
const $user = useUserStore()
|
||||||
// uni.navigateTo({ url: '/pages/login/login' })
|
$user.logout()
|
||||||
|
uni.switchTab({
|
||||||
|
url: '/pages/home/home'
|
||||||
|
})
|
||||||
|
uni.showToast({
|
||||||
|
icon: 'none',
|
||||||
|
title: '登录已过期,请重新登录'
|
||||||
|
})
|
||||||
reject(res)
|
reject(res)
|
||||||
} else {
|
} else {
|
||||||
resolve(res.data as IResData<T>)
|
resolve(res.data as IResData<T>)
|
||||||
@ -30,6 +38,7 @@ export const http = <T>(options: CustomRequestOptions) => {
|
|||||||
},
|
},
|
||||||
// 响应失败
|
// 响应失败
|
||||||
fail(err) {
|
fail(err) {
|
||||||
|
console.log('请求失败', err)
|
||||||
uni.showToast({
|
uni.showToast({
|
||||||
icon: 'none',
|
icon: 'none',
|
||||||
title: '网络错误,请重试'
|
title: '网络错误,请重试'
|
||||||
@ -37,7 +46,6 @@ export const http = <T>(options: CustomRequestOptions) => {
|
|||||||
reject(err)
|
reject(err)
|
||||||
},
|
},
|
||||||
complete(res) {
|
complete(res) {
|
||||||
console.log(1111, options)
|
|
||||||
console.log(options.url, {
|
console.log(options.url, {
|
||||||
env: import.meta.env.VITE_APP_ENV,
|
env: import.meta.env.VITE_APP_ENV,
|
||||||
statusCode: res?.statusCode,
|
statusCode: res?.statusCode,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user