1、理论核心:传参-》pid,评论父id需要在wxml页面传递;小程序端和WEB端不同核心:前者操纵数据,后者操纵DOM元素对象

2、不废话,直接代码:wxml

<view class="comment-new">
<block wx:if="{{home_comment!='暂无评论~'&&home_comment!=undefined}}">
<block wx:for="{{home_comment}}" wx:for-item="comment" >
<p class="c_1">{{comment.username}}:{{comment.content}}</p>
<a class="reply" bindtap="reply" data-cid="{{comment.c_id}}">回复</a>
<a class="reply" wx:if="{{comment.uid==comment.login_uid}}" bindtap="del" data-cid="{{comment.c_id}}">删除</a>
<!-- 点击回复,展示以下回复form -->
<view wx:if="{{comment.c_id==reply_id}}" hidden="{{reply}}" class="reply_box">
<form bindsubmit="commentForm" report-submit >
<textarea auto-focus="true" name="evaContent" maxlength="500" value="回复 {{comment.username}}:" class="textarea" />
<input hidden="true" name="comment_pid" value="{{comment.c_id}}" />
<button class="save_btn" form-type="submit">发送</button>
</form>
</view>
</block>
<form bindsubmit="commentForm2" report-submit >
<textarea auto-focus="true" name="evaContent" maxlength="500" placeholder="发表评论(50字以内)" class="textarea" />
<input hidden="true" name="comment_pid" value="0" />
<button class="save_btn" form-type="submit">发送</button>
</form>
</block> <!-- 上面判断有评论、有回复、有删除;下面判断无评论,只需要一个发表评论textarea即可 --> <block wx:else>
暂无评论~
<!--这里单独写一个发表评论功能,与上面【回复、删除和展示评论区分开】-->
<form bindsubmit="commentForm3" report-submit >
<textarea auto-focus="true" name="evaContent" maxlength="500" placeholder="发表评论(50字以内)" class="textarea" />
<input hidden="true" name="comment_pid" value="0" />
<button class="save_btn" form-type="submit">发送</button>
</form>
</block>
</view>

3、js:

var getList = function(that){
/* 获取房间评论信息 -xzz 0714*/
wx.request({
url: 'https://m.****.com/index.php/Home/Xiaoxxf/home_comment?home_id=' + that.data.home_id, //房间评论
data: {
'openid': wx.getStorageSync('openid')
},
method: 'GET',
header: {
"Content-Type": "application/x-www-form-urlencoded"
},
dataType: 'json',
success: function (res) {
console.log(res.data);
that.setData({
home_comment: res.data //一维数组,房间评论所有信息
})
console.log(that.data.home_comment);
},
fail: function (res) { },
complete: function (res) { },
})
} page({
onLoad: function (options) {
var that = this;
that.setData({
home_id: options.home_id,
// 评论数据
reply: "true" // 默认隐藏回复
})
/* 初始化获取房间评论信息 -xzz 0714*/
getList(that);
}, reply:function(e){
var that = this;
// 回复form隐藏、展示切换
if(that.data.reply==true){
that.setData({
reply: false //展示回复框
})
}else{
that.setData({
reply: true //隐藏回复框
})
}
that.setData({
reply_id: e.currentTarget.dataset.cid //用户点击回复的评论id
})
},
del:function(e){
var that = this;
console.log(e.currentTarget.dataset.cid);
wx.request({
url: 'https://m.****.com/index.php/Home/Xiaoxxf/home_comment_del?c_id=' + e.currentTarget.dataset.cid, //删除房间评论
data: '',
header: {
'Content-Type': 'application/json'
},
method: 'GET',
success: function(res) {
console.log(res);
wx.showToast({
title: res.data, //数据返回提示,查看后台PHP
icon: 'success',
duration: 2000
})
/* 获取房间评论信息 -xzz 0714*/
getList(that);
},
fail: function(res) {},
complete: function(res) {},
})
}, /**
* 自定义方法,commentForm表单校验,然后提交后台,最后刷新数据
*/
commentForm: function (e) {
var that = this;
// ------------- 3、检查用户登录态 ------------------
wx.checkSession({
success: function (res) { //登录态未过期
console.log(res.errMsg);
},
fail: function (res) { //登录态过期
wx.login();
},
complete: function (res) { },
}) if (e.detail.value.evaContent.length <= 0 || e.detail.value.evaContent.length > 50) {
wx.showToast({
title: '评论字数在1~50字之间',
icon: 'loading',
duration: 1500
})
setTimeout(function () {
wx.hideToast()
}, 2000)
} else if (e.detail.value.comment_pid<0 || isNaN(e.detail.value.comment_pid)) {
wx.showToast({
title: '回复评论参数有误~',
icon: 'loading',
duration: 1500
})
setTimeout(function () {
wx.hideToast()
}, 2000)
} else { //回复评论
wx.request({
url: 'https://m.xxiangfang.com/index.php/Home/Xiaoxxf/reply_comment',//回复评论
data: {
"comment_pid":e.detail.value.comment_pid,
"content": e.detail.value.evaContent,
"home_id":that.data.home_id,
"openid":wx.getStorageSync("openid")
},
header: {
'Content-Type': 'application/json'
},
method: 'GET',
success: function(res) {
console.log(res);
if(res.data.state == 1){ //回复成功,state==1
wx.showToast({
title: res.data.Msg,
icon: 'loading',
duration: 1500
})
/* 获取房间评论信息 -xzz 0714*/
getList(that);
}else{
wx.showToast({ //回复失败,查看原因
title: res.data,
icon: 'loading',
duration: 1500
})
}
},
fail: function(res) {},
complete: function(res) {},
})
}
},
commentForm2: function (e) {
var that = this;
// ------------- 3、检查用户登录态 ------------------
wx.checkSession({
success: function (res) { //登录态未过期
console.log(res.errMsg);
},
fail: function (res) { //登录态过期
wx.login();
},
complete: function (res) { },
}) if (e.detail.value.evaContent.length <= 0 || e.detail.value.evaContent.length > 50) {
wx.showToast({
title: '评论字数在1~50字之间',
icon: 'loading',
duration: 1500
})
setTimeout(function () {
wx.hideToast()
}, 2000)
} else if (e.detail.value.comment_pid < 0 || isNaN(e.detail.value.comment_pid)) {
wx.showToast({
title: '回复评论参数有误~',
icon: 'loading',
duration: 1500
})
setTimeout(function () {
wx.hideToast()
}, 2000)
} else { //回复评论
wx.request({
url: 'https://m.xxiangfang.com/index.php/Home/Xiaoxxf/reply_comment',//回复评论
data: {
"comment_pid": e.detail.value.comment_pid,
"content": e.detail.value.evaContent,
"home_id": that.data.home_id,
"openid": wx.getStorageSync("openid")
},
header: {
'Content-Type': 'application/json'
},
method: 'GET',
success: function (res) {
console.log(res);
if (res.data.state == 1) { //回复成功,state==1
wx.showToast({
title: res.data.Msg,
icon: 'loading',
duration: 1500
})
/* 获取房间评论信息 -xzz 0714*/
getList(that);
} else {
wx.showToast({ //回复失败,查看原因
title: res.data,
icon: 'loading',
duration: 1500
})
}
},
fail: function (res) { },
complete: function (res) { },
})
}
},
commentForm3: function (e) {
var that = this;
// ------------- 3、检查用户登录态 ------------------
wx.checkSession({
success: function (res) { //登录态未过期
console.log(res.errMsg);
},
fail: function (res) { //登录态过期
wx.login();
},
complete: function (res) { },
}) if (e.detail.value.evaContent.length <= 0 || e.detail.value.evaContent.length > 50) {
wx.showToast({
title: '评论字数在1~50字之间',
icon: 'loading',
duration: 1500
})
setTimeout(function () {
wx.hideToast()
}, 2000)
} else if (e.detail.value.comment_pid < 0 || isNaN(e.detail.value.comment_pid)) {
wx.showToast({
title: '回复评论参数有误~',
icon: 'loading',
duration: 1500
})
setTimeout(function () {
wx.hideToast()
}, 2000)
} else { //回复评论
wx.request({
url: 'https://m.xxiangfang.com/index.php/Home/Xiaoxxf/reply_comment',//回复评论
data: {
"comment_pid": e.detail.value.comment_pid,
"content": e.detail.value.evaContent,
"home_id": that.data.home_id,
"openid": wx.getStorageSync("openid")
},
header: {
'Content-Type': 'application/json'
},
method: 'GET',
success: function (res) {
console.log(res);
if (res.data.state == 1) { //回复成功,state==1
wx.showToast({
title: res.data.Msg,
icon: 'loading',
duration: 1500
})
/* 获取房间评论信息 -xzz 0714*/
getList(that);
} else {
wx.showToast({ //回复失败,查看原因
title: res.data,
icon: 'loading',
duration: 1500
})
}
},
fail: function (res) { },
complete: function (res) { },
})
}
}
})

4、后台PHP代码:都是一些很常规的增删改查操作,就不贴了。

【微信小程序】:评论、回复和删除功能 -- 2017/7/14的更多相关文章

  1. 记录使用微信小程序的NFC和蓝牙功能读取15693芯片的开发历程

    开发目标: (1) 对于Android手机,直接通过微信小程序调用手机的NFC功能,对15693协议的芯片进行读写操作: (2)对于苹果手机(及没有NFC模块的手机),通过微信小程序的蓝牙功能连接到蓝 ...

  2. 基于微信小程序的用户列表点赞功能

    代码地址如下:http://www.demodashi.com/demo/13997.html 一.前言 (1).适合人群 1.微信小程序开发者 2.前端工程师 3.想入门学习小程序开发的人员 4.想 ...

  3. 微信小程序之换肤的功能

    pc或者移动端实现换肤功能还是比较简单的,大致就是需要换肤的css,还有正常的css:把当前皮肤类型存入本地:然后通过js读取并判断当前应该加载哪套css. 由于微信小程序没有操作wxss的api,所 ...

  4. 微信小程序上传Excel文本文件功能

    问题: 在开发过程中会发现微信小程序有很多功能都还不能满足我们的需求,谁叫客户就是上帝呢,前几天小编遇到了这么个问题,就是用微信小程序上传文件,但是还以为微信带有这个模块,可是查了许久还是没有找到,只 ...

  5. 微信小程序背景音频播放分享功能

    如果正常背景音频播放的话,只能跳转到自己对应的微信小程序,无法分享朋友圈,我们需要设置分享朋友圈,需要调用一个API 音频背景播放 注意:背景播放在锁屏后播放只支持IOS端,安卓端虽然可以播放,但是锁 ...

  6. 图解微信小程序---实现行的删除和增加操作

    图解微信小程序之实现行的删除和增加操作 代码笔记部分 第一步:在项目的app.json中创建一个新的页面(页面名称英文,可自定义) 第二步:在创建的新页面中编写页面(注意bindtap属性值,因为是我 ...

  7. 微信小程序实现连续扫码功能(uniapp)

    注:本文使用的是 uniapp 语法. 微信小程序提供了扫码API:wx.scanCode,但它只能扫一次码,想要实现连续扫码,需要借用 camera 组件.camera 组件不仅能拍照,还具有扫码功 ...

  8. [转]微信小程序实现图片上传功能

    本文转自:http://blog.csdn.net/feter1992/article/details/77877659 前端: 微信开发者工具 后端:.Net 服务器:阿里云 这里介绍微信小程序如何 ...

  9. 微信小程序实现即时通信聊天功能的实例代码

    项目背景:小程序中实现实时聊天功能 一.服务器域名配置 配置流程 配置参考URL:https://developers.weixin.qq.com/miniprogram/dev/api/api-ne ...

随机推荐

  1. Top N的MapReduce程序MapReduce for Top N items

    In this post we'll see how to count the top-n items of a dataset; we'll again use the flatland book ...

  2. 如何读取抓取的wifi包内容

    有密码的WIFI,WIFI的密码会生成一个临时会话密钥,这个临时会话密钥可以用来加密会话内容,也就是说.比如你在浏览网页,用有密码的WIFI,连接上以后,浏览的网页流量是加密了的,所以更安全.无密码的 ...

  3. maven生命周期(lifecycle)—— maven权威指南学习笔记(四)

    定义: 生命周期是包含在一个项目构建中的一系列有序的阶段 举个例子来说就是maven 对一个工程进行: 验证(validate) …… 编译源码(compile) …… 编译测试源码(test-com ...

  4. mini-parser

    https://leetcode.com/problems/mini-parser/ /** * // This is the interface that allows for creating n ...

  5. Balanced Binary Tree leetcode java

    题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...

  6. Largest Rectangle in Histogram leetcode java

    题目: Given n non-negative integers representing the histogram's bar height where the width of each ba ...

  7. Singleton 单例模式 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  8. 8个对程序员来说有用的jQuery小贴士和技巧

    1) 禁用鼠标右键单击 jQuery程序员可以使用此代码在网页上禁用鼠标右键点击. 1 2 3 4 5 6 7 8 9 10 $(document).ready(function() {     // ...

  9. [置顶] ios 无限循环翻页源码例子

    原创文章,转载请注明出处:http://blog.csdn.net/donny_zhang/article/details/9923053 demo功能:ios 无限循环翻页源码例子.iphone 6 ...

  10. (转)AssetBundle系列——游戏资源打包(二)

    转自:http://www.cnblogs.com/sifenkesi/p/3557290.html 本篇接着上一篇.上篇中说到的4步的代码分别如下所示: (1)将资源打包成assetbundle,并 ...