微信小程序---问卷调查

//index.js
const app = getApp()
var arr = new Array()
Page({
  data: {
    name: '',
    age: '',
    currentID: ' ',
    answer: arr
  },
  ForName: function(e) {
    this.setData({
      name: e.detail.value
    })
    console.log('ForName :', this.data.name)
  },
  ForAge: function(e) {
    this.setData({
      age: e.detail.value
    })
    console.log('ForAge :', this.data.age)
  },
  btnChange: function(e) {
    var name = this.data.name
    var age = this.data.ag
    const db = wx.cloud.database()
    db.collection('user-info2').where({
      name: name,
      age: age
    }).get({
      success: res => {
        if (res.data.length != 0) {
          wx.showToast({
            icon: 'success',
            title: '登录成功!'
          })
          this.setData({
            currentID: res.data[0]._id,
          })
          console.log("currentIDIndex", this.data.currentID)
          wx.navigateTo({
            url: '/pages/detail/detail?name=' + this.data.name + '&currentID=' + this.data.currentID   
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: '登录失败!请先注册!'
          })
        }
      },
      fail: err => {
        wx.showToast({
          icon: 'none',
          title: '查找用户失败,请先注册!'
        })
      }
    }) 
  },
  btn2Change: function() {
    wx.navigateTo({
      url: '/pages/registered/registered'
    })
  }
});
<!-- index.wxml -->
<view class="page">
  <view class="tab">
    <text class="tab1">问卷调查</text> </view>
  <view class="container">
    <view class="message">
      <view class="Name">
        <view class="label">姓名</view>
        <input class="input" bindinput="ForName" name="name" placeholder="请输入姓名" />
      </view>
      <view class="Age">
        <view class="label">年龄</view>
        <input class="input" bindinput="ForAge" name="age" placeholder="请输入年龄" />
      </view>
    </view>
    <view>
      <button class="btn1" bindtap="btnChange">登录</button>
      <button class="btn2" bindtap="btn2Change">注册</button>
      <!-- <button class="btn2" bindtap="questionsChange">填写问卷</button> -->
    </view>
  </view>
</view>
.tab {
  display: flex;
  flex-direction: column;
  width: 90%;
  height: 100px;
  background-color: rgba(137, 156, 219, 0.623);
  margin: 20px 15px 20px 15px;
  font-size: 30px;
  color: #fff;
  /* text-align: center;
      align-items: center; */
  border-top-left-radius: 5px;
  border-top-right-radius: 5px;
  border-bottom-left-radius: 5px;
  border-bottom-right-radius: 5px;
}
 

.tab1 {
  margin-top: 20px;
  text-align: center;
}

.container {
  margin-top: 50px;
}

/* .btn{
   margin: 100px;
   align-items: center;
 } */

.Name {
  display: flex;
  flex-direction: row;
  margin-bottom: 10px;
}

.Age {
  display: flex;
  flex-direction: row;
}

.label {
  margin-left: 30px;
}

.input {
  margin-left: 40px;
  align-items: center;
}

.btn1 {
  margin-top: 100px;
  background-color: rgba(137, 156, 219, 0.623);
  /* color: rgba(137, 156, 219, 0.623); *//* background-color: rgba(218, 112, 191, 0.658); */
}

.btn2 {
  margin-top: 10px;
}

// miniprogram/pages/registered/registered.js
var arr=new Array()
Page({
  data: {
    name: '',
    age: '',
    answer: arr
  },
  ForName: function (e) {
    this.setData({
      name: e.detail.value
    })
    console.log('ForName :', this.data.name)
  },
  ForAge: function (e) {
    this.setData({
      age: e.detail.value
    })
    console.log('ForAge :', this.data.age)
  },
  onAdd:function(e){
    const db = wx.cloud.database()
    db.collection('user-info2').add({
      data: {
        name: this.data.name,
        age: this.data.age,
        answer: this.data.answer
      },
      success: res => {
        this.setData({
          currentID: res._id
        })
        wx.showToast({
          icon: 'success',
          title:'注册成功!返回登录'
        })
        console.log('注册成功currentID', this.data.currentID)
        wx.navigateBack({
          url: '/pages/index/index?currentID=' + this.data.currentID
        })
        console.log("currentID", this.data.currentID)
      },
      fail: err => {
        console.error('[数据库] [新增记录] 失败:', err)
      }
    })
  }
  
})
<!-- registered.wxml -->
<view class="tab">
  <text class="tab1">用户注册</text>
</view>
<view class="container">
  <view class="message">
    <view class="Name">
      <view class="label">姓名</view>
      <input class="input" bindinput="ForName" name="name" placeholder="请输入姓名" />
    </view>
    <view class="Age">
      <view class="label">年龄</view>
      <input class="input" bindinput="ForAge" name="age" placeholder="请输入年龄" />
    </view>
  </view>
  <view>
    <!-- <button class="btn1" bindtap="btnChange">登录</button> -->
    <button class="btn2" bindtap="onAdd">注册</button>
    <!-- <button class="btn2" bindtap="questionsChange">填写问卷</button> -->
  </view>

</view>
 /* registered.wxss */
 .tab{
      display: flex;
      flex-direction: column;
      width: 90%; 
      height:100px;
      background-color:rgb(137, 156, 219, 0.623);
      margin: 20px 15px 20px 15px;
      font-size: 30px;
      color: #fff;
      border-top-left-radius: 5px;
      border-top-right-radius: 5px;
      border-bottom-left-radius: 5px;
      border-bottom-right-radius: 5px;
 }
  .tab1{
    margin-top: 20px;
   text-align: center;
 }
.container{
  margin-top: 50px;
}
 /* .btn{
   margin: 100px;
   align-items: center;
 } */
 .Name{
   display: flex;
   flex-direction: row;
   margin-bottom: 10px;
 }
 .Age{
  display: flex;
   flex-direction: row
 }
 .label{
   margin-left: 30px;
 }
 .input{
    margin-left: 40px;
    align-items: center;
 }
 .btn1{
   margin-top:100px;
   color: rgba(170, 102, 130, 0.445);
   /* background-color: rgba(218, 112, 191, 0.658); */
   
 }
 .btn2{
   margin-top: 10px;
 }
// detail.js
var ans = new Array(5);
Page({
  data: {
    question: [],
    name: ' ',
    answer: ans,
    id: 0,
    currentID: null
  },
  onLoad: function(options) {
    this.setData({
      name: options.name,
      currentID: options.currentID
    })
     console.log("DetailoptionscurrentID", this.data.currentID)
    // console.log("name", this.data.name, "currentID", this.data.currentID)
    const db = wx.cloud.database()
    db.collection('questions').get({
      success: res => {
        this.setData({
          question: res.data,
        })
        console.log('[questions] [查询记录] 成功: ', res.data)
      },
      fail: err => {
        wx.showToast({
          icon: 'none',
          title: '查询记录失败'
        })
        console.error('[数据库] [查询记录] 失败:', err)
      }
    })
  },
  last: function(e) {
    if (this.data.id != 0) {
      this.setData({
        id: this.data.id - 1
      })
    }
  },
  next: function(e) {
    if (this.data.id < 4) {
      this.setData({
        id: this.data.id + 1
      })
    }
  },
  radioChange: function(e) {
    console.log("radioChange", e.detail.value);
  },
  submit: function(e) {
    console.log("id", this.data.id)
    console.log(e.detail.value);
    // var ans = new Array(5);
    var a = e.detail.value.answer;
    var id = this.data.id;
    ans[id] = a;
    this.setData({
      answer: ans,
    })
    console.log('当前的answer数组', this.data.answer);
  },

  //把结果提交到云数据库
  submitAnswerDB: function() {
    const db = wx.cloud.database()
    db.collection('user-info2').doc(this.data.currentID).update({
      data: {
        answer: this.data.answer
      },
      success: res => {
        console.log("答案上传成功!", this.data.answer);
        wx.navigateTo({
          url: '/pages/finalPage/finalPage?currentID=' + this.data.currentID + '&answer=' + this.data.answer + '&name=' + this.data.name + '&age=' + this.data.age
        })
      },
      fail: err => {
        icon: 'none',
        console.error('[数据库] [更新记录] 失败:', err)
      }    
    })
  },
})
<!--miniprogram/pages/detail/detail.wxml-->
<view class="container">
  <view class="text">亲爱的{{name}},感谢您填写问卷!</view>
  <!-- <view class="text">当前ID{{currentID}}</view> -->
  <swiper class="swiper" current="{{id}}">
    <block wx:for="{{question}}" wx:key="key">
      <swiper-item>
        <form bindsubmit='submit'>
          <view class="weui-cells__title label">{{item._id}}题、{{item.question}}</view>
          <radio-group class="radio-group" name="answer" bindchange="radioChange">
            <label class='radio'>
              <radio value="{{item.optionA}}" />{{item.optionA}}</label>
            <label class='radio'>
              <radio value="{{item.optionB}}" />{{item.optionB}}</label>
            <label class='radio'>
              <radio value="{{item.optionC}}" />{{item.optionC}}</label>
            <label class='radio'>
              <radio value="{{item.optionD}}" />{{item.optionD}}</label>
          </radio-group>
          <view class="btn-group">
            <button class="btn" form-type="submit" bindtap='last' disabled='{{id==0}}'>上一题</button>
            <button class="btn" form-type="submit" bindtap='next' disabled='{{id==4}}'>下一题</button>
            <button class="btn" form-type="submit" bindtap='submitAnswerDB' disabled='{{id==0}}'>提交</button>
          </view>
        </form>
      </swiper-item>
    </block>
  </swiper>
</view>
/* miniprogram/pages/detail/detail.wxss */

.container {
  margin-top: 50px;
  height: 350px;
}

.btn {
  margin: 20px;
}

.radio {
  display: flex;
  flex-direction: column;
  color: rgb(128, 119, 119);
  font-size: 14px;
  margin-left: 20px;
}

.label {
  color: rgb(46, 43, 43);
}

.radio-group {
  display: flex;
  flex-direction: column;
  font-size: 30rpx;
  text-indent: 14rpx;
  margin-top: 40rpx;
}

.radio {
  display: flex;
  flex-direction: row;
  margin-bottom: 14rpx;
}

.swiper {
  height: 350px;
  display: flex;
  flex-direction: column;
  /* justify-content: flex-start; */
  margin-top: 20px;
}

.btn-group {
  margin-top: 20px;
  display: flex;
  flex-direction: row;
  width: 90%;
  margin: auto;
}

.btn {
  background-color: rgba(137, 156, 219, 0.623);
  font-size: 12px;
 
}

.btn-right {
  background-color: rgba(214, 160, 187, 0.555);
  font-size: 12px;
 
}

.btn-left {
  background-color: rgba(214, 160, 187, 0.555);
  font-size: 12px;
 
}

// miniprogram/pages/finalPage.js
const db = wx.cloud.database()
Page({
  data: {
    final: [],
    currentID: null,
    question: [],
    name: null
  },
  onLoad: function(options) {
    this.setData({
      currentID: options.currentID,
      // answer: options.answer,
       name: options.name
    })
     console.log("finalCurrentID", this.data.currentID )
    db.collection('user-info2').where({
      _id:this.data.currentID
    }).get({
      success: res => {
      this.setData({
         final:res.data
      })
        console.log('[数据库] [查询记录] 成功final: ', this.data.final)
      },
      fail: err => {
        wx.showToast({
          icon: 'none',
          title: '查询记录失败'
        })
        console.error('[数据库] [查询记录] 失败:', err)
      }
    })
    db.collection('questions').get({
      success: res => {
        this.setData({
          question: res.data
        })
        console.log('[数据库] [查询记录] 成功answer: ', this.data.question)
      },
      fail: err => {
        wx.showToast({
          icon: 'none',
          title: '查询记录失败'
        })
        console.error('[数据库] [查询记录] 失败:', err)
      }
    })
  } 
})
<!-- finalpage.wxml -->
<view class="container">

  <view class="one" wx:for='{{final}}' wx:key='{{key}}'>
    <view class="name">您的名字:{{item.name}}</view>
    <view class="age">您的年龄:{{item.age}}</view>
  </view>

  <!-- <view wx:for='{{question}}' wx:key='key'>
    <view class="question">{{item.question}}</view>
  </view> -->
  <view class="two" wx:for='{{final}}' wx:key='key'>
    <view class="option">{{item.answer }}</view>
  </view>
</view>
/* miniprogram/pages/finalPage.wxss */
.con{
  height: 100%;
  width: 100%;
  display: flex;
  flex-direction: column;
}
 
 .one{
   margin-left: 40px;
   margin-top: 80px; 
 }
 .name{
   margin-bottom: 15px;
 }
 .age{
    margin-bottom: 50px; 
 }
 .two{
    margin-left: 40px; 
 }
  • 0
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值