user.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // pages/user/user.js
  2. const app = getApp()
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. userInfo: null,
  9. hasUserInfo: false,
  10. pubInfo: []
  11. },
  12. // bindGetUserInfo: function (e) {
  13. // app.globalData.userInfo = e.detail.userInfo
  14. // this.onShow()
  15. // },
  16. setUserInfo: function () {
  17. this.setData({
  18. userInfo: app.globalData.userInfo,
  19. hasUserInfo: true
  20. })
  21. wx.cloud.callFunction({
  22. name: 'listPublishers',
  23. data: {
  24. type: 'manage'
  25. }
  26. }).then(res => {
  27. if (res.result.list !== undefined) {
  28. this.setData({
  29. pubInfo: res.result.list
  30. })
  31. } else {
  32. wx.showToast({
  33. title: res.result.errMsg,
  34. icon: 'none'
  35. })
  36. }
  37. })
  38. },
  39. getUserProfile: function () {
  40. wx.getUserProfile({
  41. desc: '用于完善个人信息',
  42. success: (res) => {
  43. app.globalData.userInfo = {
  44. name: res.userInfo.nickName,
  45. avatar: res.userInfo.avatarUrl,
  46. gender: res.userInfo.gender
  47. }
  48. app.globalData.hasUserInfo = true
  49. if (this.data.hasUserInfo) {
  50. wx.cloud.callFunction({
  51. name: 'updateUser',
  52. data: app.globalData.userInfo
  53. })
  54. } else {
  55. wx.cloud.callFunction({
  56. name: 'createUser',
  57. data: app.globalData.userInfo
  58. })
  59. }
  60. this.setUserInfo()
  61. }
  62. })
  63. },
  64. previewAvatar: function () {
  65. if (this.data.hasUserInfo) {
  66. wx.previewImage({
  67. urls: [this.data.userInfo.avatar]
  68. })
  69. }
  70. },
  71. publisherLogin: function () {
  72. wx.navigateTo({
  73. url: "/pages/publisherLogin/publisherLogin",
  74. })
  75. },
  76. publisherPage: function () {
  77. wx.navigateTo({
  78. url: "/pages/publisherPage/publisherPage?id=" + this.data.pubInfo[0].pub_id,
  79. })
  80. },
  81. myFollow: function () {
  82. wx.navigateTo({
  83. url: '/pages/myFollow/myFollow',
  84. })
  85. },
  86. myFavor: function () {
  87. wx.navigateTo({
  88. url: '/pages/myFavor/myFavor',
  89. })
  90. },
  91. setting: function () {
  92. wx.navigateTo({
  93. url: '/pages/setting/setting',
  94. })
  95. },
  96. myQuestion: function () {
  97. wx.navigateTo({
  98. url: '/pages/myQuestion/myQuestion',
  99. })
  100. },
  101. aboutUs: function () {
  102. wx.navigateTo({
  103. url: '/pages/aboutUs/aboutUs',
  104. })
  105. },
  106. opinion: function () {
  107. wx.navigateTo({
  108. url: '/pages/opinion/opinion',
  109. })
  110. },
  111. /**
  112. * 生命周期函数--监听页面加载
  113. */
  114. onLoad: function (options) {
  115. // wx.showNavigationBarLoading()
  116. // if (app.globalData.userInfo && app.globalData.publisherId) {
  117. // this.setData({
  118. // userInfo: app.globalData.userInfo,
  119. // hasUserInfo: true,
  120. // publisherId: app.globalData.publisherId
  121. // })
  122. // if (this.data.publisherId.length != 0) {
  123. // const db = wx.cloud.database()
  124. // db.collection("publisherInfoData").doc(this.data.publisherId[0]).get({
  125. // success: function (res) {
  126. // this.setData({
  127. // publisherName: res.data.publisherName
  128. // })
  129. // wx.hideNavigationBarLoading()
  130. // }.bind(this)
  131. // })
  132. // } else {
  133. // wx.hideNavigationBarLoading()
  134. // }
  135. // } else {
  136. // app.userInfoReadyCallback = res => {
  137. // this.setData({
  138. // userInfo: res.userInfo,
  139. // hasUserInfo: true,
  140. // publisherId: app.globalData.publisherId
  141. // })
  142. // if (this.data.publisherId.length != 0) {
  143. // const db = wx.cloud.database()
  144. // db.collection("publisherInfoData").doc(this.data.publisherId[0]).get({
  145. // success: function (res) {
  146. // this.setData({
  147. // publisherName: res.data.publisherName
  148. // })
  149. // wx.hideNavigationBarLoading()
  150. // }.bind(this)
  151. // })
  152. // } else {
  153. // wx.hideNavigationBarLoading()
  154. // }
  155. // }
  156. // }
  157. },
  158. /**
  159. * 生命周期函数--监听页面初次渲染完成
  160. */
  161. onReady: function () {
  162. },
  163. /**
  164. * 生命周期函数--监听页面显示
  165. */
  166. onShow: function () {
  167. // const db = wx.cloud.database()
  168. // db.collection("userInfoData").get({
  169. // success: function (res) {
  170. // if (res.data.length == 0) {
  171. // db.collection("userInfoData").add({
  172. // data: {
  173. // publisherId: []
  174. // },
  175. // success: function (res) {
  176. // app.globalData.publisherId = []
  177. // this.setData({
  178. // userInfo: app.globalData.userInfo,
  179. // hasUserInfo: true,
  180. // publisherId: app.globalData.publisherId
  181. // })
  182. // }.bind(this)
  183. // })
  184. // } else {
  185. // app.globalData.publisherId = res.data[0].publisherId
  186. // this.setData({
  187. // userInfo: app.globalData.userInfo,
  188. // hasUserInfo: true,
  189. // publisherId: app.globalData.publisherId
  190. // })
  191. // if (this.data.publisherId.length != 0) {
  192. // db.collection("publisherInfoData").doc(this.data.publisherId[0]).get({
  193. // success: function (res) {
  194. // this.setData({
  195. // publisherName: res.data.publisherName
  196. // })
  197. // }.bind(this)
  198. // })
  199. // }
  200. // }
  201. // }.bind(this)
  202. // })
  203. if (app.globalData.hasUserInfo) {
  204. this.setUserInfo()
  205. } else {
  206. app.onUserInfoReady = () => {
  207. this.setUserInfo()
  208. }
  209. }
  210. },
  211. /**
  212. * 生命周期函数--监听页面隐藏
  213. */
  214. onHide: function () {
  215. },
  216. /**
  217. * 生命周期函数--监听页面卸载
  218. */
  219. onUnload: function () {
  220. },
  221. /**
  222. * 页面相关事件处理函数--监听用户下拉动作
  223. */
  224. onPullDownRefresh: function () {
  225. },
  226. /**
  227. * 页面上拉触底事件的处理函数
  228. */
  229. onReachBottom: function () {
  230. },
  231. /**
  232. * 用户点击右上角分享
  233. */
  234. onShareAppMessage: function () {
  235. }
  236. })