Env.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * 运行环境处理
  3. */
  4. window.Promise = require('promise');
  5. export default class Env {
  6. constructor() {
  7. this.isWeiXin = false;
  8. this.isWeiXinDev = false;
  9. this.isIe = false;
  10. this.isChrome = false;
  11. this.isSafari = false;
  12. this.isFirefox = false;
  13. this.isOpera = false;
  14. this.isIpad = false;
  15. this.isIpod = false;
  16. this.isIphone = false;
  17. this.isAndroid = false;
  18. this.isWindowPhone = false;
  19. this.isMobile = false;
  20. this.isIos = false;
  21. this.width = 0;
  22. this.height = 0;
  23. this.initPlant();
  24. this.initFunc();
  25. }
  26. initPlant() {
  27. /**
  28. * 当前访问场景初始化
  29. */
  30. const agent = window.navigator.userAgent.toLowerCase();
  31. this.isIpad = agent.match(/ipad/i) === 'ipad';
  32. this.isIpod = agent.match(/ipod/i) === 'ipod';
  33. this.isIphone = agent.match(/iphone os/i) === 'iphone os';
  34. this.isAndroid = agent.match(/android/i) === 'android';
  35. this.isWindowPhone = agent.match(/windows phone/i) === 'windows phone';
  36. this.isSymbian = agent.match(/symbianos/i) === 'symbianos';
  37. this.isWeiXin = agent.match(/MicroMessenger/i) === 'micromessenger';
  38. this.isIe = agent.match(/msie/i) === 'msie';
  39. this.isFirefox = agent.match(/firefox/i) === 'firefox';
  40. this.isChrome = agent.match(/chrome/i) === 'chrome';
  41. this.isSafari = agent.match(/safari/i) === 'safari';
  42. this.isOpera = agent.match(/opera/i) === 'opera';
  43. if (this.isIpad || this.isIpod || this.isIphone) this.isIos = true;
  44. if (this.isIpad || this.isIpod || this.isIphone || this.isAndroid || this.isWindowPhone || this.isSymbian) {
  45. this.isMobile = true;
  46. } else {
  47. this.isPc = true;
  48. }
  49. }
  50. initFunc() {
  51. /**
  52. * 当前环境基本接口初始化
  53. */
  54. window.Promise.prototype.end = function(callback) {
  55. let error = null;
  56. window.Promise._87 = function(self, err) {
  57. error = err;
  58. };
  59. window.Promise.resolve()
  60. .then(() => {
  61. return this;
  62. })
  63. .catch(err => {
  64. callback(err);
  65. throw err;
  66. })
  67. .then(result => {
  68. callback(error || result);
  69. return result;
  70. });
  71. return this;
  72. };
  73. }
  74. }