一、开发前的准备
二、调用方法
三、代码
1.home.wxml
< scroll-view scroll-y class = " scrollPage" >
< view class = " UCenter-bg" >
< view class = " padding flex flex-wrap justify-between align-center bg-gray" wx: if= " {{noLogin}}" >
< image src = " {{avatarUrl}}" class = " png" mode = " widthFix" style = " width : 130rpx; height : 130rpx; background-color : white; border-radius : 100rpx; position : absolute; left : 50rpx; top : 160rpx; " > </ image>
< button class = " cu-btn bg-green lg" style = " position : absolute; left : 220rpx; top : 185rpx; " bindtap = " login" > 登录</ button>
</ view>
< view wx: if= " {{!noLogin}}" >
< image src = " {{avatarUrl}}" class = " png" mode = " widthFix" style = " width : 130rpx; height : 130rpx; background-color : white; border-radius : 100rpx; position : absolute; left : 50rpx; top : 160rpx; " > </ image>
< text style = " font-size : 14px; color : black; position : absolute; bottom : 120rpx; left : 210rpx; " > {{nickName}}</ text>
< br/>
< text style = " font-size : 14px; color : gray; position : absolute; bottom : 70rpx; left : 210rpx; " > 欢迎您来到云上挂号厅</ text>
</ view>
</ view>
< view class = " cu-list menu card-menu margin-top-xl margin-bottom-xl shadow-lg radius" >
< view class = " cu-item arrow" bindtap = " logout" >
< navigator class = " content" hover-class = " none" >
< text class = " cuIcon-creativefill text-orange" > </ text>
< text class = " text-grey" > 退出登录</ text>
</ navigator>
</ view>
</ view>
</ scroll-view>
2.home.js
Component ( {
options: {
addGlobalClass: true ,
} ,
data: {
nickName: '' ,
avatarUrl: '../../../images/tabbar/about.png' ,
noLogin: true ,
} ,
ready ( ) {
let getNickName= wx. getStorageSync ( 'nickName' )
if ( getNickName== '' ) {
wx. setStorageSync ( 'noLogin' , true )
}
let getAvatarUrl= '' ;
if ( wx. getStorageSync ( 'avatarUrl' ) != '' ) {
getAvatarUrl= wx. getStorageSync ( 'avatarUrl' )
} else {
getAvatarUrl= '../../../images/tabbar/about.png' ;
}
let newnoLogin= true ;
if ( wx. getStorageSync ( 'noLogin' ) == true ) {
newnoLogin= true ;
} else {
newnoLogin= false ;
}
this . setData ( {
nickName: getNickName,
avatarUrl: getAvatarUrl,
noLogin: newnoLogin,
} )
} ,
methods: {
login ( e ) {
wx. getUserProfile ( {
desc: '必须授权才可以继续登录' ,
success : res => {
let user= res. userInfo;
let avatarUrlSub = user. avatarUrl. substring ( 0 , 8 ) + res. userInfo. avatarUrl. substring ( 13 , res. userInfo. avatarUrl. length - 3 ) + '0' ;
user. avatarUrl= avatarUrlSub;
wx. setStorageSync ( 'nickName' , user. nickName)
wx. setStorageSync ( 'avatarUrl' , user. avatarUrl)
wx. setStorageSync ( 'noLogin' , false )
this . setData ( {
nickName: user. nickName,
avatarUrl: user. avatarUrl,
noLogin: false ,
} )
} ,
fail : res => {
console. log ( "授权失败" , res)
}
} )
} ,
logout ( e ) {
this . setData ( {
nickName: '' ,
avatarUrl: '../../../images/tabbar/about.png' ,
noLogin: true ,
} )
wx. setStorageSync ( 'nickName' , '' )
wx. setStorageSync ( 'avatarUrl' , '' )
wx. setStorageSync ( 'noLogin' , true )
}
}
} )
四、遇到的问题
报错:TypeError: wx.getUserProfile is not a function 由于微信官方文档更新较快,getUserProfile是新的方法,所以需要更新版本