求助,怎么解决。
- 发布:2024-06-25 15:37
- 更新:2024-06-26 13:23
- 阅读:85
套马杆的套子 - 没有解决不了的问题,只有解决不完的问题
给代码贴出来瞅一眼
-
1***@qq.com (作者)
template里面的代码:
<view class="login-box">
<uni-forms ref="loginForm" :rules="rules" :modelValue="formData" label-position="top">
<uni-forms-item class="login-label" label="手机号" name="username" required>
<uni-easyinput class="login-input" type="text" v-model="formData.username"
placeholder="请输入手机号" />
</uni-forms-item>
<uni-forms-item class="login-label" label="密码" name="password" required>
<uni-easyinput class="login-input" type="password" v-model="formData.password"
placeholder="请输入密码" />
</uni-forms-item>
<uni-forms-item>
<uni-button class="login-button" type="primary" @click="handleSubmit">登录</uni-button>
</uni-forms-item>
</uni-forms>
</view>
//触发函数
const handleSubmit = () => {
// console.log(loginForm.validate())
console.log(1)
loginForm.value.validate().then(valid => {
if (valid) {
login()
} else {
console.log('验证失败')
}
}).catch(error => {
console.log('表单验证发生错误', error)
})
}
const login = async () => {
//直接在此处连接云数据库进行登录操作。
const db = uniCloud.database()
const _ = db.command
const user = await db.collection('user').where({
username: formData.value.username,
password: formData.value.password
}).get()
// console.log(user)
// 调用云函数登录
uniCloud.callFunction({
name: 'loginUser', // 替换为你的云函数名称
data: {
username: formData.value.username,
password: formData.value.password
}
}).then(res => {
if (res.result.code === 0) {
// 登录成功,保存登录信息到本地存储
const session = {
username: formData.value.username,
userId: res.result.userId,
token: res.result.token, // 假设登录返回了一个 token
expirationTime: Date.now() + 3600 * 1000 * 24 // 设置为24小时后过期
}
uni.setStorageSync('session', session)
// 在登录成功后调用此函数
configureTabBarForRole(res.result.userId)
// uni.switchTab({
// url:"/pages/index/index"
// })
} else {
uni.showToast({
title: '登录失败:' + res.result.message,
icon: 'none'
})
}
}).catch(err => {
})
}
2024-06-26 21:13
1***@qq.com (作者)
看一下下面
2024-06-26 21:14