- 首页 > it技术 > >
微信小程序--简单的信息管理
//index.js
const app = getApp()
const db = wx.cloud.database()
Page({
data: {
username: " ",
userId: " ",
result:' ',
currentId:' '
},
onLoad: function() {
},forName: function(e) {
this.setData({
username: e.detail.value
})
console.log("username", this.data.username)
},forUserId: function(e) {
this.setData({
userId: e.detail.value
})
console.log("userId", this.data.userId)
},
onQuery: function () {
db.collection('exam-userinfo').where({
username: this.data.username
}).get({
success: res => {
this.setData({
result:res.data,
currentId: res.data[0]._id
})
console.log('[数据库] [查询记录] 成功: ', this.data.result,this.data.currentId)
wx.showToast({
title: '查询记录成功'
})
wx: wx.navigateTo({
url: '/pages/second/second?currentId=' + this.data.currentId + '&username=' + this.data.username + '&userId=' + this.data.userId
})},
fail: err => {
console.error('[数据库] [查询记录] 失败:', err)
}
})
},
onAdd: function () {
db.collection('exam-userinfo').add({
data: {
username: this.data.username,
userId: this.data.userId
},
success: res => {
wx: wx.showToast({
title: '提交成功'
})
console.log('[数据库] [新增记录] 成功,记录 _id: ', res._id)
},
fail: err => {
console.error('[数据库] [新增记录] 失败:', err)
}
})
},
display: function() {
wx:wx.navigateTo({
url: '/pages/detail/detail?username=' + this.data.username + '&userId=' + this.data.userId
})
}
})
姓名
学号
/* miniprogram/pages/detail/detail.wxss */
.display {
display: flex;
flex-direction: row;
margin-top: 15px;
background-color: rgba(27, 223, 86, 0.651);
}.name {
margin-left: 10px;
width: 35%;
}.id {
width: 50%;
}.user {
display: flex;
flex-direction: row;
}.btn {
margin-top: 10px;
display: flex;
flex-direction: row;
}.btn2 {
margin-top: 10px;
background-color: rgba(27, 223, 86, 0.651);
width: 40%;
}.btn1 {
background-color: rgba(179, 241, 198, 0.651);
width: 40%;
}
// miniprogram/pages/detail/detail.js
Page({
data: {
username: ' ',
userId: ' ',
queryResult: [],
currentID: ' '
},
onLoad: function(options) {
const db = wx.cloud.database()
db.collection('exam-userinfo').get({
success: res => {
this.setData({
queryResult: res.data
})
console.log('[数据库] [查询记录] 成功: ', res)
},
fail: err => {
wx.showToast({
icon: 'none',
title: '查询记录失败'
})
console.error('[数据库] [查询记录] 失败:', err)
}
})
},
forName: function(e) {
this.setData({
username: e.detail.value
})
console.log("username", this.data.username)
},
forUserId: function(e) {
this.setData({
userId: e.detail.value
})
console.log("userId", this.data.userId)
},
onQuery: function() {
const db = wx.cloud.database()
// 查询当前用户所有的 counters
db.collection('exam-userinfo').where({
username: this.data.username
}).get({
success: res => {
wx.showToast({
title: '查询记录成功'
})
wx: wx.navigateTo({
url: '/pages/second/second'
})
console.log('[数据库] [查询记录] 成功: ', res)
},
fail: err => {
console.error('[数据库] [查询记录] 失败:', err)
}
})
},
onAdd: function() {
const db = wx.cloud.database()
db.collection('exam-userinfo').add({
data: {
username: this.data.username,
userId: this.data.userId
},
success: res => {
wx: wx.showToast({
title: '提交成功'
})
console.log('[数据库] [新增记录] 成功,记录 _id: ', res._id)
},
fail: err => {
console.error('[数据库] [新增记录] 失败:', err)
}
})
},})
// miniprogram/pages/second/second.js
const db = wx.cloud.database()
Page({
data: {
username: " ",
userId: " ",
currentId: ' ',
result: [ ]
},
onLoad: function(options) {
this.setData({
currentId: options.currentId,
username:options.username,
userId:options.userId
})
db.collection('exam-userinfo').where({
username: this.data.username
}).get({
success: res => {
this.setData({
result: res.data
})
console.log('second[数据库] [查询记录] 成功: ', this.data.currentId)
wx.showToast({
title: '查询记录成功'
})
},
fail: err => {
console.error('[数据库] [查询记录] 失败:', err)
}
})
},
onUpdate: function () {
const db = wx.cloud.database()
db.collection('exam-userinfo').doc(this.data.currentId).update({
data: {
userId:this.data.userId
},
success: res => {
// this.setData({
//count: newCount
// })
wx.showToast({
title: '更新成功',
})
console.log("update :" ,res.data)
},
fail: err => {
icon: 'none',
console.error('[数据库] [更新记录] 失败:', err)
}
})
},
onDeleta: function () {
if (this.data.currentId) {
const db = wx.cloud.database()
db.collection('exam-userinfo').doc(this.data.currentId).remove({
success: res => {
wx.showToast({
title: '删除成功',
})
console.log("delete :" ,res)
},
fail: err => {
wx.showToast({
icon: 'none',
title: '删除失败',
})
console.error('[数据库] [删除记录] 失败:', err)
}
})
} else {
wx.showToast({
title: '无记录可删,请见创建一个记录',
})
}
}
})
姓名:{{item.username}}
学号:{{item.userId}}
// miniprogram/pages/detail/detail.js
Page({
data: {
username: ' ',
userId: ' ',
queryResult: [],
currentID: ' '
},
onLoad: function(options) {
const db = wx.cloud.database()
db.collection('exam-userinfo').get({
success: res => {
this.setData({
queryResult: res.data
})
console.log('[数据库] [查询记录] 成功: ', res)
},
fail: err => {
wx.showToast({
icon: 'none',
title: '查询记录失败'
})
console.error('[数据库] [查询记录] 失败:', err)
}
})
},
forName: function(e) {
this.setData({
username: e.detail.value
})
console.log("username", this.data.username)
},
forUserId: function(e) {
this.setData({
userId: e.detail.value
})
console.log("userId", this.data.userId)
},
onQuery: function() {
const db = wx.cloud.database()
// 查询当前用户所有的 counters
db.collection('exam-userinfo').where({
username: this.data.username
}).get({
success: res => {
wx.showToast({
title: '查询记录成功'
})
wx: wx.navigateTo({
url: '/pages/second/second'
})
console.log('[数据库] [查询记录] 成功: ', res)
},
fail: err => {
console.error('[数据库] [查询记录] 失败:', err)
}
})
},
onAdd: function() {
const db = wx.cloud.database()
db.collection('exam-userinfo').add({
data: {
username: this.data.username,
userId: this.data.userId
},
success: res => {
wx: wx.showToast({
title: '提交成功'
})
console.log('[数据库] [新增记录] 成功,记录 _id: ', res._id)
},
fail: err => {
console.error('[数据库] [新增记录] 失败:', err)
}
})
},})
推荐阅读