Base.js 3.4 KB

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