<template>
<view class="wrap">
<view class="bgwhite info">
<view class="item">
<view class="label">账号</view>
<input placeholder="请输入账号" v-model="account"/>
</view>
<view class="item">
<view class="label">密码</view>
<input placeholder="请输入密码" v-model="passwd" type="password"/>
</view>
</view>
<view class="bgwhite info auto">
<view class="item">
<view class="label">自动登录</view>
<switch :checked="autologin" @change="setAuto"/>
</view>
</view>
<button type="primary" class="login" @click="doLogin">登录</button>
<view class="reg">
<a href="#">注册账号</a>
<view class="line">|</view>
<a href="#">忘记密码</a>
</view>
<view class="footer">
<image src=""></image>
<image src=""></image>
<image src=""></image>
</view>
</view>
</template>
<script>
import store from '@/store/index.js';//vuex
export default {
data() {
return {
account:'',//存储账号信息
passwd:'',//存储密码
autologin:false//存储是否自动登录
}
},
onLoad() {
let token=uni.getStorageSync('token');//获取storage中的token,判断上次是否是自动登录保存了身份token
if(token){//自动登录
store.commit('setToken',token);//更新vuex
uni.redirectTo({url:'/pages/index/index'});//跳转到需要的页面,这里跳转首页
}
},
methods: {
setAuto(e){this.autologin=e.detail.value;},
doLogin(){
if(this.account==''){uni.showModal({content:'请输入账号!',showCancel:false});return false}
if(this.passwd==''){uni.showModal({content:'请输入密码!',showCancel:false});return false}
//显示加载等待层
uni.showLoading({mask:true,title:'正在验证信息...'});
//验证通过发送请求,un.request更多配置参考:https://uniapp.dcloud.net.cn/api/request/request.html#request
uni.request({
url:'http://124.93.196.45:10001/prod-api/api/login',
method:'POST',
data:{
username:this.account,
password:this.passwd
},
headers:{'content-type':'application/json'},
success:(res)=>{
if(res.data.code==200){//成功,跳转用户后台或者需要的页面
if(this.autologin){//自动登录,保存token到localStorage中
uni.setStorageSync('token',res.data.token);
store.commit('setToken',token);//更新vuex,以便其他页面使用
}
else uni.removeStorageSync('token');
uni.redirectTo({url:'/pages/index/index'});
}
//失败提示错误信息
else uni.showModal({content:res.data.msg,showCancel:false});
},
fail(res){
uni.showModal({content:'请求失败!',showCancel:false});
},
complete(){uni.hideLoading()}
})
}
}
}
</script>
<style>
page{background-color: #f0eff4;font-size: 16px;}
.wrap{max-width:480px;margin: 0 auto;}
.bgwhite{background-color: #fff;}
.info{margin-top: 25px;border:solid 1px #ccc;padding:0 20px;margin-bottom:10px;}
.info .item{display: flex;padding:10px 0;}
.info .item:first-of-type{border-bottom: solid 1px #eee;}
.info .item .label{margin-right:30px;}
input{flex-grow:1}
.auto{margin-top: 10px;}
.auto .item{line-height: 30px;justify-content: space-between}
.login{line-height: 55px;margin: 30px 0;font-size: 18px;}
.reg{text-align: center;}
.reg .line{padding:0 10px;display: inline-block;}
.reg a{color: #6096b9;text-decoration: none;}
.footer{position:absolute;bottom:40px;text-align:center;left:0;width:100%;}
.footer image{border-radius: 50%;border:solid 1px #eee;background-color: #ccc;width:60px;height: 60px;margin-right: 30px;display: inline-block;}
.footer image:last-of-type{margin-right: 0;}
</style>
设置pages.json中这个页面的style
{
"pages": [
{
"path" : "pages/loginpage/loginpage",
"style" :
{
"navigationBarTitleText": "智慧城市",
"enablePullDownRefresh": false,
"navigationBarBackgroundColor": "#f7f7f7"
}
}]
......其他配置
}