123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- 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()
- },
- })
- },
- })
|