暑期实训9- 7.15&16&拦截器

1 触底刷新

实现了滚动到底加载更多的效果,
在这里插入图片描述
每一页读三条数据,鼠标向下滚动,将把下一页的三条数据读出来,加在原来的list后面,展示的是list,变成了六条数据。
在这里插入图片描述

2 utils

需要将data的时间格式处理成我想要的。参考:小程序页面处理后台返回时间格式
他上面是在utils文件夹下创建.wxs文件,然后引入。我找了一下,这个utils文件夹:

可以将一些公共的代码抽离成为一个单独的 js (utils.js)文件,作为一个模块; 模块只有通过 module.exports 或者 exports 才能对外暴露接口。 所以当你在util.js里封装的方法想要在外部使用的话,必须通过 module.exports 或者exports 对外暴露
在需要使用这些模块的文件中使用:使用 require(path) 将公共代码引入
require 暂时不支持绝对路径

var util= require('../../utils/util.js')

参考:https://blog.csdn.net/caohoucheng/article/details/82012489

新建的项目里边生成了一个utils/util.js
在这里插入图片描述

3 时间格式处理

重点应该是这个filter.wxs文件,官方文档:WXSWXS语法参考

//通过substring截取处理时间
function newFormatTime(timestamp) {
  return (timestamp.substring(0, 10))
}

module.exports = {
    newFormatTime:newFormatTime
}

在需要渲染的页面引入filter.wxs文件

<wxs module="filter"  src='../../utils/filter.wxs'></wxs>//引入


//使用
	<view class="two">{{filter.newFormatTime(item.productionCycleStart)}}</view>
	//item.productionCycleStart要渲染的数据
	//newFormatTime方法名

我想,在utils文件夹下,创建一个.wxs文件,里面的函数用来处理获得的时间戳,在wxml文件中使用,在需要渲染的页面引入.wxs文件。

filter.wxs:


 /**
   * 
   * @param {*} times  时间戳
   * 转换为  yyyy-MM-dd HH:MM:SS 格式的日期
   */
function formatDate1(times) {
  var date = getDate(parseInt(times));
  var year = date.getFullYear(); //年份
  var month = date.getMonth() + 1; //月份
  var day = date.getDate(); //日
  var hour = function () { //获取小时
      return date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  }
  var minute = function () { //获取分钟
      return date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
  }

  var second = function () { //获取秒数
      return date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  }

  console.log(year+'.'+month+'.'+day);
  console.log(hour() + ':' + minute() + ':' + second());
  return year + '.' + month + '.' + day + ' ' //+ hour() + ':' + minute() + ':' + second()

}

function formatDate2 (times) {
  var date = getDate(times);
  var year = date.getFullYear(); //年份
  var month = date.getMonth() + 1; //月份
  var day = date.getDate(); //日
  var hour = function () { //获取小时
      return date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  }
  var minute = function () { //获取分钟
      return date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
  }

  var second = function () { //获取秒数
      return date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  }

  console.log(year+'.'+month+'.'+day);
  console.log(hour() + ':' + minute() + ':' + second());
  return  hour() + ':' + minute() ;

}

module.exports={
  formatDate1:formatDate1,
  formatDate2:formatDate2,
};

test1.wxml

<wxs module="formatdate" src="../../utils/filter.wxs"></wxs> <!--引入处理时间的格式-->
...
 <text wx:if="{{list}}" class="time" space="emsp">{{formatdate.formatDate1(item.act_start_timestamp)}} </text>
 <text wx:if="{{list}}" class="time">{{formatdate.formatDate2(item.act_start_timestamp)}}-{{formatdate.formatDate2(item.act_end_timestamp)}}</text>

出错:(参考:微信小程序学习之wxs使用教程

  1. 报错“Unexpected token Date”,原因:WXS 中不能使用 new Date() 应该使用 。 改正:使用var date=getDate(timestamp)
  2. 一直没有内容,NAN,[WXS Runtime info], 原因:我传的参数出错,应该是item.act_start_timestamp,缺少了item. 。改正:添加即可。

拦截器

拦截器,会先对所有向后端的请求拦截。(指定的不会拦截,比如登录)
拦截先检查请求投中有没有token,没有就返回状态403,并发送消息your token is null。
然后解析token,如果解析出来的openid是数据库中存在的,就通过了,没有也是返回状态403,然后发送消息your openid is null
当发起正常请求时,如果token有问题就会返回一个json包,和登录返回的差不多,只是少了date。如果返回这个就得要求用户重新登陆了。

在正常请求中,是将token放在请求头中。后端会先拦截检查token,如果token有问题我就会返回一个json包,里面有code和message。小程序端发现返回的不是预期的json包,就说明token过期或者有错,就需要重新登录获得新的token

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值