后台交互-首页->与后台数据进行交互,wsx的使用

  • 与后台数据进行交互
  • wsx的使用

1.与后台数据进行交互

java 复制代码
// index.js
// 获取应用实例
const app = getApp()
const api=require("../../config/app.js")
const util=require("../../utils/util.js")
Page({
  data: {
    imgSrcs:[
      {
        "img": "https://cdn-we-retail.ym.tencent.com/tsr/home/v2/banner1.png",
        "text": "1"
      },
      {
        "img": "https://cdn-we-retail.ym.tencent.com/tsr/home/v2/banner2.png",
        "text": "2"
      },
      {
        "img": "https://cdn-we-retail.ym.tencent.com/tsr/home/v2/banner3.png",
        "text": "3"
      },
      {
        "img": "https://cdn-we-retail.ym.tencent.com/tsr/home/v2/banner4.png",
        "text": "4"
      },
      {
        "img": "https://cdn-we-retail.ym.tencent.com/tsr/home/v2/banner5.png",
        "text": "5"
      },
      {
        "img": "https://cdn-we-retail.ym.tencent.com/tsr/home/v2/banner6.png",
        "text": "6"
      },

    ],
    
  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  loadMeetInfos(){
  util.request(api.IndexUrl).then(res => {
  console.log(res);
  this.setData({
  lists:res.data.infoList
  })
});
    // let that=this;
      //   wx.request({
      //   url: api.IndexUrl,
      //   dataType: 'json',
      //   success(res) {
      //     console.log(res)
      //     that.setData({
      //         lists:res.data.data.infoList
      //     })
      //   }
      // })
  },
  loadSwiperImgs(){
    let that=this;
    // wx.request({
    //     url: api.SwiperImgs,
    //     dataType: 'json',
    //     success(res) {
    //       console.log(res)
    //       that.setData({
    //           imgSrcs:res.data.images
    //       })
    //     }
    //   })
  },
  onLoad() {
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }
    this.loadSwiperImgs();
    this.loadMeetInfos();
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认,开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  getUserInfo(e) {
    // 不推荐使用getUserInfo获取用户信息,预计自2021年4月13日起,getUserInfo将不再弹出弹窗,并直接返回匿名的用户个人信息
    console.log(e)
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
java 复制代码
<!--index.wxml-->
<view>
  <swiper indicator-dots="{{true}}" autoplay="{{true}}">
        <block wx:for="{{imgSrcs}}" wx:key="*text">
          <swiper-item>
            <image src="{{item.img}}"></image>
          </swiper-item>
        </block>
      </swiper>
</view>
<view class="mobi-title">
    <text class="mobi-icon"></text>
    <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/1.jpg'}}"></image>
        </view>
        <view class="list-detail">
            <view class="list-title"><text>{{item.title}}</text></view>
            <view class="list-tag">
                <view class="state">{{item.state}}</view>
                <view class="join"><text class="list-num">{{item.num}}</text>人报名</view>
            </view>
            <view class="list-info"><text>{{item.location}}</text>|<text>{{item.starttime}}</text></view>
        </view>
    </view>
</block>
<view class="section bottom-line">
		<text>到底啦</text>
</view>
java 复制代码
//oamin\config\app.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', //会议信息
  };

2.wsx的使用

java 复制代码
<!--index.wxml-->
<view>
  <swiper indicator-dots="{{true}}" autoplay="{{true}}">
        <block wx:for="{{imgSrcs}}" wx:key="*text">
          <swiper-item>
            <image src="{{item.img}}"></image>
          </swiper-item>
        </block>
      </swiper>
</view>
<wxs src="/utils/comm.wxs" module="tools" />
<view class="mobi-title">
    <text class="mobi-icon"></text>
    <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/1.jpg'}}"></image>
        </view>
        <view class="list-detail">
            <view class="list-title"><text>{{item.title}}</text></view>
            <view class="list-tag">
                <view class="state">
                {{tools.getStateName(item.state)}}
                </view>
                <view class="join"><text class="list-num">{{tools.getNum(item.cnyuze,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 bottom-line">
		<text>到底啦</text>
</view>
java 复制代码
//utils\comm.wxs
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 '星期日'
  }
}
function getStateName(state){
  console.log(getStateName);
if(state==1){
 return "待审核"
}else if(state==2){
return "审核通过"
}else if(state==3){
return "审核不通过"
}else if(state==4){
  return "代开会议"
}  
return "其他"
}
function getNum(canyuze,liexize,zhuchiren){
  var person=canyuze + "," + liexize + "," + zhuchiren;
  return person.split(",").length;
}
module.exports = {
  getStateName: getStateName,
  getNum:getNum,
  formatDate:formatDate
  
};
相关推荐
圣光SG6 分钟前
Java类与对象及面向对象基础核心详细笔记
java·前端·数据库
Jinuss15 分钟前
源码分析之React中的useImperativeHandle
开发语言·前端·javascript
ZC跨境爬虫27 分钟前
CSS核心知识点与定位实战全解析(结合Playwright爬虫案例)
前端·css·爬虫
Jinuss29 分钟前
源码分析之React中的forwardRef解读
前端·javascript·react.js
mengsi5531 分钟前
Antigravity IDE 在浏览器上 verify 成功但本地 IDE 没反应 “开启Tun依然无济于事” —— 解决方案
前端·ide·chrome·antigravity
南风知我意95744 分钟前
JavaScript 惰性函数深度解析:从原理到实践的极致性能优化
开发语言·javascript·性能优化
Можно1 小时前
pages.json 和 manifest.json 有什么作用?uni-app 核心配置文件详解
前端·小程序·uni-app
hzhsec1 小时前
钓鱼邮件分析与排查
服务器·前端·安全·web安全·钓鱼邮件
爱看老照片1 小时前
uniapp传递数值(数字)时需要使用v-bind的形式(加上冒号)
javascript·vue.js·uni-app