Files
uni-fans-score/pages/login/index.vue
T
2025-10-14 19:20:26 +08:00

162 lines
3.6 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<view class="login-container">
<view class="logo">
<image src="/static/logo.png" mode="aspectFit" />
<text class="app-name">风电者共享风扇&充电宝</text>
</view>
<view class="title">登录您的账号</view>
<view class="subtitle">为保障使用体验请先完成登录</view>
<!-- 微信一键手机号快捷登录推荐 -->
<button class="btn primary" open-type="getPhoneNumber" @getphonenumber="onGetPhoneNumber">
手机号快捷登录
</button>
<!-- 仅微信登录不授权手机号时使用 -->
<!-- <button class="btn outline" @click="onWeChatLogin">仅微信登录</button> -->
<view class="tips">
登录即表示同意
<text class="link" @tap="go('/pages/legal/agreement')">用户协议</text>
<text class="link" @tap="go('/pages/legal/privacy')">隐私政策</text>
</view>
</view>
</template>
<script setup>
import { ref } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import { wxLogin, getUserPhoneNumber, getUserInfo } from '../../util/index.js'
const redirect = ref('/pages/index/index')
const navigateAfterLogin = async () => {
try {
// 可选:刷新一次用户信息
await getUserInfo().catch(() => {})
} catch (e) {}
// 读取跳转路径(支持 tabBar 页面)
const target = '/pages/index/index'
const tabPages = ['/pages/index/index', '/pages/my/index']
if (tabPages.includes(target)) {
uni.reLaunch({ url: target })
return
}
uni.reLaunch({ url: target })
}
const onWeChatLogin = async () => {
try {
await wxLogin()
uni.showToast({ title: '登录成功', icon: 'success' })
await navigateAfterLogin()
} catch (error) {
uni.showToast({ title: error.message || '登录失败', icon: 'none' })
}
}
const onGetPhoneNumber = async (e) => {
if (!e || e.detail.errMsg !== 'getPhoneNumber:ok') {
uni.showToast({ title: '已取消手机号授权', icon: 'none' })
return
}
try {
// 先微信登录,获取 token
await wxLogin()
// 再用微信返回的临时 code 换取手机号
await getUserPhoneNumber(e.detail.code)
uni.showToast({ title: '登录成功', icon: 'success' })
await navigateAfterLogin()
} catch (error) {
uni.showToast({ title: error.message || '登录失败', icon: 'none' })
}
}
onLoad((opts) => {
if (opts && opts.redirect) {
try {
redirect.value = decodeURIComponent(opts.redirect)
} catch (_) {}
}
})
const go = (url) => {
uni.navigateTo({ url })
}
</script>
<style lang="scss" scoped>
.login-container {
min-height: 100vh;
background: #f8f8f8;
padding: 80rpx 40rpx 40rpx;
display: flex;
flex-direction: column;
align-items: center;
box-sizing: border-box;
.logo {
display: flex;
flex-direction: column;
align-items: center;
margin-bottom: 60rpx;
image {
width: 160rpx;
height: 160rpx;
margin-bottom: 16rpx;
}
.app-name {
font-size: 36rpx;
font-weight: 600;
color: #333;
}
}
.title {
font-size: 40rpx;
font-weight: 600;
color: #222;
margin-bottom: 12rpx;
}
.subtitle {
font-size: 26rpx;
color: #888;
margin-bottom: 60rpx;
}
.btn {
width: 100%;
height: 96rpx;
border-radius: 48rpx;
font-size: 32rpx;
margin-bottom: 24rpx;
}
.primary {
background: #07c160;
color: #fff;
box-shadow: 0 8rpx 24rpx rgba(7, 193, 96, 0.3);
}
.outline {
background: #fff;
color: #07c160;
border: 2rpx solid #07c160;
}
.tips {
margin-top: 24rpx;
font-size: 22rpx;
color: #999;
.link { color: #07c160; }
}
}
</style>