publisherQuestion.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. Page({
  2. /**
  3. * 页面的初始数据
  4. */
  5. data: {
  6. publisherId: '',
  7. questions: [],
  8. notAnswered: true,
  9. answered: false
  10. },
  11. updateFilter: function (e) {
  12. this.setData({
  13. notAnswered: e.detail.value.indexOf("0") !== -1,
  14. answered: e.detail.value.indexOf("1") !== -1
  15. })
  16. },
  17. updateText: function (e) {
  18. this.data.questions[e.currentTarget.dataset.index].answer = e.detail.value
  19. this.data.questions[e.currentTarget.dataset.index].saved = false
  20. this.setData({
  21. questions: this.data.questions
  22. })
  23. },
  24. togglePin: function (e) {
  25. const currentRank = this.data.questions[e.currentTarget.dataset.index].rank
  26. wx.showLoading({
  27. title: currentRank === 1 ? '正在置顶' : '取消置顶'
  28. })
  29. wx.cloud.callFunction({
  30. name: 'updateQuestion',
  31. data: {
  32. que_id: this.data.questions[e.currentTarget.dataset.index]._id,
  33. rank: currentRank === 1 ? 2 : 1
  34. }
  35. }).then(res => {
  36. wx.hideLoading()
  37. if (res.result.status !== 'OK') {
  38. wx.showToast({
  39. title: res.result.errMsg,
  40. icon: 'none'
  41. })
  42. return
  43. }
  44. wx.showToast({
  45. title: currentRank === 1 ? '置顶成功' : '取消置顶成功',
  46. icon: 'none'
  47. })
  48. this.data.questions[e.currentTarget.dataset.index].rank = currentRank === 1 ? 2 : 1
  49. this.setData({
  50. questions: this.data.questions
  51. })
  52. })
  53. },
  54. saveQuestion: function (e) {
  55. wx.showLoading({
  56. title: '正在保存'
  57. })
  58. wx.cloud.callFunction({
  59. name: 'updateQuestion',
  60. data: {
  61. que_id: this.data.questions[e.currentTarget.dataset.index]._id,
  62. answer: this.data.questions[e.currentTarget.dataset.index].answer
  63. }
  64. }).then(res => {
  65. wx.hideLoading()
  66. if (res.result.status !== 'OK') {
  67. wx.showToast({
  68. title: res.result.errMsg,
  69. icon: 'none'
  70. })
  71. return
  72. }
  73. wx.showToast({
  74. title: '保存成功',
  75. icon: 'none'
  76. })
  77. this.data.questions[e.currentTarget.dataset.index].saved = true
  78. this.setData({
  79. questions: this.data.questions
  80. })
  81. })
  82. },
  83. removeQuestion: function (e) {
  84. wx.showModal({
  85. content: "确认删除提问?",
  86. confirmColor: "#009195"
  87. }).then(res => {
  88. if (res.confirm) {
  89. wx.showLoading({
  90. title: '正在删除'
  91. })
  92. wx.cloud.callFunction({
  93. name: 'deleteQuestion',
  94. data: {
  95. que_id: this.data.questions[e.currentTarget.dataset.index]._id
  96. }
  97. }).then(res => {
  98. wx.hideLoading()
  99. if (res.result.status !== 'OK') {
  100. wx.showToast({
  101. title: res.result.errMsg,
  102. icon: 'none'
  103. })
  104. return
  105. }
  106. wx.showToast({
  107. title: "删除成功",
  108. icon: 'none'
  109. })
  110. this.data.questions.splice(e.currentTarget.dataset.index, 1)
  111. this.setData({
  112. questions: this.data.questions
  113. })
  114. })
  115. }
  116. })
  117. },
  118. /**
  119. * 生命周期函数--监听页面加载
  120. */
  121. onLoad: function (options) {
  122. this.setData({
  123. publisherId: options.id
  124. })
  125. wx.showNavigationBarLoading()
  126. wx.cloud.callFunction({
  127. name: 'listQuestions',
  128. data: {
  129. pub_id: this.data.publisherId,
  130. page_token: 0,
  131. page_size: 20
  132. }
  133. }).then(res => {
  134. wx.hideNavigationBarLoading()
  135. if (res.result.status !== 'OK') {
  136. wx.showToast({
  137. title: res.result.errMsg,
  138. icon: 'none'
  139. })
  140. return
  141. }
  142. for (let i = 0; i < res.result.list.length; i++) {
  143. res.result.list[i].saved = true
  144. }
  145. this.setData({
  146. questions: res.result.list
  147. })
  148. })
  149. },
  150. /**
  151. * 生命周期函数--监听页面初次渲染完成
  152. */
  153. onReady: function () {
  154. },
  155. /**
  156. * 生命周期函数--监听页面显示
  157. */
  158. onShow: function () {
  159. },
  160. /**
  161. * 生命周期函数--监听页面隐藏
  162. */
  163. onHide: function () {
  164. },
  165. /**
  166. * 生命周期函数--监听页面卸载
  167. */
  168. onUnload: function () {
  169. },
  170. /**
  171. * 页面相关事件处理函数--监听用户下拉动作
  172. */
  173. onPullDownRefresh: function () {
  174. },
  175. /**
  176. * 页面上拉触底事件的处理函数
  177. */
  178. onReachBottom: function () {
  179. },
  180. /**
  181. * 用户点击右上角分享
  182. */
  183. onShareAppMessage: function () {
  184. }
  185. })