위챗 애플릿 - 간편한 정보 관리

65064 단어
//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
    })
  }
})
<!-- index.wxml -->
<view class='con'>
  <view class="weui-cells weui-cells_after-title">
    <view class="weui-cell ">
      <view class="weui-cell__hd">
        <view class="weui-label"> </view>
      </view>
      <view class="weui-cell__bd">
        <input class="weui-input" bindinput="forName" placeholder=" " />
      </view>
    </view>
  </view>
  <view class="weui-cells weui-cells_after-title">
    <view class="weui-cell ">
      <view class="weui-cell__hd">
        <view class="weui-label"> </view>
      </view>
      <view class="weui-cell__bd">
        <input class="weui-input" bindinput="forUserId" placeholder=" " />
      </view>
    </view>
  </view>
  <!--   -->
  <view class="button-sp-area btn btn">
    <button class="weui-btn" type="default" bindtap="onAdd"> </button>
    <button class="weui-btn" type="default" bindtap="display"> </button>
    <button class="weui-btn" type="default" bindtap="onQuery"> </button>
  </view>
</view>
/* 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: ' , ',
      })
    }
  }
})
<!--miniprogram/pages/second/second.wxml-->
<view>
  <view wx:for='{{result}}' wx:key='key'>
    <view> :{{item.username}}</view>
    <view> :{{item.userId}}</view>
  </view>
  <button bindtap="onDeleta"> </button>
  <button bindtap="onUpdate"> </button>
</view>
// 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)
      }
    })
  },

})

좋은 웹페이지 즐겨찾기