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. 解决sqoop报错:java.lang.OutOfMemoryError: Java heap space

    报错栈: -- ::, INFO [main] org.apache.sqoop.mapreduce.db.DBRecordReader: Executing query: = ) AND ( = ) ...

  2. 5.synchronized锁重入

    package demo1; /** * synchronized锁重入 * Created by liudan on 2017/6/5. */ public class MyThread5_sync ...

  3. [转]一键安装藏隐患,phpStudy批量入侵的分析与溯源

    一.前言 近日,腾讯安全云鼎实验室监测到大量主机被入侵并添加了一个名为“vusr_dx$”的隐藏帐号:同时,云鼎实验室还监测到此类帐号被大量创建的同时存在对应帐号异地登录的情况. Windows 的帐 ...

  4. python环境搭建-Pycharm模块安装方法

    不懂直接看图顺序操作: 方法一: 方法二:

  5. SQL Server Instance无法启动了, 因为TempDB所在的分区没有了, 怎么办?

    我的SQL 2014的虚拟机在迁移的时候, 存放TempDB的LUN被删掉了. 在虚拟机的操作系统启动了之后, SQL Server Instance却启动不起来了. 检查Event Log, 报错. ...

  6. Git的简单介绍

    每次看到别人写Git的文章,同学中也有用Git感觉很高大上的感觉,工作中用的是SVN,周末倒腾了一下Git,Git是一款免费.开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目.Git 与 ...

  7. Loadrunner视频教程汇总

    小布老师视频:测试工具概述,兼LoadRunner介绍 -1-4http://www.boobooke.com/v/bbk1046http://www.boobooke.com/v/bbk1046.z ...

  8. 使用C#创建及调用WCF完整实例 (Windows服务宿主)

    关于WCF的概念.原理.优缺点等,在这里就不多说了,网上很多,可以自行搜索,比我解释的要专业的多. 这里直接说使用Windows 服务(Windows Service)作为宿主如何实现,其它方式不在此 ...

  9. Inf2Cat应用的参数使用详细介绍

    http://msdn.microsoft.com/zh-cn/subscriptions/ff547089   Inf2Cat Inf2Cat (Inf2Cat.exe) 是一个命令行工具,该工具确 ...

  10. 3. Layout -- 1

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orient ...