用户登录-账户名密码登录
# 用户登录-账户名密码登录
# 一、前端数据整理
定义发送函数,将用户信息以及本次请求的用户登录类型传递给后端
this._sendUserInfo({ ...res, type: this.type })创建请求方法 user_login
定义user_login函数
'use strict'; // 获取数据库引用 const db = uniCloud.database() exports.main = async (event, context) => { const { loginName, password, phone, type } = event; const { affectedDocs, data } = await db.collection('user') .aggregate() .match(type === 'account' ? { loginName, password } : { phone }) .end() //返回数据给客户端 return affectedDocs ? { code: 0, msg: "获取用户信息成功", data: data[0] } : { code: 1, msg: type === 'account' ? '获取用户信息失败,请检查用户名或密码' : '验证码或手机号码错误' } };前端接收返回信息,进行数据处理
- 跳转界面到上一个历史记录
- 存储用户信息
# 二、使用store进行用户信息存储
- 创建实例化Store对象
import Vue from 'vue'
import VueX from 'vuex'
import state from './state.js'
import mutations from './mutations.js'
import actions from './actions.js'
Vue.use(VueX)
export default new VueX.Store({
state,
actions,
mutations
})
main.js中进行store注册
获取用户信息之后,进行用户信息保存操作
// login.vue async _sendUserInfo (data) { const res = await this.$http.user_login(data) if (res) { this.updateUserInfo(res) uni.showToast({ title: '登录成功', icon: 'none', }) setTimeout(() => { // #ifdef H5 uni.switchTab({ url: '/pages/index/index' }) // #endif // #ifndef H5 uni.navigateBack() // #endif }, 2000) } } // mutation.js export default { updateUserInfo(state,userInfo) { uni.setStorageSync('userInfo',userInfo); state.userInfo = userInfo; } }