WxApp/page/component/user/user.js

177 lines
3.8 KiB
JavaScript
Raw Normal View History

2024-08-31 17:33:35 +08:00
const app = getApp();
const baseUrl = app.globalData.baseUrl;
2024-08-29 14:36:16 +08:00
Page({
data: {
2024-08-31 17:33:35 +08:00
thumb: "", // 用户头像
nickname: "", // 用户昵称
orders: [], // 订单列表
hasAddress: false, // 是否有地址信息
address: {}, // 地址信息
2024-08-29 14:36:16 +08:00
},
2024-08-31 17:33:35 +08:00
2024-08-29 14:36:16 +08:00
onLoad() {
2024-08-31 17:33:35 +08:00
this.checkUserProfile(); // 检查并获取用户信息
2024-08-29 14:36:16 +08:00
/**
* 发起请求获取订单列表信息
*/
2024-08-31 17:33:35 +08:00
this.loadOrders(); // 加载订单列表
2024-08-29 14:36:16 +08:00
},
2024-08-31 17:33:35 +08:00
/**
* 检查本地缓存中是否有用户信息
*/
checkUserProfile() {
const userInfo = wx.getStorageSync('userInfo');
if (userInfo) {
// 如果有用户信息,直接设置数据
this.setData({
thumb: userInfo.userInfo.avatarUrl,
nickname: userInfo.userInfo.nickName,
});
} else {
// 没有用户信息,提示用户登录授权
wx.showModal({
title: '温馨提示',
content: '请先登录!',
showCancel: false,
success: (res) => {
if (res.confirm) {
this.getUserProfile();
}
}
});
}
},
2024-08-29 14:36:16 +08:00
/**
2024-08-30 15:23:27 +08:00
* 获取用户信息
*/
2024-08-29 14:36:16 +08:00
getUserProfile() {
wx.getUserProfile({
2024-08-30 15:23:27 +08:00
desc: "用于完善会员资料", // 这里必须声明用途
2024-08-31 17:33:35 +08:00
success: (userInfo) => {
// 存储用户信息到本地缓存中
wx.setStorageSync('userInfo', userInfo);
// 在页面中展示用户信息
this.setData({
thumb: userInfo.userInfo.avatarUrl,
nickname: userInfo.userInfo.nickName,
});
console.log('用户信息', userInfo);
2024-08-29 14:36:16 +08:00
},
2024-08-31 17:33:35 +08:00
fail: (err) => {
2024-08-29 14:36:16 +08:00
wx.showToast({
2024-08-30 15:23:27 +08:00
title: "用户拒绝了授权",
icon: "none",
2024-08-31 17:33:35 +08:00
});
console.log(err);
}
});
},
/**
* 微信登录
*/
wxLogin() {
wx.login({
success: (res) => {
if (res.code) {
console.log('微信登录成功code:', res.code);
// 可以将code发送到服务器进行登录验证
} else {
console.log('登录失败!' + res.errMsg);
}
}
});
},
/**
* 登录到服务器可选基于需求
*/
loginToServer(code, userInfo) {
wx.request({
url: `${baseUrl}/login`, // 替换为你的服务器登录接口
method: 'POST',
data: {
code: code, // 微信登录凭证
avatar: userInfo.avatarUrl,
nickname: userInfo.nickName,
gender: userInfo.gender
},
success: (res) => {
if (res.data.code === 1) {
// 登录成功,保存服务器返回的 token
wx.setStorageSync('token', res.data.token);
// 其他登录后的初始化操作
}
},
fail: (err) => {
console.log('服务器登录失败', err);
}
});
},
/**
* 加载订单列表
*/
loadOrders() {
wx.request({
url: `${baseUrl}/order/loadData`,
method: "POST",
data: {
userId: 1, // 示例用户ID
},
success: (res) => {
this.setData({
orders: res.data,
});
2024-08-30 15:23:27 +08:00
},
2024-08-31 17:33:35 +08:00
fail: (err) => {
console.log('获取订单失败', err);
}
});
2024-08-29 14:36:16 +08:00
},
2024-08-31 17:33:35 +08:00
2024-08-29 14:36:16 +08:00
onShow() {
/**
* 获取本地缓存 地址信息
*/
wx.getStorage({
2024-08-30 15:23:27 +08:00
key: "address",
2024-08-31 17:33:35 +08:00
success: (res) => {
this.setData({
2024-08-29 14:36:16 +08:00
hasAddress: true,
2024-08-30 15:23:27 +08:00
address: res.data,
2024-08-31 17:33:35 +08:00
});
2024-08-30 15:23:27 +08:00
},
2024-08-31 17:33:35 +08:00
});
2024-08-29 14:36:16 +08:00
},
2024-08-31 17:33:35 +08:00
2024-08-29 14:36:16 +08:00
/**
* 发起支付请求
*/
payOrders() {
wx.requestPayment({
2024-08-30 15:23:27 +08:00
timeStamp: "String1",
nonceStr: "String2",
package: "String3",
signType: "MD5",
paySign: "String4",
2024-08-31 17:33:35 +08:00
success: (res) => {
console.log(res);
2024-08-29 14:36:16 +08:00
},
2024-08-31 17:33:35 +08:00
fail: (res) => {
2024-08-29 14:36:16 +08:00
wx.showModal({
2024-08-30 15:23:27 +08:00
title: "支付提示",
content: "<text>",
showCancel: false,
2024-08-31 17:33:35 +08:00
});
2024-08-30 15:23:27 +08:00
},
2024-08-31 17:33:35 +08:00
});
2024-08-30 15:23:27 +08:00
},
2024-08-31 17:33:35 +08:00
});