import AuthApi from '../../../services/auth' import UserApi from '../../../services/user' import { uploadProps } from '../../../utils/http' import Navi from '../../../utils/navi' import { clearUser, getUser, updateAvatar, updateName } from '../../../utils/util' Page({ /** * 页面的初始数据 */ data: { avatarUrl: '', name: '', }, /** * 生命周期函数--监听页面加载 */ onLoad() {}, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() {}, /** * 生命周期函数--监听页面显示 */ onShow() { this.getUserInfo() }, /** * 生命周期函数--监听页面隐藏 */ onHide() {}, /** * 生命周期函数--监听页面卸载 */ onUnload() {}, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() {}, /** * 页面上拉触底事件的处理函数 */ onReachBottom() {}, /** * 用户点击右上角分享 */ onShareAppMessage() {}, onChooseAvatar(e: WechatMiniprogram.CustomEvent<{ avatarUrl: string }>) { const self = this const { avatarUrl } = e.detail if (avatarUrl && avatarUrl.length > 0) { wx.cropImage({ src: avatarUrl, // 图片路径 cropScale: '1:1', // 裁剪比例 success(rsp) { self.goupload(rsp.tempFilePath) }, fail() { self.goupload(avatarUrl) }, }) } }, onInputChange( e: WechatMiniprogram.CustomEvent<{ value: string; cursor: number; keyCode: number }>, ) { this.setData({ name: e.detail.value, }) }, getUserInfo() { const info = getUser() if (info != null) { this.setData({ avatarUrl: info.avatarUrl, name: info.nickName, }) } }, goexit() { clearUser() setTimeout(() => { Navi.switchTab({ url: '/pages/mine/personal', }) }, 10) }, gosave() { wx.showLoading({ title: '正在保存', }) UserApi.UpdateName({ name: this.data.name, }) .then((rsp) => { wx.hideLoading() if (rsp.result === 'OK') { console.log(rsp, rsp.result === 'OK', this.data.name) updateName(this.data.name) wx.showToast({ title: '昵称修改成功', duration: 1000, }) } }) .catch(() => { wx.hideLoading() wx.showToast({ title: '昵称修改不成功', duration: 1000, }) }) }, goupload(path: string) { const self = this const props = uploadProps('mode=wxavatar') as any if (!props) { wx.showToast({ title: '登录信息已过期,请重新登录', duration: 1000, success() { Navi.navigateTo({ url: '/pages/login/index', }) }, }) } wx.showLoading({ title: '数据加载中', }) wx.uploadFile({ ...props, filePath: path, name: 'file', timeout: 5000, success(rsp) { if (rsp.data && rsp.data.length > 0) { const response = JSON.parse(rsp.data) as IRestResult<{ attachment: string }> if (response.result && response.result.attachment) { self.setData( { avatarUrl: response.result.attachment, }, () => { updateAvatar(response.result.attachment) }, ) } } }, fail() { wx.showToast({ title: '修改头像不成功', }) }, complete() { wx.hideLoading() }, }) }, })