记《高校考勤系统》小程序(2)

1,650 阅读9分钟

这是其他几篇的地址:
记《高校考勤系统》小程序(1)
记《高校考勤系统》小程序(2)
记《高校考勤系统》小程序(3)

前面讲了用户注册和首页天气功能,下面讲讲课程表页的实现.

五.课程表页

  • 这里参考了简书上面 轩辕夜空 位作者的案例,以及其参考 极乐叔 的课程表的思路.
  • 同样制作课程表需要用到云开发来存储数据,以及结合云函数对数据的修改,后面会讲到为什么要用到云函数.

先来看看完成后的效果图

  • 1.先搭建整体结构
    • 头部和左侧因为样式是一样的,可以将数据写入data中,再通过for循环渲染出来,这里就直接展示了.(偷个懒😊😊)
    <!-- 星期 -->
    <view class="top">  
        <view class="top-text">
            <text>节\日</text>
        </view>
        <view class="top-text">
            <text>一</text>
        </view>
        <view class="top-text">
            <text>二</text>
        </view>
        <view class="top-text">
            <text>三</text>
        </view>
        <view class="top-text">
            <text>四</text>
        </view>
        <view class="top-text">
            <text>五</text>
        </view>
        <view class="top-text">
            <text>六</text>
        </view>
        <view class="top-text">
            <text>日</text>
        </view>
    </view>
    <!-- 课程 -->
    <view class="cont">
        <view class="cont-left">
            <view class="left"> 1 </view>
            <view class="left"> 2 </view>
            <view class="left"> 3 </view>
            <view class="left"> 4 </view>
            <view class="left"> 5 </view>
            <view class="left"> 6 </view>
            <view class="left"> 7 </view>
            <view class="left"> 8 </view>
        </view>
        <view class="cont-right">
        </view>
    </view>
    <view class="bottom">
        ————<text>读万卷书 行万里路</text>————
    </view>
    
    点击查看wxss代码
    page {
        width: 100%;
        height: 100%;
        position: relative;
    }
    .top {
        padding: 16rpx 0;
        border-top: 1px solid #e9e9e9;
        border-bottom: 1px dashed #d3d3d3;
        display: flex;
        justify-content: flex-start;
    }
    .top-text {
        width: 12.5%;
        text-align: center;
        font-size: 32rpx;
        font-weight: 600;
        align-items: center;
    }
    .top .top-text {
        border-left: 1px dashed #d3d3d3;
    }
    .top .top-text:nth-child(1) {
        font-size: 24rpx;
        border-left: none;
        line-height: 46rpx;
    }
    .cont {
        display: flex;
        justify-content: start;
    }
    .cont-left {
        display: inline-block;
    }
    .left {
        width: 90rpx;
        height: 120rpx;
        justify-content: center;
        display: flex;
        align-items: center;
        border-bottom: 1px dashed #d3d3d3;
        box-sizing: border-box;
        color: #666;
        font-size: 28rpx;
        font-weight: 600;
    }
    .cont-right {
        width: calc(100% - 90rpx);
    }
    .bottom {
        width: 100%;
        text-align: center;
        position: absolute;
        bottom: 20rpx;
        font-size: 24rpx;
        color: #ddd;
        display: inline-block;
    }
    .bottom text {
        margin-left: 20rpx;
        margin-right: 20rpx;
        color: #9b9b9b;
    }
    

  • 2.结合云开发编写课程表.
    • 这里需要考虑到云开发中单次获取数据上限是20条,所以我将课程表数据分为两部分(上午和下午),当然你可以直接使用云函数来提高获取数据的上限,在后面会有介绍,这里就先不做过多的解释了,上代码.

    单个数据结构

    {
        //"_id": "296065c95da529b2055b57301b5afa75",  云开发导入数据会直接生成_id,这里不用自己编写
        "data_name": "Java高级开发技术(JavaEE)",  //课程名
        "address": "@康庄行知楼301",              //地点
        "weekNum": "10-15周",                     //周数
        "pitchNum": "3-4",                        //节数
        "teacher": "赵老师",                      //任课老师
        "_openid": "oQnNa5NJfKqSZntKFLGZWnZuXNbo"  //修改者的openid,本来是想做判断,后面使用了云函数,发现可有可无
    }
    
    • 下面是从云开发数据库中获取我们编写好的课程表数据,如果对操作不熟悉可以查看 官方文档 .
      1.打开云开发控制台.
      2.创建两个集合对应上午下午课程表.
      3.导入我们已经编写好的数据 (一个星期早上和下午的课程分别为14节,所以导入数据时需要注意,如果想要当前课程没有信息,也是需要导入空的字段数据来占一格).
      4.打开权限管理.
      5.选中第一个.
    • 其中最后两个步骤一定不能忘记!

    点击查看js代码
    data: {
        colorArr: ["rgb(229,188,76, 0.8)", "rgb(104,172,246, 0.8)", "rgb(183,135,242, 0.8)", "rgb(149,226,48, 0.8)", "#ff7070",
            "#e54d42", "#0081ff", "#7DC67D", "#E17572", "#C35CFF", "#33BCBA", "#FF8533", "#6E6E6E", "#ebd4ef",
            "#428BCA", "#39b54a", "#FF674F", "#e03997", "#00CED1", "#9F79EE", "#FFC125", "#32CD32", "#00BFFF", "#8799a3","#FF69B4"
        ],
        // 存储随机颜色
        randomColorArr: [],
        randomColorArr2: [],
        i: 25,
        random: '',
        random2: '',
    },
    onLoad: function(options) {
        this.data.randomColorArr = [] // 重置颜色数组1为空
        this.data.randomColorArr2 = [] // 重置颜色数组2为空
        const db = wx.cloud.database({
            env: '*****'  //你的云开发环境名
        })
    
        //获取课程表上午数据
        db.collection('数据集合中你的表名').get().then((res) => {
            this.kechengbiao = res.data
            for (let j = 0; j <= 13; j++) { //for循环判断课名和地名为空则不加颜色
                if (this.kechengbiao[j].data_name == '' && this.kechengbiao[j].address == '') {
                    this.random = 'none'
                    this.data.randomColorArr.push(this.random)
                } else {
                    this.random = this.data.colorArr[Math.floor(Math.random() * this.data.i)] //随机颜色
                    this.data.randomColorArr.push(this.random)
                }
            }
            this.setData({
                loding: true,
                kechengbiao: this.kechengbiao,
                randomColorArr: this.data.randomColorArr
            })
        })
        //获取课程表下午
        db.collection('数据集合中你的表名').get().then((res) => {
            this.kechengbiao2 = res.data
            for (let j = 0; j <= 13; j++) { //for循环判断课名和地名为空则不加颜色
                if (this.kechengbiao2[j].data_name == '' && this.kechengbiao2[j].address == '') {
                    this.random2 = 'none'
                    this.data.randomColorArr2.push(this.random2)
                } else {
                    this.random2 = this.data.colorArr[Math.floor(Math.random() * this.data.i)] //随机颜色
                    this.data.randomColorArr2.push(this.random2)
                }
            }
            this.setData({
                kechengbiao2: this.kechengbiao2,
                randomColorArr2: this.data.randomColorArr2
            })
        })
    },
    
    <view class="cont-right">
        <view>
            <view class='appointent-date'>  //上午
                <view class="appointent-date-div" bindtap="select_date" wx:for="{{kechengbiao}}" wx:key="{{index}}" data-key='{{index}}' style="background-color:{{randomColorArr[index]}}">
                    <view class="flex-item" >
                        <text class='data_name'>{{item.data_name}}</text>
                        <text class='address'>{{item.address}}</text>
                    </view>
                </view>
            </view>
            <view class='appointent-date'>  //下午
                <view class="appointent-date-div" bindtap="select_date2" wx:for="{{kechengbiao2}}" wx:key="{{index}}" data-key='{{index}}' style="background-color:{{randomColorArr2[index]}}">
                    <view class="flex-item">
                        <text class='data_name'>{{item.data_name}}</text>
                        <text class='address'>{{item.address}}</text>
                </view>
            </view>
        </view>
    </view>
    
    点击查看wxss代码
    .appointent-date {
        display: flex;
        justify-content: space-around;
        flex-wrap: wrap;
    }
    .appointent-date-div {
        height: 236rpx;
        border-radius: 10rpx;
        margin-bottom: 6rpx;
        color: white;
    }
    .flex-item {
        display: flex;
        justify-content: flex-start;
        flex-direction: column;
        width: 76rpx;
        height: 212rpx;
        font-size: 24rpx;
        padding: 6rpx;
        border: 1rpx solid transparent;
        text-align: left;
        border-radius: 10rpx;
        cursor: pointer;
        overflow: hidden;
    }
    .data_name {
        display: inline-block;
    }
    .address {
        display: inline-block;
    }
    

  • 3.对课程实现增删改查
    • 首先来做最简单的 吧,我们需要在for循环中加入 data-='{{index}}' ,如上代码所示👆我加了data-key='{{index}}',其中key可以自定义,目的是为了在点击课程时,可以获取相循环中对应的下标,这样我就可以在数据库中搜索到当前点击的数据并渲染出来.

    //点击课程内容弹出详细框
    select_date: function(e) {
      this.id = e.currentTarget.dataset.key //获取当前点击课程的下标
      const db = wx.cloud.database({
        env: '****'
      })
      db.collection('***').get().then(res => {
        console.log(res.data[this.id])   //获取点击时课程表数据
      })
    },
    

    • 获取到数据后我们可以根据自己的需求渲染在页面上,这里我结合了vant的tab组件,左上角为返回,右上角为删除课程表信息,下面就讲 除课程信息.
    • 这里的删除不是说真的将数据从我们的数据库中删除,而是将数据赋值为“”也就是空值,这样就做到了删除的功能,在此结合 云函数 来实现,因为云开发中的操作权限无法满足我们对数据操作的要求.

    首先我们在云函数中创建一个新的 云函数 ,修改index.js文件

    • 这里会有一个问题也就是为什么我们要使用云函数,而不直接用云开发对数据进行处理,是因为云开发中的操作权限只能对自己提交到数据库中的数据进行修改,如果是别人那么就无法修改.正常情况下,管理员肯定不止一位,所以对数据操作不能只限定一个人.
    //修改课程表
    const cloud = require('wx-server-sdk')
    cloud.init({
        env: '***',//你的开发环境
        traceUser: true
    })
    const db = cloud.database();
    exports.main = async (event, context) => { // 云函数入口函数
        try {
            return await db.collection('***').doc(event.id).update({  //需要修改的数据库
                data: {
                    data_name: event.data_name,
                    address: event.address,
                    weekNum: event.weekNum,
                    pitchNum: event.pitchNum,
                    teacher: event.teacher
              },
            })
        } catch (e) {
            console.error(e)
        }
        return {
            event,
            openid: wxContext.OPENID,
            appid: wxContext.APPID,
            unionid: wxContext.UNIONID,
        }
    }
    

    然后在js文件中编写对应的代码

    tapDialogButton(e) { //从课表删除课程
        wx.cloud.callFunction({
            name: '***',// 你的云函数名称
            data: {
              id: this._id,  //将数据进行空值赋值
              data_name: "",
              address: "",
              weekNum: "",
              pitchNum: "",
              teacher: ""
            },
            success: res => {
              // 关闭当前点击课程详情
            }
        })
    },
    

    改,增

    • 做完了删其实对改和增的实现应该也变得相对简单,这里也是需要用到云函数,道理和上面讲的一致.
    • 在获取当前点击的数据时先将此条数据存储在 data 中,需要修改时,可以将数据赋值给 input 的 value ,在通过云函数来修改数据库中对应的数据.增也是同样的道理.这里就拿改为例.

    wxml

    <!-- 编辑页 -->
    <view class="edit" hidden="{{ editShow }}">
        <van-nav-bar title="编辑课程" right-text="完成" left-arrow bind:click-left="editLeft" bind:click-right="editRight" />
        <view class="label className">
            <text>课名</text>
            <input  value="{{ nowClass.data_name }}" bindinput="bindKeyInput1"></input>
        </view>
        <view class="label">
            <text>教室</text>
            <input  value="{{ nowClass.address }}" bindinput="bindKeyInput2"></input>
        </view>
        <view class="label">
            <text>周数</text>
            <input  value="{{ nowClass.weekNum }}" bindinput="bindKeyInput3"></input>
        </view>
        <view class="label">
            <text>节数</text>
            <input  value="{{ nowClass.pitchNum }}" bindinput="bindKeyInput4"></input>
        </view>
        <view class="label">
            <text>老师</text>
            <input  value="{{ nowClass.teacher }}" bindinput="bindKeyInput5"></input>
        </view>
    </view>
    

    新建修改课程表数据的云函数

    //修改课程表
    const cloud = require('wx-server-sdk')
    cloud.init({
        env: '***',//你的开发环境
        traceUser: true
    })
    const db = cloud.database();
    exports.main = async (event, context) => { // 云函数入口函数
        try {
            return await db.collection('***').doc(event.id).update({  //你要操作的数据库
                data: {
                    data_name: event.data_name,
                    address: event.address,
                    weekNum: event.weekNum,
                    pitchNum: event.pitchNum,
                    teacher: event.teacher
                },
            })
        } catch (e) {
            console.error(e)
        }
        return {
            event,
            openid: wxContext.OPENID,
            appid: wxContext.APPID,
            unionid: wxContext.UNIONID,
        }
    }
    

    js

    //  1.首先获取输入框的值,存在data中
    bindKeyInput1(e) { //课名
        this.editClassName = e.detail.value
    },
    bindKeyInput2(e) { //教室
        this.editAddress = e.detail.value
    },
    bindKeyInput3(e) { //周数
        this.editWeekNum = e.detail.value
    },
    bindKeyInput4(e) { //节数
        this.editPitchNum = e.detail.value
    },
    bindKeyInput5(e) { //老师
        this.editTeacher = e.detail.value
    },
    editRight() { //  2.编辑完成,点击提交按钮时将输入框的值赋值给对应的字段名
      wx.cloud.callFunction({
        name: '***',// 修改课程表数据的云函数名称
        data: {
          id: this._id,
          data_name: this.editClassName,
          address: this.editAddress,
          weekNum: this.editWeekNum,
          pitchNum: this.editPitchNum,
          teacher: this.editTeacher
        },
        success: res => {
        },
        fail: console.error
      })
    
    },
    

到这里课程表功能就做完了,如果有什么不懂得地方欢迎留言,或者写的不好的地方,请大家指出一起探讨,之后会继续分享后面的内容。大家也可以提前扫码查看小程序,欢迎指出不足,谢谢 🌞😃😃😃