【微信小程序】:评论、回复和删除功能 -- 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 ...
随机推荐
- 《学习opencv》笔记——矩阵和图像操作——cvInRange,cvInRangeS,cvInvert and cvMahalonobis
矩阵和图像的操作 (1)cvInRange函数 其结构 void cvInRange(//提取图像中在阈值中间的部分 const CvArr* src,//目标图像 const CvArr* lowe ...
- XCL-Charts画曲线图(CurveChart) 例2
还有一个横向的曲线图(CurveChart) 效果: 代码: //图基类 chart = new CurveChart(); //图所占范围大小 chart.setChartRange(0, 0, t ...
- QT vs x64编译
下载qt-everywhere-opensource-src-5.3.0 这个设置非常重要,不对的话,一大堆编译错误,已经折腾了好多回了 configure -mp -confirm-license ...
- Python Post img
from poster.encode import multipart_encode from poster.streaminghttp import register_openers import ...
- 企业版Oracle10g的安装-过程
ylbtech-Oracle:企业版Oracle10g的安装-过程 Oracle10g的安装 在Windows操作系统上安装Oracle10g数据库的步骤如下: 0.1)从Oracle的官方网站上下载 ...
- Informatica 常用组件Expression之一 概述
转换类型:被动.已连接 可以在写入目标前,使用表达式转换计算单行中的值.例如,您可能需要调整员工薪酬.连接姓名或将字符串转换为数字.您可以使用表达式转换执行任意非聚合计算.在将结果输出 ...
- 【Docker】基于docker+etcd+confd + haproxy构建高可用、自发现的web服务
各个工具介绍 (1)Docker:Docker是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux机器上,也可以实现虚拟化,docker ...
- C# 中奇妙的函数–8. String Remove() 和 Replace()
http://www.cnblogs.com/multiplesoftware/archive/2011/09/27/2192710.html 当对字符串进行操作时,我们经常要删除或者是替换一部分子字 ...
- 敏捷方法之极限编程(XP)和 Scrum区别
敏捷(Agile)作为一种开发流程, 目前为各大公司所采用, 敏捷流程的具体实践有XP 和Scrum, 似乎很少有文章介绍这两者的区别, 发现一篇外文, 见解非常深刻, 特将其翻译一把. 原文(DIF ...
- 解决Sqlserver 2008 R2在创建登录名出错"此版本的 Microsoft Windows 不支持 MUST_CHANGE 选项。 (Microsoft SQL Server,错误: 15195)"
错误信息: 执行 Transact-SQL 语句或批处理时发生了异常. (Microsoft.SqlServer.ConnectionInfo) 此版本的 Microsoft Windows ...