mine.js 3.5 KB

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