• 第八章作业


    留言板是一款能实现浏览留言、发表留言、删除留言和编辑留言的小程序,用户能够浏览当前的已留言内容,并且能按照时间的升序来查看最新的留言内容;能够发表自己的留言内容,在留言发表页填写相关项后即可发表,并能查看到新留言内容;能够删除不需要的留言;能够修改留言内容。因此,简单留言板的功能主要为显示留言、发表留言、删除留言和编辑留言。

    创建两个文件夹index、new。

    1.应用配置

    小程序代码实现的第一步是设置整个应用的配置,修改app.json,示例代码如下:

    1. {
    2. "pages": [
    3. "pages/index/index",
    4. "pages/new/new"
    5. ],
    6. "window":{
    7. "backgroundTextStyle":"light",
    8. "navigationBarBackgroundColor":"#3891f8",
    9. "navigationBarTitleText":"小小留言板",
    10. "navigationBarTextStyle": "black"
    11. }
    12. }

    index文件夹

    wxml:

    1. <image class ="toWrite " bindtap ="toAddDiary"src ="../../images/SWK.png" style="width: 400px;"/>
    2. <!-- 显示留言 -->
    3. <view class = "page">
    4. <scroll-view lower-threshold="800"
    5. bindscrolltolower="pullUp-Load"upper-threshold ="0 "scroll-y="true"
    6. style = "height: {{win-dowHeight}}px;">
    7. <view class ="page__bd">
    8. <button style="width: 100px;height: 50px; border: 1px saddlebrown;"><view class = "weui-panel__hd" style="text-align: center;">留言列表</view>
    9. </button>
    10. <view>
    11. <block wx:if="{{diaryList.length >0}}">
    12. <navigator class="weui-media-box weui-media-box_text"
    13. wx:for="{{diaryList}}"
    14. wx:key = "diaryItem"url ="/pages/index /detail? ob-jectId={{item.objectId}}&count={{item.count}}" >
    15. <view class="title">
    16. 主题:{{item.title}}</view>
    17. <view class ="content">留言内容:{{item.content}}</view><view class= "info">
    18. <view class="time">时间:{{item.updatedAt}}</view ><view class="count">浏览:{{item.count}}</view>
    19. <view class="operate">
    20. <icon type ="cancel dels"size ="16" > </icon>
    21. <text class ="del"catchtap ="deleteDiary"data-id ="iitem.objectId}}">删除</text>
    22. <icon type="success edits"size="16"></icon>
    23. <text catchtap ="toModifyDiary" data-id ="{{item.objectId}}"data-content="{{item.content}}"data-title="{{item.title}}">编辑</text>
    24. </view>
    25. </view>
    26. </navigator>
    27. </block>
    28. </view>
    29. </view>
    30. </scroll-view>
    31. </view>
    32. <!-- 添加留言 -->
    33. <view class ="js dialog" id="androidDialog1" style="opacity:1;"wx:if="{{writeDiary}}">
    34. <view class="weui-mask"></view>
    35. <view class ="weui-dialog weui-skin_android">
    36. <view class="weui-dialog__hd">
    37. <strong class="weui-dialog__title" style="margin-left: 150px;">添加留言</strong>
    38. </view>
    39. <form bindsubmit="addDiary" report-submit="true">
    40. <view class="weui-dialog__bd">
    41. <view class="weui-cells__title" style="text-align: center;">标题</view>
    42. <view class="weui-cells weui-cells after-title" >
    43. <view class="weui-cell weui-cell input">
    44. <view class="weui-cell__bd">
    45. <input class="weui-input"name="title" placeholder="请输入标题" style="text-align: center;"/>
    46. </view>
    47. </view>
    48. </view>
    49. <view class ="weui-cel1s title" style="text-align: center;">留言内容</view>
    50. <view class="weui-cells weui-cells after-title">
    51. <view class="weui-cell">
    52. <view class="weui-cell bd">
    53. <textarea class ="weui-textarea"name ="content"placeholder ="请输入留言内容" style="margin-left: 130px;" />
    54. <view class="weui-textarea-counter" style="text-align: center;">0/200</view>
    55. </view>
    56. </view>
    57. </view>
    58. <view class="pic">
    59. <view class ="pictext"bindtap="uppic" style="text-align: center;">添加图片</view><block wx:if="{isTypeof(url)})">
    60. <image src="../../images/SWK.png" style="width: 400px;"/>
    61. </block>
    62. <block wx:else>
    63. <image src="{{url}}"/>
    64. </block>
    65. </view>
    66. </view >
    67. <view class="weui-dialog ft">
    68. <button loading="{{loading}}"
    69. class="weui-dialog__btn weui-diaLog__btn__primary"formType="info">取消</button>
    70. <button loading="{{loading}}"
    71. class="weui-dialog__btn weui-diaLog__btn__primary"formType="info">提交</button>
    72. </view>
    73. </form>
    74. </view>
    75. </view>
    76. <!-- 修改留言 -->
    77. <view class ="js_dialog"id="androidDialog2"style ="opacity: 1;"
    78. wx:if ="{{modifyDiarys}}">
    79. <view class="weui-mask"></view>
    80. <view class="weui-dialog weui-skin android">
    81. <view class="weui-dialog hd">
    82. <strong class="weui-dialog title" style="text-align: center;">修改留言</strong>
    83. </view >
    84. <form bindsubmit="modifyDiary">
    85. <view class="weui-dialog bd">
    86. <view class="weui-cells title" style="text-align: center;">标题</view>
    87. <input class ="weui =input"name ="title" value ="inowTitle"placeholder="请输入标题" style="text-align: center;"/>
    88. <view class="weui-cells title" style="text-align: center;">留言内容</view>
    89. <view class="weui-cells weui-cells after-title">
    90. <view class="weui-cell">
    91. <view class="weui-cell bd">
    92. <textarea class ="weui-textarea"name ="content"value ="{{nowContent}}"placeholder="请输入留言内容"style="margin-left: 150px;" />
    93. <view class="weui-textarea-counter" style="text-align: center;" style="text-align: center;">0/200 </view>
    94. </view>
    95. </view>
    96. </view >
    97. </view>
    98. <view class="weui-dialog ft">
    99. <view class="weui-dialog btn weui -dialog btn_default" bindtag="noneWindows">取消</view>
    100. <button loading ="{{loading}}" class ="weui-dialog btn weui-diaLog btn primary"
    101. formType="submit">提交</button>
    102. </view>
    103. </form>
    104. </view>
    105. </view>
    js :
    
    1. //引入Bmob逻辑文件及初始化数据
    2. var Bmob = require('../../utils/bmob');
    3. var common = require('../../utils/underscore');
    4. var app = getApp();
    5. var that;
    6. var url = ""
    7. Page({
    8. data:{
    9. writeDiary:false,//写留言
    10. loading:false,
    11. windowHeight:0,//定义窗口高度
    12. windowWidth:0,//定义窗口宽度
    13. limit:10, //定义数据提取条数
    14. diaryList:[],//定义数据列表
    15. modifyDiarys:false
    16. },
    17. //获取并显示留言数据
    18. onShow:function(){
    19. getList(this);
    20. wx.getSystemInfo({ success:(res) => { that.setData({
    21. windowHeight:res.windowHeight, windowWidth:res.windowWidth
    22. })
    23. }
    24. })
    25. /*
    26. *获取数据*/
    27. function getList(t,k){
    28. that =t;
    29. var Diary = Bmob.Object.extend("happy");//数据表 happy
    30. var query = new Bmob.Query(Diary);
    31. var queryl = new Bmob.Query(Diary);
    32. query. descending('createdAt'); query.include( "own")//查询所有数据
    33. query.limit(that.data.limit);
    34. var mainQuery = Bmob.Query.or(query);
    35. mainQuery.find({
    36. success:function(results){//循环处理查询到的数据
    37. console.log(results);
    38. that.setData({
    39. diaryList:results
    40. })
    41. },
    42. error:function(error){
    43. console.log("查询失败:"+error.code +"" + error.message);
    44. }
    45. });
    46. }
    47. //添加数据
    48. },
    49. toAddDiary:function() {
    50. that.setData({
    51. writeDiary:true
    52. })
    53. },
    54. //添加图片
    55. uppic:function(){
    56. var that = this;
    57. wx.chooseImage({
    58. count:1,//默认9
    59. sizeType:['compressed'],//可以指定是原图还是压缩图,默认二者都有
    60. sourceType:['album','camera'],//可以指定来源是相册还是相机,默认二者都有
    61. success:function(res){
    62. var tempFilePaths=res.tempFilePaths;
    63. if(tempFilePaths.length >0){
    64. var newDate =new Date();
    65. var newDateStr=newDate.toLocaleDateString();//获取当前日期做文件主
    66. var tempFilePath=[tempFilePaths[0]];
    67. var extension=/\([^.]*)$/.exec(tempFilePath[0]);//获取文件扩展名
    68. if(extension){
    69. extension =extension[1].toLowerCase();
    70. }
    71. var name =newDateStr+"."+extension;//上传的图片的别名
    72. var file = new Bmob.File(name,tempFilePaths);
    73. file.save().then(function(res){
    74. console.log(res.url());
    75. url = res.url();
    76. that.setData({
    77. url:ur1
    78. })
    79. },
    80. function(error){
    81. console.log(error);
    82. }
    83. )
    84. }
    85. }
    86. })
    87. },
    88. //添加留言数据
    89. addDiary:function(event){
    90. var title =event.detail.value.title;
    91. var content=event.detail.value.content;
    92. var formId =event.detail.formId;
    93. console.log("event".event)
    94. if(! title){
    95. common.showTip("标题不能为空","loading");
    96. elseif(! content)
    97. common.showTip("内容不能为空","loading");
    98. }
    99. else
    100. that.setData({
    101. loading:true
    102. })
    103. var currentUser=Bmob.User.current();
    104. var User =Bmob.Object.extend("User");var UserModel=new User();
    105. //增加留言
    106. var Diary =Bmob.Object.extend("happy");//数据表
    107. var diary =new Diary();
    108. diary.set("title",title);//保存title字段内容
    109. diary.set("formId",formId);//保存 formId
    110. diary.set("content",content);//保存content字段内容
    111. diary.set("image",url)//保存图片地址
    112. diary.set("count",1)//保存浏览次数
    113. if(currentUser){
    114. UserModel.id =currentUser.id;
    115. diary.set("own",UserModel);
    116. }
    117. //添加数据,第一个入口参数是nu11
    118. diary.save(null,{
    119. success:function(result){
    120. common.showTip('添加日记成功');
    121. that.setData({
    122. writeDiary:false,
    123. loading:false
    124. })
    125. var currentUser =Bmob.User.current();
    126. that.onShow();
    127. },
    128. error:function(result,error){//添加失败
    129. common.showTip('添加留言失败,请重新发布','loading');
    130. }
    131. });
    132. },
    133. //删除留言
    134. deleteDiary:function(event){
    135. var that =this;
    136. var objectId=event.target.dataset.id;
    137. wx.showModal({
    138. title:'操作提示',
    139. content:'确定要删除要留言?',
    140. success:function(res){
    141. if(res.confirm){
    142. //删除留言
    143. var Diary = Bmob.Object.extend("happy");
    144. //创建查询对象,入口参数是对象类的实例
    145. var query =new Bmob.Query(Diary);
    146. query.get(objectId,{
    147. success:function(object){
    148. //The object was retrieved successfully.
    149. object.destroy({
    150. success:function(deleteObject){
    151. console.log('删除留言成功');
    152. getList(that)
    153. },
    154. error:function(object,error){
    155. console.log('删除留言失败');
    156. }
    157. });
    158. },
    159. error,function(object,error){
    160. console.log("query object fail");
    161. }
    162. });
    163. }
    164. }
    165. })
    166. },
    167. toModifyDiary:function(event){
    168. var nowTile =event.target.dataset.title;
    169. var nowContent =event.target.dataset.content;
    170. var nowId =event.target.dataset.id;
    171. that.setData({
    172. modifyDiarys :true,
    173. nowTitle:nowTile,
    174. nowContent:nowContent,
    175. nowId:nowId
    176. })
    177. modifyDiary,function(e){
    178. var t =this;
    179. modify(t,e)
    180. function modify(t,e){
    181. var that = t;//修改日记
    182. var modyTitle = e.detail.value.title;
    183. var modyContent = e.detail.value.content;
    184. var objectId =e.detail.value.content;
    185. var thatTitle = that.data.nowTitle;
    186. var thatContent= that.data.nowContent;
    187. if((modyTitle != thatTitle ||modyContent != thatContent))
    188. { if(modyTitle == "" ||lmodyContent == ""){
    189. common.showTip('标题或内容不能为空','1oading');}
    190. else {
    191. console.log(modyContent)
    192. var Diary = Bmob.Object.extend("happy");
    193. var query = new Bmob.Query(Diary);
    194. //这个id是要修改条目的id,你在生成这个存储并成功时可以获取到,请看前面的文档
    195. query.get(that.data.nowId,{
    196. success:function(result){
    197. //回调中可以取得这个GameScore对象的一个实例,然后就可以修改它了
    198. result.set('title',modyTitle);
    199. result.set('content',modyContent);
    200. result.save();
    201. common.showTip('留言修改成功','success',function(){ that.onShow(); that.setData({
    202. modifyDiarys:false
    203. })
    204. });
    205. },
    206. error:function(object,error){
    207. }
    208. });
    209. }
    210. elseif(modyTitle == "" ||lmodyContent == "");{
    211. common.showTip('标题或内容不能为空','1oading');
    212. }
    213. }
    214. else {
    215. that.setData({
    216. modifyDiarys:false
    217. })
    218. common.showTip('修改成功','loading');
    219. }
    220. }
    221. }
    222. },
    223. })

    wxss:

    1. page {
    2. height: 100vh;
    3. display: flex;
    4. flex-direction: column;
    5. }
    6. .scrollarea {
    7. flex: 1;
    8. overflow-y: hidden;
    9. }
    10. .userinfo {
    11. display: flex;
    12. flex-direction: column;
    13. align-items: center;
    14. color: #aaa;
    15. width: 80%;
    16. }
    17. .userinfo-avatar {
    18. overflow: hidden;
    19. width: 128rpx;
    20. height: 128rpx;
    21. margin: 20rpx;
    22. border-radius: 50%;
    23. }
    24. .usermotto {
    25. margin-top: 200px;
    26. }
    27. /* .toWrite{
    28. position: absolute;
    29. left: 30px;
    30. width: 300px;
    31. height: 300px;
    32. } */
    33. .avatar-wrapper {
    34. padding: 0;
    35. width: 56px !important;
    36. border-radius: 8px;
    37. margin-top: 40px;
    38. margin-bottom: 40px;
    39. }
    40. /* .weui-panel__hd{
    41. margin-top: 330px;
    42. margin-left: 130px;
    43. } */
    44. .avatar {
    45. display: block;
    46. width: 56px;
    47. height: 56px;
    48. }
    49. .nickname-wrapper {
    50. display: flex;
    51. width: 100%;
    52. padding: 16px;
    53. box-sizing: border-box;
    54. border-top: .5px solid rgba(0, 0, 0, 0.1);
    55. border-bottom: .5px solid rgba(0, 0, 0, 0.1);
    56. color: black;
    57. }
    58. .nickname-label {
    59. width: 105px;
    60. }
    61. .nickname-input {
    62. flex: 1;
    63. }

    new文件夹

    js:

    1. var Bmob = require('../../utils/bmob');
    2. Page({ data:{
    3. rows:{} //留言详情
    4. },
    5. onLoad:function(e){
    6. //页面初始化options为页面跳转所带来的参数
    7. console.log(e.objectId)
    8. var objectid = e.objectid;
    9. var newcount =e.count;
    10. var that = this;
    11. var Diary = Bmob.Object.extend("test");
    12. var query = new Bmob.Query(Diary);
    13. query.get(objectId,{
    14. success:function(result) {
    15. console.log(result);
    16. that.setData({ rows:result,})
    17. newcount = parseInt(newcount)+1//浏览次数加1
    18. result.set("count",newcount)//保存浏览次数 result.save()
    19. },
    20. error:function(result,error){ console.log("查询失败");
    21. }
    22. });
    23. }
    24. })

    wxml:

    1. <view class= "page">
    2. <view>
    3. <view>
    4. <view>留言主题:</view>
    5. <view>{{rows.title}}</view><view>
    6. <view>留言内容:</view>
    7. <view>{{rows.content}}</view>
    8. <view class = "pic">
    9. <image src="{{rows.image}}"/></view><view>
    10. 浏览次数:{{rows.count}}</view >
    11. <view>创建时间:{{rows.createdAt}} </view>
    12. </view></view></view>
    13. <view class="footer">
    14. <text> Copyright©2017-2019www.smartbull.cn</text></view></view>

    wxss:

    1. page {
    2. height: 100vh;
    3. display: flex;
    4. flex-direction: column;
    5. }
    6. .scrollarea {
    7. flex: 1;
    8. overflow-y: hidden;
    9. }
    10. .log-item {
    11. margin-top: 20rpx;
    12. text-align: center;
    13. }
    14. .log-item:last-child {
    15. padding-bottom: env(safe-area-inset-bottom);
    16. }

  • 相关阅读:
    Unity 工具 之 Azure 微软 【GPT4o】HttpClient 异步流式请求的简单封装
    数据结构 - 二叉树
    C++心决之类和对象详解(中篇)(封装入门二阶)
    Linux安装Keepalived
    DirectX11 With Windows SDK--37 延迟渲染:光源剔除
    C++入门教程(十、联合体)
    网络编程TCP/UDP通信
    ahocorasick的报错
    互联网大厂知识点整理
    什么是Vue的Vetur插件?它有哪些功能
  • 原文地址:https://blog.csdn.net/2301_78541465/article/details/138837109