uniapp做微信小程序雲開放

1. 在App.vue中:初始化小程序雲數據庫,全局掛載。

在這裏插入圖片描述

<script>
	import Vue from 'vue';
	export default {		
		onLaunch: function() {
			let envId='xxx' //環境id
			wx.cloud.init({
				env:envId
			})	
			Vue.prototype.db=wx.cloud.database({env:envId})			
		}		
	}
</script>

2. 對小程序雲數據得增刪改查:

//1. 查
this.db.collection('user').where({      
    _openid: userOpenid,      
}).get().then(res=>{ 

})	

//2. 增
this.db.collection('user').add({
    data:{},
    success:(res)=>{
    },
    fail:(err)=>{
    }
})
//3. 改
this.db.collection('user').doc(id).update({   
      data: {},
      success: (res) => {  
      },
      fail: (err) => { 
      }
})
// 4. 刪
db.collection('user').doc(id)     
    .remove().then(res => {  
    }).catch(err => { 
})

3. 小程序雲的登陸判定:

let _this=this
uni.getUserInfo({  
    provider: 'weixin',
    success(res) {		
        let wxUserInfo=res.userInfo;
		wx.cloud.callFunction({
			name: 'login',    
		}).then(res1 => {
			let userOpenid=res1.result.openid;
			_this.db.collection('tool_user').where({      
				_openid: userOpenid,      
			}).get().then(res2=>{ 							
				let timer = new Date().getTime();
				let newToken = timer +''+userOpenid;											
				_this.db.collection('tool_user').add({
					data:{},
					success:(res3)=>{  
						_this.db.collection('tool_user').where({
							_id: res3._id,       
						}).get().then(res4=>{  														   
							uniShowToast("登錄成功")
						}).catch(err4=>{
							uniShowToast("登錄失敗")
						})
                    })
                })
            })
        })
    }
})
										
								
					

 

 

 

 

 

 

 

 

 

 

 

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