const app=getApp()
Page({
/**
* 页面的初始数据
*/
data: {
nickName:"点击登录",
avatarUrl:"../../icon/01.png",
userInfo:{},
search_word:"",
is_admin:false,
user:{},
hot_words:[]
},
hotword:function(e){
console.log(e)
var word=e.currentTarget.dataset.word
this.setData({
search_word:word
})
this.search()
},
feedback(){
if(!app.globalData.logged){
wx.showModal({
title:'提示',
content:'请先登录',
confirmText:'我知道了',
showCancel:false,
success(res){
if(res.confirm){
console.log('用户确认')
}
else if(res.cancel){
console.log('用户取消')
}
}
})
}
else{
wx.navigateTo({
url: '../feedback/feedback',
})
}
},
////////////////////////////////////////
//获取用户信息
bindGetUserInfo(res){
wx.getUserProfile({
desc:'正在获取',//不写不弹提示框
success: (res) => {
this.setData({
avatarUrl: res.userInfo.avatarUrl,
nickName: res.userInfo.nickName,
userInfo: res.userInfo,
hasUserInfo: true
}),
console.log(res.userInfo),
app.globalData.userInfo=res.userInfo,//存储用户信息
this.onGetOpenid()
//wx.showToast({
// icon: 'none',
// title: '授权成功',
// duration: 1000
// })
},
fail:function(err){
console.log("获取失败: ",err)
}
})
},
GetUserInfo已经被更改为getUserProfile,这一部分的代码修改应该还有问题
////////////////////////////////////////
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
if (!wx.cloud) {
wx.showModal({
title:'初始化失败',
content:'请使用2.2.3及以上的基础库以使用云能力',
showCancel:false,
success(res){
if(res.confirm){
console.log('用户确认')
}
else if(res.cancel){
console.log('用户取消')
}
}
})
return
}
//获取用户信息
wx.getSetting({
//withSubscriptions: true,
success:res=>{
if(res.authSetting['scope.userInfo']){
console.log(1234567)
wx.getUserInfo({
// lang: lang,
success:res=>{
this.setData({
avatarUrl: res.userInfo.avatarUrl,
nickName: res.userInfo.nickName,
userInfo: res.userInfo,
hasUserInfo: true
// logged:true
})
app.globalData.userInfo=res.userInfo
this.onGetOpenid()
}
})
}
}
})
// console.log(11111111)
// if (wx.getUserProfile) {
// this.setData({
// canIUseGetUserProfile: true,
// })
// }
},
getUserProfile() {
// 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认,开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
wx.getUserProfile({
desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
success: (res) => {
this.setData({
avatarUrl: res.userInfo.avatarUrl,
nickName: res.userInfo.nickName,
userInfo: res.userInfo,
hasUserInfo: true
})
console.log(22222222),
app.globalData.userInfo=res.userInfo,
this.onGetOpenid()
}
})
},
management: function(){
if(!app.globalData.logged){
wx.showModal({
title:'提示',
content:'请先登录',
confirmText:'我知道了',
showCancel:false,
success(res){
if(res.confirm){
console.log('用户确认')
}
else if(res.cancel){
console.log('用户取消')
}
}
})
}
else{
wx.navigateTo({
url: '../demo03/demo03',
})
}
},
bindKeyInput:function(e){
//console.log(e)
var input=e.detail.value
console.log("检测输入:"+input)
this.setData({
search_word:input,
})
},
search:function(e){
if(!app.globalData.logged){
wx.showModal({
title:'提示',
content:'请先登录',
confirmText:'我知道了',
showCancel:false,
success(res){
if(res.confirm){
console.log('用户确认')
}
else if(res.cancel){
console.log('用户取消')
}
}
})
}
else if(this.data.search_word==""){
wx.showModal({
title:'提示',
content:'请输入要查询的词语',
confirmText:'我知道了',
showCancel:false,
success(res){
if(res.confirm){
console.log('用户确认')
}
else if(res.cancel){
console.log('用户取消')
}
}
})
}
else{
var that=this
wx.navigateTo({
url: '../search/search?search_word='+that.data.search_word,
success:function(res){
//通过eventChannel向被打开页面传送数据
res.eventChannel.emit('acceptDataFromOpenerPage',{
search_word:that.data.search_word,
})
}
})
}
},
onGetUserInfo: function(e) {
if (!app.globalData.logged && e.detail.userInfo) {
this.setData({
avatarUrl: e.userInfo.avatarUrl,
nickName: e.userInfo.nickName,
userInfo: e.userInfo,
hasUserInfo: true
})
console.log(e.userInfo),
app.globalData.userInfo=e.userInfo,//存储用户信息
this.onGetOpenid()
}
},
onGetOpenid: function() {
var that=this
// 调用云函数
wx.cloud.callFunction({
name: 'wechat_sign',
data: {
avatarUrl:that.data.avatarUrl,
gender:that.data.userInfo.gender,
nickName:that.data.nickName
},
success: res => {
//console.log('[云函数] [login] user openid: ', res.result.openid)
//console.log(res)
//console.log(res.result.data.user.is_admin)
//console.log(is_admin)
if(res.result.errCode==0){
console.log("服务器返回请求成功")
that.setData({
is_admin:res.result.data.user.is_admin
})
app.globalData.logged=true
that.data.user=res.result.data.user
app.globalData.user=that.data.user
}else{
console.log("服务器返回请求失败,需要处理")
wx.showModal({
title:'抱歉,出错了',
content:res.result.errMsg,
confirmText:'我知道了',
showCancel:false,
success(res){
if(res.confirm){
console.log('用户确认')
}
else if(res.cancel){
console.log('用户取消')
}
}
})
}
// app.globalData.openid = res.result.openid
// wx.navigateTo({
// url: '../userConsole/userConsole',
// })
},
fail: err => {
console.error('[云函数] [login] 调用失败', err)
wx.showModal({
title:'调用失败',
content:'请检查云函数是否已部署',
confirmText:'我知道了',
showCancel:false,
success(res){
if(res.confirm){
console.log('用户确认')
}
else if(res.cancel){
console.log('用户取消')
}
}
})
// wx.navigateTo({
// url: '../deployFunctions/deployFunctions',
// })
}
})
},
onGetOpenid多次调用
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
var that=this
wx.cloud.callFunction({
name: 'get_hot_words',
data: {},
success: res => {
//console.log('[云函数] [login] user openid: ', res.result.openid)
//console.log(res)
//console.log(res.result.data.user.is_admin)
//console.log(is_admin)
if(res.result.errCode==0){
console.log("服务器返回请求成功")
that.setData({
hot_words:res.result.data.hot_words
})
}else{
console.log("服务器返回请求失败,需要处理")
wx.showModal({
title:'抱歉,出错了',
content:res.result.errMsg,
confirmText:'我知道了',
showCancel:false,
success(res){
if(res.confirm){
console.log('用户确认')
}
else if(res.cancel){
console.log('用户取消')
}
}
})
}
// app.globalData.openid = res.result.openid
// wx.navigateTo({
// url: '../userConsole/userConsole',
// })
},
fail: err => {
console.error('[云函数] [get_hot_words] 调用失败', err)
wx.showModal({
title:'调用失败',
content:'请检查云函数是否已部署',
confirmText:'我知道了',
showCancel:false,
success(res){
if(res.confirm){
console.log('用户确认')
}
else if(res.cancel){
console.log('用户取消')
}
}
})
// wx.navigateTo({
// url: '../deployFunctions/deployFunctions',
// })
}
})
},
页面展示期间加载onShow
这一部分负责实时更新“热词”
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function (res) {
if(res.from=='button'){
console.log(res.target)
}
return{
title:'FunaoMethod',
path:'/pages/demo02/demo02',
success:function(res){
}
}
}
})
微信云开发分享接口onShareAppMessage