index.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import {
  2. getUserInfo,
  3. setFans,
  4. getUserRead
  5. } from '~/api/user'
  6. import share from '~/mixins/share'
  7. import reachBottom from '~/mixins/reachBottom'
  8. Page({
  9. behaviors: [reachBottom, share],
  10. /**
  11. * 页面的初始数据
  12. */
  13. data: {
  14. uid: '',
  15. userInfo: {},
  16. // type为pk,顶部显示为pk时样式,user为默认样式
  17. type: 'user',
  18. uid: '',
  19. localUid: wx.getStorageSync('uid')
  20. },
  21. /**
  22. * 生命周期函数--监听页面加载
  23. */
  24. onLoad(options) {
  25. if (wx.getStorageSync('uid') == options.uid) {
  26. wx.setNavigationBarTitle({
  27. title: '我的主页'
  28. })
  29. }
  30. this.setData({
  31. type: options.type || 'user',
  32. uid: options.uid
  33. })
  34. },
  35. onShow() {
  36. this.getUserInfo()
  37. },
  38. async getUserInfo() {
  39. let res = await getUserInfo({
  40. uid: this.data.uid
  41. })
  42. this.setData({
  43. userInfo: res
  44. })
  45. this.resetData()
  46. },
  47. loadMore() {
  48. this.getData(getUserRead, {
  49. uid: this.data.userInfo.user.uid,
  50. pageSize: 20
  51. })
  52. },
  53. // 关注
  54. async setFans() {
  55. if (wx.getStorageSync('uid') == this.data.uid) {
  56. return wx.showToast({
  57. title: '不可以关注自己哦~',
  58. icon: 'none'
  59. })
  60. }
  61. let newLike = !this.data.userInfo.like
  62. let res = await setFans({
  63. uid: this.data.userInfo.user.uid,
  64. }, 'put')
  65. this.setData({
  66. ['userInfo.like']: newLike
  67. })
  68. wx.showToast({
  69. title: newLike ? '已关注' : '取消关注',
  70. icon: 'none'
  71. })
  72. },
  73. toPkPage({
  74. currentTarget
  75. }) {
  76. if (this.data.userInfo.user.profession == '官方' || wx.getStorageSync('uid') == this.data.uid) {
  77. wx.navigateTo({
  78. url: `/pages/reading/index?videoId=${currentTarget.dataset.id}`
  79. })
  80. } else {
  81. wx.navigateTo({
  82. url: `/pages/pkPage/index?videoId=${currentTarget.dataset.id}`
  83. })
  84. }
  85. },
  86. })