approve.js 7.1 KB
// pages/my/approve/approve.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        proData: {
            company_id: '',
            name: '',
            mobile: '',
            images: [],
        },
        companyName: '',
        seach: false,
        keyword: "",
        companyList: []
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

    },

    // btn_approve() {
    //     wx.navigateTo({
    //         url: '/pages/my/approveInfo/approveInfo',
    //     })
    // },

    // 点击查询公司名称
    companyFun: function() {
        this.setData({
            seach: true
        })
    },
    delAll(){
      this.setData({
        keyword: '',
        companyList:[]
      })
    },
    // 取消搜索
    cancle(){
      this.setData({
        seach:false,
        keyword: '',
        companyList: []
      })
    },

    // 搜索公司名称
    seachFun: function() {
        let url = '/api/Company/index', b = this,params = {
            keyword: b.data.keyword
        }
      b.data.keyword ? (app.post(url, params, false).then((res) => {
        b.setData({
          companyList: res.data
        })
      })):b.setData({
          companyList: []
      })
    },

    // 公司名称同步到data
    keywordFun: function(e) {
        this.setData({
            keyword: e.detail.value.replace(/\s+/g, ''),
        })
        this.seachFun();
    },

    // 选择公司名称同步到页面
    companySelect(e) {
        this.setData({
            companyName: e.currentTarget.dataset.name,
            seach: false,
            "proData.company_id": e.currentTarget.dataset.id
        })
    },

    // 姓名同步到data
    work(e) {
        this.setData({
            "proData.name": e.detail.value.replace(/\s+/g, ''),
        })
    },

    // 联系电话同步到data
    getPhone(e) {
        this.setData({
            "proData.mobile": e.detail.value.replace(/\s+/g, ''),
        })
    },

    // 上传图片
    selectCameraFun: 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) {
                    let dat = JSON.parse(res.data);
                    that.setData({
                        "proData.images": that.data.proData.images.concat(dat.data.url)
                    })

                } else {
                    wx.showModal({
                        title: '提示',
                        content: res.msg,
                        showCancel: false
                    })
                }
                //do something 返回图片地址
            },
            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);
                }
            }

        })

    },

    // 删除图片重新上传
    delFun: function (e) {
        let that = this;
        let imgUrlList = that.data.proData.images;
        imgUrlList.splice(e.currentTarget.dataset.i, 1);
        that.setData({
            "proData.images": imgUrlList
        })
    },

    // 上传认证
    subFun() {
        let b = this;
        let reg = /^1([38]\d|5[0-35-9]|7[3678])\d{8}$/
        let url = "/api/user/setCertificationInfo";
        if(b.data.companyName == "") {
            wx.showToast({
                title: '请选择公司',
                icon: "none",
                duration: 1300
            })
            return false;
        } else if (b.data.proData.name == ""){
            wx.showToast({
              title: '请输入姓名',
              icon: "none",
              duration: 1300
            })
            return false;
        } else if (!reg.test(b.data.proData.mobile)){
            wx.showToast({
              title: '请输入正确的手机号码',
              icon: "none",
              duration: 1300
            })
            return false;
        } 
        // else if (b.data.proData.images.length == 0){
        //     wx.showToast({
        //       title: '请上传营业执照',
        //       icon: "none",
        //       duration: 1300
        //     })
        //     return false;
        // }
        else {
            b.setData({
                "proData.header": true
            })
            app.post(url, b.data.proData).then((res) => {
                // console.log(res);
                wx.redirectTo({
                    url: '../approveInfo/approveInfo',
                })
            })
        }
    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})