Blame view

public/js/service/chat/view.js 7.72 KB
zzzzzzz authored
1
const loading = require('../../plugin/loading'),
陈轩 authored
2
    tip = require('plugin/tip'),
zzzzzzz authored
3 4 5
    lazyLoad = require('yoho-jquery-lazyload');

require('../../common');
zzzzzzz authored
6
陈轩 authored
7
import {EventEmitter, api} from './store';
陈轩 authored
8
import appBridge from 'yoho-app';
陈轩 authored
9 10 11 12


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

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

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

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

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

        self.toggleHide(true);
81
    }
陈轩 authored
82 83 84
});

LeaveMSGView.prototype.constructer = LeaveMSGView;
85
陈轩 authored
86 87 88 89

/*
    评价view
*/
陈轩 authored
90 91 92 93 94 95 96
const RatingView = function(elem, socket) {
    this.elem = $(elem);
    this.$ranks = this.elem.find('.stars i');
    this.$label = this.elem.find('.rank');
    this.rank = 3;
    this.bindEvents();
    this.socket = socket;
陈轩 authored
97
    this.isSending = 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
            .on('click.RatingView.close', '.close', $.proxy(this.toggle, this, false));
陈轩 authored
114 115
    },
陈轩 authored
116
    rating(starVal) {
陈轩 authored
117
        const rankMap = {
zzzzzzz authored
118 119 120 121 122 123 124
            0: '非常不满意',
            1: '不满意',
            2: '一般',
            3: '满意',
            4: '非常满意',
        };
陈轩 authored
125 126 127
        this.rank = starVal;
        this.rankText = rankMap[starVal] || rankMap[3];
        this.$label.text(this.rankText);
陈轩 authored
128
zzzzzzz authored
129
        this.$ranks.removeClass('rated');
陈轩 authored
130
        this.$ranks.toggleClass(index => {
陈轩 authored
131
            return index <= starVal ? 'rated' : null;
zzzzzzz authored
132
        });
陈轩 authored
133
    },
zzzzzzz authored
134 135

    post(data) {
陈轩 authored
136 137 138 139 140 141
        if (this.isSending) {
            tip.show('正在发送..');
            return;
        }
        this.isSending = true;
zzzzzzz authored
142 143
        const self = this,
            elem = this.elem;
zzzzzzz authored
144
        var params = {
zzzzzzz authored
145
            stars: ++this.rank,
zzzzzzz authored
146 147 148 149 150
            promoter: 1,
            reasonMsg: elem.find('.words-to-say').val(),
        };

        Object.assign(params, data);
陈轩 authored
151 152 153

        api.saveEvalute(params)
            .done(res => {
zzzzzzz authored
154
                if (res && res.code === 200) {
zzzzzzz authored
155 156
                    elem.hide();
                    elem.trigger('rating-success', [self.rankText]);
陈轩 authored
157 158

                    return;
zzzzzzz authored
159
                }
陈轩 authored
160
陈轩 authored
161 162 163 164
                tip.show('评价失败');
            })
            .fail(()=> {
                tip.show('评价失败');
陈轩 authored
165 166 167
            })
            .always(() => {
                self.isSending = false;
陈轩 authored
168
            });
陈轩 authored
169
    },
zzzzzzz authored
170
陈轩 authored
171
    toggle(willShow) {
陈轩 authored
172 173 174
        if (willShow === this.elem.is(':visible')) {
            return;
        }
陈轩 authored
175
        this.elem.toggle(willShow);
176
        this.rating(4);
陈轩 authored
177 178

        return false;
zzzzzzz authored
179
    }
陈轩 authored
180 181 182
});

RatingView.prototype.constructer = RatingView;
陈轩 authored
183
陈轩 authored
184 185 186 187 188 189 190
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
191 192 193 194 195 196 197 198 199 200 201 202
    // 分页数据
    this.state = {
        global: {
            curCount: 0,
            totalCount: null
        },
        order: {
            curCount: 0,
            totalCount: null
        }
    };
陈轩 authored
203
    this.bindEvents();
陈轩 authored
204
    // this.fetchOrders();
陈轩 authored
205
};
zzzzzzz authored
206
陈轩 authored
207 208 209 210 211
OrderListView.prototype = $.extend({}, EventEmitter.prototype, {
    bindEvents: function() {
        const self = this;

        this.$elem
陈轩 authored
212 213
            .on('click.OrderListView.back', '.chat-page-back', function(event) {
                event.stopPropagation();
陈轩 authored
214 215
                self.toggleHide(true);
            })
陈轩 authored
216 217
            .on('click.OrderListView.select', '[data-action=select]', function(event) {
                event.stopPropagation();
zzzzzzz authored
218
                self.selectOrder($(event.target));
zzzzzzz authored
219 220
            });
陈轩 authored
221
        // 被通知显示,执行显示
陈轩 authored
222 223 224
        this.on('show.OderListView', (event, type) => {
            self.toggleHide(false, type);
        });
陈轩 authored
225 226 227 228 229
    },

    /**
     *  method: 选择订单,然后关闭
     */
zzzzzzz authored
230 231
    selectOrder: function($btn) {
        let $order = $btn.closest('.order-page');
陈轩 authored
232 233 234 235

        // 订单数据
        let data = {
            imgSrc: $order.find('.thumb').attr('src'),
zzzzzzz authored
236
            orderCode: $order.find('[data-id]').attr('data-id'),
陈轩 authored
237 238 239
            cost: $order.find('.sum-cost').text(),
            createTime: $order.find('.js-create-time').val(),
            orderStatus: $order.find('.order-status').text()
zzzzzzz authored
240 241
        };
陈轩 authored
242 243 244 245 246 247
        // 通知chat,订单列表已选择订单
        this.trigger('selectOrder.OrderListView', [data]);

        // 关闭
        this.toggleHide(true);
    },
zzzzzzz authored
248
陈轩 authored
249 250 251 252 253
    /**
    * method: show/hide 订单列表
    * @param boolean willHide;
    * @return this
    */
陈轩 authored
254
    toggleHide: function(willHide, type) {
陈轩 authored
255 256
        this.$elem.toggleClass('chat-page-hide', willHide);
陈轩 authored
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
        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
272
            this.$localOrders.hide();
陈轩 authored
273 274 275 276
        }


陈轩 authored
277 278 279
        return this;
    },
陈轩 authored
280 281 282
    fetchOrderByType: function(type, orderState) {
        let self = this;
        let $order = type === 'global' ? this.$globalOrders : this.$localOrders;
陈轩 authored
283
陈轩 authored
284
        let {totalCount} = orderState;
陈轩 authored
285
陈轩 authored
286 287 288 289 290
        // TODO: 分页
        if (totalCount === null) { // 还未加载数据
            api.fetchOrders(type).done(
                result => {
                    let html = self.orderListT({
陈轩 authored
291
                        orders: result.data,
陈轩 authored
292 293
                        isApp: appBridge.isApp,
                        isGlobal: type === 'global'
陈轩 authored
294
                    });
陈轩 authored
295
陈轩 authored
296 297 298 299 300 301 302
                    orderState.totalCount = result.data.length;
                    orderState.curCount = result.data.length;

                    $order.append(html);
                }
            );
        }
zzzzzzz authored
303
    }
陈轩 authored
304 305 306
});

OrderListView.prototype.constructer = OrderListView;
陈轩 authored
307
陈轩 authored
308
export {RatingView, LeaveMSGView, OrderListView};