// pages/user/index.js import dayjs from '../../utils/dayjs/index.js' import relativeTime from '../../utils/dayjs/relativeTime.js' dayjs.extend(relativeTime); import { store } from '../../utils/store.js' import { UserModel } from '../../models/user.js' let userModel = new UserModel() import { AuthModel } from '../../models/auth.js' let authModel = new AuthModel() Page({ /** * 页面的初始数据 */ data: { messageTotal: Number, canIUse: wx.canIUse('button.open-type.getUserInfo'), authType:0 }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { this.getUserInfo() this.isAuthUserInfo() }, isAuthUserInfo(){ let that = this wx.showLoading() console.log('未授权') return new Promise(resolve =>{ that.setData({ authType: 1 }, () => { wx.hideLoading() }) wx.getSetting({ success(res) { if (res.authSetting['scope.userInfo']) { // 已经授权,可以直接调用 getUserInfo 获取头像昵称 wx.getUserInfo({ success: function (res) { console.log(res.userInfo) let nickName = res.userInfo.nickName let avatarUrl = res.userInfo.avatarUrl console.log(nickName, avatarUrl) if(nickName && avatarUrl){ that.setData({ authType: 2 },()=>{ wx.hideLoading() }) } } }) } } }) }) }, bindGetUserInfo(e) { console.log(e.detail.userInfo) let avatarUrl = e.detail.userInfo.avatarUrl let city = e.detail.userInfo.city let country = e.detail.userInfo.country let gender = e.detail.userInfo.gender let nickName = e.detail.userInfo.nickName let province = e.detail.userInfo.province this.updateUserInfo(avatarUrl, city, country, gender, nickName, province) this.isAuthUserInfo() }, hasBindUserInfo(){ return store.hasBindUserInfo(); }, updateUserInfo(avatarUrl, city, country, gender, nickName, province){ userModel.updateUserInfo(avatarUrl, city, country, gender, nickName, province,res=>{ console.log(res) }) }, getUserInfo(){ userModel.getUserInfo(res=>{ console.log(res) }) }, getMsgStatus(){ userModel.getMyMessageTotal(res=>{ console.log(res.result.total) this.setData({ messageTotal: Number(res.result.total) }) }) }, onTapItem (e) { const {type} = e.currentTarget.dataset console.log(type) wx.navigateTo({ url: `/pages/user/${type}/index`, }) // if(type != 'myInfo'){ // wx.navigateTo({ // url: `/pages/user/${type}/index`, // }) // } }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })