|
|
// 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) {
|
|
|
// 尝试使用配置文件中的环境ID,如果没有则使用动态环境ID
|
|
|
let envId = 'cloud1-4gczmwok7cacf142'; // 默认环境ID
|
|
|
|
|
|
try {
|
|
|
// 如果配置文件存在,则使用配置文件中的环境ID
|
|
|
const config = require('./config/env.js');
|
|
|
if (config && config.cloudEnvId) {
|
|
|
envId = config.cloudEnvId;
|
|
|
}
|
|
|
} catch (e) {
|
|
|
console.log('未找到环境配置文件,使用默认环境ID');
|
|
|
}
|
|
|
|
|
|
wx.cloud.init({
|
|
|
env: envId,
|
|
|
traceUser: true // 是否将用户访问记录到用户管理中,用于多用户访问的场景
|
|
|
});
|
|
|
console.log('云开发初始化成功,使用环境ID:', envId);
|
|
|
|
|
|
// 静默获取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);
|
|
|
}
|
|
|
}) |