gree_leran/pages/user/login.vue

60 lines
1.0 KiB
Vue
Raw Normal View History

2024-06-16 01:04:56 +00:00
<template>
<view class="main">
<view class="" style="width: 80%;margin: 0 auto;">
<up-button type="primary" @click="login" text="登录"></up-button>
</view>
</view>
</template>
<script setup>
import {
loginByCode
} from "@/comm/api.js"
2024-06-16 15:32:15 +00:00
import {
setToken,
getToken
} from "@/utils/Auth.js"
2024-06-16 01:04:56 +00:00
async function login() {
const __this = this
uni.login({
provider: "weixin",
async success(e) {
2024-06-16 15:32:15 +00:00
console.log(e.code)
2024-06-16 01:04:56 +00:00
try {
const resp = await loginByCode({
code: e.code
})
2024-06-16 15:32:15 +00:00
console.log(resp, "resp")
2024-06-16 01:04:56 +00:00
uni.showToast({
2024-06-16 15:32:15 +00:00
icon: "none",
title: "登录成功"
2024-06-16 01:04:56 +00:00
})
setToken(resp.token)
2024-06-16 15:32:15 +00:00
setTimeout(() => {
2024-06-16 01:04:56 +00:00
uni.switchTab({
2024-06-16 15:32:15 +00:00
url: "/pages/user/user"
2024-06-16 01:04:56 +00:00
})
2024-06-16 15:32:15 +00:00
}, 800)
} catch (err) {
console.log(err,"errorCatch")
2024-06-16 01:04:56 +00:00
uni.showToast({
2024-06-16 15:32:15 +00:00
icon: "none",
title: "登录失败"
2024-06-16 01:04:56 +00:00
})
}
}
2024-06-16 15:32:15 +00:00
,
fail(err) {
console.log(err,"error")
}
2024-06-16 01:04:56 +00:00
})
}
</script>
<style>
.main {
height: 100vh;
display: flex;
align-items: center;
}
</style>