编写一个微信小程序查找已注册的用户的信息。
开发平台:微信开发者工具
微信云开发查找的代码字段where:
-
在rg.js页面添加数据库usf
-
获取输入的学号并将其赋值给invitestuno
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LFjlPSwp-1611902417314)(https://cdn.acwing.com/media/article/image/2021/01/29/39383_1a8ef51861-invb.png)] -
在数据库中进行查找并显示
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ncbXUYaS-1611902417316)(https://cdn.acwing.com/media/article/image/2021/01/29/39383_5e98ef5d61-succ.png)]
如果没有找到,会显示该学生为注册
####完整代码:
rg.js
// pages/invite/invite.js
const db = wx.cloud.database()
const usf = db.collection('userinfo')
var app = getApp()
Page({
/**
* 页面的初始数据
*/
data: {
temp:true,
teamid:0,
invitestuno:'',
inviteteamid:0,
invitename:'',
invite_id:'',
curteam:'',
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
var self = this;
self.setData({
stuno: app.globalData.stuno
})
usf.where({
data: {
"stuno":self.data.stuno
},
success: function(res){
self.setData({
teamid:res.data.name
})
}
})
},
stunoInput: function(e){
this.setData({
invitestuno:e.detail.value
})
},
/**
* 查询
*/
inquireBtnClick: function(){
wx.showLoading({
title: '加载中..',
mask: true,
duration:100,
})
console.log(this.data.invitestuno)
usf.where({
stuno:this.data.invitestuno
}).get({
success:res=>{
console.log("成功找到学生",res.data)
if(res.data.length>0)
{
this.setData({
invitename:res.data[0].name,
inviteteamid:res.data[0].teamid,
temp:false
})
}
else{
wx.showToast({
title: '该学生未注册',
icon: 'loading',
duration:1000,
mask:true
})
}
},
fail:res=>{
console.log("没有找到该学生")
}
})
},
/**
* 邀请
*/
inviteBtnClick: function(){
var self = this
usf.where({
stuno:this.data.invitestuno
}).get({
success:(res)=>{
console.log(res.data[0].team.length)
if(!res.data[0].team.lenth){
self.setData({
invite_id:res.data[0]._id
})
console.log(self.data.invite_id)
let personinfo=wx.getStorageSync('info')
usf.where({
stuno:personinfo.stuno
}).get({
success:(res)=>{
self.setData({
curteam:res.data[0].team
})
}
})
usf.doc(self.data.invite_id).update({
data:{
team:self.data.curteam
}
})
console.log("成功加入队伍",self.data.curteam)
}
else{
wx.showModal({
title: '提示',
content:'该学生已加入队伍',
showCancel:false,
success: function(res){
self.setData({
temp:true
})
}
})
}
},
fail:(err)=>{
wx.showToast({
title: '查找失败',
mask: true,
duration:1000
})
}
})
},
returnBtnClick: function(){
wx.navigateTo({
url: '../homepage/homepage',
})
}
})
rg.json
{
"usingComponents": {}
}
rg.wxml
<view class='main'>
<image class="bjt" src='../../images/que.png' mode='widthFix'></image>
<text class='title'>队员学号:</text>
<input placeholder='请输入学号' type="number" maxlength="11" bindinput='stunoInput'></input>
<button class='cx' hover-class="anxia" bindtap='inquireBtnClick'>查询</button>
<view class="stuinfo" hidden='{{temp}}'>
<text class="stuinfotxt">姓名:{{invitename}}</text>
<text class="stuinfotxt">学号:{{invitestuno}}</text>
<button class="stuinfobtn" bindtap='inviteBtnClick'>邀请入队</button>
</view>
<button class="btn" hover-class="anxia" bindtap='returnBtnClick'>返回</button>
</view>
rg.wxss
.main{
display: flex;
flex-direction: column;
}
.bjt{
width: 100%;
}
input{
border-bottom: solid;
border-width: 4rpx;
border-color: gainsboro;
margin:50rpx;
}
.title{
margin-left: 30rpx;
margin-top: 30rpx;
}
.cx{
width: 90%;
margin-top: 15rpx;
border-radius: 20rpx;
background-color: lightblue;
}
.cx::after{
border-radius: 20rpx;
border:none;
}
.stuinfo{
margin-top: 55rpx;
display: flex;
flex-direction: column;
margin: 50rpx;
border: dotted;
border-width: 10rpx;
border-color: lightblue;
}
.stuinfotxt{
margin: 20rpx;
margin-left: 130rpx;
}
.stuinfobtn{
width: 50%;
margin-top: 15rpx;
border-radius: 20rpx;
margin-bottom: 25rpx;
}
.stuinfobtn::after{
border-radius: 20rpx;
border:none;
}
.fh{
width: 90%;
margin-top: 30rpx;
border-radius: 20rpx;
background-color: gainsboro;
}
.fh::after{
border-radius: 20rpx;
border:none;
}
.anxia{
background-color: rgba(206, 206, 207, 0.795);
}
.btn{
font-size: 10pt;
height: 55rpx;
background: #ffcc01;
color: black;
text-align: center;
border-radius: 50px;
margin: 10% 20% 10% 20%;
}