index.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. const app = getApp()
  2. import { my_menu } from "../../utils/site";
  3. Page({
  4. data: {
  5. logo: '',
  6. avatarUrl: '',
  7. user: {},
  8. // 按钮
  9. menuList: my_menu,
  10. // 状态
  11. statusList: []
  12. },
  13. toPath(e) {
  14. let data = e.detail;
  15. let url = `/${data.route}`;
  16. if (data.type == '0') wx.navigateTo({ url })
  17. else if (data.type == '1') wx.redirectTo({ url })
  18. else if (data.type == '2') wx.relaunch({ url })
  19. else if (data.type == '3') wx.switchTab({ url })
  20. },
  21. // 我的服务-功能按钮
  22. toCommon: function (e) {
  23. const that = this;
  24. if (that.data.user._id) {
  25. let { route } = e.currentTarget.dataset;
  26. if (route) wx.navigateTo({ url: `/${route}` })
  27. else {
  28. wx.showModal({
  29. title: '提示',
  30. content: '是否确认退出登录',
  31. success(res) {
  32. if (res.confirm) {
  33. wx.removeStorage({
  34. key: 'user',
  35. success(res) {
  36. return wx.redirectTo({ url: '/pagesMy/home/index' })
  37. }
  38. })
  39. }
  40. }
  41. })
  42. }
  43. } else {
  44. wx.showToast({ title: `暂无用户信息,无法查看`, icon: 'none' });
  45. wx.navigateTo({ url: '/pagesCommon/login/index' })
  46. }
  47. },
  48. // 去登录
  49. toLogin() {
  50. wx.navigateTo({ url: '/pagesCommon/login/index' })
  51. },
  52. // 获取头像
  53. async toAvatarUrl(e) {
  54. const that = this;
  55. const { avatarUrl } = e.detail
  56. let res = await app.$apifile('files/ball/users/upload', null, avatarUrl)
  57. res = JSON.parse(res);
  58. if (res.errcode == '0') {
  59. let icon = [{ id: res.id, name: res.name, uri: res.uri, url: `${app.globalData.fileserverUrl}` + res.uri }]
  60. let arr = await app.$api(`user/${that.data.user._id}`, 'POST', { icon: icon });
  61. if (arr.errcode == '0') {
  62. const user = await app.$api(`user/${that.data.user._id}`, 'get', {});
  63. if (user.errcode == '0') wx.setStorage({ key: "user", data: user.data })// 存用户信息到storage,以便之后判断用户是否登录
  64. wx.showToast({ title: `头像上传成功`, icon: 'success' });
  65. that.search()
  66. } else {
  67. wx.showToast({ title: `${arr.errmsg}`, icon: 'error' });
  68. }
  69. }
  70. },
  71. // 过滤字典表
  72. getDict(value, model) {
  73. const that = this;
  74. if (value) {
  75. let list = that.data.statusList
  76. let data = list.find(i => i.value == value);
  77. if (data) return data.label
  78. else return '暂无'
  79. }
  80. },
  81. /**
  82. * 生命周期函数--监听页面加载
  83. */
  84. async onLoad(options) {
  85. },
  86. /**
  87. * 生命周期函数--监听页面显示
  88. */
  89. async onShow() {
  90. const that = this;
  91. wx.showLoading({ title: '加载中', mask: true })
  92. await that.searchConfig();
  93. await that.searchOther();
  94. await that.search()
  95. wx.hideLoading()
  96. },
  97. searchConfig() {
  98. const that = this;
  99. wx.getStorage({
  100. key: 'config',
  101. async success(res) {
  102. // 管理员。
  103. if (res.data && res.data.logo_url && res.data.logo_url.length > 0) {
  104. let logo = res.data.logo_url[0].url
  105. that.setData({ logo })
  106. }
  107. },
  108. fail(err) {
  109. // console.log(err);
  110. }
  111. })
  112. },
  113. // 查询其他信息
  114. async searchOther() {
  115. const that = this;
  116. let res;
  117. // 状态
  118. res = await app.$api(`dictData`, 'GET', { type: 'status', is_use: '0' });
  119. if (res.errcode == '0') that.setData({ statusList: res.data })
  120. },
  121. async search() {
  122. const that = this;
  123. wx.getStorage({
  124. key: 'user',
  125. async success(res) {
  126. let arr = await app.$api(`user/${res.data._id}`, 'GET', {})
  127. if (arr.errcode == '0') {
  128. let user = arr.data;
  129. // 审核字段处理
  130. user.status_name = that.getDict(user.status, 'statusList')
  131. // 头像字段处理
  132. if (user.icon && user.icon.length > 0) {
  133. let avatarUrl = user.icon[0].url
  134. that.setData({ avatarUrl })
  135. }
  136. that.setData({ user })
  137. } else {
  138. wx.showToast({ title: `${arr.errmsg}`, icon: 'error' });
  139. }
  140. },
  141. fail(err) {
  142. // console.log(err);
  143. }
  144. })
  145. },
  146. /**
  147. * 生命周期函数--监听页面初次渲染完成
  148. */
  149. onReady() {
  150. },
  151. /**
  152. * 生命周期函数--监听页面隐藏
  153. */
  154. onHide() {
  155. },
  156. /**
  157. * 生命周期函数--监听页面卸载
  158. */
  159. onUnload() {
  160. },
  161. /**
  162. * 页面相关事件处理函数--监听用户下拉动作
  163. */
  164. onPullDownRefresh() {
  165. },
  166. /**
  167. * 页面上拉触底事件的处理函数
  168. */
  169. onReachBottom() {
  170. },
  171. /**
  172. * 用户点击右上角分享
  173. */
  174. onShareAppMessage() {
  175. }
  176. })