192 lines
4.0 KiB
JavaScript
192 lines
4.0 KiB
JavaScript
// components/welcome/welcome.js
|
||
Page({
|
||
|
||
/**
|
||
* 页面的初始数据
|
||
*/
|
||
data: {
|
||
showButton:false,
|
||
},
|
||
|
||
/**
|
||
* 生命周期函数--监听页面加载
|
||
*/
|
||
onLoad: function (options) {
|
||
wx.cloud.init({
|
||
traceUser:true,
|
||
})
|
||
this.getOpenid();
|
||
this.getConfig();
|
||
wx.hideShareMenu();
|
||
},
|
||
|
||
/**
|
||
* 生命周期函数--监听页面初次渲染完成
|
||
*/
|
||
onReady: function () {
|
||
|
||
},
|
||
|
||
/**
|
||
* 生命周期函数--监听页面显示
|
||
*/
|
||
onShow: function () {
|
||
|
||
},
|
||
|
||
/**
|
||
* 生命周期函数--监听页面隐藏
|
||
*/
|
||
onHide: function () {
|
||
|
||
},
|
||
|
||
/**
|
||
* 生命周期函数--监听页面卸载
|
||
*/
|
||
onUnload: function () {
|
||
|
||
},
|
||
|
||
/**
|
||
* 页面相关事件处理函数--监听用户下拉动作
|
||
*/
|
||
onPullDownRefresh: function () {
|
||
|
||
},
|
||
|
||
/**
|
||
* 页面上拉触底事件的处理函数
|
||
*/
|
||
onReachBottom: function () {
|
||
|
||
},
|
||
|
||
/**
|
||
* 用户点击右上角分享
|
||
*/
|
||
onShareAppMessage: function () {
|
||
|
||
},
|
||
|
||
/*
|
||
登录过程的基本步骤
|
||
1.获取个人openid
|
||
2.根据openid获取个人信息
|
||
1)如果查无此人,创建新的用户
|
||
2)有就获取个人信息
|
||
注:在小程序端创建新纪录会自动附上_openid
|
||
*/
|
||
//获取openid
|
||
getOpenid() {
|
||
var that = this;
|
||
wx.cloud.callFunction({
|
||
name:"cS_getOpenId"
|
||
}).then(res=> {
|
||
that.getUserInfoDetail(res.result.openid)
|
||
}).catch(err => {
|
||
that.dealLoginErr();
|
||
})
|
||
},
|
||
//获取个人信息
|
||
getUserInfoDetail(openid) {
|
||
var that = this;
|
||
const db = wx.cloud.database()
|
||
db.collection('sM_userInfo').where({
|
||
_openid:openid
|
||
}).get().then(res => {
|
||
if(res.data.length ===0) {
|
||
//没有这个人
|
||
that.createNewUser(openid);
|
||
} else {
|
||
//已经登陆过了
|
||
var userInfo = res.data[0];
|
||
console.log(userInfo)
|
||
wx.setStorageSync('userInfoDetail', userInfo);
|
||
if(userInfo.isSign) wx.setStorageSync('isSign', true);
|
||
else wx.setStorageSync('isSign',false);
|
||
that.getSystemUserInfo();
|
||
}
|
||
}).catch(err => {
|
||
that.dealLoginErr();
|
||
})
|
||
},
|
||
|
||
//创建新用户
|
||
createNewUser(openid) {
|
||
var that = this;
|
||
const db = wx.cloud.database();
|
||
var data = {
|
||
unreadMsg: new Array(),
|
||
issue: new Array(),
|
||
want: new Array(),
|
||
phone:'',
|
||
wxid:'',
|
||
userName:'',
|
||
isSign:false,
|
||
}
|
||
db.collection('sM_userInfo').add({
|
||
data:data,
|
||
}).then(res => {
|
||
data._id = res._id;
|
||
data._openid = openid;
|
||
wx.setStorageSync('userInfoDetail', data);
|
||
wx.setStorageSync('isSign',false);
|
||
that.getSystemUserInfo();
|
||
}).catch(err => {
|
||
that.dealLoginErr();
|
||
})
|
||
},
|
||
|
||
//出错处理
|
||
dealLoginErr() {
|
||
var that = this;
|
||
wx.showModal({
|
||
title: '警告',
|
||
content: '网络不佳,登陆失败',
|
||
confirmText: '重试',
|
||
success (res) {
|
||
if (res.confirm) {
|
||
that.getOpenid();
|
||
console.log('用户点击确定')
|
||
} else if (res.cancel) {
|
||
console.log('用户点击取消')
|
||
}
|
||
}
|
||
})
|
||
},
|
||
|
||
//获取用户信息以及权限
|
||
getSystemUserInfo() {
|
||
var that = this;
|
||
wx.getSetting({
|
||
success(res) {
|
||
// console.log(res)
|
||
if (res.authSetting['scope.userInfo']) {
|
||
// 已经授权,可以直接调用 getUserInfo 获取头像昵称
|
||
wx.getUserInfo({
|
||
success(res) {
|
||
wx.setStorageSync('userInfo', res.userInfo);
|
||
// console.log(res.userInfo)
|
||
setTimeout(function () {}, 2000);
|
||
wx.switchTab({
|
||
url: '../homePage/homePage'
|
||
})
|
||
}
|
||
})
|
||
} else {
|
||
that.setData({
|
||
showButton:true
|
||
})
|
||
}
|
||
}
|
||
})
|
||
},
|
||
|
||
getConfig() {
|
||
const db = wx.cloud.database();
|
||
db.collection('cS_config').doc('94b1e1fc5d050646020dacc27c1a848d').get().then(res => {
|
||
wx.setStorageSync('swiperList',res.data.swiperList)
|
||
})
|
||
}
|
||
}) |