interactPost.js 4.5 KB
// pages/interact/interactPost/interactPost.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    params:{
      title:'',
      content:'',
      images:[]
    },
    images: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  setText(e){
    let t = this,s = e.currentTarget.dataset.type;
    s=='title'?(t.setData({
      'params.title':e.detail.value
    })):(t.setData({
      'params.content': e.detail.value
    }))
  },
  deleteImg(e){
    let index = e.currentTarget.dataset.index;
    this.data.images.splice(index,1);
    this.setData({
      images: this.data.images
    })
  },

  choose: function () {
    let that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        // console.log(res);
        let successUp = 0; //成功个数
        let failUp = 0; //失败个数
        let i = 0; //第几个
        let tempFilePaths = res.tempFilePaths //总文件
        let length = res.tempFilePaths.length //总共个数
        wx.showNavigationBarLoading()
        wx.showLoading({
          title: '上传中',
        })
        that.uploadAllfile(tempFilePaths, successUp, failUp, i, length);
      },
      fail: function (res) {
        console.log(res);
      },

    })
  },

  // 上传图片至后台
  uploadAllfile(filePaths, successUp, failUp, i, length) {
    let that = this;
    let url = '/api/common/upload';
    let headers = {
      "token": wx.getStorageSync("token"),
      'content-type': 'multipart/form-data'
    }
    wx.uploadFile({
      url: app.globalData.baseUrl + url, //仅为示例,非真实的接口地址
      header: headers,
      filePath: filePaths[i],
      name: 'file',
      formData: {
        act: "Index_upload",
        file: '',
        filetype: 'image'
      },
      success: function (res) {
        // console.log(res)
        wx.hideNavigationBarLoading()
        wx.hideLoading()
        if (res.statusCode == 200) {
          // console.log(res)
          let dat = JSON.parse(res.data);
          // console.log(dat);
          that.setData({
            images: that.data.images.concat(dat.data.url)
          })
          // console.log(that.data.proData.images);
        } else {
          wx.showModal({
            title: '提示',
            content: res.msg,
            showCancel: false
          })
        }
      },
      fail: function (res) {
        wx.hideNavigationBarLoading()
        wx.hideLoading()

        // console.log(res);
      },
      complete() {
        i++;
        let img = that.data.img
        if (i == length) {
          // console.log('总共' + successUp + '张上传成功,' + failUp + '张上传失败!');
        }
        else { //递归调用uploadDIY函数
          that.uploadAllfile(filePaths, successUp, failUp, i, length);
        }
      }

    })

  },
  subArticle(){
    let t = this, url ='/api/interact/setPosts',params= t.data.params;
    params.images = params.images.length?params.images.join(','):'';
    !params.title?(wx.showToast({
      title: '请输入主题',
      icon:'none'
    })) : (!params.content ? wx.showToast({
      title: '请输入内容',
      icon: 'none'
      }) : (app.post(url, params).then((r) => {
        wx.showToast({
          title: '发表成功',
          icon: 'none'
        })
        setTimeout(() => {
          wx.switchTab({
            url: '/pages/enterprise/enterprise',
          })
        }, 1100)
      })))
    // app.post(url,params).then((r)=>{
    //   wx.showToast({
    //     title: '发表成功',
    //     icon:'none'
    //   })
    //   setTimeout(()=>{
    //     wx.redirectTo({
    //       url: '/pages/enterprise/enterprise',
    //     })
    //   },1100)
    // })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})