Files
xuan_wechat/pages/user/setup/setup.js
2023-08-15 17:18:15 +08:00

135 lines
3.0 KiB
JavaScript

// pages/user/setup/setup.js
Page({
/**
* 页面的初始数据
*/
data: {
userData : '', // 基础信息
avatar : '',
nickName : '',
nameState : false,
disabled : false,
reviseType: '', // 修改类型
nameValue : '' // 限制5个字符
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
// 获取登录状态
if(wx.getStorageSync("token") != ''){
// 获取用户信息
this.userInfo();
}
},
/**
* 用户设置信息
*/
userInfo() {
wx.$api.user.userSetup().then(res => {
this.setData({
userData : res.data,
avatar : res.data.avatar,
nickName : res.data.nickname
})
}).catch(err => {})
},
/**
* 头像上传
*/
updImg(e){
let type = e.currentTarget.dataset.type
this.setData({
reviseType: e.currentTarget.dataset.type
})
if(type == 'avatar') {
wx.chooseMedia({
count : 1,
mediaType: ['image'],
success : path => {
// 上传图片
wx.$api.file.uploadImg(path.tempFiles[0].tempFilePath, {}).then(res=>{
this.setData({
avatar:res.url
})
this.settingInfo(type, res.path)
})
}
})
return
}
// 修改用户名
this.setData({
nameState: true
})
},
/*
姓名截取
*/
bindinput(e) {
this.setData({
nameValue: e.detail.value.substr(0,5)
})
},
// 修改用户名
freeform() {
this.settingInfo(this.data.reviseType, this.data.nameValue)
},
/**
* 上传用户信息
*/
settingInfo(key, value) {
wx.$api.user.setting(key, {
value: value
}).then(() => {
this.setData({
nameState: false,
nameValue: ''
})
// 获取用户信息
this.userInfo();
}).catch(err => {})
},
// 关闭弹框
establish() {
this.setData({
nameState: false,
nameValue: ''
})
},
/**
* 退出登录
*/
outLogin() {
wx.showModal({
title : '提示',
content : '是否退出登录',
success : res=> {
if (res.confirm) {
// 清理客户端登录缓存
wx.removeStorageSync("token")
wx.switchTab({
url: '/pages/user/index'
})
}
}
})
},
})