{"size": 2060, "ext": "js", "max_stars_repo_path": "pages/mine/mine.js", "max_stars_repo_name": "fox-ahri/ahri-cloudbook-wx", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "pages/mine/mine.js", "max_issues_repo_name": "fox-ahri/ahri-cloudbook-wx", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "pages/mine/mine.js", "max_forks_repo_name": "fox-ahri/ahri-cloudbook-wx", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 17.9130434783, "max_line_length": 62, "alphanum_fraction": 0.4655339806}
import ajax from '../../service/ajax'

Page({

  data: {
    books: [],
    show: false,
    username: '',
    password: '',
    loading: false
  },

  onLoad: function (options) {
    let token = wx.getStorageSync('token')
    if (!token) {
      this.setData({
        show: true
      })
    } else {
      this.get_books()
    }
  },

  bindKeyInput: function (e) {
    this.setData({
      [e.target.id]: e.detail.value
    })
  },

  get_books: function () {
    this.setData({
      loading: true
    })
    ajax({
      url: '/admin/notebook/book/'
    }).then(res => {
      if (res.data.code === 0) {
        this.setData({
          show: true
        })
      } else {
        this.setData({
          books: res.data.data
        })
      }
      this.setData({
        loading: false
      })
    }).catch(err => {
      wx.showToast({
        title: err.errMsg,
        icon: 'none',
        duration: 2000
      })
      this.setData({
        loading: false
      })
    })
  },

  cancel: function () {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },

  login: function () {
    this.setData({
      loading: true
    })
    ajax({
      url: '/login/',
      data: {
        username: this.data.username,
        password: this.data.password
      },
      method: 'post'
    }).then(res => {
      if (res.data.code === 200) {
        wx.setStorageSync('token', res.data.data.token)
        this.setData({
          show: false
        })
        this.get_books()
      } else {
        wx.showToast({
          title: '登录失败',
          icon: 'none',
          duration: 2000
        })
      }
      this.setData({
        loading: false
      })
    }).catch(err => {
      this.setData({
        loading: false
      })
    })
  },

  exit: function () {
    wx.removeStorageSync('token')
    wx.reLaunch({
      url: '/pages/index/index',
    })
  },

  toRead: function (val) {
    let book = val.currentTarget.dataset.val
    wx.navigateTo({
      url: `/pages/read/read?id=${book.id}&name=${book.name}`,
    })
  }
})