sav7ng/WeHalo

小程序增加点赞功能的方法

hgmsq opened this issue · 1 comments

hgmsq commented

只需要在post.js 增加以下方法就可以了

//点赞功能
Likes: function () {
    var that = this;
    var urlLikeList = app.globalData.url + '/api/content/posts/'+that.data.postId+'/likes';
    var token = app.globalData.token;
    var params = {            
    };
    //@todo 网络请求API数据
    request.requestPostApi(urlLikeList, token, params, this, this.successSendLike, this.failSendLike);  
},
// 点赞成功回调
successSendLike: function (res, selfObj) {
    var that = this;        
    wx.showToast({
        title: '感谢你的点赞!',
        icon: 'none',
        duration: 2000
    });
    // 设置点赞数+1
    var likes = Number(parseInt(that.data.postLikes) + 1);   
    console.log(likes);
    that.setData({
         postLikes: likes       
    })
},
//点赞失败回调
failSendLike: function (res, selfObj) {
    wx.showToast({
        title: '点赞失败,请联系作者!',
        icon: 'none',
        duration: 2000
    })
},

小程序名称:IT技术分享社区
IT技术分享社区
gh_32334eb097cd_430

感谢贡献代码~