Blame view

public/js/service/chat/view.js 9.76 KB
李奇 authored
1
const tip = require('plugin/tip');
zzzzzzz authored
2
郝肖肖 authored
3
require('common');
zzzzzzz authored
4
陈轩 authored
5
import {EventEmitter, api} from './store';
陈轩 authored
6
import appBridge from 'yoho-app';
陈轩 authored
7 8 9 10


const LeaveMSGView = function(elem) {
    this.$elem = $(elem);
陈轩 authored
11
    this.$input = this.$elem.find('.leave-text');
陈轩 authored
12 13
    this.$doc = $(document);
    this.bindEvents();
陈轩 authored
14
    this.isSending = false;
陈轩 authored
15 16
};
陈轩 authored
17
LeaveMSGView.prototype = $.extend({}, EventEmitter.prototype, {
陈轩 authored
18 19 20 21 22 23 24 25
     /**
     * 绑定事件
     * 1. 提交按钮事件
     **/
    bindEvents() {
        let self = this;

        this.$elem
陈轩 authored
26 27
            .on('click', '.chat-page-back', function(event) {
                event.stopPropagation();
陈轩 authored
28
                self.toggleHide(true);
陈轩 authored
29 30
            })
            .on('click.LeavseMSG.submit', '.leave-submit', function() {
陈轩 authored
31
                event.stopPropagation();
陈轩 authored
32 33 34
                self.submit();
            });
陈轩 authored
35
        this.on('show.LeaveMSGView', $.proxy(this.toggleHide, this, false));
陈轩 authored
36 37 38 39 40 41
    },

    /**
    * method: show/hide 留言
    * @param boolean show;
    */
陈轩 authored
42
    toggleHide(willHide) {
陈轩 authored
43 44
        this.$elem.toggleClass('chat-page-hide', willHide);
陈轩 authored
45
        return false;
陈轩 authored
46
    },
47
陈轩 authored
48 49 50 51
    /**
     * 提交留言的hanlder
    */
    submit() {
陈轩 authored
52 53 54 55 56 57 58
        if (this.isSending) {
            tip.show('正在发送..');
            return;
        }

        this.isSending = true;
陈轩 authored
59
        let self = this;
陈轩 authored
60 61 62 63 64
        let content = $.trim(this.$input.val());

        if (!content) {
            return;
        }
陈轩 authored
65
陈轩 authored
66 67
        api.leaveMsg(content)
            .done(function() {
李奇 authored
68
                self.trigger('save.LeaveMSGView', '留言成功');
陈轩 authored
69
                self.$input.val('');
陈轩 authored
70 71 72
            })
            .fail(function() {
                self.trigger('save.LeaveMSGView', '留言失败');
陈轩 authored
73 74 75
            })
            .always(function() {
                self.isSending = false;
陈轩 authored
76 77 78
            });

        self.toggleHide(true);
79
    }
陈轩 authored
80 81 82
});

LeaveMSGView.prototype.constructer = LeaveMSGView;
83
陈轩 authored
84 85 86 87

/*
    评价view
*/
李奇 authored
88
const RatingView = function(elem, cmEntity) {
陈轩 authored
89 90 91
    this.elem = $(elem);
    this.$ranks = this.elem.find('.stars i');
    this.$label = this.elem.find('.rank');
李奇 authored
92 93
    this.$more = this.elem.find('.more-comment');
    this.cmEntity = cmEntity;
陈轩 authored
94 95
    this.rank = 3;
    this.bindEvents();
陈轩 authored
96
    this.isSending = false;
李奇 authored
97
    this.reasonsRendered = false;
陈轩 authored
98
};
zzzzzzz authored
99
陈轩 authored
100 101 102 103
RatingView.prototype = $.extend({}, EventEmitter.prototype, {

    bindEvents() {
        const self = this;
zzzzzzz authored
104
陈轩 authored
105 106
        this.elem
            .on('click.RatingView.rating', '.stars i', function(event) {
陈轩 authored
107 108
                event.stopPropagation();
陈轩 authored
109 110 111
                let starVal = $(this).index();

                self.rating(starVal);
陈轩 authored
112
            })
李奇 authored
113 114 115 116 117 118 119 120 121 122 123 124 125
            .on('click.RatingView.close', '.close', $.proxy(this.toggle, this, false))
            .on('moreComment.toggle', '.more-comment', function(event, starVal) {
                let $this = $(this);
                let visible = $this.is(':visible');

                if (starVal < 3 && visible) {
                    return;
                }

                $this.find('.words-to-say').val('');
                $this.find('.cause span').removeClass('select');

                if (starVal < 3) {
126
                    return self.renderReasons();
李奇 authored
127 128 129 130 131 132 133 134 135 136 137
                }

                $this.hide();
            })
            .on('click.CauseItem.SelectToggle', '.cause span', function() {
                let $this = $(this);

                $this.toggleClass('select');
            });
    },
138 139 140
    renderReasons() {
        const self = this;
        const YOHO_CS = 2; // 1:企业端 2:YOHO在线客服 3:BLK在线客服 4:MARS在线客服 5:机器人
李奇 authored
141 142 143 144 145 146 147 148

        if (self.reasonsRendered) {
            self.$more.show();
            return;
        }

        // 评价原因渲染
        api.queryReasons({
149
            type: YOHO_CS
李奇 authored
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
        })
        .done(res => {
            if (res && res.code === 200) {
                let $html;
                let cause = [];

                res.data.forEach((item) => {
                    cause.push(`<span data-id="${item.id}">${item.content}</span>`);
                });

                $html = cause.join('');
                self.$more.find('.cause').append($html);
                self.reasonsRendered = true;
                self.$more.show();
                return;
            }

            tip.show('拉取评价原因失败');
        })
        .fail(()=> {
            tip.show('拉取评价原因失败');
        });
陈轩 authored
172 173
    },
陈轩 authored
174
    rating(starVal) {
陈轩 authored
175
        const rankMap = {
zzzzzzz authored
176 177 178 179 180 181 182
            0: '非常不满意',
            1: '不满意',
            2: '一般',
            3: '满意',
            4: '非常满意',
        };
陈轩 authored
183 184 185
        this.rank = starVal;
        this.rankText = rankMap[starVal] || rankMap[3];
        this.$label.text(this.rankText);
陈轩 authored
186
zzzzzzz authored
187
        this.$ranks.removeClass('rated');
陈轩 authored
188
        this.$ranks.toggleClass(index => {
陈轩 authored
189
            return index <= starVal ? 'rated' : null;
zzzzzzz authored
190
        });
李奇 authored
191 192 193

        // added for customer service v1.2 optimization
        this.$more.trigger('moreComment.toggle', starVal);
陈轩 authored
194
    },
zzzzzzz authored
195 196

    post(data) {
李奇 authored
197 198
        var params;
陈轩 authored
199 200 201 202 203 204
        if (this.isSending) {
            tip.show('正在发送..');
            return;
        }
        this.isSending = true;
zzzzzzz authored
205 206
        const self = this,
            elem = this.elem;
李奇 authored
207 208

        params = {
zzzzzzz authored
209
            stars: ++this.rank,
zzzzzzz authored
210
            promoter: 1,
李奇 authored
211
            reasonIds: '',
李奇 authored
212
            reasonMsg: elem.find('.words-to-say').val().trim(),
zzzzzzz authored
213 214
        };
李奇 authored
215 216 217 218 219 220 221 222
        let temp = [];
        let $select = self.$more.find('.cause span.select');

        [].forEach.call($select, (item) => {
            temp.push($(item).data('id'));
        });
        params.reasonIds = temp.join(':');
zzzzzzz authored
223
        Object.assign(params, data);
陈轩 authored
224 225 226

        api.saveEvalute(params)
            .done(res => {
zzzzzzz authored
227
                if (res && res.code === 200) {
zzzzzzz authored
228 229
                    elem.hide();
                    elem.trigger('rating-success', [self.rankText]);
陈轩 authored
230 231

                    return;
zzzzzzz authored
232
                }
陈轩 authored
233
陈轩 authored
234 235 236 237
                tip.show('评价失败');
            })
            .fail(()=> {
                tip.show('评价失败');
陈轩 authored
238 239 240
            })
            .always(() => {
                self.isSending = false;
陈轩 authored
241
            });
陈轩 authored
242
    },
zzzzzzz authored
243
陈轩 authored
244
    toggle(willShow) {
陈轩 authored
245 246 247
        if (willShow === this.elem.is(':visible')) {
            return;
        }
陈轩 authored
248
        this.elem.toggle(willShow);
249
        this.rating(4);
陈轩 authored
250 251

        return false;
zzzzzzz authored
252
    }
陈轩 authored
253 254 255
});

RatingView.prototype.constructer = RatingView;
陈轩 authored
256
陈轩 authored
257 258 259 260 261 262 263
const OrderListView = function(elem) {
    this.$elem = $(elem);
    this.orderListT = require('service/chat/orders.hbs');

    this.$localOrders = this.$elem.find('[data-tab=local]');
    this.$globalOrders = this.$elem.find('[data-tab=global]');
陈轩 authored
264 265 266 267 268 269 270 271 272 273 274 275
    // 分页数据
    this.state = {
        global: {
            curCount: 0,
            totalCount: null
        },
        order: {
            curCount: 0,
            totalCount: null
        }
    };
陈轩 authored
276
    this.bindEvents();
ccbikai(👎🏻🍜) authored
277
陈轩 authored
278
    // this.fetchOrders();
陈轩 authored
279
};
zzzzzzz authored
280
陈轩 authored
281 282 283 284 285
OrderListView.prototype = $.extend({}, EventEmitter.prototype, {
    bindEvents: function() {
        const self = this;

        this.$elem
陈轩 authored
286 287
            .on('click.OrderListView.back', '.chat-page-back', function(event) {
                event.stopPropagation();
陈轩 authored
288 289
                self.toggleHide(true);
            })
陈轩 authored
290 291
            .on('click.OrderListView.select', '[data-action=select]', function(event) {
                event.stopPropagation();
zzzzzzz authored
292
                self.selectOrder($(event.target));
zzzzzzz authored
293 294
            });
陈轩 authored
295
        // 被通知显示,执行显示
陈轩 authored
296 297 298
        this.on('show.OderListView', (event, type) => {
            self.toggleHide(false, type);
        });
陈轩 authored
299 300 301 302 303
    },

    /**
     *  method: 选择订单,然后关闭
     */
zzzzzzz authored
304 305
    selectOrder: function($btn) {
        let $order = $btn.closest('.order-page');
陈轩 authored
306 307 308 309

        // 订单数据
        let data = {
            imgSrc: $order.find('.thumb').attr('src'),
zzzzzzz authored
310
            orderCode: $order.find('[data-id]').attr('data-id'),
陈轩 authored
311 312 313
            cost: $order.find('.sum-cost').text(),
            createTime: $order.find('.js-create-time').val(),
            orderStatus: $order.find('.order-status').text()
zzzzzzz authored
314 315
        };
陈轩 authored
316 317 318 319 320 321
        // 通知chat,订单列表已选择订单
        this.trigger('selectOrder.OrderListView', [data]);

        // 关闭
        this.toggleHide(true);
    },
zzzzzzz authored
322
陈轩 authored
323 324 325 326 327
    /**
    * method: show/hide 订单列表
    * @param boolean willHide;
    * @return this
    */
陈轩 authored
328
    toggleHide: function(willHide, type) {
陈轩 authored
329 330
        this.$elem.toggleClass('chat-page-hide', willHide);
陈轩 authored
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
        if (!willHide) {
            let orderState = this.state[type];
            let {totalCount} = orderState;

            totalCount === null && this.fetchOrderByType(type, orderState);

            if (type === 'global') {
                this.$globalOrders.show();
                this.$localOrders.hide();
            } else if (type === 'order') {
                this.$globalOrders.hide();
                this.$localOrders.show();
            }
        } else {
            this.$globalOrders.hide();
陈轩 authored
346
            this.$localOrders.hide();
陈轩 authored
347 348 349 350
        }


陈轩 authored
351 352 353
        return this;
    },
陈轩 authored
354 355 356
    fetchOrderByType: function(type, orderState) {
        let self = this;
        let $order = type === 'global' ? this.$globalOrders : this.$localOrders;
陈轩 authored
357
陈轩 authored
358
        let {totalCount} = orderState;
陈轩 authored
359
陈轩 authored
360 361 362 363 364
        // TODO: 分页
        if (totalCount === null) { // 还未加载数据
            api.fetchOrders(type).done(
                result => {
                    let html = self.orderListT({
陈轩 authored
365
                        orders: result.data,
陈轩 authored
366 367
                        isApp: appBridge.isApp,
                        isGlobal: type === 'global'
陈轩 authored
368
                    });
陈轩 authored
369
陈轩 authored
370 371 372 373 374 375 376
                    orderState.totalCount = result.data.length;
                    orderState.curCount = result.data.length;

                    $order.append(html);
                }
            );
        }
zzzzzzz authored
377
    }
陈轩 authored
378 379 380
});

OrderListView.prototype.constructer = OrderListView;
陈轩 authored
381
陈轩 authored
382
export {RatingView, LeaveMSGView, OrderListView};