106 lines
2.7 KiB
JavaScript
106 lines
2.7 KiB
JavaScript
const app = getApp()
|
|
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
|
|
|
|
|
|
Page({
|
|
/**
|
|
* 页面的初始数据
|
|
*/
|
|
data: {
|
|
avatarUrl: defaultAvatarUrl,
|
|
theme: "",
|
|
|
|
},
|
|
|
|
onChooseAvatar(e) {
|
|
const { avatarUrl } = e.detail;
|
|
wx.uploadFile({
|
|
url: app.globalData.baseUrl + "/user/upload", // 替换为你服务器的上传接口
|
|
filePath: avatarUrl,
|
|
name: 'file',
|
|
success: (res) => {
|
|
const data = JSON.parse(res.data);
|
|
const permanentAvatarUrl = data.fileDownloadUri; // 假设服务器返回上传后的URL
|
|
this.setData({
|
|
avatarUrl: permanentAvatarUrl,
|
|
});
|
|
|
|
},
|
|
fail: (err) => {
|
|
console.error('上传失败', err);
|
|
}
|
|
});
|
|
},
|
|
|
|
formSubmit(e) {
|
|
const openid = wx.getStorageSync('openid'); // 从本地存储中获取 openid
|
|
const avatarUrl = this.data.avatarUrl; // 从 data 中获取头像 URL
|
|
const nickname = e.detail.value.nickname; // 从表单中获取用户输入的昵称
|
|
|
|
// 检查是否获取到所有需要的数据
|
|
if (!openid || !avatarUrl || !nickname) {
|
|
wx.showToast({
|
|
title: '信息不完整',
|
|
icon: 'none',
|
|
});
|
|
return;
|
|
}
|
|
//将用户信息存到StorageSync
|
|
// 将用户信息存到 StorageSync
|
|
const userInfo = {
|
|
avatarUrl: avatarUrl,
|
|
nickName: nickname
|
|
};
|
|
wx.setStorageSync('userInfo', userInfo); // 存储到本地
|
|
// 将数据发送到后端
|
|
wx.request({
|
|
url: app.globalData.baseUrl + "/user/addOrUpdate", // 替换为你的后端接口地址
|
|
method: 'POST',
|
|
data: {
|
|
openId: openid,
|
|
avatarUrl: avatarUrl,
|
|
userName: nickname,
|
|
},
|
|
header: {
|
|
'Content-Type': 'application/json', // 设置请求头
|
|
},
|
|
success(res) {
|
|
if (res.statusCode === 200) {
|
|
wx.showToast({
|
|
title: '信息提交成功',
|
|
icon: 'success',
|
|
});
|
|
// 提交成功后,跳转到首页或其他页面
|
|
wx.switchTab({
|
|
url: '/page/component/index', // 替换为你的首页路径
|
|
});
|
|
} else {
|
|
wx.showToast({
|
|
title: '提交失败',
|
|
icon: 'none',
|
|
});
|
|
}
|
|
},
|
|
fail(err) {
|
|
wx.showToast({
|
|
title: '请求失败',
|
|
icon: 'none',
|
|
});
|
|
console.error('请求失败', err);
|
|
}
|
|
});
|
|
},
|
|
|
|
|
|
/**
|
|
* 生命周期函数--监听页面加载
|
|
*/
|
|
onLoad(options) {
|
|
wx.onThemeChange((result) => {
|
|
this.setData({
|
|
theme: result.theme
|
|
})
|
|
})
|
|
}
|
|
})
|