App({
getUserInfo(){
return new Promise((resovle,reject)=>{
if(this.userInfo) resovle(this.userInfo);
//調用用戶授權 api 獲取用戶信息
my.getAuthCode({
success:(authcode) =>{
console.info(authcode);
my.getAuthUserInfo({
scopes: ['auth_user'],
success: (res) => {
this.userInfo = res;
resovle(this.userInfo);
},
fail:() =>{
reject({});
}
});
},
fail:() =>{
reject({});
}
});
});
},
onLaunch(options) {
// 第一次打開
// options.query == {number:1}
console.info('App onLaunch');
},
onShow(options) {
// 從後臺被 scheme 重新打開
// options.query == {number:1}
},
});
Index.js 中通過 調用 app.js 中定義的 getUserInfo 獲取用戶信息,這樣只要需要獲取用戶信息的頁面都可以調用這個函數
const app = getApp();
Page({
data: {
src: ''
},
imageError: function (e) {
console.log('image 發生錯誤', e.detail.errMsg)
},
imageLoad: function (e) {
console.log('image 加載成功', e);
},
onLoad(query) {
// 頁面加載
},
onReady() {
// 頁面加載完成
app.getUserInfo().then(
user => this.setData({src: user.avatar})
);
},
onShow() {
// 頁面顯示
},
onHide() {
// 頁面隱藏
},
onUnload() {
// 頁面被關閉
},
onTitleClick() {
// 標題被點擊
},
onPullDownRefresh() {
// 頁面被下拉
},
onReachBottom() {
// 頁面被拉到底部
},
onShareAppMessage() {
// 返回自定義分享信息
return {
title: '校園碼',
desc: '校園二維碼',
path: 'pages/index/index',
};
},
});