index.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import AuthApi from '../../../services/auth'
  2. import UserApi from '../../../services/user'
  3. import { uploadProps } from '../../../utils/http'
  4. import Navi from '../../../utils/navi'
  5. import { clearUser, getUser, updateAvatar, updateName } from '../../../utils/util'
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. avatarUrl: '',
  12. name: '',
  13. },
  14. /**
  15. * 生命周期函数--监听页面加载
  16. */
  17. onLoad() {},
  18. /**
  19. * 生命周期函数--监听页面初次渲染完成
  20. */
  21. onReady() {},
  22. /**
  23. * 生命周期函数--监听页面显示
  24. */
  25. onShow() {
  26. this.getUserInfo()
  27. },
  28. /**
  29. * 生命周期函数--监听页面隐藏
  30. */
  31. onHide() {},
  32. /**
  33. * 生命周期函数--监听页面卸载
  34. */
  35. onUnload() {},
  36. /**
  37. * 页面相关事件处理函数--监听用户下拉动作
  38. */
  39. onPullDownRefresh() {},
  40. /**
  41. * 页面上拉触底事件的处理函数
  42. */
  43. onReachBottom() {},
  44. /**
  45. * 用户点击右上角分享
  46. */
  47. onShareAppMessage() {},
  48. onChooseAvatar(e: WechatMiniprogram.CustomEvent<{ avatarUrl: string }>) {
  49. const self = this
  50. const { avatarUrl } = e.detail
  51. if (avatarUrl && avatarUrl.length > 0) {
  52. wx.cropImage({
  53. src: avatarUrl, // 图片路径
  54. cropScale: '1:1', // 裁剪比例
  55. success(rsp) {
  56. self.goupload(rsp.tempFilePath)
  57. },
  58. fail() {
  59. self.goupload(avatarUrl)
  60. },
  61. })
  62. }
  63. },
  64. onInputChange(
  65. e: WechatMiniprogram.CustomEvent<{ value: string; cursor: number; keyCode: number }>,
  66. ) {
  67. this.setData({
  68. name: e.detail.value,
  69. })
  70. },
  71. getUserInfo() {
  72. const info = getUser()
  73. if (info != null) {
  74. this.setData({
  75. avatarUrl: info.avatarUrl,
  76. name: info.nickName,
  77. })
  78. }
  79. },
  80. goexit() {
  81. clearUser()
  82. setTimeout(() => {
  83. Navi.switchTab({
  84. url: '/pages/mine/personal',
  85. })
  86. }, 10)
  87. },
  88. gosave() {
  89. wx.showLoading({
  90. title: '正在保存',
  91. })
  92. UserApi.UpdateName({
  93. name: this.data.name,
  94. })
  95. .then((rsp) => {
  96. wx.hideLoading()
  97. if (rsp.result === 'OK') {
  98. console.log(rsp, rsp.result === 'OK', this.data.name)
  99. updateName(this.data.name)
  100. wx.showToast({
  101. title: '昵称修改成功',
  102. duration: 1000,
  103. })
  104. }
  105. })
  106. .catch(() => {
  107. wx.hideLoading()
  108. wx.showToast({
  109. title: '昵称修改不成功',
  110. duration: 1000,
  111. })
  112. })
  113. },
  114. goupload(path: string) {
  115. const self = this
  116. const props = uploadProps('mode=wxavatar') as any
  117. if (!props) {
  118. wx.showToast({
  119. title: '登录信息已过期,请重新登录',
  120. duration: 1000,
  121. success() {
  122. Navi.navigateTo({
  123. url: '/pages/login/index',
  124. })
  125. },
  126. })
  127. }
  128. wx.showLoading({
  129. title: '数据加载中',
  130. })
  131. wx.uploadFile({
  132. ...props,
  133. filePath: path,
  134. name: 'file',
  135. timeout: 5000,
  136. success(rsp) {
  137. if (rsp.data && rsp.data.length > 0) {
  138. const response = JSON.parse(rsp.data) as IRestResult<{ attachment: string }>
  139. if (response.result && response.result.attachment) {
  140. self.setData(
  141. {
  142. avatarUrl: response.result.attachment,
  143. },
  144. () => {
  145. updateAvatar(response.result.attachment)
  146. },
  147. )
  148. }
  149. }
  150. },
  151. fail() {
  152. wx.showToast({
  153. title: '修改头像不成功',
  154. })
  155. },
  156. complete() {
  157. wx.hideLoading()
  158. },
  159. })
  160. },
  161. })