【微信小程序】:评论、回复和删除功能 -- 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 ...
随机推荐
- 分析oracle索引空间使用情况,以及索引是否须要重建
分析索引空间使用情况.以及索引是否须要重建 分析其它用户下的索引须要 analyze any的权限 分析索引前先查看表的大小和索引的大小,假设索引大小和表大小一样大或者大于表的大小,那么能够推断索引可 ...
- Windows 8 Metro 应用开发入门(一):开发环境介绍
摘 要 Windows8已经发布,随之而来的基于WinRT的Metro应用也正向我们走来,正像它所宣传的:光滑.快.现代.看习惯了玻璃.立体风格的应用,或许Metro的简洁能给你留下不一样的体验.Vi ...
- Lombok的安装及入门
lombok 的官方网址:http://projectlombok.org/ lombok 其实到这里我就介绍完了,开个玩笑,其实官网上有 lombok 三分四十九秒的视频讲解,里面讲的也很清楚了,而 ...
- 快速近似最近邻搜索库 FLANN - Fast Library for Approximate Nearest Neighbors
What is FLANN? FLANN is a library for performing fast approximate nearest neighbor searches in high ...
- Informatica 常用组件Expression之一 概述
转换类型:被动.已连接 可以在写入目标前,使用表达式转换计算单行中的值.例如,您可能需要调整员工薪酬.连接姓名或将字符串转换为数字.您可以使用表达式转换执行任意非聚合计算.在将结果输出 ...
- 树行控件TreeView 在WinForm下 怎么实现重命名功能
public Form1() { InitializeComponent(); this.Load+=new EventHandler(Form1_Load); treeVie ...
- 一行代码轻松实现拖动效果[JQuery]
写JS实现拖动需要一大堆不便维护的代码,实属麻烦,Google了大半天,发现了一个优秀的Jquery插件EasyDrag,只需要一行代码便可轻松在主流浏览器上 实现拖动效果. $(document ...
- 关于css优先级
css的优先级从低到高依次是:内部样式表的优先级为(1,0,0,0),id选择器优先级为(0,1,0,0),class选择器为(0.0,1,0),tag标签为(0.0,0,1).除此之外,!impor ...
- go语言string、int、int64互相转换
#string到int int,err:=strconv.Atoi(string) #string到int64 int64, err := strconv.ParseInt(string, 10, 6 ...
- simple-libfm-example-part1
原文:https://thierrysilbermann.wordpress.com/2015/02/11/simple-libfm-example-part1/ I often get email ...