183 lines
4.3 KiB
JavaScript
183 lines
4.3 KiB
JavaScript
import { fetchPerson } from '../../../services/usercenter/fetchPerson';
|
|
import { phoneEncryption } from '../../../utils/util';
|
|
import Toast from 'tdesign-miniprogram/toast/index';
|
|
|
|
Page({
|
|
data: {
|
|
personInfo: {
|
|
avatarUrl: '',
|
|
nickName: '',
|
|
gender: 0,
|
|
phoneNumber: '',
|
|
},
|
|
avatarUrl: '',
|
|
showUnbindConfirm: false,
|
|
pickerOptions: [
|
|
{
|
|
name: '男',
|
|
code: '1',
|
|
},
|
|
{
|
|
name: '女',
|
|
code: '2',
|
|
},
|
|
],
|
|
typeVisible: false,
|
|
genderMap: ['', '男', '女'],
|
|
},
|
|
onLoad() {
|
|
this.init();
|
|
},
|
|
onShow() {
|
|
this.init();
|
|
},
|
|
init() {
|
|
this.fetchData();
|
|
},
|
|
fetchData() {
|
|
fetchPerson().then((res) => {
|
|
console.log(res)
|
|
let personInfo = res.data;
|
|
this.setData({
|
|
personInfo,
|
|
'personInfo.phoneNumber': phoneEncryption(personInfo.phoneNumber),
|
|
});
|
|
});
|
|
},
|
|
onChooseAvatar(e) {
|
|
console.log(e);
|
|
const { avatarUrl } = e.detail
|
|
this.setData({
|
|
avatarUrl,
|
|
})
|
|
wx.uploadFile({
|
|
url: wx.$api.UploadFile,
|
|
filePath: avatarUrl,
|
|
name: 'file',
|
|
formData: {
|
|
'upload_type': 'avatar'
|
|
},
|
|
success: (response) => {
|
|
console.log(response)
|
|
const res = JSON.parse(response.data);
|
|
if (res.code === 1 && res.data && res.data.length > 0) {
|
|
const uploadedFile = res.data[0];
|
|
this.setData({
|
|
'personInfo.avatarUrl': uploadedFile.url,
|
|
});
|
|
this.setPersonInfo({ avatar: uploadedFile.url });
|
|
wx.showToast({
|
|
title: '上传成功',
|
|
icon: 'success'
|
|
});
|
|
} else {
|
|
wx.showToast({
|
|
title: res.msg || '上传失败',
|
|
icon: 'error'
|
|
});
|
|
}
|
|
},
|
|
fail: (err) => {
|
|
console.log('Upload failed:', err);
|
|
wx.showToast({
|
|
title: '上传失败',
|
|
icon: 'error'
|
|
});
|
|
}
|
|
});
|
|
},
|
|
onClickCell({ currentTarget }) {
|
|
const { dataset } = currentTarget;
|
|
const { nickName } = this.data.personInfo;
|
|
|
|
switch (dataset.type) {
|
|
case 'gender':
|
|
this.setData({
|
|
typeVisible: true,
|
|
});
|
|
break;
|
|
case 'name':
|
|
wx.navigateTo({
|
|
url: `/pages/usercenter/name-edit/index?name=${nickName}`,
|
|
});
|
|
break;
|
|
case 'avatarUrl':
|
|
// this.toModifyAvatar();
|
|
wx.showToast({
|
|
title: '修改头像请点击右侧>图标',
|
|
icon: 'none',
|
|
duration: 2000
|
|
});
|
|
break;
|
|
default: {
|
|
break;
|
|
}
|
|
}
|
|
},
|
|
onClose() {
|
|
this.setData({
|
|
typeVisible: false,
|
|
});
|
|
},
|
|
setPersonInfo(params) {
|
|
console.log(params);
|
|
wx.$util.request(wx.$api.SetPersonInfo, params, 'POST').then((res) => {
|
|
console.log(res)
|
|
});
|
|
},
|
|
onConfirm(e) {
|
|
const { value } = e.detail;
|
|
this.setPersonInfo({ gender: value });
|
|
this.setData(
|
|
{
|
|
typeVisible: false,
|
|
'personInfo.gender': value,
|
|
},
|
|
() => {
|
|
Toast({
|
|
context: this,
|
|
selector: '#t-toast',
|
|
message: '设置成功',
|
|
theme: 'success',
|
|
});
|
|
},
|
|
);
|
|
},
|
|
async toModifyAvatar() {
|
|
try {
|
|
const tempFilePath = await new Promise((resolve, reject) => {
|
|
wx.chooseImage({
|
|
count: 1,
|
|
sizeType: ['compressed'],
|
|
sourceType: ['album', 'camera'],
|
|
success: (res) => {
|
|
const { path, size } = res.tempFiles[0];
|
|
if (size <= 10485760) {
|
|
resolve(path);
|
|
} else {
|
|
reject({ errMsg: '图片大小超出限制,请重新上传' });
|
|
}
|
|
},
|
|
fail: (err) => reject(err),
|
|
});
|
|
});
|
|
const tempUrlArr = tempFilePath.split('/');
|
|
const tempFileName = tempUrlArr[tempUrlArr.length - 1];
|
|
Toast({
|
|
context: this,
|
|
selector: '#t-toast',
|
|
message: `已选择图片-${tempFileName}`,
|
|
theme: 'success',
|
|
});
|
|
} catch (error) {
|
|
if (error.errMsg === 'chooseImage:fail cancel') return;
|
|
Toast({
|
|
context: this,
|
|
selector: '#t-toast',
|
|
message: error.errMsg || error.msg || '修改头像出错了',
|
|
theme: 'error',
|
|
});
|
|
}
|
|
},
|
|
});
|