Base.js 2.9 KB

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