publisherQuestion.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. console.log(res)
  66. wx.hideLoading()
  67. if (res.result.status !== 'OK') {
  68. wx.showToast({
  69. title: res.result.errMsg,
  70. icon: 'none'
  71. })
  72. return
  73. }
  74. wx.showToast({
  75. title: '保存成功',
  76. icon: 'none'
  77. })
  78. this.data.questions[e.currentTarget.dataset.index].saved = true
  79. this.setData({
  80. questions: this.data.questions
  81. })
  82. })
  83. },
  84. removeQuestion: function (e) {
  85. wx.showModal({
  86. content: "确认删除提问?",
  87. confirmColor: "#009195"
  88. }).then(res => {
  89. if (res.confirm) {
  90. wx.showLoading({
  91. title: '正在删除'
  92. })
  93. wx.cloud.callFunction({
  94. name: 'deleteQuestion',
  95. data: {
  96. que_id: this.data.questions[e.currentTarget.dataset.index]._id
  97. }
  98. }).then(res => {
  99. wx.hideLoading()
  100. if (res.result.status !== 'OK') {
  101. wx.showToast({
  102. title: res.result.errMsg,
  103. icon: 'none'
  104. })
  105. return
  106. }
  107. wx.showToast({
  108. title: "删除成功",
  109. icon: 'none'
  110. })
  111. this.data.questions.splice(e.currentTarget.dataset.index, 1)
  112. this.setData({
  113. questions: this.data.questions
  114. })
  115. })
  116. }
  117. })
  118. },
  119. /**
  120. * 生命周期函数--监听页面加载
  121. */
  122. onLoad: function (options) {
  123. this.setData({
  124. publisherId: options.id
  125. })
  126. wx.showNavigationBarLoading()
  127. wx.cloud.callFunction({
  128. name: 'listQuestions',
  129. data: {
  130. pub_id: this.data.publisherId,
  131. page_token: 0,
  132. page_size: 20
  133. }
  134. }).then(res => {
  135. wx.hideNavigationBarLoading()
  136. if (res.result.status !== 'OK') {
  137. wx.showToast({
  138. title: res.result.errMsg,
  139. icon: 'none'
  140. })
  141. return
  142. }
  143. for (let i = 0; i < res.result.list.length; i++) {
  144. res.result.list[i].saved = true
  145. }
  146. this.setData({
  147. questions: res.result.list
  148. })
  149. })
  150. },
  151. /**
  152. * 生命周期函数--监听页面初次渲染完成
  153. */
  154. onReady: function () {
  155. },
  156. /**
  157. * 生命周期函数--监听页面显示
  158. */
  159. onShow: function () {
  160. },
  161. /**
  162. * 生命周期函数--监听页面隐藏
  163. */
  164. onHide: function () {
  165. },
  166. /**
  167. * 生命周期函数--监听页面卸载
  168. */
  169. onUnload: function () {
  170. },
  171. /**
  172. * 页面相关事件处理函数--监听用户下拉动作
  173. */
  174. onPullDownRefresh: function () {
  175. },
  176. /**
  177. * 页面上拉触底事件的处理函数
  178. */
  179. onReachBottom: function () {
  180. },
  181. /**
  182. * 用户点击右上角分享
  183. */
  184. onShareAppMessage: function () {
  185. }
  186. })