NurseStationApp/pages/login/login.vue
2022-11-01 14:54:09 +08:00

262 lines
7.5 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="app">
<view class="title">
账号登录
</view>
<view class="loginphone">
<input class="phone" type="text" placeholder="手机号码" maxlength="11" v-model="phone" />
</view>
<view class="logincode loginphone" style="top: 40%;" v-if='switchs'>
<u-input class='code phone' placeholder="密码" maxlength="10" v-model="password" type="password"
:border="true" :password-icon="true" />
</view>
<view class="logincode loginphone" style="top: 40%;" v-else>
<input class="code phone" type="text" placeholder="验证码" maxlength="6" v-model="verification" />
<view class="obtaincode get-code" :style="{'color':getCodeBtnColor}" @click.stop="getCode()">
{{getCodeText}}
</view>
</view>
<view class="loginbtn" @tap='pwdlogin' v-if='switchs'>
登录
</view>
<view class="loginbtn" @tap='verificationlogin' v-else>
登录
</view>
<view class="switch" @tap='switchs=!switchs'>
{{switchs?'切换手机验证码登录':'切换密码登录'}}
</view>
<view class="switch" @tap='goregister' style="left:55%" v-if="switchs">
没有账号,请先注册
</view>
<u-toast ref="uToast" />
</view>
</template>
<script>
import {
appLoginpwd,
appLoginvfn
} from '../../api/login/index.js'
export default {
data() {
return {
phone: '13645678913',
password: '123456',
verification: '111111',
getCodeText: '获取验证码', //获取验证码的文字
getCodeBtnColor: "#4C7BC9", //获取验证码的color
getCodeisWaiting: false, //判断是否在倒计时中
switchs: false, //切换验证码 密码登录
};
},
//获取到传值
onLoad(options) {
this.switchs = options.switchs
},
methods: {
//验证码登录
verificationlogin() {
appLoginvfn(this.phone, this.verification).then(res => {
console.log(res)
if (res.code == 200) {
if (res.data.registerFlag == false) {
this.$refs.uToast.show({
title: res.data.message,
type: 'success',
url: '',
duration: '1500'
})
setInterval(() => {
//TODO
uni.switchTab({
url: '/pages/homepage/homepage',
})
}, 1500);
} else {
this.$refs.uToast.show({
title: res.data.message,
type: 'error'
})
}
} else {
console.log(res)
this.$refs.uToast.show({
title: res.msg,
type: 'error'
})
}
})
},
//密码登录
pwdlogin() {
appLoginpwd(this.phone, this.password).then(res => {
if (res.code == 200) {
if (res.data.registerFlag == false) {
this.$refs.uToast.show({
title: res.data.message,
type: 'success',
url: '',
duration: '1500'
})
setInterval(() => {
//TODO
uni.switchTab({
url: '/pages/homepage/homepage',
})
}, 1500);
} else {
this.$refs.uToast.show({
title: res.data.message,
type: 'error'
})
}
} else {
console.log(res)
this.$refs.uToast.show({
title: res.msg,
type: 'error'
})
}
})
},
//跳转注册页
goregister() {
uni.navigateTo({
url: '/pages/register/register'
})
},
//点击获取验证码
getCode() {
uni.hideKeyboard() //隐藏已经显示的软键盘,如果软键盘没有显示则不做任何操作。
if (this.getCodeisWaiting) { //是否在倒计时中
return;
}
if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(this.phone))) { //校验手机号码是否有误
uni.showToast({
title: '请填写正确手机号码',
icon: "none"
});
return false;
}
this.getCodeText = "发送中..." //发送验证码
this.getCodeisWaiting = true;
this.getCodeBtnColor = "rgba(138,139,133,1)" //追加样式,修改颜色
//示例用定时器模拟请求效果
//setTimeout(()用于在指定的毫秒数后调用函数或计算表达式
setTimeout(() => {
uni.showToast({
title: '验证码已发送',
icon: "none"
}); //弹出提示框
// this.code = '1234'; //发送验证码,进行填入 示例默认1234生产中请删除这一句。
this.setTimer(); //调用定时器方法
}, 1000)
},
//获取验证码的倒计时 setTimer 需要每隔一段时间执行一件事的的时候就需要使用SetTimer函数
setTimer() {
let holdTime = 60; //定义变量并赋值
this.getCodeText = "重新获取(60)"
//setInterval是一个实现定时调用的函数可按照指定的周期以毫秒计来调用函数或计算表达式。
//setInterval方法会不停地调用函数直到 clearInterval被调用或窗口被关闭。
this.Timer = setInterval(() => {
if (holdTime <= 0) {
this.getCodeisWaiting = false;
this.getCodeBtnColor = "#4C7BC9";
this.getCodeText = "获取验证码"
clearInterval(this.Timer); //清除该函数
return; //返回前面
}
this.getCodeText = "重新获取(" + holdTime + ")"
holdTime--;
}, 1000)
},
doReg() {
// uni.hideKeyboard() //隐藏已经显示的软键盘,如果软键盘没有显示则不做任何操作。
// //模板示例部分验证规则
// if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(this.phoneNumber))) { //校验手机号码
// uni.showToast({
// title: '请填写正确手机号码',
// icon: "none"
// });
// return false;
// }
// //示例验证码,实际使用中应为请求服务器比对验证码是否正确。
// if (this.code != 1234) {
// uni.showToast({
// title: '验证码不正确',
// icon: "none"
// });
// return false;
// }
// uni.showLoading({
// title: '提交中...'
// })
// //模板示例把用户注册信息储存在本地,实际使用中请替换为上传服务器。
// setTimeout(() => {
// uni.getStorage({
// key: 'UserList',
// success: (res) => {
// //增加记录密码md5
// res.data.push({
// username: this.phoneNumber,
// passwd: md5(this.passwd)
// })
// uni.setStorage({
// key: 'UserList',
// data: res.data,
// success: function() {
// uni.hideLoading()
// uni.showToast({
// title: '注册成功',
// icon: "success"
// });
// setTimeout(function() {
// uni.navigateBack();
// }, 1000)
// }
// });
// },
// fail: (e) => {
// uni.hideLoading()
// console.log('error');
// //新建UserList
// uni.setStorage({
// key: 'UserList',
// data: [{
// username: this.phoneNumber,
// passwd: md5(this.passwd)
// }],
// success: function() {
// uni.hideLoading()
// uni.showToast({
// title: '注册成功',
// icon: "success"
// });
// setTimeout(function() {
// uni.navigateBack();
// }, 1000)
// },
// fail: function(e) {
// console.log('set error:' + JSON.stringify(e));
// }
// });
// }
// });
// }, 1000)
},
toLogin() {
// uni.hideKeyboard() //隐藏已经显示的软键盘,如果软键盘没有显示则不做任何操作。
// uni.redirectTo({
// url: 'login'
// }); //跳转到登录页
// uni.navigateBack(); //关闭当前页面,返回上一页面
}
},
}
</script>
<style lang="scss">
@import "./login.scss";
</style>