You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
store_node/miniprogram/app.ts

140 lines
4.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// app.ts
App<IAppOption>({
globalData: {
userInfo: null,
openid: '',
isLoggedIn: false,
hasUserInfo: false,
canIUseGetUserProfile: false
},
onLaunch() {
// 展示本地存储能力
const logs = wx.getStorageSync('logs') || [];
logs.unshift(Date.now());
wx.setStorageSync('logs', logs);
// 初始化云开发环境
if (wx.cloud) {
wx.cloud.init({
env: 'cloud1-5gqeisa06659849a', // 云开发环境ID
traceUser: true // 是否将用户访问记录到用户管理中,用于多用户访问的场景
});
console.log('云开发初始化成功');
// 静默获取openid
wx.cloud.callFunction({
name: 'login',
success: res => {
console.log('云函数登录成功', res);
if (res.result && res.result.success) {
this.globalData.openid = res.result.openid;
wx.setStorageSync('openid', res.result.openid);
}
},
fail: err => {
console.error('云函数登录失败', err);
}
});
// 检查本地是否有用户信息
const userInfo = wx.getStorageSync('userInfo');
if (userInfo) {
this.globalData.userInfo = userInfo;
this.globalData.hasUserInfo = true;
this.globalData.isLoggedIn = true;
} else {
this.globalData.hasUserInfo = false;
this.globalData.isLoggedIn = false;
}
// 检查用户信息获取能力
if (wx.getUserProfile) {
this.globalData.canIUseGetUserProfile = true;
}
} else {
console.error('请使用 2.2.3 或以上的基础库以使用云能力');
}
console.log('小程序启动成功');
},
// 静默登录获取openid但不弹窗要求用户授权
silentLogin() {
wx.cloud.callFunction({
name: 'login',
data: {},
success: res => {
console.log('云函数登录成功', res);
this.globalData.openid = res.result.openid;
this.globalData.isLoggedIn = true;
wx.setStorageSync('openid', res.result.openid);
// 尝试从数据库获取用户信息
wx.cloud.callFunction({
name: 'getUserInfo',
data: { openid: res.result.openid },
success: userRes => {
console.log('获取用户信息成功', userRes);
if (userRes.result && userRes.result.userInfo) {
this.globalData.userInfo = userRes.result.userInfo;
this.globalData.hasUserInfo = true;
wx.setStorageSync('userInfo', userRes.result.userInfo);
}
},
fail: err => {
console.error('获取用户信息失败', err);
}
});
},
fail: err => {
console.error('云函数登录失败', err);
}
});
},
// 获取用户信息(需要用户主动授权)
getUserProfile(callback: Function) {
wx.getUserProfile({
desc: '用于完善用户资料',
success: (res: any) => {
// 更新全局数据
this.globalData.userInfo = res.userInfo;
this.globalData.hasUserInfo = true;
wx.setStorageSync('userInfo', res.userInfo);
// 保存到云数据库
wx.cloud.callFunction({
name: 'updateUserInfo',
data: {
userInfo: res.userInfo
},
success: (result: any) => {
console.log('用户信息上传成功', result);
},
fail: (err: any) => {
console.error('用户信息上传失败', err);
}
});
// 执行回调
if (callback && typeof callback === 'function') {
callback(res.userInfo);
}
},
fail(res: any) {
wx.showToast({
title: '授权失败,部分功能将受限',
icon: 'none'
});
}
});
},
onShow() {
console.log('小程序显示');
},
onHide() {
console.log('小程序隐藏');
},
onError(error) {
console.error('小程序错误:', error);
}
})