mine.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. const uploadJS = require('../../mixin/upload.js')
  2. const { updateUserInfo, userLoginOut } = require('../../api/common')
  3. const app = getApp()
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. form: {
  10. user_head_img_url: '',
  11. user_nickname: '',
  12. account: ''
  13. },
  14. user_head_img_url: [],
  15. user_nickname: '',
  16. isAutoFocus: false,
  17. booNickname: false,
  18. booLogout: false,
  19. booLock: false
  20. },
  21. /**
  22. * 生命周期函数--监听页面加载
  23. */
  24. onLoad(options) {
  25. },
  26. /**
  27. * 生命周期函数--监听页面初次渲染完成
  28. */
  29. onReady() {
  30. },
  31. /**
  32. * 生命周期函数--监听页面显示
  33. */
  34. onShow() {
  35. this.getTabBar().init()
  36. },
  37. /**
  38. * 生命周期函数--监听页面隐藏
  39. */
  40. onHide() {
  41. },
  42. /**
  43. * 生命周期函数--监听页面卸载
  44. */
  45. onUnload() {
  46. },
  47. ...uploadJS,
  48. // 图片上传成功回调
  49. uploadCallBack(res) {
  50. const temp = res.map(item => {
  51. return {
  52. 'url': item.url,
  53. 'formkey': item.formkey
  54. }
  55. })
  56. let formkey = ''
  57. if (temp.length > 0) {
  58. formkey = temp[0].formkey
  59. }
  60. if (formkey === 'user_head_img_url') {
  61. this._updateUserInfo(formkey, temp[0].url, this.data.form.user_head_img_url)
  62. }
  63. },
  64. // 有昵称时:编辑昵称
  65. editNickname() {
  66. this.setData({
  67. user_nickname: this.data.form.user_nickname,
  68. booNickname: true
  69. })
  70. setTimeout(() => {
  71. this.setData({
  72. isAutoFocus: true
  73. })
  74. }, 500)
  75. },
  76. bindInput(e) {
  77. this.setData({
  78. user_nickname: e.detail.value.trim()
  79. })
  80. },
  81. // 失去焦点:编辑昵称
  82. bindblur() {
  83. const user_nickname = this.data.user_nickname
  84. if (user_nickname.length < 1) {
  85. this.setData({
  86. booNickname: false
  87. })
  88. return
  89. }
  90. this._updateUserInfo('user_nickname', user_nickname, this.data.form.user_nickname)
  91. },
  92. async _updateUserInfo(key, newVal, oldVal) {
  93. let val = newVal
  94. try {
  95. const { status, msg } = await updateUserInfo({ [key]: newVal })
  96. if (status) {
  97. await app.fetchUserData()
  98. } else {
  99. wx.showToast({
  100. title: msg,
  101. icon: 'none'
  102. })
  103. }
  104. } catch (e) {
  105. val = oldVal
  106. }
  107. const temp = {
  108. [`form.${key}`]: val
  109. }
  110. if (key === 'user_head_img_url') {
  111. temp.user_head_img_url = [
  112. {
  113. 'url': val,
  114. 'formkey': key
  115. }
  116. ]
  117. }
  118. if (key === 'user_nickname') {
  119. temp.booNickname = false
  120. }
  121. this.setData(temp)
  122. },
  123. jump(e) {
  124. const { page } = e.currentTarget.dataset
  125. wx.navigateTo({
  126. url: `/pages/${page}/${page}`
  127. })
  128. },
  129. showLogout() {
  130. this.setData({
  131. booLogout: true
  132. })
  133. },
  134. hideLogout() {
  135. this.setData({
  136. booLogout: false
  137. })
  138. },
  139. async confirmLogout() {
  140. this.setData({
  141. booLock: true
  142. })
  143. try {
  144. const { status, msg } = await userLoginOut()
  145. if (status) {
  146. this.hideLogout()
  147. wx.removeStorageSync(sessionStorageKey)
  148. // 更新用户信息
  149. app.globalData.userInfo = {}
  150. wx.reLaunch({
  151. url: '/pages/mine/mine'
  152. })
  153. } else {
  154. wx.showToast({
  155. title: msg,
  156. icon: 'none'
  157. })
  158. }
  159. } catch (e) {}
  160. this.setData({
  161. booLock: false
  162. })
  163. }
  164. })