Base.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import WebSokcet from "./socket/WebSokcet.js";
  2. import SocketEvent from "./Event/SocketEvent.js";
  3. class Base {
  4. Vue = {};
  5. //容器
  6. Container = null;
  7. constructor(vue,container) {
  8. this.Vue = vue;
  9. this.Container = container;
  10. this.init();
  11. }
  12. init() {
  13. //添加全局库
  14. this.Vue.prototype.global = this.Container.make(require("@/config/global.js"),"global");
  15. //添加com库
  16. this.Vue.prototype.utils = this.Container.make(require("@/library/utils/Comm.js"),"utils");
  17. //请求库
  18. this.Vue.prototype.request = this.Container.make(require('@/library/Request.js'));
  19. //webSocket
  20. let websocket = new WebSokcet({wsUrl:this.Vue.prototype.global.wsSocketUrl});
  21. websocket.setKey(this.Vue.prototype.global.putoken);
  22. this.Vue.prototype.$deviceType = 'web';
  23. //#ifdef H5
  24. var ua = navigator.userAgent.toLowerCase();
  25. //判断登录模式
  26. if (/MicroMessenger/i.test(ua)) {
  27. //var wx = require('jweixin-module');
  28. //this.Vue.prototype.$wx = wx;
  29. this.Vue.prototype.$deviceType = 'weixin';
  30. }
  31. //判断支付宝
  32. if (/alipayclient/.test(ua)) {
  33. this.Vue.prototype.$deviceType = 'alipay';
  34. }
  35. //#endif
  36. //小程序
  37. // #ifdef MP-WEIXIN
  38. this.Vue.prototype.$deviceType = 'mp_weixin';
  39. // #endif
  40. //APP
  41. // #ifdef APP-PLUS
  42. this.Vue.prototype.$deviceType = 'app';
  43. // #endif
  44. //获取设备信息
  45. uni.getSystemInfo({
  46. success:(res) => {
  47. this.Vue.prototype.$device = res;
  48. }
  49. });
  50. let socketEvent = new SocketEvent(this.Vue.prototype);
  51. websocket.setBackCall((type,res)=>{
  52. if(type == 'onOpen'){
  53. socketEvent.onOpen(res);
  54. }
  55. if(type == 'onMessage'){
  56. socketEvent.onMessage(res);
  57. }
  58. if(type == 'onError'){
  59. socketEvent.onError(res);
  60. }
  61. if(type == 'onStop'){
  62. socketEvent.onStop(res);
  63. }
  64. if(type == 'onClose'){
  65. socketEvent.onClose(res);
  66. }
  67. if(type == 'onSendError') {
  68. socketEvent.onSendError(res);
  69. }
  70. if(type == 'onSendSuccess') {
  71. socketEvent.onSendSuccess(res);
  72. }
  73. if(type == 'onHeartBeat') {
  74. socketEvent.onHeartBeat(res);
  75. }
  76. });
  77. // websocket.connect();
  78. this.Vue.prototype.webSocket = websocket;
  79. }
  80. /**
  81. * ipc全局拦截访问
  82. * @param {Object} page 当前页面page
  83. * @param {Object} options 参数
  84. */
  85. ipc(page,options){
  86. var tj_uid = options.tj_uid || '';
  87. var ipc = options.ipc || '';
  88. if(tj_uid != '' && tj_uid != null) {
  89. this.Vue.prototype.$store.commit('setTjuid',tj_uid);
  90. }
  91. if(ipc == '') {
  92. return ;
  93. }
  94. //微信登录ipc
  95. if(ipc == 'weixin_login') {
  96. var data = options.data || '';
  97. try{
  98. let dataAr = JSON.parse(data);
  99. this.Vue.prototype.$store.commit('setUser',dataAr);
  100. } catch(e){}
  101. }
  102. }
  103. }
  104. export default Base;