• 微信小程序连接数据库与WXS的使用



    )

    1.搭建数据库连接,使用后端获取数据

    为了后期方便维护,我们先将所有的后端接口通过一个文件来保存,在根目录下新建config文件夹随后建立api.js文件。

    // 以下是业务服务器API地址
     // 本机开发API地址
     var WxApiRoot = 'http://localhost:8080/wx/';
     // 测试环境部署api地址
     // var WxApiRoot = 'http://192.168.0.101:8070/demo/wx/';
     // 线上平台api地址
     //var WxApiRoot = 'https://www.oa-mini.com/demo/wx/';
     
     module.exports = {
       IndexUrl: WxApiRoot + 'home/index', //首页数据接口
       SwiperImgs: WxApiRoot+'swiperImgs', //轮播图
       MettingInfos: WxApiRoot+'meeting/list', //会议信息
     };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    1.请求方式的封装

    loadMeetingInfos(){
         let that=this;
         wx.request({
             url: api.IndexUrl,
             dataType: 'json',
            success(res) {
               console.log(res)
               that.setData({
                   lists:res.data.data.infoList
               })
             }
           })
             })
        })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    从后台获取数据

    2.化一下代码,这样写太繁琐了

    在/utils/util.js中添加下列代码

    /**
     * 封装微信的request请求
     */
    function request(url, data = {}, method = "GET") {
      return new Promise(function (resolve, reject) {
        wx.request({
          url: url,
          data: data,
          method: method,
          header: {
            'Content-Type': 'application/json',
          },
          success: function (res) {
            if (res.statusCode == 200) {
                resolve(res.data);//会把进行中改变成已成功
            } else {
              reject(res.errMsg);//会把进行中改变成已失败
            }
          },
          fail: function (err) {
            reject(err)
          }
        })
      });
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    然后可以改成

    loadMeetingInfos(){
        // let that=this;
        // wx.request({
        //     url: api.IndexUrl,
        //     dataType: 'json',
        //     success(res) {
        //       console.log(res)
        //       that.setData({
        //           lists:res.data.data.infoList
        //       })
        //     }
        //   })
        let that = this;
        utils.request(api.IndexUrl).then(res => {
          console.log(res);
          this.setData({
            lists: res.data.infoList
          })
        })
    
      },
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    大大的·简化了代码
    注意在module.exports中导出和需要使用的页面js中使用实const util = require(“…/…/utils/util”)

    3.前端代码

    <!--index.wxml-->
    <view>
      <swiper autoplay="true" indicator-dots="true">
        <block wx:for="{{imgSrcs}}" wx:key="text">
          <swiper-item>
            <view>
              <image src="{{item.img}}" class="swiper-item" />
            </view>
          </swiper-item>
        </block>
      </swiper>
    </view>
    <wxs src="/utils/comm.wxs" module="tools" />
    
     
    <view class="mobi-title">
      <text class="mobi-icon"></text>
      <text class="mobi-text">会议信息</text>
    </view>
    <block wx:for-items="{{lists}}" wx:for-item="item" wx:key="item.id">
      <view class="list" data-id="{{item.id}}">
        <view class="list-img">
          <image class="video-img" mode="scaleToFill" src="{{item.image !=null? item.image : '/static/persons/2.jpg'}}"></image>
        </view>
        <view class="list-detail">
          <view class="list-title"><text>{{item.title}}</text></view>
          <view class="list-tag">
            <view class="state"> {{tools.getState(item.state)}}</view>
            <view class="join"><text class="list-num"> {{tools.getNumber(item.canyuze,item.liexize,item.zhuchiren)}}</text>人报名</view>
          </view>
          <view class="list-info"><text>{{item.location}}</text>|<text>{{tools.formatDate(item.starttime)}}</text></view>
        </view>
      </view>
    </block>
    <view class="section">
      <text>到底啦</text>
    </view>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    /* pages/index/index.wxss */
    .mobi-title {
      font-size: 12pt;
      color: #777;
      line-height: 110%;
      font-weight: bold;
      width: 100%;
      padding: 15rpx;
      background-color: #f3f3f3;
    }
    
    .mobi-icon {
      padding: 0rpx 3rpx;
      border-radius: 3rpx;
      background-color: #ff7777;
      position: relative;
      margin-right: 10rpx;
    }
    
    /*list*/
    .list {
      display: flex;
      flex-direction: row;
      width: 100%;
      padding: 0 20rpx 0 0;
      border-top: 1px solid #eeeeee;
      background-color: #fff;
      margin-bottom: 5rpx;
      /* border-radius: 20rpx;
      box-shadow: 0px 0px 10px 6px rgba(0,0,0,0.1); */
    }
    
    .list-img {
      display: flex;
      margin: 10rpx 10rpx;
      width: 150rpx;
      height: 220rpx;
      justify-content: center;
      align-items: center;
    }
    
    .list-img .video-img {
      width: 120rpx;
      height: 120rpx;
      
    }
    
    .list-detail {
      margin: 10rpx 10rpx;
      display: flex;
      flex-direction: column;
      width: 600rpx;
      height: 220rpx;
    }
    
    .list-title text {
      font-size: 11pt;
      color: #333;
      font-weight: bold;
    }
    
    .list-detail .list-tag {
      display: flex;
      height: 70rpx;
    }
    
    .list-tag .state {
      font-size: 9pt;
      color: #81aaf7;
      width: 120rpx;
      border: 1px solid #93b9ff;
      border-radius: 2px;
      margin: 10rpx 0rpx;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    
    .list-tag .join {
      font-size: 11pt;
      color: #bbb;
      margin-left: 20rpx;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    
    .list-tag .list-num {
      font-size: 11pt;
      color: #ff6666;
    }
    
    .list-info {
      font-size: 9pt;
      color: #bbb;
      margin-top: 20rpx;
    }
    .bottom-line{
      display: flex;
      height: 60rpx;
      justify-content: center;
      align-items: center;
      background-color: #f3f3f3;
    }
    .bottom-line text{
      font-size: 9pt;
      color: #666;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108

    在这里插入图片描述

    四、WXS的使用

    用于解决一些样式问题
    先再utils创建一个文件名字为comm.wxs

    1…解决数据显示数字问题

    function getState(state){
      // 状态:0取消会议1待审核2驳回3待开4进行中5开启投票6结束会议,默认值为1
      if(state == 0 ){
          return '取消会议';
      }else if(state == 1 ){
          return '待审核';
      }else if(state == 2 ){
          return '驳回';
      }else if(state == 3 ){
          return '待开';
      }else if(state == 4 ){
          return '进行中';
      }else if(state == 5 ){
          return '开启投票';
      }else if(state == 6 ){
          return '结束会议';
      }
          
      return '其它';
     
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    在wxs页面导出

    module.exports = {
      getState: getState,
      getNumber:getNumber,
      formatDate:formatDate,
    };
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在wxlm页面引入


    即可使用

    2. 解决统计人数问题

    var getNumber = function(canyuze,liexize,zhuchiren) {
      var person = canyuze+","+liexize+","+zhuchiren;
      return person.split(",").length;
    }
    
    • 1
    • 2
    • 3
    • 4

    3.解决时间进制问题

    function formatDate(ts, option) {
      var date = getDate(ts)
      var year = date.getFullYear()
      var month = date.getMonth() + 1
      var day = date.getDate()
      var week = date.getDay()
      var hour = date.getHours()
      var minute = date.getMinutes()
      var second = date.getSeconds()
      
      //获取 年月日
      if (option == 'YY-MM-DD') return [year, month, day].map(formatNumber).join('-')
    
      //获取 年月
      if (option == 'YY-MM') return [year, month].map(formatNumber).join('-')
    
      //获取 年
      if (option == 'YY') return [year].map(formatNumber).toString()
    
      //获取 月
      if (option == 'MM') return  [mont].map(formatNumber).toString()
    
      //获取 日
      if (option == 'DD') return [day].map(formatNumber).toString()
    
      //获取 年月日 周一 至 周日
      if (option == 'YY-MM-DD Week')  return [year, month, day].map(formatNumber).join('-') + ' ' + getWeek(week)
    
      //获取 月日 周一 至 周日
      if (option == 'MM-DD Week')  return [month, day].map(formatNumber).join('-') + ' ' + getWeek(week)
    
      //获取 周一 至 周日
      if (option == 'Week')  return getWeek(week)
    
      //获取 时分秒
      if (option == 'hh-mm-ss') return [hour, minute, second].map(formatNumber).join(':')
    
      //获取 时分
      if (option == 'hh-mm') return [hour, minute].map(formatNumber).join(':')
    
      //获取 分秒
      if (option == 'mm-dd') return [minute, second].map(formatNumber).join(':')
    
      //获取 时
      if (option == 'hh')  return [hour].map(formatNumber).toString()
    
      //获取 分
      if (option == 'mm')  return [minute].map(formatNumber).toString()
    
      //获取 秒
      if (option == 'ss') return [second].map(formatNumber).toString()
    
      //默认 时分秒 年月日
      return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
    }
    function formatNumber(n) {
      n = n.toString()
      return n[1] ? n : '0' + n
    }
    
    function getWeek(n) {
      switch(n) {
          case 1:
          return '星期一'
          case 2:
          return '星期二'
          case 3:
          return '星期三'
          case 4:
          return '星期四'
          case 5:
          return '星期五'
          case 6:
          return '星期六'
          case 7:
          return '星期日'
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78

    在这里插入图片描述

  • 相关阅读:
    力扣46:全排列(Java回溯)
    Win11下无法打开丛林之狐,提示未检测到DirectX 8.1
    elementPlus表格组件el-table实现只能同时选择一行,全选按第一行处理
    【已解决】pycharm 突然每次点击都开新页面,关不掉怎么办?
    Python二分查找
    flv.js源码知识点(中)
    JVM启动参数简介
    链表动画演示 等比数列 linux视频等
    GEE开发之Modis_NDVI数据分析获取大总结
    个人玩航拍,如何申请无人机空域?
  • 原文地址:https://blog.csdn.net/m0_74018330/article/details/133959272