contact-table.js 5.05 KB
import {
  getBindtapData,
  checkMobile
} from '../../utils/util';

let app = getApp();
Page({
  data: {
    authorizeVisible: false,
    commonTipsCompVisible: false,
    innerTitle: "留言提交成功!",
    innerText: "感谢您宝贵的意见\n我们将尽快回电或回复信息",
    maxImg: 9, // 上传数量
    files: [], // 上传文件列表
    name: "",
    phone: "",
    messageContant: "", // 单词拼写错误


  },
  onShareAppMessage() {},
  showAuth() {
    this.setData({
      authorizeVisible: true
    })
  },
  onLoad(options) {
    this.initData();
  },
  initData() {
    // this.setData({
    //   commonTipsCompVisible: false
    // })
  },
  /**
   * 提交表单
   */
  onSubmitHandler() {
    let {
      name,
      phone,
      messageContant,
      files
    } = this.data;

    let pics = [];
    files.forEach(element => {
      pics.push(element.path)
    });
    // 校验
    if (!name) {
      wx.showToast({
        title: "请输入用户姓名",
        icon: 'none'
      })
      return;
    }
    if (!phone) {
      wx.showToast({
        title: "请输入联系方式",
        icon: 'none'
      })
      return;
    }
    if (!checkMobile(phone)) {
      wx.showToast({
        title: "请输入正确联系方式",
        icon: 'none'
      })
      return;
    }
    if (!messageContant) {
      wx.showToast({
        title: "请输入联系方式",
        icon: 'none'
      })
      return;
    }
    if (pics.length <= 0) {
      wx.showToast({
        title: "请上传图片",
        icon: 'none'
      })
      return;
    }


    // 上传图片到服务器
    this.uploadToCustomService(pics).then((result) => {
      // 提交表单
      app.post({
        url: app.api.messageSave,
        data: {
          name: name,
          phone: phone,
          messageContant: messageContant,
          images: result.join(','),
        }
      }).then((result2) => {
        this.setData({
          commonTipsCompVisible: true
        })
      });
    })
  },

  /**
   * 上传到自定义服务器
   * urlList 需要上传的图片地址
   */
  uploadToCustomService(urlList) {
    let _this = this;
    return new Promise((resolve, reject) => {
      // promise列表
      let p = [];
      // 用promise上传音频
      urlList.forEach(element => {
        let myPromise = new Promise((resolve2, reject2) => {
          _this.uploadfileMultiple(element).then((result2) => {
            resolve2(result2)
          }).catch((err) => {
            // _this.tip("声音上传失败")
            reject2();
          });
        });
        p.push(myPromise);
      });
      Promise.all(p).then(uploadFiles => {
        resolve(uploadFiles)
      }, reason => {
        reject();
      });
    });
  },
  /**
   * 多文件上传
   * @param {*} filePath
   */
  uploadfileMultiple(filePath) {
    let _this = this;
    return new Promise((resolve, reject) => {
      wx.uploadFile({
        url: app.config.NET_CONFIG.commonApi + app.api.uploadFile,
        filePath: filePath,
        name: 'file',
        // header: {}, // 设置请求的 header
        header: {
          'content-type': 'multipart/form-data'
        },
        formData: {
          path: '/weapp/zhiliang-light/'
        },
        // HTTP 请求中其他额外的 form data
        success(res) {
          let result = JSON.parse(res.data).content;
          resolve(result);
        },
        fail() {
          reject()
        },
        complete: function () {}
      })
    });
  },


  /**
   * 调起微信图片上传
   */
  onUploadHandler() {
    let _this = this;
    let count = _this.data.maxImg - _this.data.length;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: ['album'],
      count: count,
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFiles = res.tempFiles
        let maxImg = _this.data.maxImg;
        let files = _this.data.files;
        files = files.concat(tempFiles);
        if (files.length > maxImg) {
          files.splice(0, maxImg);
        }
        _this.setData({
          files
        });
      }
    })
  },
  /**
   * 删除选中图片
   */
  remove(evt) {
    let index = getBindtapData(evt, "index");
    let files = this.data.files;
    files.splice(index, 1);
    this.setData({
      files
    })
  },
  /**
   * 绑定键盘输入
   */
  bindNameInput(e) {
    this.setData({
      name: e.detail.value
    });
  },
  /**
   * 绑定键盘输入
   */
  bindPhoneInput(e) {
    this.setData({
      phone: e.detail.value
    });
  },
  /**
   * 绑定键盘输入
   */
  bindMessageContantInput(e) {
    this.setData({
      messageContant: e.detail.value
    });
  },
  // 隐藏蒙层
  hideMask() {
    this.setData({
      commonTipsCompVisible: false
    })
  },
  // 子组件事件
  evtcomp(evt) {
    let {
      name,
      data
    } = evt.detail;
    switch (name) {

      case "_evt_common_comp_button":
        this.hideMask();
        wx.navigateBack({
          delta: 1
        });
        break;

      default:
        break;
    }
  },
})