<template>
<view id="recommendation">
    <view class="content-list">
		
        <block v-for="(items, idx) in list" :key="idx">
			<view class="item" v-for="(item, index) in items" :key="index" :data-id="item.id" @tap="jump" >
				<view class="name">
					{{item.title}}
				</view>
				<view class="author">
					<span class="iconfont icon-yonghu"></span>
					{{item.author}}
				</view>
				<view class="img-box">
					<image mode="widthFix" :src="item.img"></image>
				</view>
			</view>
        </block>
    </view>
</view>
</template>
<script>
import {pageLogin, getUrl,config} from '@/common/js/utils.js';

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

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

  onShow(e) {
    this.getArticleList(1);
    wx.getSystemInfo({
      success: res => {
		  this.windowWidth=res.windowWidth;
      }
    });
  },
  
  methods: {
    jump(e) {
      let id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: '/pages/article/detail/detail?id=' + id
      });
    },

    //获取文章列表
    getArticleList(page) {
      wx.showLoading({
        title: "加载中",
        mask: true
      });
      this.$http
	  .get({
        api: 'api/article/list',
        data: {
          page: page,
          type: 1
        }
      }).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,
              banner: res.meta.img_list
            });
			this.$set(this.list, page -1, res.data);
			 
            wx.setNavigationBarTitle({
              title: res.meta.title
            });
          } else {
            wx.showModal({
              content: res.message || '请求失败',
              showCancel: false
            });
          }
        } else {
          wx.showModal({
            content: res.message || '请求失败',
            showCancel: false
          });
        }

		this.show=false;
		
        wx.hideLoading();
      }).catch(rej => {
        wx.hideLoading();
		console.log(rej);return;
        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];  
         })  
    });  
} 
  },
};
</script>

<style rel="stylesheet/less" lang="less">
    @import "recommendation";
</style>
