<template>
<view id="article-list">
    <view class="content">
        <block v-for="(item, index) in list" :key="index"  >
            <view class="item" v-for="(item, index) in items" :key="index" :data-id="item.id" @tap="jump">
                <image mode="widthFix" :src="item.img"></image>
            </view>
        </block>
    </view>
</view>
</template>
<script>
import {pageLogin, getUrl,config,is} from '@/common/js/utils.js';

export default {
  data() {
    return {
      list: '',
      type: '',
      more: true,
      page: 1
    };
  },

  //下拉刷新
  onReachBottom() {
    if (this.more) {
      let page = this.page + 1;
      this.getArticleList(page, this.type);
    } else {
      wx.showToast({
        image: '../../../static/error.png',
        title: '再拉也没有啦'
      });
    }
  },

  onLoad(e) {
    if (e.type) {
      this.setData({
        type: e.type
      });
      this.getArticleList(1, e.type);
    }
  },

  components: {},
  props: {},
  methods: {
    jump(e) {
      let id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: '/pages/article/detail/detail?id=' + id
      });
    },

    //获取文章列表
    getArticleList(page, type) {
      wx.showLoading({
        title: "加载中",
        mask: true
      });
      this.$http.get({
        api: 'api/article/list',
        data: {
          page: page,
          type: type
        }
      }).then(res => {
        if (res.statusCode == 200) {
          res = res.data;

          if (res.status) {
            var pages = res.meta.pagination;
            var current_page = pages.current_page;
            var total_pages = pages.total_pages;
            this.setData({
              [`list[${page - 1}]`]: res.data,
              more: current_page < total_pages,
              page: current_page
            });
            wx.setNavigationBarTitle({
              title: res.meta.title
            });
          } else {
            wx.showModal({
              content: res.message || '请求失败',
              showCancel: false
            });
          }
        } else {
          wx.showModal({
            content: res.message || '请求失败',
            showCancel: false
          });
        }

        this.setData({
          show: false
        });
        wx.hideLoading();
      }).catch(rej => {
        wx.hideLoading();
        wx.showModal({
          content: res.message || '请求失败',
          showCancel: false
        });
      });
    },

    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  },
  computed: {},
  watch: {}
};
</script>
<style rel="stylesheet/less" lang="less">
    @import "list";
</style>