2022-04-10 01:21:21 +08:00
|
|
|
|
<template>
|
2022-04-16 22:04:02 +08:00
|
|
|
|
<view class="container">
|
2022-04-10 01:21:21 +08:00
|
|
|
|
<view class="unp-header">
|
|
|
|
|
<view class="unp-logo">
|
|
|
|
|
<u-avatar size="80" icon="github-circle-fill" fontSize="80"></u-avatar>
|
|
|
|
|
</view>
|
|
|
|
|
</view>
|
|
|
|
|
|
|
|
|
|
<view class="unp-box">
|
2022-04-10 20:31:45 +08:00
|
|
|
|
<!-- 登录方式选择 -->
|
|
|
|
|
<view class="mode-section">
|
|
|
|
|
<u-subsection mode="subsection" fontSize="15" :list="loginModeList" :current="currentModeIndex" @change="handleModeChange"></u-subsection>
|
|
|
|
|
</view>
|
|
|
|
|
<u-gap height="40"></u-gap>
|
|
|
|
|
|
|
|
|
|
<!-- 登录表单 -->
|
2022-04-19 17:58:39 +08:00
|
|
|
|
<u--form labelPosition="left" :model="formData" :rules="rules" ref="form">
|
2022-04-10 20:31:45 +08:00
|
|
|
|
<u-form-item label="手机号" prop="mobile" labelWidth="60" borderBottom ref="item-mobile">
|
|
|
|
|
<u-input type="number" maxlength="11" v-model="formData.mobile" clearable placeholder="请填写手机号" border="none"></u-input>
|
2022-04-10 01:21:21 +08:00
|
|
|
|
</u-form-item>
|
|
|
|
|
|
|
|
|
|
<u-gap height="20"></u-gap>
|
|
|
|
|
|
2022-04-10 20:31:45 +08:00
|
|
|
|
<u-form-item v-if="currentModeIndex === 0" label="密码" prop="password" labelWidth="60" borderBottom ref="item-password">
|
2022-04-16 22:04:02 +08:00
|
|
|
|
<u-input :type="inputType" maxlength="16" v-model="formData.password" placeholder="请填写密码" border="none">
|
|
|
|
|
<template slot="suffix">
|
2022-04-10 01:21:21 +08:00
|
|
|
|
<u-icon v-if="inputType === 'password'" size="20" color="#666666" name="eye-fill" @click="inputType = 'text'"></u-icon>
|
|
|
|
|
<u-icon v-if="inputType === 'text'" size="20" color="#666666" name="eye-off" @click="inputType = 'password'"></u-icon>
|
2022-04-16 22:04:02 +08:00
|
|
|
|
</template>
|
|
|
|
|
</u-input>
|
2022-04-10 01:21:21 +08:00
|
|
|
|
</u-form-item>
|
|
|
|
|
|
2022-04-10 20:31:45 +08:00
|
|
|
|
<u-form-item v-else label="验证码" prop="code" labelWidth="60" borderBottom>
|
|
|
|
|
<u--input type="number" maxlength="4" v-model="formData.code" border="none" placeholder="请填写验证码"></u--input>
|
|
|
|
|
<u-button slot="right" @tap="getCode" :text="codeTips" type="success" size="mini" :disabled="codeDisabled"></u-button>
|
|
|
|
|
<u-code ref="uCode" @change="codeChange" seconds="60" @start="codeDisabled = true" @end="codeDisabled = false"></u-code>
|
|
|
|
|
</u-form-item>
|
|
|
|
|
|
2022-04-19 17:58:39 +08:00
|
|
|
|
<view class="btn-group">
|
|
|
|
|
<u-button type="primary" text="登录" customStyle="margin-top: 50px" @click="handleSubmit"></u-button>
|
2022-04-10 01:21:21 +08:00
|
|
|
|
|
2022-04-19 17:58:39 +08:00
|
|
|
|
<u-gap height="20"></u-gap>
|
|
|
|
|
<u-button type="info" text="返回" @click="navigateBack()"></u-button>
|
|
|
|
|
</view>
|
2022-04-10 01:21:21 +08:00
|
|
|
|
</u--form>
|
|
|
|
|
</view>
|
2022-04-16 22:04:02 +08:00
|
|
|
|
</view>
|
2022-04-10 01:21:21 +08:00
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script>
|
2022-04-16 22:04:02 +08:00
|
|
|
|
import { passwordLogin, sendSmsCode, smsLogin } from '../../common/api'
|
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
data() {
|
|
|
|
|
return {
|
|
|
|
|
currentModeIndex: 0,
|
|
|
|
|
loginModeList: ['密码登录', '验证码登录'],
|
|
|
|
|
inputType: 'password',
|
|
|
|
|
codeDisabled: false,
|
|
|
|
|
codeTips: '',
|
|
|
|
|
formData: {
|
2022-04-20 03:08:59 +08:00
|
|
|
|
mobile: '',
|
2022-04-16 22:04:02 +08:00
|
|
|
|
password: '',
|
|
|
|
|
code: ''
|
|
|
|
|
},
|
|
|
|
|
rules: {
|
|
|
|
|
mobile: [
|
|
|
|
|
{
|
2022-04-10 20:31:45 +08:00
|
|
|
|
type: 'integer',
|
2022-04-10 01:21:21 +08:00
|
|
|
|
required: true,
|
2022-04-16 22:04:02 +08:00
|
|
|
|
message: '请填写手机号',
|
2022-04-10 01:21:21 +08:00
|
|
|
|
trigger: ['blur', 'change']
|
2022-04-16 22:04:02 +08:00
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
// 自定义验证函数,见上说明
|
|
|
|
|
validator: (rule, value, callback) => {
|
|
|
|
|
// 上面有说,返回true表示校验通过,返回false表示不通过
|
|
|
|
|
// uni.$u.test.mobile()就是返回true或者false的
|
|
|
|
|
return uni.$u.test.mobile(value)
|
|
|
|
|
},
|
|
|
|
|
message: '手机号码不正确',
|
|
|
|
|
// 触发器可以同时用blur和change
|
|
|
|
|
trigger: ['change', 'blur']
|
2022-04-10 01:21:21 +08:00
|
|
|
|
}
|
2022-04-16 22:04:02 +08:00
|
|
|
|
],
|
|
|
|
|
password: {
|
|
|
|
|
type: 'string',
|
|
|
|
|
min: 4,
|
|
|
|
|
max: 16,
|
|
|
|
|
required: true,
|
|
|
|
|
message: '密码长度4-16位密码',
|
|
|
|
|
trigger: ['blur', 'change']
|
|
|
|
|
},
|
|
|
|
|
code: {
|
|
|
|
|
type: 'integer',
|
|
|
|
|
len: 4,
|
|
|
|
|
required: true,
|
|
|
|
|
message: '请填写4位验证码',
|
|
|
|
|
trigger: ['blur', 'change']
|
2022-04-10 01:21:21 +08:00
|
|
|
|
}
|
2022-04-16 22:04:02 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
onLoad() {},
|
|
|
|
|
onReady() {
|
|
|
|
|
// 如果需要兼容微信小程序,并且校验规则中含有方法等,只能通过setRules方法设置规则
|
|
|
|
|
this.$refs.form.setRules(this.rules)
|
|
|
|
|
},
|
|
|
|
|
methods: {
|
|
|
|
|
handleModeChange(index) {
|
|
|
|
|
if (index !== this.currentModeIndex) {
|
|
|
|
|
this.currentModeIndex = index
|
|
|
|
|
this.$refs.form.clearValidate()
|
|
|
|
|
}
|
2022-04-10 20:31:45 +08:00
|
|
|
|
},
|
2022-04-16 22:04:02 +08:00
|
|
|
|
codeChange(text) {
|
|
|
|
|
this.codeTips = text
|
|
|
|
|
},
|
|
|
|
|
getCode() {
|
|
|
|
|
const mobile = this.formData.mobile
|
|
|
|
|
if (!mobile) {
|
|
|
|
|
uni.$u.toast('请填写手机号')
|
|
|
|
|
} else if (!uni.$u.test.mobile(mobile)) {
|
|
|
|
|
uni.$u.toast('手机号格式不正确')
|
|
|
|
|
} else if (this.$refs.uCode.canGetCode) {
|
|
|
|
|
// 模拟向后端请求验证码
|
|
|
|
|
uni.showLoading({
|
|
|
|
|
title: '正在获取验证码'
|
|
|
|
|
})
|
2022-04-10 20:31:45 +08:00
|
|
|
|
|
2022-04-16 22:04:02 +08:00
|
|
|
|
//scene:1登陆获取验证码场景
|
2022-04-20 03:08:59 +08:00
|
|
|
|
sendSmsCode({ mobile: mobile, scene: 1 })
|
2022-04-16 22:04:02 +08:00
|
|
|
|
.then(res => {
|
2022-04-10 20:31:45 +08:00
|
|
|
|
//console.log(res)
|
2022-04-16 22:04:02 +08:00
|
|
|
|
uni.hideLoading()
|
2022-04-20 03:08:59 +08:00
|
|
|
|
if (res.code === 0) {
|
2022-04-10 20:31:45 +08:00
|
|
|
|
// 这里此提示会被this.start()方法中的提示覆盖
|
2022-04-16 22:04:02 +08:00
|
|
|
|
uni.$u.toast('验证码已发送')
|
2022-04-10 20:31:45 +08:00
|
|
|
|
// 通知验证码组件内部开始倒计时
|
2022-04-16 22:04:02 +08:00
|
|
|
|
this.$refs.uCode.start()
|
2022-04-10 20:31:45 +08:00
|
|
|
|
} else {
|
2022-04-20 03:08:59 +08:00
|
|
|
|
uni.$u.toast(res.msg)
|
2022-04-10 20:31:45 +08:00
|
|
|
|
}
|
|
|
|
|
})
|
2022-04-16 22:04:02 +08:00
|
|
|
|
.catch(err => {
|
|
|
|
|
uni.$u.toast('服务器接口请求异常')
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
uni.$u.toast('倒计时结束后再发送')
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
handleSubmit() {
|
|
|
|
|
this.$refs.form.validate().then(res => {
|
|
|
|
|
if (this.currentModeIndex === 0) {
|
2022-04-20 03:08:59 +08:00
|
|
|
|
this.handleLoginPromise(passwordLogin({ mobile: this.formData.mobile, password: this.formData.password }))
|
2022-04-16 22:04:02 +08:00
|
|
|
|
} else if (this.currentModeIndex === 1) {
|
2022-04-20 03:08:59 +08:00
|
|
|
|
this.handleLoginPromise(smsLogin({ mobile: this.formData.mobile, code: this.formData.code }))
|
2022-04-16 22:04:02 +08:00
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
},
|
2022-04-20 03:08:59 +08:00
|
|
|
|
handleLoginPromise(promise) {
|
|
|
|
|
promise
|
|
|
|
|
.then(res => {
|
|
|
|
|
if (res.code === 0) {
|
|
|
|
|
this.$store.commit('setToken', res.data)
|
|
|
|
|
uni.$u.toast('登录成功')
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
this.navigateBack()
|
|
|
|
|
}, 1000)
|
|
|
|
|
} else {
|
|
|
|
|
uni.$u.toast(res.msg)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.catch(err => {
|
|
|
|
|
uni.$u.toast('接口请求失败')
|
|
|
|
|
})
|
|
|
|
|
},
|
2022-04-16 22:04:02 +08:00
|
|
|
|
navigateBack() {
|
|
|
|
|
uni.navigateBack()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-10 01:21:21 +08:00
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
|
.unp-header {
|
|
|
|
|
height: 400rpx;
|
2022-04-16 22:04:02 +08:00
|
|
|
|
@include flex-center;
|
2022-04-10 01:21:21 +08:00
|
|
|
|
.unp-logo {
|
2022-04-16 22:04:02 +08:00
|
|
|
|
@include flex-center(column);
|
2022-04-10 01:21:21 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.unp-box {
|
2022-04-16 22:04:02 +08:00
|
|
|
|
@include flex-center(column);
|
2022-04-10 20:31:45 +08:00
|
|
|
|
|
2022-04-16 22:04:02 +08:00
|
|
|
|
.mode-section {
|
2022-04-10 20:31:45 +08:00
|
|
|
|
width: 560rpx;
|
|
|
|
|
}
|
2022-04-19 17:58:39 +08:00
|
|
|
|
.btn-group {
|
2022-04-10 01:21:21 +08:00
|
|
|
|
width: 560rpx;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.lk-group {
|
|
|
|
|
height: 40rpx;
|
|
|
|
|
margin-top: 40rpx;
|
2022-04-16 22:04:02 +08:00
|
|
|
|
@include flex-space-between;
|
2022-04-10 01:21:21 +08:00
|
|
|
|
font-size: 12rpx;
|
|
|
|
|
|
2022-04-11 23:02:08 +08:00
|
|
|
|
color: $u-primary;
|
|
|
|
|
text-decoration: $u-primary;
|
2022-04-10 01:21:21 +08:00
|
|
|
|
}
|
|
|
|
|
</style>
|