mine.js 4.0 KB

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