whisper-tool.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. var whisper = function(){
  2. //var isOpen = 0;
  3. var baseConfig = {
  4. id: 0
  5. ,whisper_domain: ''
  6. ,title: '客服'
  7. ,name: ''
  8. ,group: 0
  9. ,avatar: ''
  10. };
  11. var self = this;
  12. self.init = function(config){
  13. baseConfig = self.extend(baseConfig, config || {});
  14. if(0 == baseConfig.id || '' == baseConfig.url || '' == baseConfig.name
  15. || 0 == baseConfig.group || '' == baseConfig.avatar){
  16. alert("参数缺失");
  17. return false;
  18. }
  19. var url = baseConfig.whisper_domain + '/index/index/chat' + '?group=' + baseConfig.group + '&id=' + baseConfig.id +
  20. '&name=' + baseConfig.name + '&avatar=' + baseConfig.avatar;
  21. if(self.isMobile()){
  22. window.location.href = url;
  23. }else{
  24. self.openWindow(url, baseConfig.title);
  25. }
  26. };
  27. // 合并配置项
  28. self.extend = function(target, source){
  29. for (var obj in source) {
  30. target[obj] = source[obj];
  31. }
  32. return target;
  33. };
  34. // 打开窗口
  35. self.openWindow = function(url, name){
  36. layer.open({
  37. type: 2,
  38. id: 'whisper', // 允许打开一次
  39. title: name,
  40. shade: 0,
  41. area: ['800px', '640px'],
  42. content: url
  43. });
  44. };
  45. // 是否是移动端
  46. self.isMobile = function(){
  47. if( navigator.userAgent.match(/Android/i)
  48. || navigator.userAgent.match(/webOS/i)
  49. || navigator.userAgent.match(/iPhone/i)
  50. || navigator.userAgent.match(/iPad/i)
  51. || navigator.userAgent.match(/iPod/i)
  52. || navigator.userAgent.match(/BlackBerry/i)
  53. || navigator.userAgent.match(/Windows Phone/i)
  54. ){
  55. return true;
  56. } else {
  57. return false;
  58. }
  59. };
  60. };