actDetail.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // pages/actDetail/actDetail.js
  2. const app = getApp()
  3. var host = app.globalData.host;
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. info: {},
  10. uid:'',
  11. },
  12. /**
  13. * 生命周期函数--监听页面加载
  14. */
  15. onLoad: function (options) {
  16. // options.id=89;
  17. var that = this;
  18. wx.showNavigationBarLoading()
  19. wx.getStorage({
  20. key: 'uid',
  21. success: function (res) {
  22. that.setData({
  23. uid:res.data,
  24. id: options.id
  25. })
  26. that.getData();
  27. },
  28. })
  29. },
  30. getData:function(){
  31. var that = this;
  32. wx.request({
  33. url: host + '/api/detail',
  34. method: 'GET',
  35. data: {
  36. type: 'article',
  37. id: this.data.id,
  38. uid: this.data.uid
  39. },
  40. success: function (res) {
  41. wx.hideNavigationBarLoading();
  42. that.setData({
  43. info: res.data.data
  44. })
  45. },
  46. fail: function () {
  47. wx.hideLoading();
  48. wx.showToast({
  49. title: '服务器开小差啦!',
  50. icon: 'none'
  51. })
  52. }
  53. })
  54. },
  55. collect:function(e){
  56. var that=this,info=this.data.info;
  57. var data={
  58. type: 'article',
  59. id: this.data.info.id,
  60. uid: this.data.uid
  61. }
  62. if(e.target.dataset.id){
  63. data.action='cancel'
  64. }
  65. wx.request({
  66. url: host+'/api/collect',
  67. data:data,
  68. success:function(res){
  69. if(res.data.code == 0){
  70. if(data.action){
  71. wx.showToast({
  72. title: '已取消',
  73. icon: 'none'
  74. })
  75. info.is_collected = 0
  76. }else{
  77. wx.showToast({
  78. title: '已收藏',
  79. icon:'none'
  80. })
  81. info.is_collected = 1
  82. }
  83. that.setData({
  84. info: info
  85. })
  86. }
  87. }
  88. })
  89. },
  90. /**
  91. * 生命周期函数--监听页面初次渲染完成
  92. */
  93. onReady: function () {
  94. },
  95. /**
  96. * 生命周期函数--监听页面显示
  97. */
  98. onShow: function () {
  99. },
  100. /**
  101. * 生命周期函数--监听页面隐藏
  102. */
  103. onHide: function () {
  104. },
  105. /**
  106. * 生命周期函数--监听页面卸载
  107. */
  108. onUnload: function () {
  109. },
  110. /**
  111. * 页面相关事件处理函数--监听用户下拉动作
  112. */
  113. onPullDownRefresh: function () {
  114. },
  115. /**
  116. * 页面上拉触底事件的处理函数
  117. */
  118. onReachBottom: function () {
  119. },
  120. /**
  121. * 用户点击右上角分享
  122. */
  123. onShareAppMessage: function () {
  124. wx.request({
  125. url: host+'/api/article/forward',
  126. data:{
  127. id:this.data.id
  128. }
  129. })
  130. }
  131. })