vue监控-2

<script>
  let read = ''
  export default{
    data () { // 这里存放数据
      return {
        timer: '', // 定时器
        currentTime: 0, // 开始时间
        readTime : 0 // 阅读时间
      };
    },
    // 监听属性 类似于data概念
    computed: {
      userCheck(){ // 同步
        if (this.$store.getters.User  == ''){
          this.disableTP()
        } else {
          this.enableTP()
        }
      }
    },
    // 监控data中的数据变化
    watch: {
      // 监听computed中的useChange值
      userCheck(){
        this.enableTP()
      }
      // '$store.getters.User':'enableTP'
    },
    methods: {
      requestTP () {
        this.$axios.get(url + this.id, {
          params: {
            articleTypeName: this.name
          }
        }) // .then(data => {console.log(data)})
      },
      enableTP () { // 启用浏览
        this.printViolet('检测用户登录状态...' + this.$store.getters.User)
        if (this.$store.getters.User) {
          this.printViolet('检测浏览器状态...' + document.visibilityState === 'hidden')
          if (document.visibilityState != 'hidden') {
            this.printViolet('检测计时器状态...' + (this.timer == null || this.timer === '')) // || "undefined" == typeof this.timer
            if (this.timer == null || this.timer === '') {
              this.timer = setInterval(this.startTimer, 1000);
            }
          }
        }
      },
      // 关闭浏览
      disableTP () {
        clearInterval(this.timer) // 关闭定时器,需要请求
        this.timer = '' // 清楚计时器后,计时器的id还在,所以需要赋空
      },
      // 浏览启用判定
      startTimer () {
        this.currentTime++
        if (this.readTime == null || "undefined" == typeof this.readTime || this.readTime == 0) {
          this.disableTP()
        }
        if (this.currentTime >= this.readTime) {
          this.disableTP() // 关闭
          this.requestTP() // 触发
        }
      },
      // 浏览开关
      switchTP(){
        if(document.visibilityState == 'hidden'){
          this.disableTP()
        }else{
          this.enableTP()
        }
      },
      // 状态
      stateTP () {
        document.addEventListener("visibilitychange", this.switchTP)
      },
      // 打印绿色提示消息
      printViolet (str) {
        console.log('%c%s', 'color:green', str)
      },
      // 判断是否存在
      isExistObject (obj) {
        return obj == null || "undefined" == typeof obj || obj === ''
      },
      // 判断是否为空
      isNullObject (obj) {
        return obj == null || "undefined" == typeof obj
      },
      // 彩色打印
      printColor (str) {
        let color = ['violet', 'red', 'green', 'blue', 'yellow']
        console.log('%c%s', 'color:' + color[2], str)
      }
    }
  }
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值