mine.js 4.2 KB

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