// pages/shop.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    password: app.remoteConfig.cloudPwd || "U2FsdGVkX1+jfEkF2OXTQ5iIG4mrYc5/TLOiIntyENU=",
    username: app.remoteConfig.cloudEmail || "1575057249@qq.com",
    baseCloudUrl: app.remoteConfig.baseCloudUrl,
    token: '',
    name: '',
    page: 1,
    pageSize: 99,
    productList: []
  },

  getUserToken() {
    const that = this
    wx.showLoading({
      title: 'gen token loading',
    });
    wx.request({
      url: this.data.baseCloudUrl + 'token/gen',
      method: 'POST',
      data: {
        username: this.data.username,
        password: this.data.password
      },
      success: (res => {
        that.setData({
          token: res.data.token
        })
        wx.hideLoading()
        that.getProductList()
      }),
      fail: r => {
        console.log('cloud r', r)
        wx.hideLoading()
      }
    })
  },
  previewImage(e){
    console.log('click png',e)
    const url=e.target.dataset.url
    wx.previewImage({
      current: url, // 当前显示图片的http链接
      urls: [url] // 需要预览的图片http链接列表
    })
  },

  getRenderSrc(id) {
    return `${this.data.baseCloudUrl}media/render?id=${id}`
  },
  getProductList() {
    const that = this
    wx.showLoading({
      title: 'get list loading',
    });
    wx.request({
      url: this.data.baseCloudUrl + '/product/list',
      method: 'POST',
      header: {
        Authorization: `bearer ${this.data.token}`
      },
      data: {
        name: '',
        page: this.data.page,
        pageSize: this.data.pageSize,
      },
      success: (res => {
        that.setData({
          productList: res.data.data.map(item => {
            return {
              ...item,
              imgSrc: item.img_id?that.getRenderSrc(item.img_id):'',
              mvSrc: item.mov_id?that.getRenderSrc(item.mov_id):'',
              mode: 'scaleToFill',
            }
          })
        })
        wx.hideLoading()
      }),
      fail: r => {
        console.log('cloud r', r)
        wx.hideLoading()
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getUserToken()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})