actDetail.js 2.5 KB

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