flower-mp/components/profile/index.js
2026-01-09 11:37:52 +08:00

104 lines
2.1 KiB
JavaScript

const {
api
} = require("../../utils/api")
const config = require("../../config/config")
// components/profile/index.js
Component({
/**
* 组件的属性列表
*/
properties: {
visible: {
type: Boolean,
require: false
},
},
/**
* 组件的初始数据
*/
data: {
avatar: '',
avatarId: '',
name: ''
},
/**
* 组件的方法列表
*/
methods: {
change() {
const show = !this.data.visible
this.setData({
visible: show
})
},
onChooseAvatar(e) {
const avatarUrl = e.detail.avatarUrl
// 上传
const _this = this
wx.uploadFile({
filePath: avatarUrl,
name: 'file',
header: {
'Authorization': 'Bearer ' + wx.getStorageSync('token'),
},
url: config.baseUrl + '/oss/upload',
success: res => {
var data = JSON.parse(res.data)
console.log(data);
_this.setData({
avatar: data.data.file.url,
avatarId: data.data.file.id
})
}
})
},
input(e) {
const value = e.detail.value
this.data.name = value
},
update() {
const user = wx.getStorageSync('user')
if (this.data.name.length === 0 || this.data.avatarId.length === 0) {
wx.showToast({
icon: 'error',
title: '请完善信息',
})
return
}
user.avatarId = this.data.avatarId
user.name = this.data.name
console.log(user);
wx.showLoading({
title: '请稍后...',
})
api('/user/update','POST',user,'json').then(res => {
if (res.code === 200){
this.fetchInfo(user.id)
} else {
wx.showToast({
icon:'none',
title: res.msg,
})
}
})
},
fetchInfo(id){
api('/user/detail','GET',{id:id}).then(res => {
console.log(res);
wx.hideLoading()
if (res.code === 200){
wx.setStorageSync('user', res.data)
this.change()
this.triggerEvent('ok', {
value: res.data
});
}
})
},
}
})