detail.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // pages/detail/detail.js
  2. import api from '../../utils/api.js'
  3. import util from '../../utils/util.js'
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. TabIndex: 0,
  10. inputFocus: false,
  11. commentParams: {},
  12. commentList: [],
  13. commentCount: 0,
  14. detailInfo: {},
  15. sendParams: {}
  16. },
  17. /**
  18. * 生命周期函数--监听页面加载
  19. */
  20. onLoad: function (opt) {
  21. if (opt.id) {
  22. this.setData({
  23. commentParams: {
  24. page: 1,
  25. offset: 10,
  26. game_id: opt.id
  27. },
  28. sendParams: {
  29. content: '',
  30. game_id: opt.id
  31. }
  32. })
  33. this.getDetailInfo(opt.id)
  34. this.getDetailComment()
  35. }
  36. },
  37. routeComment: function (e) {
  38. util.routeTo(`/pages/detail/comment?id=${e.target.dataset.gmid}`)
  39. },
  40. FocusInput: function (e) {
  41. this.setData({
  42. inputFocus: true
  43. })
  44. },
  45. ChangeTab: function (e) {
  46. this.setData({
  47. TabIndex: e.target.dataset.index
  48. })
  49. },
  50. inputChange: function (e) {
  51. this.data.sendParams.content = e.detail.value
  52. this.setData({
  53. sendParams: this.data.sendParams
  54. })
  55. },
  56. sendComment: function () {
  57. api.addComment(this.data.sendParams).then(res => {
  58. if (res.code === 200) {
  59. this.data.commentParams.page = 1
  60. this.data.sendParams.content = ''
  61. this.setData({
  62. commentParams: this.data.commentParams,
  63. sendParams: this.data.sendParams
  64. })
  65. this.getDetailComment()
  66. }
  67. })
  68. },
  69. getDetailInfo: function (id) {
  70. api.getGameDetail({ game_id: id}).then(res => {
  71. if (res.code === 200) {
  72. this.setData({
  73. detailInfo: res.data
  74. })
  75. }
  76. })
  77. },
  78. getDetailComment: function () {
  79. api.getGmaeCommentList(this.data.commentParams).then(res => {
  80. if (res.code === 200) {
  81. res.data.list.forEach((item, ids) => {
  82. item.create_time = util.formatTime(item.create_time, 'yyyy/MM/dd hh:mm')
  83. })
  84. this.setData({
  85. commentList: this.data.commentParams.page === 1 ? res.data.list : this.data.commentList.concat(res.data.list),
  86. commentCount: res.data.count
  87. })
  88. }
  89. })
  90. },
  91. /**
  92. * 页面上拉触底事件的处理函数
  93. */
  94. onReachBottom: function () {
  95. if (this.data.commentCount <= this.data.commentList.length) {
  96. return false
  97. }
  98. this.data.commentParams.page = ++this.data.commentParams.page
  99. this.setData({
  100. commentParams: this.data.commentParams
  101. })
  102. this.getDetailComment()
  103. },
  104. openGame: function (e) {
  105. let id = e.currentTarget.dataset.id
  106. api.openGame({
  107. game_id: id
  108. }).then(res => {
  109. console.log(res)
  110. }, err => {
  111. console.log(err)
  112. })
  113. }
  114. })