boutiqueComment.js 7.63 KB
// packagePersonal/pages/boutique/boutique.js
const filter = require('../../../utils/filter.js');
import api from '../../restapi'
import util from '../../../utils/util.js'
const app = getApp();

Page(filter.identityFilter({

  /**
   * 页面的初始数据
   */
  data: {
    initFlag: true,
    showFlag: false,
    orderCode: '',
    batchCode: '',
    commentOrNot: false, // 是否评价过
    score: 0,
    commentResult: '',
    orderItem: [],
    getTotalPrice: '',
    label: [
      {
        text: '服务态度好'
        // active: false
      }, 
      {
        text: '性价比高'
        // active: false
      }, 
      {
        text: '价格便宜'
        // active: false
      }
    ],
    commentVal: '',
    errMsg: ''
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onLoad: function (options) {
    this.initData(options);
    console.log('boutiqueComment onload', options);
  },
  onShow() {
    // 解决二次扫码数据缓存问题
    if(this.data.initFlag) {
      let pages = getCurrentPages(),
          currentPage = pages[pages.length-1],
          options = currentPage.options,
          appSceneData = app.globalData.sceneData,
          hasScene = options.scene ? options.scene : null;
      if(appSceneData){
        options.mainOrderCode = appSceneData.query.mainOrderCode;
        options.batchCode = appSceneData.query.batchCode;
      }

      if(hasScene && options.mainOrderCode) {
        options.fromMenu = false;
        this.initData(options);
        console.log('boutiqueComment onshow', options);
      }
    }
  },
  initData(options) {
    /** 三种情况:

     *  1、orderCode从正式版本扫码过来的scene获取,且还需验证手机号是否一致
     * 2、orderCode从体验版的query获取
     * 3、点击菜单栏status为1时直接过来*/
    if(this.data.initFlag) {
      this.setData({
        initFlag: false
      })

      const needRefresh = wx.getStorageSync('needRefresh');
      let orderCode = '',
          batchCode = '',
          scene = '',
          query = '',
          fromMenu = options.fromMenu;
      wx.showLoading({title: '加载中'});
      if(!needRefresh) {
        // 若用户未登录要缓存场景值, 解决跳转登录页面场景值丢失的问题
        orderCode = options.mainOrderCode;
        batchCode = options.batchCode;
        scene = options.scene;
        query = options.query;
        options.sceneUrl = getCurrentPages()[0].route;
        wx.setStorageSync('needRefresh', options);
      } else {
        // 登录后回来直接拿缓存的值(此时options为空),但排除登录以外的情况因此要优先拿options的值
        const getOptionsType = options.mainOrderCode ? options : needRefresh;
        orderCode = getOptionsType.mainOrderCode;
        batchCode = getOptionsType.batchCode;
        scene = getOptionsType.scene;
        query = getOptionsType.query;
        wx.removeStorageSync('needRefresh');
      }
  
      if(scene) {
        this.setData({
          orderCode,
          batchCode
        })
  
      } else if(query) {
        orderCode = query.mainOrderCode;
        batchCode = query.batchCode;
        fromMenu = query.fromMenu;
  
        this.setData({
          orderCode,
          batchCode
        })
      }
      
      if(fromMenu) {
        let data = wx.getStorageSync('fromMenuData');
        this.render(data, true);
      } else {
        this.getAllOrderList();
      }
  
      this.getLabel();
    }
  },
  getAllOrderList() {
    // 二次扫码获取订单详情
    let { orderCode, batchCode } = this.data;
    // 测试数据:{orderCode: '2007071702209842', batchCode : '08320070910592739'};

    if(orderCode && batchCode) {
      api.getBoutiqueSecondScanData({ orderCode, batchCode }, res => {
        this.render(res, false);
      }, error => {
        util.showToast('网络连接错误');
        console.log(error, 'secondScan request error');
      })
    } else {
      wx.hideLoading();
      this.setData({
        errMsg: '暂无精品交付单~'
      })
    }
  },
  render(res, fromMenu) {
    let resData = fromMenu ? res.data.order : res.data,
        { returnCode } = res,
        getTotalPrice = 0;
    if(resData && returnCode == '0' ) {
      let { subOrderList: orderItem, status } = resData,
          commentOrNot = status == 1 ? false : true; // status:1未评价,其余评价过
          
      wx.hideLoading();
      if(orderItem && orderItem.length > 0) {
        orderItem.forEach(item => getTotalPrice += item.totalPrice);
      }

      let setDataRes = {
        showFlag: true,
        orderItem,
        getTotalPrice: getTotalPrice.toFixed(2),
        commentOrNot
      }

      if(commentOrNot) {
        // 评价过
        let { customerComment: commentResult, customerScore: score} = resData;
        setDataRes['commentResult'] = commentResult;
        setDataRes['score'] = score;
      }

      this.setData( setDataRes );

    } else {
      let { message } = res;
      console.log('error message', message);
      wx.redirectTo({
        url: `/packagePersonal/pages/error/error?msg=${message}`
      })
    }
  },
  getLabel() {
    // 获取标签数据
    api.getBoutiqueCommentLabel({}, res => {
      let { returnCode } = res,
          resData = res.data;

      if(resData && returnCode == '0') {
        let label = [];
        resData.forEach(item => label.push({text: item.confName}));
        this.setData({ label });
      }
    }, error => {
      console.log(error, 'getCommentLabelList request error');
    })
  },
  commentInput(e) {
    // 同步textarea内容数据
    let { value } = e.detail;
    this.setData({
      commentVal: value
    })
  },
  syncScore(e) {
    let { score } = e.detail;
    this.setData({
      score
    })
  },
  tapLabel(e) {
    // 点击标签添加内容到输入框
    let index = e.target.dataset.index,
        { label, commentVal } = this.data;
        // labelItem = `label[${index}].active`,
        // result = label[index]['active'] ? false : true;
        commentVal += label[index].text + ', ';

    this.setData({
      commentVal
      // [labelItem]: result
    })
    
  },
  haveEmoji(val) {
    // 过滤表情包
    const emojiReg = /([^\u0020-\u007E\u00A0-\u00BE\u2E80-\uA4CF\uF900-\uFAFF\uFE30-\uFE4F\uFF00-\uFFEF\u0080-\u009F\u2000-\u201f\u2026\u2022\u20ac])/g;

    return emojiReg.test(val);
  },
  confirm() {
    // 点击确认提交
    let { batchCode, commentVal: customerComment, score: customerScore } = this.data;
    let data = {
      batchCode,
      customerComment,
      customerScore
    };

    
    if(customerScore === 0) {
      // 未评星级弹出提示框
      util.showToast('请对本次服务进行打分');
      return false;
    }

    if (this.haveEmoji(customerComment)) {
      data.customerComment = util.replaceEmoji(customerComment);
    }
    
    api.sendBoutiqueCommentResult(data, res => {
      let { returnCode } = res;

      if(returnCode == '0') {
        let { commentVal } = this.data;
        util.showToast('提交成功');
        this.setData({
          score: customerScore,
          commentOrNot: true,
          commentResult: commentVal
        })
        // 提交成功后精细化小程序还需要进行消息推送
      } else {
        console.log(res.message);
        util.showToast('提交错误');
      }
    }, error => {
      console.log(error, 'confirmUpgrade request error');
    })
  },
  onLogin() {

  }
  
}))