mine.js 4.2 KB

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