小程序 第八章作业

1.简单留言板 


              以简单留言板为例,介绍小程序项目的开发过程及代码实现。

1. 需求分析

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

2. 视图层设计


                根据功能需求分析,共设计4个页面:首页(显示留言页)、发表留言页、编辑留言页和详情页。

3. 数据库设计


          根据留言板功能,设计数据库表名为test,其中设计的字段有id(编号)、title(标题)、content(内容)、image(图像)、count(次数)5个字段通过Bmob后端云设计

4. 代码实现


1.应用配置

首页,代码如下

index.wxml代码:

<view><image class="toWrite" bind:tap="toAddDiary" src="/pages/image/qing.png"></image></view>
<view class="page">
  <scroll-view lower-threshold="800" bindscrolltolower="pullUpLoad" upper-threshold="0" scroll-y="true" style="height: {{windowHeight}}px;">
    <view class="page_bd">
      <view class="weui-panel_hd">留言列表</view>
    <view>
    <block wx:if="{{diaryList.length>0}}">
      <navigator class="weui-media-box weui-media-box_text" wx:for="{{diaryList}}" wx:key="diaryItem" url="/pages/detail/detail?objectId={{item.attributes.objectId}}&count={{item.attributes.count}}">
      <view class="nov">
        <view class="title">主题:{{item.attributes.title}}</view>
        <view class="content">留言内容:{{item.attributes.content}}</view>
        <view class="info">
          <view class="tiem">时间:{{item.updatedAt}}</view>
          <view class="count">浏览:{{item.attributes.count}}</view>
          <view class="operate">
            <icon type="cancel dels" size="16"/>
            <text class="del" catch:tap="deleteDiary" data-id="{{item.attributes.objectId}}">删除</text>
            <icon type="success edits" size="16"/>
            <text catch:tap="toModifyDiary" data-id="{{item.id}}" data-content="{{item.content}}" data-title="{{item.attributes.title}}">编辑</text>
          </view>
          </view>
        </view>
      </navigator>
    </block>
    </view>
   </view>
  </scroll-view>
</view>
<view class="js_dialog" id="androidDialog1" style="opacity: 1;" wx:if="{{writeDiary}}">
  <view class="weui-mask"></view>
  <view class="weui-dialog weui-skin_android">
    <view class="weui-dialog_hd">
      <strong class="weui-dialog_title">添加留言</strong>
    </view>
    <form bindsubmit="toAddDiary" >
      <view class="weui-dialog_bd">
        <view class="weui-cells_title">标题:</view>
        <view class="weui-cells weui-cells_after-title">
          <view class="weui-cells weui-cell_input">
            <view class="weui-cell_bd">
              <input class="weui-input" name="title" placeholder="请输入标题"/>
            </view>
          </view>
        </view>
        <view class="weui-cells_title">留言内容:</view>
        <view class="weui-cells weui-cells_after-title">
          <view class="weui-cell">
            <view class="weui-cells_bd">
              <textarea class="weui-textarea" name="content" placeholder="请输入留言内容" style="height:3.3em"></textarea>
              <view class="weui-textarea-counter">0/200</view>
            </view>
          </view>
        </view>
        <view class="pic">
         <view class="pictext" bind:tap="uppic">添加图片</view>
           <block wx:if="{{isTypeof(url)}}">
            <image src="/pages/image/qing.png"/>
           </block>
           <block wx:else>
             <image src="{{url}}"/>
           </block>
        </view>
      </view>
      <view class="weui-dialog_ft">
        <button class="weui-dialog_btn weui-dialog_btn_default" bind:tap="noneWindows">取消</button>
        <button loading="{{loading}}" class="weui-dialog_btn weui-dialog_btn_primary" form-type="submit">提交</button>
      </view>
    </form>
  </view>
</view>

index.js代码:

// pages/index1/index1.js
var Bmob = require('../../utils/bmob');
var common = require('../../utils/common');
var app = getApp();
var that;
var url = '';

Page({
  data: {
    writeDiary: false,
    loading: false,
    windowHeight: 0,
    windowWidth: 0,
    limit: 10,
    diaryList: [],
    modifyDiarys: false
  },
  // 获取并显示留言数据
  onShow: function () {
    getList(this);
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth
        })
      }
    })
  },
  // 添加数据
  toAddDiary: function () {
    that.setData({
      writeDiary: true
    })
  },
  uppic : function () {
    var that = this;
    wx.chooseImage({
      count: 9, // 默认值为9
      sizeType: ['compressed'], // 可以指定是原图还是压缩图,默认二者都有 
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
      success: function (res) {
        var tempFilePaths = res.tempFilePaths;
        if (tempFilePaths.length > 0) {
          var newDate = new Date();
          var newDateStr = newDate.toLocaleDateString(); // 获取当前日期为文件主名   
          var tempFilePath = [tempFilePaths[0]];
          var extension = /\.([^.]*)$/.exec(tempFilePath[0]); // 获取文件扩展名
          if (extension) {
            extension = extension[1].toLowerCase();
          }
          var name = newDateStr + "." + extension; // 上传的图片的别名
          var file = new Bmob.File(name, tempFilePaths);
          file.save().then(function (res) {
            console.log(res.url);
            var url = res.url;
            that.setData({
              url: url
            })
          }, function (error) {
            console.log(error);
          })
        }
      }
    })
  },
  addDiary:function(event){
    var title = event.detail.value.title;
    var content = event.detail.value.content;
    var formId = event.detail.value.formId;
    console.log("event",event)
    if(!title){
      common.showTip("标题不能为空","loading");
    }else if(!content){
      common.showTip("内容不能为空","loading")
    }else{
      that.setData({
        loading:true
      })
      var currentUser = Bmob.User.current();
      var User = Bmob.Object.extend("_User");
      var UserModel = new User();
      var Diary = Bmob.Object.extend("test");
      var diary = new Diary();
      diary.set("title",title);
      diary.set("formId",formId);
      diary.set("content",content);
      diary.set("image",url);
      diary.set("count",1)
      if(currentUser){
        UserModel.id = currentUser.id;
        diary.set("own",UserModel);
      }
      diary.save(null,{
        success:function(result){
          common.showTip('添加日记成功');
          that.setData({
            writeDiary:false,
            loading:false
          })
          var currentUser = Bmob.User.current();
          that.onShow();
        },
        error:function(result,error){
          common.showTip('添加留言失败,请重新发布','loading')
        }
      })
    }
  },
  deleteDiary:function(event){
    var that = this;
    var objectId = event.target.dataset.id;
    wx.showModal({
      title: '操作提示',
      content: '确定要删除留言?',
      success: function(res){
        if (res.confirm) {
          var Diary = Bmob.Object.extend("test");
          var query = new Bmob.Query(Diary);
          query.get(objectId,{
            success:function(object){
              object.destroy({
                success:function(deleteObject){
                  console.log('删除留言成功')
                  getList(that)
                },
                error:function(object,error){
                  console.log('删除留言失败')
                }
              })
            },
            error:function(object,error){
              console.log("query object fail");
            }
          })
        }
      }
    })
  },
  toModifyDiary:function(event){
    var nowTitle = event.target.dataset.title;
    var nowContent = event.target.dataset.content;
    var nowId = event.target.dataset.id;
    that.setData({
      modifyDiarys:true,
      nowTitle:nowTitle,
      nowContent:nowContent,
      nowId:nowId
    })
  },
  modifyDiarys:function(e){
    var t=this;
    modify(t,e)
  }
});
// 定义获取留言数据的函数
function getList(t,k) {
  that = t;
  var Diary = Bmob.Object.extend("test"); // 数据表 test
  var query = new Bmob.Query(Diary);
  var query1 = new Bmob.Query(Diary);
  query.descending('createdAt');
  query.include("own");
  query.limit(that.data.limit);
  var mainQuery = Bmob.Query.or(query,query1);
  mainQuery.find({
    success: function (results) {
      // 循环处理查询到的数据
      console.log(results);
      that.setData({
        diaryList: results
      })
    },
    error: function (error) {
      console.log("查询失败:" + error.code + " " + error.message);
    }
  });
}

function modify(t,e){
  var that = t;
  var modyTitle = e.detail.value.title;
  var modyContent = e.detail.value.content;
  var objectId = e.detail.value.content;
  var thatTitle = that.data.nowTitle;
  var thatContent = that.data.nowContent;
  if ((modyTitle!=thatTitle||modyContent!=thatContent)) {
    if (modyTitle==""||modyContent=="") {
      common.showTip('标题或内容不能为空','loading')
    }else{
      console.log(modyContent)
      var Diary = Bmob.Object.extend("test");
      var query = new Bmob.Query(Diary);
      query.get(that.data.nowId,{
        success:function(result){
          result.set('title',modyTitle);
          result.set('content',modyContent);
          result.save();
          common.showTip('留言修改成功','success',function(){
            that.onShow();
            that.setData({
              modifyDiarys:false
            })
          })
        },
        error:function(object,error){

        }
      })
    }
  }else if (modyTitle==""||modyContent=="") {
    common.showTip('标题或内容不能为空','loading')
  }else{
    that.setData({
      modifyDiarys:false
    })
    common.showTip('修改成功','loading')
  }
}

 index.wxss代码:

/**index.wxss**/
.toWrite{
 
  width: 50px;
  height: 50px;
}
.page_bd{
  text-align: center;
  height: 220px;
  background: chartreuse;
}
.weui-panel_hd{
  text-align: center;
  font-size: 25px;
  color: deeppink;
}
.title{
  font-size: 20px;
  margin-top: 5px;
  color: brown;
}
.content{
  margin-top: 5px;
  font-size: 20px;
  color: blue;
}
.tiem{
  margin-top: 5px;
}
.count{
  margin-top: 5px;
}
.operate{
  margin-top: 5px;
}
.js_dialog{
  margin-top: -260px;
}
.weui-dialog_hd{
  text-align: center;
  font-weight: 200;
  font-size: 20px;
}
.weui-input{
  border: brown solid 1px;
  width: 300px;
  margin-left: 5%;
  height: 30px;
}
.weui-textarea {
  border: brown solid 1px;
  width: 300px;
  margin-left: 5%;
  height: 30px;
}
.weui-textarea-counter{
  margin-left: 270px;
}

detail.wxml代码: 

<view class="page">
  <view>
    <view>
      <view>留言主题:</view>
      <view>{{rows.attributes.title}}</view>
      <view>
        <view>留言内容:</view>
        <view>{{rows.attributes.content}}</view>
        <view class="pic">
          <image src="{{rows.attributes.image}}" />
        </view>
        <view>浏览次数:{{rows.attributes.count}}</view>
        <view>创建时间:{{rows.createdAt}}</view>
      </view>
    </view>
  </view>
  <view class="footer">
    <text>  Copyright © 2017-2019 www.smartbull.cn</text>
  </view>
</view>

 detail.js代码:

var Bmob = require('../../utils/bmob')

Page({
  data:{
    rows:{}
  },
  onLoad:function(e){
    console.log(e.id)
    var objectId = e.id;
    var newcount = e.count;
    var that = this;
    var Diary = Bmob.Object.extend("test");
    var query = new Bmob.Query(Diary);
    query.get(objectId,{
      success:function(result){
        console.log(result);
        that.setData({
          rows:result,
        })
        newcount = parseInt(newcount)+1
        result.set("count",newcount)
        result.save()
      },
      error:function(result,error){
        console.log("查询失败")
      }
    })
  }
})

 

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是Python第八章作业人机猜拳小游戏的代码和说明: ```python import random # 定义一个函数,用于判断猜拳胜负 def judge(player, computer): if player == computer: return "平局" elif player == "石头" and computer == "剪刀" or \ player == "剪刀" and computer == "布" or \ player == "布" and computer == "石头": return "你赢了" else: return "计算机赢了" # 定义一个列表,存储猜拳的三种手势 gestures = ["石头", "剪刀", "布"] # 主程序 while True: # 计算机随机出拳 computer_gesture = random.choice(gestures) # 玩家输入手势 player_gesture = input("请出拳(石头/剪刀/布),输入end结束游戏:") # 判断玩家是否结束游戏 if player_gesture == "end": print("游戏结束") break # 判断玩家输入是否合法 if player_gesture not in gestures: print("输入有误,请重新输入") continue # 判断胜负并输出结果 result = judge(player_gesture, computer_gesture) print("你出了%s,计算机出了%s,%s" % (player_gesture, computer_gesture, result)) ``` 说明: 1. 首先导入random库,用于产生随机数。 2. 定义一个函数judge,用于判断猜拳胜负。函数接受两个参数,分别是玩家出的手势和计算机出的手势。函数返回值为胜负结果。 3. 定义一个列表gestures,存储猜拳的三种手势。 4. 进入主程序,使用while循环不断进行猜拳游戏。 5. 在每一轮游戏中,计算机随机出拳,玩家输入手势。 6. 判断玩家是否结束游戏,如果输入end,则跳出循环,游戏结束。 7. 判断玩家输入是否合法,如果不在手势列表中,则提示输入有误,重新输入。 8. 调用judge函数判断胜负,并输出结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值