complain.js 6.8 KB
// pages/order/complain/complain.js
const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        content:'',
        images:[],
        images_params:[]
    },
    //输入投诉内容
    inputContent(e) {
        this.setData({content:e.detail.value})
    },
    //投诉
    submit() {
        const self = this;
        if(self.data.content === '') {
            wx.showToast({title:'请填写投诉内容',icon:'none'})
        }else {
            let images_params = '';
            if(self.data.images_params.length === 0) {
                images_params = '';
            }else {
                images_params = self.data.images_params.join(',')
            }
            let url = '/portal/Order/complaint';
            let params = {
                token: wx.getStorageSync('token'),
                orderId: self.data.order_id,
                content:'',
                pic:images_params,
            };
            app.post(url, params, {}).then((res) => {
                // console.log('投诉', res);
                if (+res.code === 1) {
                    wx.showToast({title:'投诉成功',icon:'none',
                        success:function () {
                            setTimeout(function () {
                                wx.navigateBack({delta:1})
                            },500)
                        }
                    });
                }else {
                    wx.showToast({title:res.message,icon:'none'});
                }
            })
        }
    },
    //上传图片
    choose(e) { //这里是选取图片的方法
        let that = this;
        wx.chooseImage({
            count: 6, //最多可以选择的图片总数
            sizeType: ['compressed'], // 可以指定是原图还是压缩图,默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
            success: function(res) {
                // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
                let tempFilePaths = res.tempFilePaths;
                //启动上传等待中...
                wx.showToast({
                    title: '正在上传...',
                    icon: 'loading',
                    mask: true,
                    duration: 10000
                })
                // console.log('tempFilePaths', tempFilePaths);
                let token = wx.getStorageSync('token');
                let uploadImgCount = 0;
                for (let i = 0, h = tempFilePaths.length; i < h; i++) {
                    wx.uploadFile({
                        url: 'https://wmatchrd.com/portal/Index/upload',
                        filePath: tempFilePaths[i],
                        name: 'file',
                        formData: {
                            'imgIndex': i
                        },
                        header: {
                            'content-type': 'application/x-www-form-urlencoded',
                            'XX-Token': token,
                            'XX-Device-Type': 'wxapp',
                        },
                        success: function(res) {
                            uploadImgCount++;
                            let data = JSON.parse(res.data);
                            // console.log('data',data);
                            if (+data.code == 1) {
                                //服务器返回格式: { "Catalog": "testFolder", "FileName": "1.jpg", "Url": "https://test.com/1.jpg" }
                                let images = that.data.images;
                                let images_params = that.data.images_params;
                                images.push(data.data.preview_url);
                                images_params.push(data.data.filepath);
                                that.setData({
                                    images: images,
                                    images_params:images_params,
                                });
                                // console.log('images---images_params',images,images_params);
                                //如果是最后一张,则隐藏等待中
                                // if (uploadImgCount == tempFilePaths.length) {
                                //     wx.hideToast();
                                // }
                                wx.hideToast();
                            } else {
                                wx.hideToast();
                                wx.showModal({
                                    title: '提示',
                                    content: data.msg,
                                    showCancel: false
                                })
                            }

                        },
                        fail: function(res) {
                            wx.hideToast();
                            wx.showModal({
                                title: '错误提示',
                                content: '上传图片失败',
                                showCancel: false,
                                success: function(res) {}
                            })
                        }
                    });
                }
            }
        });
    },
    // 删除图片
    deleteImg(e) {
        var imgs = this.data.images;
        var index = e.currentTarget.dataset.index;
        imgs.splice(index, 1);
        this.setData({
            images: imgs
        });
    },

    // 预览图片
    previewImg: function (e) {
        //获取当前图片的下标
        var index = e.currentTarget.dataset.index;
        //所有图片
        var imgs = this.data.images;
        wx.previewImage({
            //当前显示图片
            current: imgs[index],
            //所有图片
            urls: imgs
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // console.log('options', options,JSON.parse(options.order_detail));
        this.setData({order_id:+options.id?+options.id:'',
            order_detail:JSON.parse(options.order_detail),
        })
    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    },

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

    }
})