edu/app/src/pages/login/login.vue

367 lines
12 KiB
Vue
Raw Normal View History

2022-09-07 08:10:28 +00:00
<template>
2022-09-07 13:00:03 +00:00
<view
class="bg-white login min-h-full flex flex-col items-center px-[40rpx] pt-[80rpx] box-border"
>
2022-09-07 08:10:28 +00:00
<view>
2022-09-08 11:09:53 +00:00
<u-image :src="appStore.config.website.logo" mode="widthFix" height="160" width="160" />
2022-09-07 08:10:28 +00:00
</view>
2022-09-08 11:09:53 +00:00
<view class="mt-4">{{ appStore.config.website.name }}</view>
2022-09-15 06:15:12 +00:00
<view class="w-full mt-[60rpx] pb-[60rpx]">
2022-09-07 12:58:01 +00:00
<u-form borderBottom>
2022-09-13 09:41:24 +00:00
<template
v-if="loginWay == LoginWayEnum.ACCOUNT && includeLoginWay(LoginWayEnum.ACCOUNT)"
>
2022-09-07 12:58:01 +00:00
<u-form-item borderBottom>
2022-09-08 08:28:56 +00:00
<u-icon class="mr-2" :size="36" name="/static/images/icon/icon_user.png" />
2022-09-07 12:58:01 +00:00
<u-input
class="flex-1"
v-model="formData.username"
:border="false"
placeholder="请输入账号/手机号码"
/>
</u-form-item>
<u-form-item borderBottom>
2022-09-08 08:28:56 +00:00
<u-icon
class="mr-2"
:size="36"
name="/static/images/icon/icon_password.png"
/>
2022-09-07 12:58:01 +00:00
<u-input
class="flex-1"
v-model="formData.password"
type="password"
placeholder="请输入密码"
:border="false"
/>
2022-09-09 01:50:39 +00:00
<navigator url="/pages/forget_pwd/forget_pwd" hover-class="none">
<view
class="border-l border-solid border-0 border-light pl-3 text-muted leading-4 ml-3"
>
忘记密码
</view>
</navigator>
2022-09-07 12:58:01 +00:00
</u-form-item>
</template>
2022-09-13 09:41:24 +00:00
<template
v-if="loginWay == LoginWayEnum.MOBILE && includeLoginWay(LoginWayEnum.MOBILE)"
>
2022-09-07 12:58:01 +00:00
<u-form-item borderBottom>
2022-09-08 08:28:56 +00:00
<u-icon
class="mr-2"
:size="36"
name="/static/images/icon/icon_mobile.png"
/>
2022-09-07 12:58:01 +00:00
<u-input
class="flex-1"
v-model="formData.mobile"
:border="false"
placeholder="请输入手机号码"
/>
</u-form-item>
<u-form-item borderBottom>
2022-09-08 08:28:56 +00:00
<u-icon class="mr-2" :size="36" name="/static/images/icon/icon_code.png" />
2022-09-07 12:58:01 +00:00
<u-input
class="flex-1"
v-model="formData.code"
placeholder="请输入验证码"
:border="false"
/>
<view
class="border-l border-solid border-0 border-light pl-3 text-muted leading-4 ml-3 w-[180rpx]"
@click="sendSms"
>
<u-verification-code
ref="uCodeRef"
:seconds="60"
@change="codeChange"
change-text="x秒"
/>
{{ codeTips }}
</view>
</u-form-item>
</template>
2022-09-07 08:10:28 +00:00
</u-form>
2022-09-13 09:41:24 +00:00
<view class="mt-[40rpx]" v-if="isOpenAgreement">
2022-09-07 12:58:01 +00:00
<u-checkbox v-model="isCheckAgreement" shape="circle">
<view class="text-xs flex">
已阅读并同意
2022-09-09 07:59:18 +00:00
<navigator
@click.stop=""
class="text-primary"
hover-class="none"
url="/pages/agreement/agreement?type=service"
>
服务协议
</navigator>
<navigator
@click.stop=""
class="text-primary"
hover-class="none"
url="/pages/agreement/agreement?type=privacy"
>
2022-09-09 01:50:39 +00:00
隐私协议
</navigator>
2022-09-07 12:58:01 +00:00
</view>
</u-checkbox>
</view>
2022-09-13 09:41:24 +00:00
<view class="mt-[60rpx]">
<u-button type="primary" shape="circle" @click="handleLogin(formData.scene)">
2022-09-07 12:58:01 +00:00
</u-button>
</view>
<view class="text-content flex justify-between mt-[40rpx]">
2022-09-13 09:41:24 +00:00
<view class="flex-1">
<view
v-if="
loginWay == LoginWayEnum.MOBILE && includeLoginWay(LoginWayEnum.ACCOUNT)
"
@click="changeLoginWay(LoginTypeEnum.ACCOUNT, LoginWayEnum.ACCOUNT)"
>
账号密码登录
</view>
<view
v-if="
loginWay == LoginWayEnum.ACCOUNT && includeLoginWay(LoginWayEnum.MOBILE)
"
@click="changeLoginWay(LoginTypeEnum.MOBILE, LoginWayEnum.MOBILE)"
>
短信验证码登录
</view>
2022-09-07 12:58:01 +00:00
</view>
2022-09-13 09:41:24 +00:00
2022-09-09 01:50:39 +00:00
<navigator url="/pages/register/register" hover-class="none">注册账号</navigator>
2022-09-07 12:58:01 +00:00
</view>
2022-09-15 02:53:31 +00:00
<!-- #ifdef MP-WEIXIN || H5 -->
2022-09-13 09:41:24 +00:00
<view class="mt-[80rpx]" v-if="isOpenOtherAuth">
2022-09-07 12:58:01 +00:00
<u-divider>第三方登录</u-divider>
<div class="flex justify-center mt-[40rpx]">
2022-09-13 09:41:24 +00:00
<div
v-if="includeAuthWay(LoginAuthEnum.WX)"
class="flex flex-col items-center"
@click="wxLogin"
>
2022-09-15 02:53:31 +00:00
<img src="@/static/images/icon/icon_wx.png" class="w-[80rpx] h-[80rpx]" />
2022-09-07 12:58:01 +00:00
<div class="text-sm mt-[10px]">微信登录</div>
</div>
</div>
</view>
2022-09-09 01:50:39 +00:00
<!-- #endif -->
2022-09-07 08:10:28 +00:00
</view>
</view>
</template>
<script setup lang="ts">
2022-09-07 12:58:01 +00:00
import { login } from '@/api/account'
import { smsSend } from '@/api/app'
import { SMSEnum } from '@/enums/appEnums'
2022-09-08 08:28:56 +00:00
import { useLockFn } from '@/hooks/useLockFn'
2022-09-08 11:09:53 +00:00
import { useAppStore } from '@/stores/app'
2022-09-08 08:28:56 +00:00
import { useUserStore } from '@/stores/user'
2022-09-15 02:53:31 +00:00
import { isWeixinClient } from '@/utils/client'
2022-09-09 09:58:18 +00:00
import { currentPage } from '@/utils/util'
2022-09-15 02:53:31 +00:00
// #ifdef H5
import wechatOa from '@/utils/wechat'
// #endif
import { onLoad, onShow } from '@dcloudio/uni-app'
2022-09-13 09:41:24 +00:00
import { computed, reactive, ref, shallowRef, watch } from 'vue'
2022-09-07 12:58:01 +00:00
enum LoginTypeEnum {
MOBILE = 'mobile',
ACCOUNT = 'account',
MNP = 'mnp'
}
2022-09-13 09:41:24 +00:00
enum LoginWayEnum {
ACCOUNT = 1,
MOBILE = 2
}
enum LoginAuthEnum {
WX = 1,
QQ = 2
}
2022-09-15 02:53:31 +00:00
const isWeixin = ref(true)
// #ifdef H5
// isWeixin.value = isWeixinClient()
// #endif
2022-09-13 09:41:24 +00:00
const userStore = useUserStore()
const appStore = useAppStore()
2022-09-07 12:58:01 +00:00
const uCodeRef = shallowRef()
2022-09-13 09:41:24 +00:00
const loginWay = ref<LoginWayEnum>()
2022-09-07 12:58:01 +00:00
const codeTips = ref('')
const isCheckAgreement = ref(false)
2022-09-08 08:28:56 +00:00
2022-09-07 08:10:28 +00:00
const formData = reactive({
2022-09-13 09:41:24 +00:00
scene: '',
2022-09-07 12:58:01 +00:00
username: '',
password: '',
code: '',
2022-09-07 08:10:28 +00:00
mobile: ''
})
2022-09-07 12:58:01 +00:00
const codeChange = (text: string) => {
codeTips.value = text
}
const sendSms = async () => {
if (!formData.mobile) return uni.$u.toast('请输入手机号码')
if (uCodeRef.value?.canGetCode) {
await smsSend({
scene: SMSEnum.LOGIN,
mobile: formData.mobile
})
uni.$u.toast('发送成功')
uCodeRef.value?.start()
}
}
2022-09-08 08:28:56 +00:00
2022-09-13 09:41:24 +00:00
const changeLoginWay = (type: LoginTypeEnum, way: LoginWayEnum) => {
formData.scene = type
loginWay.value = way
}
const includeLoginWay = (way: LoginWayEnum) => {
return appStore.getLoginConfig.loginWay.includes(way)
}
const includeAuthWay = (way: LoginAuthEnum) => {
return appStore.getLoginConfig.autoLoginAuth.includes(way)
}
const isOpenAgreement = computed(() => appStore.getLoginConfig.openAgreement == 1)
const isOpenOtherAuth = computed(() => appStore.getLoginConfig.openOtherAuth == 1)
const isForceBindMobile = computed(() => appStore.getLoginConfig.forceBindMobile == 1)
2022-09-08 08:28:56 +00:00
const loginFun = async (scene: LoginTypeEnum, code?: string) => {
2022-09-13 09:41:24 +00:00
if (!isCheckAgreement.value && isOpenAgreement.value)
return uni.$u.toast('请勾选已阅读并同意《服务协议》和《隐私协议》')
2022-09-07 12:58:01 +00:00
if (scene == LoginTypeEnum.ACCOUNT) {
if (!formData.username) return uni.$u.toast('请输入账号/手机号码')
if (!formData.password) return uni.$u.toast('请输入密码')
}
if (scene == LoginTypeEnum.MOBILE) {
if (!formData.mobile) return uni.$u.toast('请输入手机号码')
if (!formData.code) return uni.$u.toast('请输入验证码')
}
2022-09-07 13:00:03 +00:00
const params = {
...formData,
scene
}
if (code) params.code = code
2022-09-08 08:28:56 +00:00
uni.showLoading({
title: '请稍后...'
})
try {
const data = await login(params)
2022-09-15 02:53:31 +00:00
loginHandle(data)
2022-09-08 08:28:56 +00:00
} catch (error: any) {
uni.hideLoading()
throw new Error(error)
}
2022-09-07 12:58:01 +00:00
}
2022-09-15 02:53:31 +00:00
const loginHandle = async (data: any) => {
const { token, isBindMobile } = data
if (!isBindMobile && isForceBindMobile.value) {
userStore.temToken = token
uni.navigateTo({
url: '/pages/bind_mobile/bind_mobile'
})
uni.hideLoading()
return
}
userStore.login(data.token)
await userStore.getUser()
uni.$u.toast('登录成功')
uni.hideLoading()
uni.navigateBack({
success: () => {
// @ts-ignore
const { onLoad, options } = currentPage()
// 刷新上一个页面
onLoad && onLoad(options)
}
})
}
2022-09-13 09:41:24 +00:00
const { lockFn: handleLogin } = useLockFn(loginFun)
2022-09-08 08:28:56 +00:00
2022-09-07 12:58:01 +00:00
const wxLogin = async () => {
2022-09-15 02:53:31 +00:00
// #ifdef MP-WEIXIN
2022-09-07 12:58:01 +00:00
const data: any = await uni.login({
provider: 'weixin'
})
2022-09-08 08:28:56 +00:00
handleLogin(LoginTypeEnum.MNP, data.code)
2022-09-15 02:53:31 +00:00
// #endif
// #ifdef H5
if (isWeixin.value) {
wechatOa.getUrl()
}
// #endif
2022-09-07 12:58:01 +00:00
}
2022-09-13 09:41:24 +00:00
watch(
() => appStore.getLoginConfig,
(value) => {
if (value.loginWay) {
loginWay.value = value.loginWay[0]
//@ts-ignore
formData.scene = LoginTypeEnum[LoginWayEnum[loginWay.value]]
}
},
{
immediate: true
}
)
onShow(async () => {
try {
if (userStore.isLogin) {
uni.showLoading({
title: '请稍后...'
})
await userStore.getUser()
uni.hideLoading()
uni.navigateBack()
}
} catch (error) {
uni.hideLoading()
}
})
2022-09-15 02:53:31 +00:00
onLoad(async (options) => {
if (userStore.isLogin) {
// 已经登录 => 首页
uni.reLaunch({
url: '/pages/index/index'
})
return
}
const { code } = options
if (code) {
uni.showLoading({
title: '请稍后...'
})
// #ifdef H5
try {
const data = await wechatOa.authLogin(code)
loginHandle(data)
} catch (error: any) {
uni.hideLoading()
throw new Error(error)
}
// #endif
}
})
2022-09-07 08:10:28 +00:00
</script>
<style lang="scss">
page {
height: 100%;
}
</style>