2025-06-06 15:17:30 +08:00

193 lines
3.9 KiB
JavaScript

const {
getUserInfo,
getTeacherUserInfo,
changeUserInfo,
uploadPic
} = require("../../../utils/serve/user")
import {ipAddress} from '../../../utils/http'
// subpackage/user/info/info.js
Page({
/**
* 页面的初始数据
*/
data: {
user: {},
theme: 'user',
nowDate: '',
genderList: ['男', '女'],
heightList: [],
weightList: []
},
/**
* 生命周期函数--监听页面加载
*/
async onLoad(options) {
await this.getInfo()
const now = new Date();
const year = now.getFullYear();
const month = String(now.getMonth() + 1).padStart(2, '0');
const day = String(now.getDate()).padStart(2, '0');
let list1 = []
let list2 = []
for (let index = 0; index < 200; index++) {
list1.push(index + 'cm')
list2.push(index + 'kg')
}
this.setData({
nowDate: year + '-' + month + '-' + day,
heightList: list1,
weightList: list2
})
},
//获取个人信息
async getInfo() {
const theme = wx.getStorageSync('theme')
let res
if (theme == 'teacher') {
res = await getTeacherUserInfo()
console.log(res)
} else {
res = await getUserInfo()
console.log(res)
}
this.setData({
user: res.data,
theme: theme
})
},
async bindPickerChange(e) {
console.log(e)
const that = this
let data = this.data.user
if (e.target.id == 'gender') {
data.gender = Number(e.detail.value) + 1
} else {
data[e.target.id] = e.detail.value
}
const res = await changeUserInfo(data)
await that.getInfo()
if (res.errorCode == 0) {
wx.showToast({
title: '修改成功',
})
}
console.log(res)
},
onChooseAvatar(e) {
wx.showLoading({
title: '头像上传中',
})
console.log(e)
const that = this
const token = wx.getStorageSync('token')
wx.uploadFile({
url: ipAddress + 'User/UploadPhoto', // 替换为你的服务器上传接口地址
filePath: e.detail.avatarUrl, // 临时文件路径
name: 'file', // 服务器端用来接收文件的字段名
header: {
'content-type': 'multipart/form-data', // 必须设置,
'Authorization': 'Bearer ' + token
// 其他请求头
},
success:async(res) =>{
console.log('上传成功', res);
// 处理服务器返回的数据
if (res.statusCode === 200) {
let uu = that.data.user
uu.headImageUrl = JSON.parse(res.data).data
const aa = await changeUserInfo(uu)
await that.getInfo()
if (aa.errorCode == 0) {
wx.setStorageSync('updateAvater', '1')
wx.showToast({
title: '更新头像成功',
icon:'none'
})
}
} else {
wx.showToast({
title: '上传失败',
icon: 'none'
});
}
},
fail(err) {
console.error('上传失败', err);
wx.showToast({
title: '上传失败',
icon: 'none'
});
}
});
},
async namechange(e){
console.log(e)
const that = this
if(e.detail.value !=''){
const uss = this.data.user
uss.userTrueName = e.detail.value
await changeUserInfo(uss)
await that.getInfo()
wx.setStorageSync('updateAvater', '1')
}
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady() {
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide() {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload() {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage() {
}
})