<template>
<view>
<view class="u-page login-from">
<u--form labelPosition="top" labelWidth="auto" :model="formModel" :rules="rules" ref="uform">
<u-form-item label="用户名" prop="userName" borderBottom ref="item1">
<u--input v-model="formModel.userName" border="none"></u--input>
</u-form-item>
<u-form-item label="密码" prop="password" borderBottom ref="item2">
<u--input v-model="formModel.password" border="none" type="password"></u--input>
</u-form-item>
<button type="primary" shape="circle" @click="login">登录</button>
<br>
<button type="default" shape="circle" form-type="reset">注册</button>
</u--form>
</view>
</view>
</template>
<script>
import {
doLogin
} from '@/common/http/api.js';
export default {
data() {
return {
formModel: {
userName: '',
password: ''
},
rules: {
'userName': [{
type: 'string',
required: true,
message: '请填写用户名',
trigger: ['blur', 'change']
}],
'password': [{
type: 'string',
required: true,
message: '请填写用户密码',
trigger: ['blur', 'change']
}]
}
}
},
onReady() {
// 如果需要兼容微信小程序,并且校验规则中含有方法等,只能通过setRules方法设置规则
this.$refs.uform.setRules(this.rules)
},
/* onLoad({userName,password}) {
this.userName=userName
this.password=password
}, */
methods: {
login() {
this.$refs.uform.validate().then(obj => {
uni.$u.toast('校验通过')
const params = {
userCode: this.formModel.userName,
userPwd: this.formModel.password
}
doLogin(params).then((res) => {
console.log(res)
uni.$u.vuex('vuex_token', res.token)
uni.$u.vuex('vuex_user', res.loginUser)
//uni.setStorageSync('token', res.token);
// 登录成功提示
uni.showToast({
title: "登录成功"
})
setTimeout(() => {
// 回源跳转
const url = uni.getStorageSync("currentRoute") || "/pages/index/index"
uni.$u.route({
type: "reLaunch",
url
})
}, 1500)
})
}).catch(errors => {
uni.$u.toast('校验失败')
})
},
}
}
</script>
<style lang="scss" scoped>
.login-from {
width: 600rpx;
margin: auto;
height: 200rpx;
left: 0;
top: 0;
bottom: 0;
right: 0;
position: absolute;
}
</style>
模拟器运行和真机运行都能跳转,就是打包成apk后不得行,登录成功是提示了的
6***@qq.com (作者)
谢谢 换成uni.reLaunch就可以了 但是之前用 uni.redirectTo试过,不清楚为什么也不得行
2022-01-28 14:47