【微信小程序】:评论、回复和删除功能 -- 2017/7/14
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的更多相关文章
- 记录使用微信小程序的NFC和蓝牙功能读取15693芯片的开发历程
开发目标: (1) 对于Android手机,直接通过微信小程序调用手机的NFC功能,对15693协议的芯片进行读写操作: (2)对于苹果手机(及没有NFC模块的手机),通过微信小程序的蓝牙功能连接到蓝 ...
- 基于微信小程序的用户列表点赞功能
代码地址如下:http://www.demodashi.com/demo/13997.html 一.前言 (1).适合人群 1.微信小程序开发者 2.前端工程师 3.想入门学习小程序开发的人员 4.想 ...
- 微信小程序之换肤的功能
pc或者移动端实现换肤功能还是比较简单的,大致就是需要换肤的css,还有正常的css:把当前皮肤类型存入本地:然后通过js读取并判断当前应该加载哪套css. 由于微信小程序没有操作wxss的api,所 ...
- 微信小程序上传Excel文本文件功能
问题: 在开发过程中会发现微信小程序有很多功能都还不能满足我们的需求,谁叫客户就是上帝呢,前几天小编遇到了这么个问题,就是用微信小程序上传文件,但是还以为微信带有这个模块,可是查了许久还是没有找到,只 ...
- 微信小程序背景音频播放分享功能
如果正常背景音频播放的话,只能跳转到自己对应的微信小程序,无法分享朋友圈,我们需要设置分享朋友圈,需要调用一个API 音频背景播放 注意:背景播放在锁屏后播放只支持IOS端,安卓端虽然可以播放,但是锁 ...
- 图解微信小程序---实现行的删除和增加操作
图解微信小程序之实现行的删除和增加操作 代码笔记部分 第一步:在项目的app.json中创建一个新的页面(页面名称英文,可自定义) 第二步:在创建的新页面中编写页面(注意bindtap属性值,因为是我 ...
- 微信小程序实现连续扫码功能(uniapp)
注:本文使用的是 uniapp 语法. 微信小程序提供了扫码API:wx.scanCode,但它只能扫一次码,想要实现连续扫码,需要借用 camera 组件.camera 组件不仅能拍照,还具有扫码功 ...
- [转]微信小程序实现图片上传功能
本文转自:http://blog.csdn.net/feter1992/article/details/77877659 前端: 微信开发者工具 后端:.Net 服务器:阿里云 这里介绍微信小程序如何 ...
- 微信小程序实现即时通信聊天功能的实例代码
项目背景:小程序中实现实时聊天功能 一.服务器域名配置 配置流程 配置参考URL:https://developers.weixin.qq.com/miniprogram/dev/api/api-ne ...
随机推荐
- Log4j输出格式控制
参数说明例子 %c 列出logger名字空间的全称,如果加上{<层数>}表示列出从最内层算起的指定层数的名字空间 log4j配置文件参数举例 输出显示媒介 假设当前logger名字空间是& ...
- Unix/Linux环境C编程新手教程(30) 字符串操作那些事儿
函数介绍 rindex(查找字符串中最后一个出现的指定字符) 相关函数 index,memchr,strchr,strrchr 表头文件 #include<string.h> 定义函数 c ...
- go语言基础之append扩容特点
1.append扩容特点 示例: package main //必须有个main包 import "fmt" func main() { //如果超过原来的容量,通常以2倍容量扩容 ...
- SQL Server AlwaysOn Setup Step-By-Step Guide
Step-By-Step: Creating a SQL Server 2012 AlwaysOn Availability Group http://blogs.technet.com/b/cani ...
- Android 跳转到系统应用管理
Intent i = new Intent("android.settings.APPLICATION_DETAILS_SETTINGS");String pkg = " ...
- IOS Xib使用——Xib表示局部界面
Xib文件是一个轻量级的用来描述局部界面的文件,在之前的文章讲解了为控制器添加Xib文件,本节主要讲解一下通过xib文件表示局部界面. <一> 创建Xib文件 Xib文件创建的时候是选择U ...
- WPF 处理路由事件
(1)img.MouseUp+= img_MouseUp;(2)调用 UIElement.AddHandler()直接连接事件:img.AddHandler(Image.MouseUpEvent, n ...
- kettle根据参数动态派生列
抽取数据的时候没有日期字段,需要根据抽取日期自动生成月份,如下图结构 表输入_参数部分,接收来自其他系统传过来的参数(JAVA程序或者页面),具体设置如图 在查询数据时候派生列 运行模型的时候,给参数 ...
- mssql2008R2 RCU-6083:ALTER database FWC SET READ_COMMITTED_SNAPSHOT ON
RCU-6083:失败 - 检查所选组件的先决条件要求:MDS 有关详细资料, 请参阅 E:\Setup\ofm_rcu\rcu\log\logdir.2014-11-27_12-39\rcu.log ...
- 如何解决Win7将任务栏程序自动分组的困扰
Win7默认把任务栏程序自动分组,比如多个资源管理器窗口被分到一起,其实这挺让人恼火的,关键弊病是多出一个人工检查的步骤,这在操作繁忙时容易增加人的负担,不能按预定记忆处理. 还好微软也没把蠢事做绝, ...