mine.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. const { user_head_img_url, user_nickname, user_phone } = app.globalData.userInfo
  61. let temp = {}
  62. for (let key in userInfo) {
  63. let value = userInfo[key]
  64. if ((Array.isArray(value) && value.length >= 1) || (Object.prototype.toString.call(value) === '[object Object]') || (typeof value === 'string' && value) || typeof value === 'number') {
  65. if (this.data.hasOwnProperty(key)) {
  66. if (key === 'user_head_img_url') {
  67. value = [
  68. {
  69. url: value,
  70. formkey: key
  71. }
  72. ]
  73. }
  74. temp[`${key}`] = value
  75. }
  76. if (this.data.form.hasOwnProperty(key)) {
  77. temp[`form.${key}`] = value
  78. }
  79. }
  80. }
  81. this.setData(temp)
  82. }
  83. await app.fetchUserData()
  84. },
  85. ...uploadJS,
  86. // 图片上传成功回调
  87. uploadCallBack(res) {
  88. const temp = res.map(item => {
  89. return {
  90. 'url': item.url,
  91. 'formkey': item.formkey
  92. }
  93. })
  94. let formkey = ''
  95. if (temp.length > 0) {
  96. formkey = temp[0].formkey
  97. }
  98. if (formkey === 'user_head_img_url') {
  99. this._updateUserInfo(formkey, temp[0].url)
  100. }
  101. },
  102. // 有昵称时:编辑昵称
  103. editNickname() {
  104. this.setData({
  105. booNickname: true
  106. })
  107. setTimeout(() => {
  108. this.setData({
  109. isAutoFocus: true
  110. })
  111. }, 500)
  112. },
  113. bindInput(e) {
  114. this.setData({
  115. user_nickname: e.detail.value.trim()
  116. })
  117. },
  118. // 失去焦点:编辑昵称
  119. bindblur() {
  120. const user_nickname = this.data.user_nickname
  121. if (user_nickname.length < 1) {
  122. this.setData({
  123. booNickname: false
  124. })
  125. return
  126. }
  127. this._updateUserInfo('user_nickname', user_nickname)
  128. },
  129. async _updateUserInfo(key, val) {
  130. try {
  131. const { status, msg } = await updateUserInfo({ [key]: val })
  132. if (status) {
  133. this.init()
  134. } else {
  135. wx.showToast({
  136. title: msg,
  137. icon: 'none'
  138. })
  139. }
  140. } catch (e) {}
  141. if (key === 'user_nickname') {
  142. this.setData({
  143. booNickname: false
  144. })
  145. }
  146. },
  147. jump(e) {
  148. const { page } = e.currentTarget.dataset
  149. wx.navigateTo({
  150. url: `/pages/${page}/${page}`
  151. })
  152. },
  153. showLogout() {
  154. this.setData({
  155. booLogout: true
  156. })
  157. },
  158. hideLogout() {
  159. this.setData({
  160. booLogout: false
  161. })
  162. },
  163. async confirmLogout() {
  164. this.setData({
  165. booLock: true
  166. })
  167. try {
  168. const { status, msg } = await userLoginOut()
  169. if (status) {
  170. this.hideLogout()
  171. wx.removeStorageSync(sessionStorageKey)
  172. // 更新用户信息
  173. app.globalData.userInfo = {}
  174. wx.reLaunch({
  175. url: '/pages/mine/mine'
  176. })
  177. } else {
  178. wx.showToast({
  179. title: msg,
  180. icon: 'none'
  181. })
  182. }
  183. } catch (e) {}
  184. this.setData({
  185. booLock: false
  186. })
  187. }
  188. })