40 lines
844 B
TypeScript
40 lines
844 B
TypeScript
import { createToken } from '@/api/token'
|
|
import { defineStore } from 'pinia'
|
|
import { computed, ref } from 'vue'
|
|
|
|
export const useAppStore = defineStore(
|
|
'app',
|
|
() => {
|
|
const token = ref('')
|
|
|
|
const login = () => {
|
|
uni.showLoading({ title: '登录中' })
|
|
return new Promise<void>((resolve, reject) => {
|
|
uni.login({
|
|
provider: 'weixin',
|
|
success: async (result) => {
|
|
const data = await createToken(result.code)
|
|
token.value = data.token
|
|
uni.hideLoading()
|
|
resolve()
|
|
},
|
|
fail: async (error) => {
|
|
uni.hideLoading()
|
|
reject(error)
|
|
},
|
|
})
|
|
})
|
|
}
|
|
|
|
const isLogin = computed(() => {
|
|
return !!token.value
|
|
})
|
|
|
|
return {
|
|
token,
|
|
isLogin,
|
|
login,
|
|
}
|
|
},
|
|
)
|