1
hao
2025-04-16 8f175e836561887b940ae5d3a125425ee9095370
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
<body contextmenu="return false;">
<header class="mui-bar mui-bar-nav">
    <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
    <h1 class="mui-title">chat (聊天窗口)</h1>
</header>
<pre id='h'></pre>
<script id='msg-template' type="text/template">
    <% for(var i in record){ var item=record[i]; %>
        <div class="msg-item <%= (item.sender=='self'?' msg-item-self':'') %>" msg-type='<%=(item.type)%>' msg-content='<%=(item.content)%>'>
            <% if(item.sender=='self' ) { %>
                <i class="msg-user mui-icon mui-icon-person"></i>
            <% } else { %>
                <img class="msg-user-img" src="../images/logo.png" alt="" />
            <% } %>
            <div class="msg-content">
                <div class="msg-content-inner">
                    <% if(item.type=='text' ) { %>
                        <%=( item.content|| '&nbsp;&nbsp;') %>
                    <% } else if(item.type=='image' ) { %>
                        <img class="msg-content-image" src="<%=(item.content)%>" style="max-width: 100px;" />
                    <% } else if(item.type=='sound' ) { %>
                        <span class="mui-icon mui-icon-mic" style="font-size: 18px;font-weight: bold;"></span>
                        <span class="play-state">点击播放</span>
                    <% } %>
                </div>
                <div class="msg-content-arrow"></div>
            </div>
            <div class="mui-item-clear"></div>
        </div>
    <% } %>
</script>
<div class="mui-content">
    <div id='msg-list'>
    </div>
</div>
<footer>
    <div class="footer-left">
        <i id='msg-image' class="mui-icon mui-icon-camera" style="font-size: 28px;"></i>
    </div>
    <div class="footer-center">
        <textarea id='msg-text' type="text" class='input-text'></textarea>
        <button id='msg-sound' type="button" class='input-sound' style="display: none;">按住说话</button>
    </div>
    <label for="" class="footer-right">
        <i id='msg-type' class="mui-icon mui-icon-mic"></i>
    </label>
</footer>
<div id='sound-alert' class="rprogress">
    <div class="rschedule"></div>
    <div class="r-sigh">!</div>
    <div id="audio_tips" class="rsalert">手指上滑,取消发送</div>
</div>
<script src="../js/mui.min.js"></script>
<script src="../js/mui.imageViewer.js"></script>
<script src="../js/arttmpl.js"></script>
<script type="text/javascript" charset="utf-8">
(function($, doc) {
    var MIN_SOUND_TIME = 800;
    $.init({
        gestureConfig: {
            tap: true, //默认为true
        doubletap: true, //默认为false
        longtap: true, //默认为false
        swipe: true, //默认为true
        drag: true, //默认为true
        hold: true, //默认为false,不监听
        release: true //默认为false,不监听
    }
});
template.config('escape', false);
//$.plusReady=function(fn){fn();};
$.plusReady(function() {
    plus.webview.currentWebview().setStyle({
        softinputMode: "adjustResize"
    });
    //强制弹出键盘
    var showKeyboard = function() {
        if ($.os.ios) {
            var webView = plus.webview.currentWebview().nativeInstanceObject();
            webView.plusCallMethod({
                "setKeyboardDisplayRequiresUserAction": false
            });
        } else {
            var Context = plus.android.importClass("android.content.Context");
            var InputMethodManager = plus.android.importClass("android.view.inputmethod.InputMethodManager");
            var main = plus.android.runtimeMainActivity();
            var imm = main.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
            //var view = ((ViewGroup)main.findViewById(android.R.id.content)).getChildAt(0);
            imm.showSoftInput(main.getWindow().getDecorView(), InputMethodManager.SHOW_IMPLICIT);
            //alert("ll");
        }
    };
    var record = [{
        sender: 'zs',
        type: 'text',
        content: 'Hi,我是 MUI 小管家!'
    }];
    var ui = {
        body: doc.querySelector('body'),
        footer: doc.querySelector('footer'),
        footerRight: doc.querySelector('.footer-right'),
        footerLeft: doc.querySelector('.footer-left'),
        btnMsgType: doc.querySelector('#msg-type'),
        boxMsgText: doc.querySelector('#msg-text'),
        boxMsgSound: doc.querySelector('#msg-sound'),
        btnMsgImage: doc.querySelector('#msg-image'),
        areaMsgList: doc.querySelector('#msg-list'),
        boxSoundAlert: doc.querySelector('#sound-alert'),
        h: doc.querySelector('#h'),
        content: doc.querySelector('.mui-content')
    };
    //将文本框宽度赋值给h
    ui.h.style.width = ui.boxMsgText.offsetWidth + 'px';
    //alert(ui.boxMsgText.offsetWidth );
    //让文本框居中
    var footerPadding = ui.footer.offsetHeight - ui.boxMsgText.offsetHeight;
    //语音输入相关操作
    var msgItemTap = function(msgItem, event) {
        var msgType = msgItem.getAttribute('msg-type');
        var msgContent = msgItem.getAttribute('msg-content')
        if (msgType == 'sound') {
            player = plus.audio.createPlayer(msgContent);
            var playState = msgItem.querySelector('.play-state');
            playState.innerText = '正在播放...';
            player.play(function() {
                playState.innerText = '点击播放';
            }, function(e) {
                playState.innerText = '点击播放';
            });
        }
    };
    //图片预览组件
    var imageViewer = new $.ImageViewer('.msg-content-image', {
        dbl: false
    });
    //绑定消息节点
    var bindMsgList = function() {
        //绑定数据:
        /*tp.bind({
            template: 'msg-template',
            element: 'msg-list',
            model: record
        });*/
        //将数据绑定到界面上
        ui.areaMsgList.innerHTML = template('msg-template', {
            "record": record
        });
        //拿到所有的聊天节点
        var msgItems = ui.areaMsgList.querySelectorAll('.msg-item');
        //因为document.querySelectorAll()返回的并不是我们想当然的数组,而是NodeList,对NodeList,它里面没有.forEach方法,我们使用了这样的方法进行循环:
        //通过call将this绑定到msgItems(以[]下标的方式去遍历msgItems)
        [].forEach.call(msgItems, function(item, index) {
            item.addEventListener('tap', function(event) {
                //处理语音消息播放
                msgItemTap(item, event);
            }, false);
        });
        //查找所有符合条件的图片
        imageViewer.findAllImage();
        //聊天界面的高度修改
        ui.areaMsgList.scrollTop = ui.areaMsgList.scrollHeight + ui.areaMsgList.offsetHeight;
    };
    bindMsgList();
    //平滑高度过渡
    window.addEventListener('resize', function() {
        ui.areaMsgList.scrollTop = ui.areaMsgList.scrollHeight + ui.areaMsgList.offsetHeight;
    }, false);
    //发送对象声明
    var send = function(msg) {
        //将消息内容体push进record
        record.push(msg);
        //绑定消息节点
        bindMsgList();
        toRobot(msg.content);
    };
    var toRobot = function(info) {
        var apiUrl = 'http://www.tuling123.com/openapi/api';
        $.getJSON(apiUrl, {
            "key": 'acfbca724ea1b5db96d2eef88ce677dc',
            "info": info,
            "userid": plus.device.uuid
        }, function(data) {
            //alert(JSON.stringify(data));
            record.push({
                sender: 'zs',
                type: 'text',
                content: data.text
            });
            bindMsgList();
        });
    };
    
    //让输入框获取焦点
    function msgTextFocus() {
            ui.boxMsgText.focus();
            setTimeout(function() {
                ui.boxMsgText.focus();
            }, 150);
        }
        //解决长按“发送”按钮,导致键盘关闭的问题;
    ui.footerRight.addEventListener('touchstart', function(event) {
        if (ui.btnMsgType.classList.contains('mui-icon-paperplane')) {
            msgTextFocus();
            event.preventDefault();
        }
    });
    //解决长按“发送”按钮,导致键盘关闭的问题;
    ui.footerRight.addEventListener('touchmove', function(event) {
        if (ui.btnMsgType.classList.contains('mui-icon-paperplane')) {
            msgTextFocus();
            event.preventDefault();
        }
    });
    //                  ui.footerRight.addEventListener('touchcancel', function(event) {
    //                      if (ui.btnMsgType.classList.contains('mui-icon-paperplane')) {
    //                          msgTextFocus();
    //                          event.preventDefault();
    //                      }
    //                  });
    //                  ui.footerRight.addEventListener('touchend', function(event) {
    //                      if (ui.btnMsgType.classList.contains('mui-icon-paperplane')) {
    //                          msgTextFocus();
    //                          event.preventDefault();
    //                      }
    //                  });
    //长按离开屏幕时触发
    ui.footerRight.addEventListener('release', function(event) {
        //当是文字时
        if (ui.btnMsgType.classList.contains('mui-icon-paperplane')) {
            //showKeyboard();
            ui.boxMsgText.focus();
            setTimeout(function() {
                ui.boxMsgText.focus();
            }, 150);
            //                          event.detail.gesture.preventDefault();
            send({
                sender: 'self',
                type: 'text',
                content: ui.boxMsgText.value.replace(new RegExp('\n', 'gm'), '<br/>')
            });
            ui.boxMsgText.value = '';
            $.trigger(ui.boxMsgText, 'input', null);
        } 
        //当是语音时
        else if (ui.btnMsgType.classList.contains('mui-icon-mic')) {
            ui.btnMsgType.classList.add('mui-icon-compose');
            ui.btnMsgType.classList.remove('mui-icon-mic');
            ui.boxMsgText.style.display = 'none';
            ui.boxMsgSound.style.display = 'block';
            ui.boxMsgText.blur();
            document.body.focus();
        }
        //当是文本状态时
        else if (ui.btnMsgType.classList.contains('mui-icon-compose')) {
            ui.btnMsgType.classList.add('mui-icon-mic');
            ui.btnMsgType.classList.remove('mui-icon-compose');
            ui.boxMsgSound.style.display = 'none';
            ui.boxMsgText.style.display = 'block';
            //--
            //showKeyboard();
            ui.boxMsgText.focus();
            setTimeout(function() {
                ui.boxMsgText.focus();
            }, 150);
        }
    }, false);
    
    //点击左边按钮时
    ui.footerLeft.addEventListener('tap', function(event) {
        var btnArray = [{
            title: "拍照"
        }, {
            title: "从相册选择"
        }];
        plus.nativeUI.actionSheet({
            title: "选择照片",
            cancel: "取消",
            buttons: btnArray
        }, function(e) {
            var index = e.index;
            switch (index) {
                case 0:
                    break;
                case 1:
                    var cmr = plus.camera.getCamera();
                    cmr.captureImage(function(path) {
                        send({
                            sender: 'self',
                            type: 'image',
                            content: "file://" + plus.io.convertLocalFileSystemURL(path)
                        });
                    }, function(err) {});
                    break;
                case 2:
                    plus.gallery.pick(function(path) {
                        send({
                            sender: 'self',
                            type: 'image',
                            content: path
                        });
                    }, function(err) {}, null);
                    break;
            }
        });
    }, false); 
    //控制按住语音时的显示与隐藏
    var setSoundAlertVisable=function(show){
        if(show){
            ui.boxSoundAlert.style.display = 'block';
            ui.boxSoundAlert.style.opacity = 1;
        }else{
            ui.boxSoundAlert.style.opacity = 0;
            //fadeOut 完成再真正隐藏
            setTimeout(function(){
                ui.boxSoundAlert.style.display = 'none';
            },200);
        }
    };
    var recordCancel = false;
    var recorder = null;
    var audio_tips = document.getElementById("audio_tips");
    var startTimestamp = null;
    var stopTimestamp = null;
    var stopTimer = null;
    //按住说话时候触发
    ui.boxMsgSound.addEventListener('hold', function(event) {
        recordCancel = false;
        //如果有结束时间,清除定时器
        if(stopTimer)clearTimeout(stopTimer);
        //修改显示文字
        audio_tips.innerHTML = "手指上划,取消发送";
        //移除rprogress-sigh
        ui.boxSoundAlert.classList.remove('rprogress-sigh');
        //显示样式
        setSoundAlertVisable(true);
        //获取录音对象  5+模块
        recorder = plus.audio.getRecorder();
        if (recorder == null) {
            plus.nativeUI.toast("不能获取录音对象");
            return;
        }
        //记录当前录音时间
        startTimestamp = (new Date()).getTime();
        //保存录音http://www.html5plus.org/doc/zh_cn/audio.html#plus.audio.RecordOption
        recorder.record({
            filename: "_doc/audio/"
        }, function(path) {
            if (recordCancel) return;
            send({
                sender: 'self',
                type: 'sound',
                content: path
            });
        }, function(e) {
            plus.nativeUI.toast("录音时出现异常: " + e.message);
        });
    }, false);
    
    //监听drag(拖动中)事件 上滑;下滑事件
    ui.body.addEventListener('drag', function(event) {
        //console.log('drag');
        if (Math.abs(event.detail.deltaY) > 50) {
            //此时没有录音操作执行 检查recordCancel状态
            if (!recordCancel) {
                recordCancel = true;
                if (!audio_tips.classList.contains("cancel")) {
                    audio_tips.classList.add("cancel");
                }
                audio_tips.innerHTML = "松开手指,取消发送";
            }
        } else {
            if (recordCancel) {
                recordCancel = false;
                if (audio_tips.classList.contains("cancel")) {
                    audio_tips.classList.remove("cancel");
                }
                audio_tips.innerHTML = "手指上划,取消发送";
            }
        }
    }, false);
    //长按离开录音节点时执行
    ui.boxMsgSound.addEventListener('release', function(event) {
        //console.log('release');
        //初始化
        if (audio_tips.classList.contains("cancel")) {
            audio_tips.classList.remove("cancel");
            audio_tips.innerHTML = "手指上划,取消发送";
        }
        //判断录音是否小于800毫秒,若小于,则废弃
        stopTimestamp = (new Date()).getTime();
        if (stopTimestamp - startTimestamp < MIN_SOUND_TIME) {
            audio_tips.innerHTML = "录音时间太短";
            ui.boxSoundAlert.classList.add('rprogress-sigh');
            recordCancel = true;
            stopTimer=setTimeout(function(){
                setSoundAlertVisable(false);
            },800);
        }else{
            setSoundAlertVisable(false);
        }
        //停止录音模块
        recorder.stop();
    }, false);
    
    //阻止浏览器默认的事件冒泡
    ui.boxMsgSound.addEventListener("touchstart", function(e) {
        //console.log("start....");
        e.preventDefault();
    });
    //监听用户输入时触发
    ui.boxMsgText.addEventListener('input', function(event) {
        //当输入为空时去掉发送文字,当不为空时,显示发送文字
        ui.btnMsgType.classList[ui.boxMsgText.value == '' ? 'remove' : 'add']('mui-icon-paperplane');
        //当用户输入值不为空时,设置自定义属性for 赋值msg-text
        ui.btnMsgType.setAttribute("for", ui.boxMsgText.value == '' ? '' : 'msg-text');
        //替换与正则表达式相匹配的值(转义)
        ui.h.innerText = ui.boxMsgText.value.replace(new RegExp('\n', 'gm'), '\n-') || '-';
        //动态调整输入框高度
        ui.footer.style.height = (ui.h.offsetHeight + footerPadding) + 'px';
        //调整显示信息高度
        ui.content.style.paddingBottom = ui.footer.style.height;
    });
    var focus = false;
    //监听用户点击发送时触发
    ui.boxMsgText.addEventListener('tap', function(event) {
        //得到焦点
        ui.boxMsgText.focus();
        setTimeout(function() {
            ui.boxMsgText.focus();
        }, 0);
        focus = true;
        setTimeout(function () {
            focus = false;
        },1000);
        //阻止iOS2.0中的手势事件:gesture事件 
        event.detail.gesture.preventDefault();
    }, false);
    //点击消息列表,关闭键盘
    ui.areaMsgList.addEventListener('click',function (event) {
        if(!focus){
            ui.boxMsgText.blur();
        }
    })  
});
}(mui, document));
    </script>
</body>