151 lines
3.8 KiB
JavaScript
151 lines
3.8 KiB
JavaScript
const app = getApp();
|
|
const baseUrl = app.globalData.baseUrl;
|
|
|
|
Page({
|
|
data: {
|
|
thumb: "", // 用户头像
|
|
nickname: "", // 用户昵称
|
|
hasAddress: false, // 是否有地址信息
|
|
address: {}, // 地址信息
|
|
},
|
|
|
|
onLoad() {
|
|
this.checkUserProfile(); // 检查并获取用户信息
|
|
this.getUserProfile();
|
|
},
|
|
|
|
/**
|
|
* 检查本地缓存中是否有用户信息
|
|
*/
|
|
checkUserProfile() {
|
|
const userInfo = wx.getStorageSync('userInfo');
|
|
if (userInfo) {
|
|
// 如果有用户信息,直接设置数据
|
|
this.setData({
|
|
thumb: userInfo.avatarUrl,
|
|
nickname: userInfo.nickName,
|
|
});
|
|
} else {
|
|
// 没有用户信息,提示用户登录授权
|
|
wx.showModal({
|
|
title: '温馨提示',
|
|
content: '请先登录!',
|
|
showCancel: false,
|
|
success: (res) => {
|
|
if (res.confirm) {
|
|
wx.switchTab({
|
|
url: '/page/component/login/login',
|
|
});
|
|
}
|
|
}
|
|
});
|
|
}
|
|
},
|
|
|
|
/**
|
|
* 获取用户信息
|
|
*/
|
|
getUserProfile() {
|
|
//通过wx.login()获取登录凭证code
|
|
wx.login({
|
|
success: (res) => {
|
|
console.log("code:" + res.code)
|
|
const wxConfig = {
|
|
appid: "wx865aefa5a7115ae0",
|
|
secret: "3f9849429894435abc935eea88178dfd",
|
|
code: res.code
|
|
}
|
|
wx.request({
|
|
url: "https://api.weixin.qq.com/sns/jscode2session?appid=" + wxConfig.appid + "&secret=" + wxConfig.secret + "&code=" + wxConfig.code + "&js_code=" + wxConfig.code + '&grant_type=authorization_code',
|
|
success: (res) => {
|
|
wx.setStorageSync('openid', res.data.openid);
|
|
console.log(res);
|
|
},
|
|
fail: (err) => {
|
|
console.error('wx.login 失败', err);
|
|
}
|
|
})
|
|
},
|
|
})
|
|
},
|
|
// getUserInfo() {
|
|
// wx.getUserProfile({
|
|
// desc: "用于完善会员资料", // 这里必须声明用途
|
|
// success: (userInfo) => {
|
|
// // 存储用户信息到本地缓存中
|
|
// wx.setStorageSync('userInfo', userInfo);
|
|
// // 在页面中展示用户信息
|
|
// this.setData({
|
|
// thumb: userInfo.userInfo.avatarUrl,
|
|
// nickname: userInfo.userInfo.nickName,
|
|
// });
|
|
// console.log('用户信息', userInfo);
|
|
// wx.request({
|
|
// url: baseUrl + "/user/addOrUpdate",
|
|
// method: "POST",
|
|
// data: {
|
|
// openId: wx.getStorageSync('openid'),
|
|
// userName: userInfo.userInfo.nickName
|
|
// },
|
|
// success(res) {
|
|
// console.log(res)
|
|
// },
|
|
// })
|
|
// },
|
|
// fail: (err) => {
|
|
// wx.showToast({
|
|
// title: "用户拒绝了授权",
|
|
// icon: "none",
|
|
// });
|
|
// console.log(err);
|
|
// }
|
|
// });
|
|
// },
|
|
/**
|
|
* 登录到服务器(可选,基于需求)
|
|
*/
|
|
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);
|
|
}
|
|
});
|
|
},
|
|
|
|
/**
|
|
* 加载订单列表
|
|
*/
|
|
|
|
|
|
onShow() {
|
|
/**
|
|
* 获取本地缓存 地址信息
|
|
*/
|
|
wx.getStorage({
|
|
key: "address",
|
|
success: (res) => {
|
|
this.setData({
|
|
hasAddress: true,
|
|
address: res.data,
|
|
});
|
|
},
|
|
});
|
|
},
|
|
|
|
});
|