小程序-批量收集formid

小程序-自定义组件封装.收集formid

第一次写文章,主要是记录一下之前公司中业务的需要,封装formid的一个思路,方便自己和其他人,不多说,上代码

//index.wxml

//index.js
const common = require("../../utils/common");
let isHide = false
Page({
  data: {
    defaultHead: ''
  },
  onLoad() {
    this.fetchListData(1);
    this.setData({
      defaultHead: common.imgUrl + 'head.png'
    })
  },
  onShow() {
    if (isHide) {
      console.log("onShow")
      this.fetchListData(1);
    }
  },
  onHide() {
    isHide = true
  },
  data: {
    page: 1,
    newsList: [],
    total: 0
  },
  formSubmit(e) {
    if (e.detail.errMsg == "getUserInfo:fail auth deny") {
      return;
    }
    if (!wx.getStorageSync("userInfo")) {
      common.BindUser(e);
    }
    if (e.detail.formId) {
      common.collectFormID(e);
    }
    let type = e.detail.target ?
      e.detail.target.dataset.type - 0 :
      e.target.dataset.type - 0;

    if (type == 1) {
      wx.navigateTo({
        url: "/pages/index/modules/submitQuestion/submitQuestion"
      });
      return;
    }
    if (type == 2) {
      wx.navigateTo({
        url: "/pages/index/modules/questionDetail/questionDetail?id=" +
          e.detail.target.dataset.id
      });
      return;
    }
    if (type == 3) {
      wx.navigateTo({
        url: "/pages/search/search?type=1"
      });
      return;
    }
  },
  onReachBottom() {
    if (this.data.page * 10 >= this.data.total) {
      wx.showToast({
        title: "到底了~~",
        icon: "none"
      });
      return;
    }
    this.data.page++;
    this.fetchListData(2);
  },
  fetchListData(type) {
    let params = {
      url: "/index/community/Consult/selectConsultList.html",
      data: {
        page: this.data.page,
        limit: 10
      }
    };
    common.request(params).then(res => {
      let newList = []
      if (type == 1) {
        newList = res.data.obj
      } else {
        newList = this.data.newsList.concat(res.data.obj)
      }
      this.setData({
        newsList: newList,
        total: res.data.total
      })
      // let newsList = JSON.parse(JSON.stringify(this.data.newsList)) || [];
      // newsList = newsList.concat(res.data.obj || []);
      // this.setData({
      //   newsList,
      //   total: res.data.total
      // });
    });
  }
});
//common.js
 collectFormID(e) {
    let formID = e.detail.formId;
    // wx.setStorageSync("form_id_time");
    let formIDList = wx.getStorageSync("formIDList") || [];
    formIDList.unshift({
      form_id: formID,
      create_time: parseInt(new Date().getTime() / 1000)
    });
    if (formIDList.length > 2) {
      // return;
      let params = {
        url: "/index/common/Formid/saveFormID.html",
        data: {
          form_id: formIDList
        }
      };
      this.request(params).then(res => {
        formIDList = [];
        wx.setStorage({
          key: "formIDList",
          data: formIDList
        });
      });
      return;
    }
    wx.setStorage({
      key: "formIDList",
      data: formIDList
    });
  },

其实思路比较简单,是wxml中,以from为最大容器,所以的action都是button组件

你可能感兴趣的:(小程序)