mine.js 4.3 KB

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