小程序雲開發登錄,存用戶的數據到雲數據庫

代碼:

建立一個雲函數 getOpenid , 獲取用戶openid

// 雲函數入口文件
const cloud = require('wx-server-sdk')

cloud.init()

// 雲函數入口函數
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()

  return {
    event,
    openid: wxContext.OPENID,
    appid: wxContext.APPID,
    unionid: wxContext.UNIONID,
  }
}

登錄頁面的wxml代碼

<!--index.wxml-->
<view class="page" data-weui-theme="{{theme}}">
	<view class="weui-form">
		<view class="weui-form__text-area">
			<image bindtap="bindViewTap" class="userinfo-avatar" src="../../img/LOGO.png" mode="cover"></image>
		</view>

		<view class="weui-cells weui-cells_after-title">
			<radio-group bindchange="radioChange">
				<label class="weui-cell weui-check__label" wx:for="{{items}}" wx:key="{{item.value}}">
					<view class="weui-cell__hd">
						<radio value="{{item.value}}" />
					</view>
					<view class="weui-cell__bd">{{item.name}}</view>
				</label>
			</radio-group>
		</view>

		<view class="weui-form__opr-area begin">
			<button open-type="getUserInfo" bindgetuserinfo='getUserInfo' class="weui-btn submit weui-btn_primary">登錄</button>
		</view>
	</view>
</view>

 

登錄頁面的js代碼,

邏輯,調用雲函數獲取openid,錄入用戶的信息存到 user數據庫表。

 

//index.js
//獲取應用實例
const app = getApp();
var userInfo;
wx.cloud.init({
  env: 'vclass-34p02'
})
const DB = wx.cloud.database()
var that;
Page({
  data: {
    userType:'',
    items:[
      {value: '我是學生', name: '我是學生'},
      {value: '我是老師', name: '我是老師'}]
  }, 
  radioChange(e) {
    console.log('radio發生change事件,攜帶value值爲:', e.detail.value)
    this.setData({
      userType:e.detail.value
    })
  },
  onLoad() {
    that = this;
    if(wx.getStorageSync('user')){
      wx.switchTab({
        url: '/pages/studycentre/studycentre',
      })
    }
  },
  userAdd(openId) {
    var user = {
      userType: this.data.userType,
      nickName: userInfo.nickName,
      avatarUrl: userInfo.avatarUrl,
      openId: openId
    };
    wx.setStorageSync('user', user);

    DB.collection('user').add({
      // data 字段表示需新增的 JSON 數據
      data: user
    }).then(res => {
      console.log(res)
      wx.switchTab({
        url: '/pages/studycentre/studycentre',
      })
    })
  },
  getUserInfo: function (e) {
    if(!this.data.userType){
      wx.showToast({
        title: '選擇身份後纔可登錄',
        icon:'none'
      })
      return
    }
    console.log(e)
    userInfo = e.detail.userInfo;

    wx.cloud.callFunction({
      name: "getOpenid",
      success(res) {
        let openId = res.result.openid
        // 判斷數據庫中是否已經有數據
        DB.collection('user').where({
          openId: openId,
        })
        .get().then(ress => {
          console.log('ressressressressressressressress',ress.data[0])
          
          if (ress.data.length == 0) {
            that.userAdd(openId)
          } else {
            wx.setStorageSync('user', ress.data[0]);
            wx.switchTab({
              url: '/pages/studycentre/studycentre',
            })
          }
        })
      },
      fail(res) {
        console.log('登錄失敗', res)
      }
    })

  }
})

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章