comment.js 15.2 KB
// pages/my/comment/comment.js
const app = getApp();
const qiniuUploader = require("../../../utils/qiniuUploader");
Page({

        /**
         * 页面的初始数据
         */
        data: {
                flagId: 9,
                id: 0,
                gid: 1122,
                imgUrl: app.globalData.imgUrl,
                photoPath: '',
                arr: [{
                        id: 0
                }, {
                        id: 1
                }, {
                        id: 2
                }, {
                        id: 3
                }, {
                        id: 4
                }],
                arr1: [{
                        id: 0
                }, {
                        id: 1
                }, {
                        id: 2
                }, {
                        id: 3
                }, {
                        id: 4
                }],
                test: '',
                length: 0,
                imgBox: [],
                imgUrls: [],
                logistics_stars: 0,
                text: "",
                index: 0,
                orderList: [],
                imgURL: [],
                token: '',
                imageURL: '',
                key: '',
                goods: {}
        },
        //返回
        get_back() {
                wx.navigateBack({})
        },
        //点亮星星
        changeStarId(e) {
                let flag = e.currentTarget.dataset.flag
                let index = e.currentTarget.dataset.index * 1
                // let indexs=index*1+1
                let new_arr = this.data.arr
                for (let i = 0; i < new_arr.length; i++) {
                        new_arr[i].flag = false;
                }
                for (let i = 0; i < index; i++) {
                        new_arr[i].flag = true;
                }
                this.setData({
                        arr: new_arr,
                        index: index
                });
        },
        changeStarIds(e) {
                let flag = e.currentTarget.dataset.flag
                let index = e.currentTarget.dataset.index
                let new_arr = this.data.arr1
                for (let i = 0; i < new_arr.length; i++) {
                        new_arr[i].flag = false;
                }
                for (let i = 0; i < index; i++) {
                        new_arr[i].flag = true;
                }
                console.log(index)
                this.setData({
                        arr1: new_arr,
                        logistics_stars: index * 1
                });
        },
        //点赞
        changeSupport(e) {
                let gid = e.currentTarget.dataset.gid
                let list = this.data.orderList
                list.forEach(function (ele, index) {
                        if (gid == ele.id) {
                                ele.flag1 = true
                                ele.flag2 = false
                        }
                })
                this.setData({
                        gid: gid,
                        orderList: list
                });
        },
        //踩
        changeSupport2(e) {
                let gid = e.currentTarget.dataset.gid
                let list = this.data.orderList
                list.forEach(function (ele, index) {
                        if (gid == ele.id) {
                                ele.flag2 = true
                                ele.flag1 = false
                        }
                })
                this.setData({
                        gid: gid,
                        orderList: list
                });
        },
        //限制字数
        limit(e) {
                let value = e.detail.value;
                var length = parseInt(value.length);
                this.setData({
                        length: length,
                        text: value
                });
        },

        //点击增加图片
        changePhoto: function () {
                var that = this;
                var img_arr = []
                // 选择图片
                wx.chooseImage({
                        count: 9,
                        sizeType: ['original', 'compressed'],
                        sourceType: ['album', 'camera'],
                        success: function (res) {
                                console.log(res) 
                                const filePath = res.tempFilePaths;
                                // var filePath = res.tempFilePaths[0];
                                img_arr=filePath
                                let arr = [];
                                img_arr = that.data.imgBox.concat(img_arr)
                                that.setData({
                                        imgBox: img_arr
                                });
                                // var filePath = res.tempFilePaths[0];
                                // 交给七牛上传
                                qiniuUploader.upload(filePath, (res) => {
                                        // 每个文件上传成功后,处理相关的事情
                                        // 其中 info 是文件上传成功后,服务端返回的json,形式如
                                        // {
                                        //    "hash": "Fh8xVqod2MQ1mocfI4S4KpRL6D98",
                                        //    "key": "gogopher.jpg"
                                        //  }
                                        // 参考http://developer.qiniu.com/docs/v6/api/overview/up/response/simple-response.html
                                        var img_urls = that.data.imgUrls;

                                        let aa = 'http://qiniu.himrhi.com/' + res.key
                                        img_urls.push(aa);
                                        let imgStr = img_urls.join(',')
                                        console.log(imgStr)
                                        that.setData({
                                                'imageURL': res.imageURL,
                                                key: imgStr,
                                                imgBox: img_arr,
                                                imgUrls: img_urls
                                        });

                                }, (error) => {
                                        console.log('error: ' + error);
                                }, {
                                                region: 'ECN',
                                                domain: 'qiniu.himrhi.com', // // bucket 域名,下载资源时用到。如果设置,会在 success callback 的 res 参数加上可以直接使用的 ImageURL 字段。否则需要自己拼接
                                                // key: 'customFileName.jpg', // [非必须]自定义文件 key。如果不设置,默认为使用微信小程序 API 的临时文件名
                                                // 以下方法三选一即可,优先级为:uptoken > uptokenURL > uptokenFunc
                                                // uptoken: 'JMgHPK8vEdZfUfxZIW8qIsyucRCZeTZ4aqHLWsjI:9VjchzcKJTa9uqK_0MWELzc4_zg=:eyJzY29wZSI6ImhpLWZyZXNoIiwiZGVhZGxpbmUiOjE1NjA3MzU3ODF9', // 由其他程序生成七牛 uptoken
                                                uptokenURL: 'http://www.himrhi.com/api/wxapp/public/getQiniuToken', // 从指定 url 通过 HTTP GET 获取 uptoken,返回的格式必须是 json 且包含 uptoken 字段,例如: {"uptoken": "[yourTokenString]"}
                                                uptokenFunc: function () {
                                                        return 'JMgHPK8vEdZfUfxZIW8qIsyucRCZeTZ4aqHLWsjI:9VjchzcKJTa9uqK_0MWELzc4_zg=:eyJzY29wZSI6ImhpLWZyZXNoIiwiZGVhZGxpbmUiOjE1NjA3MzU3ODF9';
                                                }
                                        }, (res) => {
                                                console.log('上传进度', res.progress)
                                                console.log('已经上传的数据长度', res.totalBytesSent)
                                                console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)
                                        }, () => {
                                                // 取消上传
                                        }, () => {
                                                // `before` 上传前执行的操作
                                        }, (err) => {
                                                // `complete` 上传接受后执行的操作(无论成功还是失败都执行)
                                        });
                        }
                })
        },
        //点击删除图片
        deleteImg(e) {
                let val = e.currentTarget.dataset.index
                let imgUrls = this.data.imgUrls
                let imgBox = this.data.imgBox
                let indexs = imgBox.indexOf(val)
                imgUrls.splice(indexs, 1)
                imgBox.splice(indexs, 1)
                this.setData({
                        imgUrls: imgUrls,
                        imgBox: imgBox
                })
        },

        //点击提交评论
        submitComment() {
                let url = '/wxapp/order/setComment'
                let goods = this.data.goods
                let gid = this.data.gid.toFixed(0)
                let list = this.data.orderList
                console.log(gid);
                list.forEach(function (ele, index) {
                        let i = ele.id
                        if (ele.flag1) {
                                goods[i] = 0
                        }
                        if (ele.flag2) {
                                goods[i] = 1
                        }
                })
                console.log(goods)
                goods = JSON.stringify(goods);
                console.log(goods)
                if (this.data.index == 0) {
                        wx.showToast({
                                title: '请对商品做评价',
                                icon: 'none'
                        })
                        return;
                }
                if (this.data.logistics_stars == 0) {
                        wx.showToast({
                                title: '请对物流做评价',
                                icon: 'none'
                        })
                        return;
                }
                for (var v in goods) {
                        if (goods[v] == '') {
                                wx.showToast({
                                        title: '请对菜品做评价',
                                        icon: 'none'
                                })
                                return;
                        }
                }
                if (this.data.id == 0) {
                        wx.showToast({
                                title: '订单错误',
                                icon: 'none'
                        })
                        return;
                }
                if (this.data.text == '') {
                        wx.showToast({
                                title: '评价内容不能为空',
                                icon: 'none'
                        })
                        return;
                }
                if (this.data.key == '') {
                        wx.showToast({
                                title: '请上传图片',
                                icon: 'none'
                        })
                        return;
                }
                let params = {
                        object_id: this.data.id,
                        goods_stars: this.data.index,
                        logistics_stars: this.data.logistics_stars,
                        goods: goods,
                        content: this.data.text,
                        images: this.data.key
                }
                app.post(url, params).then(r => {
                        console.log('上传')
                        console.log(r)
                        if (r.code == 1) {
                                wx.showToast({
                                        title: '评价成功',
                                        duration: 1000
                                })
                                wx.navigateTo({
                                        url: '../myOrder/myOrder?status=' + 5,
                                })
                                this.fetchOrderDetail();
                        }
                });
        },

        //渲染订单详情
        fetchOrderDetail() {
                let url = '/wxapp/order/detail'
                let goods = {}
                let params = {
                        id: this.data.id
                }
                app.post(url, params).then(r => {
                        console.log(r)
                        r.data.detail.forEach(function (ele, index) {
                                let i = ele.id
                                goods[i] = ''
                                ele.flag1 = true
                                ele.flag2 = false
                        })
                        this.setData({
                                orderList: r.data.detail,
                                goods: goods
                        })
                })
        },

        /**
         * 生命周期函数--监听页面加载
         */
        onLoad: function (options) {
                this.setData({
                        id: options.id
                });
                this.fetchOrderDetail();
        },

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

        },

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

        },

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

        },

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

        },

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

        },

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

        },

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

        }






        //        uploadURLFromRegionCode(code) {
        //                 var uploadURL = null;
        //                 switch(code) { 
        //                         case 'ECN': uploadURL = 'https://up.qiniup.com'; break;
        //                         case 'NCN': uploadURL = 'https://up-z1.qiniup.com'; break;
        //                         case 'SCN': uploadURL = 'https://up-z2.qiniup.com'; break;
        //                         case 'NA': uploadURL = 'https://up-na0.qiniup.com'; break;
        //                         case 'ASG': uploadURL = 'https://up-as0.qiniup.com'; break;
        //                         default: console.error('please make the region is with one of [ECN, SCN, NCN, NA, ASG]');
        //         } 
        //                 return uploadURL; 
        //         } ,
})