|
|
// pages/profile/index.js
|
|
|
const app = getApp();
|
|
|
|
|
|
Page({
|
|
|
data: {
|
|
|
hasUserInfo: false,
|
|
|
canIUseGetUserProfile: false,
|
|
|
userInfo: {
|
|
|
avatarUrl: '',
|
|
|
nickName: '',
|
|
|
school: '',
|
|
|
studentId: ''
|
|
|
},
|
|
|
wallet: {
|
|
|
balance: '0.00',
|
|
|
points: 0,
|
|
|
coupons: 0
|
|
|
},
|
|
|
orderCount: {
|
|
|
waiting: 0,
|
|
|
processing: 0,
|
|
|
completed: 0,
|
|
|
refund: 0
|
|
|
},
|
|
|
showEditDialog: false, // 控制编辑弹窗显示
|
|
|
editUserInfo: { // 编辑时临时存储用户信息
|
|
|
avatarUrl: '',
|
|
|
nickName: '',
|
|
|
school: '',
|
|
|
studentId: ''
|
|
|
}
|
|
|
},
|
|
|
|
|
|
onLoad: function(options) {
|
|
|
console.log('个人中心页面加载');
|
|
|
|
|
|
// 检查 wx.getUserProfile 是否可用
|
|
|
if (wx.getUserProfile) {
|
|
|
this.setData({
|
|
|
canIUseGetUserProfile: true
|
|
|
});
|
|
|
}
|
|
|
|
|
|
// 如果已经有用户信息,初始化数据
|
|
|
if (app.globalData.userInfo) {
|
|
|
this.setData({
|
|
|
hasUserInfo: true,
|
|
|
userInfo: {
|
|
|
avatarUrl: app.globalData.userInfo.avatarUrl,
|
|
|
nickName: app.globalData.userInfo.nickName,
|
|
|
school: app.globalData.userInfo.school,
|
|
|
studentId: app.globalData.userInfo.studentId,
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
},
|
|
|
|
|
|
onShow: function() {
|
|
|
console.log('个人中心页面显示');
|
|
|
|
|
|
// 先尝试用全局状态判断是否登录
|
|
|
if (app.globalData.isLoggedIn) {
|
|
|
// 已登录,直接拉取钱包和订单数据
|
|
|
this.getUserWalletInfo();
|
|
|
this.getUserOrderCount();
|
|
|
} else {
|
|
|
// 没登录,尝试从缓存取用户信息
|
|
|
const userInfo = wx.getStorageSync('userInfo');
|
|
|
if (userInfo) {
|
|
|
// 缓存里有用户信息,设置全局状态和页面数据
|
|
|
app.globalData.userInfo = userInfo;
|
|
|
app.globalData.hasUserInfo = true;
|
|
|
app.globalData.isLoggedIn = true;
|
|
|
this.setData({
|
|
|
hasUserInfo: true,
|
|
|
userInfo: {
|
|
|
avatarUrl: userInfo.avatarUrl,
|
|
|
nickName: userInfo.nickName,
|
|
|
school: userInfo.school,
|
|
|
studentId: userInfo.studentId
|
|
|
}
|
|
|
});
|
|
|
// 缓存里有用户信息,也拉取钱包和订单数据
|
|
|
this.getUserWalletInfo();
|
|
|
this.getUserOrderCount();
|
|
|
} else {
|
|
|
// 缓存没有用户信息,设置页面为未登录状态
|
|
|
this.setData({
|
|
|
hasUserInfo: false
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
},
|
|
|
|
|
|
// 获取微信用户信息(新版)
|
|
|
getUserProfile: function () {
|
|
|
// 检查本地是否已登陆过
|
|
|
const storedUser = wx.getStorageSync('userInfo');
|
|
|
console.log('获取缓存 userInfo:', storedUser);
|
|
|
if (storedUser) {
|
|
|
// 如果已经登录过,直接使用缓存数据
|
|
|
app.globalData.userInfo = storedUser;
|
|
|
app.globalData.hasUserInfo = true;
|
|
|
app.globalData.isLoggedIn = true;
|
|
|
|
|
|
this.setData({
|
|
|
hasUserInfo: true,
|
|
|
userInfo: {
|
|
|
avatarUrl: storedUser.avatarUrl,
|
|
|
nickName: storedUser.nickName,
|
|
|
school: storedUser.school,
|
|
|
studentId: storedUser.studentId
|
|
|
}
|
|
|
});
|
|
|
|
|
|
wx.showToast({
|
|
|
title: '欢迎回来',
|
|
|
icon: 'success'
|
|
|
});
|
|
|
|
|
|
return; // 不再调用 getUserProfile,直接结束
|
|
|
}
|
|
|
// 第二步:首次登录时走下面流程
|
|
|
wx.showLoading({ title: '登录中...' });
|
|
|
|
|
|
wx.getUserProfile({
|
|
|
desc: '用于完善用户资料',
|
|
|
success: (res) => {
|
|
|
console.log('获取用户信息成功', res);
|
|
|
const userInfo = res.userInfo;
|
|
|
|
|
|
// 先把头像和昵称放到编辑弹窗临时数据里,弹窗让用户完善
|
|
|
this.setData({
|
|
|
editUserInfo: {
|
|
|
avatarUrl: userInfo.avatarUrl,
|
|
|
nickName: userInfo.nickName,
|
|
|
school: userInfo.school,
|
|
|
studentId: userInfo.studentId
|
|
|
},
|
|
|
showEditDialog: true
|
|
|
});
|
|
|
|
|
|
wx.hideLoading();
|
|
|
},
|
|
|
fail: () => {
|
|
|
wx.hideLoading();
|
|
|
wx.showToast({
|
|
|
title: '授权失败,请重试',
|
|
|
icon: 'none'
|
|
|
});
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
|
|
|
// 选择新头像
|
|
|
chooseAvatar: function (e) {
|
|
|
const avatarUrl = e.detail.avatarUrl;
|
|
|
this.setData({
|
|
|
'editUserInfo.avatarUrl': avatarUrl
|
|
|
});
|
|
|
},
|
|
|
|
|
|
|
|
|
// 昵称输入监听
|
|
|
onNickNameInput: function (e) {
|
|
|
this.setData({
|
|
|
'editUserInfo.nickName': e.detail.value
|
|
|
});
|
|
|
},
|
|
|
|
|
|
// 学校输入监听
|
|
|
onschoolInput: function (e) {
|
|
|
this.setData({
|
|
|
'editUserInfo.school': e.detail.value
|
|
|
});
|
|
|
},
|
|
|
// 学号输入监听
|
|
|
onstudentIdInput: function (e) {
|
|
|
this.setData({
|
|
|
'editUserInfo.studentId': e.detail.value
|
|
|
});
|
|
|
},
|
|
|
|
|
|
// 取消编辑,关闭弹窗
|
|
|
cancelEdit: function () {
|
|
|
this.setData({
|
|
|
showEditDialog: false
|
|
|
});
|
|
|
},
|
|
|
|
|
|
// 确认编辑,更新用户信息并登录
|
|
|
confirmEdit: function () {
|
|
|
const self = this;
|
|
|
const newUserInfo = this.data.editUserInfo;
|
|
|
|
|
|
if (!newUserInfo.nickName || newUserInfo.nickName.trim() === '') {
|
|
|
wx.showToast({
|
|
|
title: '昵称不能为空',
|
|
|
icon: 'none'
|
|
|
});
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
|
|
|
wx.showLoading({ title: '更新中...' });
|
|
|
|
|
|
// 调用云函数更新用户信息
|
|
|
wx.cloud.callFunction({
|
|
|
name: 'updateUserInfo',
|
|
|
data: { userInfo: newUserInfo },
|
|
|
success: (res) => {
|
|
|
wx.hideLoading();
|
|
|
if (res.result && res.result.success) {
|
|
|
// 更新全局和本地缓存
|
|
|
app.globalData.userInfo = newUserInfo;
|
|
|
app.globalData.hasUserInfo = true;
|
|
|
app.globalData.isLoggedIn = true;
|
|
|
wx.setStorageSync('userInfo', newUserInfo);
|
|
|
|
|
|
// 更新页面显示
|
|
|
self.setData({
|
|
|
userInfo: {
|
|
|
...self.data.userInfo,
|
|
|
avatarUrl: newUserInfo.avatarUrl,
|
|
|
nickName: newUserInfo.nickName,
|
|
|
school: newUserInfo.school,
|
|
|
studentId: newUserInfo.studentId
|
|
|
},
|
|
|
hasUserInfo: true,
|
|
|
showEditDialog: false
|
|
|
});
|
|
|
|
|
|
wx.showToast({
|
|
|
title: '更新成功',
|
|
|
icon: 'success'
|
|
|
});
|
|
|
|
|
|
// 调用 wx.login 登录云函数
|
|
|
wx.login({
|
|
|
success(loginRes) {
|
|
|
if (loginRes.code) {
|
|
|
wx.cloud.callFunction({
|
|
|
name: 'userLogin',
|
|
|
data: {
|
|
|
code: loginRes.code,
|
|
|
userInfo: newUserInfo
|
|
|
},
|
|
|
success(cloudRes) {
|
|
|
console.log('userLogin 成功:', cloudRes);
|
|
|
},
|
|
|
fail(err) {
|
|
|
console.error('userLogin 云函数失败', err);
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
} else {
|
|
|
wx.showToast({
|
|
|
title: res.result.message || '更新失败',
|
|
|
icon: 'none'
|
|
|
});
|
|
|
}
|
|
|
},
|
|
|
fail: () => {
|
|
|
wx.hideLoading();
|
|
|
wx.showToast({
|
|
|
title: '网络错误,请重试',
|
|
|
icon: 'none'
|
|
|
});
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
|
|
|
// 退出登录
|
|
|
logout: function () {
|
|
|
wx.clearStorageSync();
|
|
|
app.globalData.userInfo = null;
|
|
|
app.globalData.hasUserInfo = false;
|
|
|
app.globalData.isLoggedIn = false;
|
|
|
|
|
|
this.setData({
|
|
|
hasUserInfo: false,
|
|
|
userInfo: {
|
|
|
avatarUrl: '',
|
|
|
nickName: '',
|
|
|
school: '某某大学',
|
|
|
studentId: '学号认证'
|
|
|
},
|
|
|
wallet: {
|
|
|
balance: '0.00',
|
|
|
points: 0,
|
|
|
coupons: 0
|
|
|
},
|
|
|
orderCount: {
|
|
|
waiting: 0,
|
|
|
processing: 0,
|
|
|
completed: 0,
|
|
|
refund: 0
|
|
|
}
|
|
|
});
|
|
|
|
|
|
wx.showToast({
|
|
|
title: '已退出登录',
|
|
|
icon: 'success'
|
|
|
});
|
|
|
},
|
|
|
|
|
|
// 获取钱包信息
|
|
|
getUserWalletInfo: function() {
|
|
|
const self = this;
|
|
|
wx.cloud.callFunction({
|
|
|
name: 'getUserInfo',
|
|
|
data: {},
|
|
|
success: function(res) {
|
|
|
if (res.result && res.result.success && res.result.userInfo) {
|
|
|
const userInfo = res.result.userInfo;
|
|
|
self.setData({
|
|
|
wallet: {
|
|
|
balance: userInfo.balance ? userInfo.balance.toFixed(2) : '0.00',
|
|
|
points: userInfo.credit || 100,
|
|
|
coupons: userInfo.coupons || 0
|
|
|
}
|
|
|
});
|
|
|
} else {
|
|
|
self.setData({
|
|
|
wallet: {
|
|
|
balance: '0.00',
|
|
|
points: 100,
|
|
|
coupons: 0
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
},
|
|
|
fail: function() {
|
|
|
self.setData({
|
|
|
wallet: {
|
|
|
balance: '0.00',
|
|
|
points: 100,
|
|
|
coupons: 0
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
|
|
|
// 获取订单数量
|
|
|
getUserOrderCount: function() {
|
|
|
const self = this;
|
|
|
wx.cloud.callFunction({
|
|
|
name: 'getUserInfo',
|
|
|
data: {},
|
|
|
success: function(res) {
|
|
|
if (res.result && res.result.success && res.result.orderCount) {
|
|
|
self.setData({
|
|
|
orderCount: res.result.orderCount
|
|
|
});
|
|
|
} else {
|
|
|
self.setData({
|
|
|
orderCount: {
|
|
|
waiting: 0,
|
|
|
processing: 0,
|
|
|
completed: 0,
|
|
|
refund: 0
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
},
|
|
|
fail: function() {
|
|
|
self.setData({
|
|
|
orderCount: {
|
|
|
waiting: 0,
|
|
|
processing: 0,
|
|
|
completed: 0,
|
|
|
refund: 0
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
|
|
|
// 页面跳转,需登录才能访问
|
|
|
navigateTo: function(e) {
|
|
|
var url = e.currentTarget.dataset.url;
|
|
|
if (!app.globalData.isLoggedIn) {
|
|
|
wx.showToast({
|
|
|
title: '请先登录',
|
|
|
icon: 'none'
|
|
|
});
|
|
|
return;
|
|
|
}
|
|
|
wx.navigateTo({ url: url });
|
|
|
},
|
|
|
|
|
|
// 分享配置
|
|
|
onShareAppMessage: function() {
|
|
|
return {
|
|
|
title: '宿小君 - 校园生活助手',
|
|
|
path: '/pages/index/index'
|
|
|
};
|
|
|
}
|
|
|
});
|