add_comment.js 5.45 KB
var t = getApp(), oo = t.globalData, a = t.globalData.setting, os = a, e = t.request, s = require("../../../utils/util.js"),
  i = require("../../../utils/selectFiles.js");
var regeneratorRuntime = require('../../../utils/runtime.js');

Page({
  data: {
    url: a.url,
    resourceUrl: a.resourceUrl,
    iurl: a.imghost,
    defaultPhoto: a.resourceUrl + "/static/images/camera.png",
    filePaths: [],
    uploadPath: [],
    maxWord: 0,
    is_anonymous: !1,
    goods_rank: 0,
    service_rank: 0,
    deliver_rank: 0,
    content: "",
    options: null,
    isLongPress: !1,
    userinfo: null,
    imgs: [],
    lenMore:0
  },
  onLoad: async function (a) {
    var th = this;
    console.log('onLoad');
    console.log(a);

    await getApp().request.promiseGet("/api/weshop/goods/get/" + os.stoid + "/" + a.goods_id, { 1: 1 }).then(res => {
      a.img = th.data.iurl + res.data.data.original_img
    })


    this.setData({ options: a });
    t.getUserInfo(function (e) {
      console.log(e);
      th.setData({
        userinfo: e
      })
    })
  },
  keyUpChangeNum: function (t) {
    this.setData({
      maxWord: t.detail.value.length
    }), this.data.content = t.detail.value;
  },
  checkAnonymous: function () {
    this.setData({
      is_anonymous: !this.data.is_anonymous
    });
  },
  checkGoodsRank: function (t) {
    this.setData({
      goods_rank: parseInt(t.currentTarget.dataset.i) + 1
    });
  },
  checkServiceRank: function (t) {
    this.setData({
      service_rank: parseInt(t.currentTarget.dataset.i) + 1
    });
  },
  checkDeliverRank: function (t) {
    this.setData({
      deliver_rank: parseInt(t.currentTarget.dataset.i) + 1
    });
  },
  selectPhotos: function (t) {
    if (this.data.isLongPress) this.data.isLongPress = !1; else {
      var a = this;
      i.selectPhotos(this.data.filePaths, t.currentTarget.dataset.idx, function (t) {
        a.setData({
          filePaths: t
        });
      });
    }
  },
  removePhoto: function (t) {
    var a = this;
    this.data.isLongPress = !0, i.removePhoto(this.data.filePaths, t.currentTarget.dataset.idx, function (t) {
      a.setData({
        filePaths: t
      });
    });
  },
  submitComment: function () {
    var a = this;
    if (!a.data.service_rank || !a.data.goods_rank || !a.data.deliver_rank) return t.showWarning("请先打完分~");
    this.uploadPhotos(function () {
      e.post("/api/weshop/comment/savecomment", {
        data: {
          email: ' ',
          is_show: 1,
          goods_rank: a.data.goods_rank,
          service_rank: a.data.service_rank,
          deliver_rank: a.data.deliver_rank,
          content: a.data.content,
          goods_id: a.data.options.goods_id,
          order_id: a.data.options.order_id,
          user_id: oo.user_id,
          store_id: os.stoid,
          username: a.data.userinfo.nickname
        },
        success: function (a) {
          wx.setStorageSync("user:comment:update", !0), t.showSuccess("评论成功", function () {
            wx.navigateBack();
          });
        }
      });
    });
  },
  uploadPhotos: function (t, a) {
    if (0 == this.data.filePaths.length) return t();
    if ("number" != typeof a) a = 0; else if (a >= this.data.filePaths.length) return t();
    var s = this;
    e.uploadFile("/api/user/upload_comment_img", {
      filePath: s.data.filePaths[a],
      name: "img_file",
      success: function (e) {
        s.data.uploadPath[a] = e.data.result, s.uploadPhotos(t, a + 1);
      }
    });
  },

  //图片失败,默认图片
  bind_bnerr2: function (e) {
    var _errImg = e.target.dataset.errorimg;
    var _errObj = {};
    _errObj[_errImg] = a.imghost + "/public/images/default_goods_image_240.gif";
    this.setData(_errObj) //注意这里的赋值方式,只是将数据列表中的此项图片路径值替换掉 ;  
  },
  // 上传图片
  chooseImg: function (e) {
    var that = this;
    var imgs = this.data.imgs;
    if (imgs.length >= 3) {
      this.setData({
        lenMore: 1
      });
      setTimeout(function () {
        that.setData({
          lenMore: 0
        });
      }, 2500);
      return false;
    }
    wx.chooseImage({
      // count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        var imgs = that.data.imgs;
        // console.log(tempFilePaths + '----');
        for (var i = 0; i < tempFilePaths.length; i++) {
          if (imgs.length >= 3) {
            that.setData({
              imgs: imgs
            });
            return false;
          } else {
            imgs.push(tempFilePaths[i]);
          }
        }
        // console.log(imgs);
        that.setData({
          imgs: imgs
        });
      }
    });
  },
  // 删除图片
  deleteImg: function (e) {
    var imgs = this.data.imgs;
    var index = e.currentTarget.dataset.index;
    imgs.splice(index, 1);
    this.setData({
      imgs: imgs
    });
  },
  // 预览图片
  previewImg: function (e) {
    //获取当前图片的下标
    var index = e.currentTarget.dataset.index;
    //所有图片
    var imgs = this.data.imgs;
    wx.previewImage({
      //当前显示图片
      current: imgs[index],
      //所有图片
      urls: imgs
    })
  },

  
});