初步完善密码登录和验证码登录的逻辑

This commit is contained in:
sfmind 2022-04-10 20:31:45 +08:00
parent 7fd8e0dd6c
commit e10b707588
3 changed files with 131 additions and 46 deletions

View File

@ -1,7 +1,15 @@
const { http } = uni.$u const { http } = uni.$u
/* login */
//使用手机 + 密码登录
export const passwordLogin = params => http.post('/app-api/member/user/login', params)
//发送手机验证码
export const sendSmsCode = params => http.post('/app-api/member/send-sms-code', params)
//使用手机 + 验证码登录
export const smsLogin = params => http.post('/app-api/member/sms-login', params)
/* index */ /* index */
// 获取滚动图数据 // 获取滚动图数据
export const getBannerData = params => http.get('/api/index', params) export const getBannerData = params => http.get('/app-api/index', params)
// 获取滚动通知数据 // 获取滚动通知数据
export const getNoticeData = params => http.get('/api/notice', params) export const getNoticeData = params => http.get('/app-api/notice', params)

View File

@ -8,15 +8,22 @@
</view> </view>
<view class="unp-box"> <view class="unp-box">
<!-- 登录方式选择 -->
<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>
<!-- 登录表单 -->
<u--form class="unp-form" labelPosition="left" :model="formData" :rules="rules" ref="form"> <u--form class="unp-form" labelPosition="left" :model="formData" :rules="rules" ref="form">
<u-form-item label="账号" prop="username" borderBottom ref="item-username"> <u-form-item label="手机号" prop="mobile" labelWidth="60" borderBottom ref="item-mobile">
<u-input type="text" maxlength="20" v-model="formData.username" clearable placeholder="账号由数字和字母组成" border="none" @change="handleUsernameChange"></u-input> <u-input type="number" maxlength="11" v-model="formData.mobile" clearable placeholder="请填写手机号" border="none"></u-input>
</u-form-item> </u-form-item>
<u-gap height="20"></u-gap> <u-gap height="20"></u-gap>
<u-form-item label="密码" prop="password" borderBottom ref="item-password"> <u-form-item v-if="currentModeIndex === 0" label="密码" prop="password" labelWidth="60" borderBottom ref="item-password">
<u-input :type="inputType" maxlength="20" v-model="formData.password" placeholder="密码由数字、字母和符号组成" border="none" @change="handlePasswordChange"> <u-input :type="inputType" maxlength="16" v-model="formData.password" placeholder="请填写密码" border="none">
<template slot="suffix"> <template slot="suffix">
<u-icon v-if="inputType === 'password'" size="20" color="#666666" name="eye-fill" @click="inputType = 'text'"></u-icon> <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> <u-icon v-if="inputType === 'text'" size="20" color="#666666" name="eye-off" @click="inputType = 'password'"></u-icon>
@ -24,55 +31,75 @@
</u-input> </u-input>
</u-form-item> </u-form-item>
<view class="lk-group"> <u-form-item v-else label="验证码" prop="code" labelWidth="60" borderBottom>
<navigator url="/pages/register/register" hover-class="none"> <u--input type="number" maxlength="4" v-model="formData.code" border="none" placeholder="请填写验证码"></u--input>
<text class="register">注册账号</text> <u-button slot="right" @tap="getCode" :text="codeTips" type="success" size="mini" :disabled="codeDisabled"></u-button>
</navigator> <u-code ref="uCode" @change="codeChange" seconds="60" @start="codeDisabled = true" @end="codeDisabled = false"></u-code>
<navigator url="/pages/forgot/forgot" hover-class="none"> </u-form-item>
<text class="forgot">忘记密码</text>
</navigator>
</view>
<u-button <u-button type="primary" text="登录" customStyle="margin-top: 50px" @click="handleSubmit"></u-button>
type="primary"
text="登录"
customStyle="margin-top: 50px"
@click="handleSubmit"
></u-button>
<u-gap height="20"></u-gap> <u-gap height="20"></u-gap>
<u-button type="info" text="返回" @click="navigateBack()"></u-button> <u-button type="info" text="返回" @click="navigateBack()"></u-button>
</u--form> </u--form>
</view> </view>
</view> </view>
</template> </template>
<script> <script>
import {passwordLogin,sendSmsCode,smsLogin} from "../../common/api";
export default { export default {
data() { data() {
return { return {
//ID
agent: 1,
currentModeIndex: 0,
loginModeList: ['密码登录', '验证码登录'],
inputType: 'password', inputType: 'password',
codeDisabled: false,
codeTips: '',
formData: { formData: {
username: '', mobile: '15601691234',
password: '', password: '',
code: ''
}, },
rules: { rules: {
'username': { 'mobile': [
type: 'string', {
max: 20, type: 'integer',
required: true, required: true,
message: '请输入您的账号', message: '请填写手机号',
trigger: ['blur', 'change'] trigger: ['blur', 'change']
}, },
{
//
validator: (rule, value, callback) => {
// truefalse
// uni.$u.test.mobile()truefalse
return uni.$u.test.mobile(value);
},
message: '手机号码不正确',
// blurchange
trigger: ['change','blur'],
}
],
'password': { 'password': {
type: 'string', type: 'string',
max: 20, min: 4,
max: 16,
required: true, required: true,
message: '请输入您的密码', message: '密码长度4-16位密码',
trigger: ['blur', 'change']
},
'code': {
type: 'integer',
len: 4,
required: true,
message: '请填写4位验证码',
trigger: ['blur', 'change'] trigger: ['blur', 'change']
} }
} }
@ -80,25 +107,69 @@
}, },
onLoad() { onLoad() {
},onReady(){
// setRules
this.$refs.form.setRules(this.rules)
}, },
methods: { methods: {
handleUsernameChange(e){ handleModeChange(index) {
let str = uni.$u.trim(e, 'all'); if (index !== this.currentModeIndex){
this.$nextTick(() => { this.currentModeIndex = index;
this.formData.username = str this.$refs.form.clearValidate();
}) }
}, },
handlePasswordChange(e){ codeChange(text) {
let str = uni.$u.trim(e, 'all'); this.codeTips = text;
this.$nextTick(() => { },
this.formData.password = str 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: '正在获取验证码'
});
//scene:1
sendSmsCode({agent: 1, mobile:mobile, scene:1 }).then(res => {
//console.log(res)
uni.hideLoading();
if (res.data.code === 0){
// this.start()
uni.$u.toast('验证码已发送');
//
this.$refs.uCode.start();
} else {
uni.$u.toast(res.data.msg);
}
}).catch(err => {
uni.$u.toast('服务器接口请求异常');
}) })
} else {
uni.$u.toast('倒计时结束后再发送');
}
}, },
handleSubmit() { handleSubmit() {
this.$refs.form.validate().then(res => { this.$refs.form.validate().then(res => {
uni.$u.toast('点击了登录') uni.$u.toast('登录');
if (this.currentModeIndex === 0){
passwordLogin({agent: 1, mobile:this.formData.mobile, password:this.formData.password}).then(res => {
if (res.data.code === 0){
uni.$u.toast('登录成功');
// TODO toke
} else {
uni.$u.toast(res.data.msg);
// TODO
}
}).catch(err => { }).catch(err => {
uni.$u.toast('服务器接口请求异常');
})
} else if (this.currentModeIndex === 1) {
smsLogin({agent: 1, mobile:this.formData.mobile, code:this.formData.code})
}
}) })
}, },
navigateBack() { navigateBack() {
@ -123,11 +194,16 @@
} }
} }
.unp-box { .unp-box {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
align-items: center; align-items: center;
justify-content: center; justify-content: center;
.mode-section{
width: 560rpx;
}
.unp-form{ .unp-form{
width: 560rpx; width: 560rpx;
} }

View File

@ -65,6 +65,7 @@
}, },
methods: { methods: {
handleUserInfoClick(){ handleUserInfoClick(){
// TODO
if (!uni.getStorageSync('token')) { if (!uni.getStorageSync('token')) {
uni.$u.route('/pages/login/login'); uni.$u.route('/pages/login/login');
} else { } else {