gemercheung 2 年之前
父節點
當前提交
31284d3383
共有 6 個文件被更改,包括 34 次插入13 次删除
  1. 1 1
      packages/core/package.json
  2. 4 1
      packages/core/src/basicTrack.ts
  3. 13 4
      packages/core/src/history.ts
  4. 8 5
      packages/core/src/sdk.ts
  5. 6 0
      play/index.html
  6. 2 2
      play/src/App.vue

+ 1 - 1
packages/core/package.json

@@ -1,6 +1,6 @@
 {
   "name": "@medici/core",
-  "version": "0.0.9",
+  "version": "0.1.0",
   "main": "dist/index",
   "types": "dist/index",
   "files": [

+ 4 - 1
packages/core/src/basicTrack.ts

@@ -13,8 +13,11 @@ export class BaseTrack implements IBaseAction, ICustomAction {
     protected _history: IHistory;
     protected _endPoint: string;
     protected _appId: string;
+    protected _sdk: SDK;
     private _timeOut: ReturnType<typeof setTimeout>;
 
+    static _stopTrack = false;
+
     constructor(params: SDKConfigType) {
         this._config = Object.assign({ user: '', version: '' }, params.config);
         this._platform = params.platform;
@@ -22,7 +25,7 @@ export class BaseTrack implements IBaseAction, ICustomAction {
         this._endPoint = params.endPoint;
     }
     get _stopTrack() {
-        return SDK._stopTrack;
+        return BaseTrack._stopTrack;
     }
 
     get trackUrl() {

+ 13 - 4
packages/core/src/history.ts

@@ -18,9 +18,10 @@ export class IHistory {
     protected _playload: playloadType;
     private _currentRef: string;
     private _currentUrl: string;
-
+    private _appId: string;
     constructor(sdk: SDK, appId?: string) {
         this._sdk = sdk;
+        this._appId = appId;
         this.init();
         this._playload.website = appId;
     }
@@ -39,9 +40,17 @@ export class IHistory {
 
     init(): void {
         console.log('history init');
-        global.document.addEventListener('readystatechange', this.handleReadystatechange.bind(this), true);
-        global.history.pushState = hook(global.history, 'pushState', this.handlePushState.bind(this));
-        global.history.replaceState = hook(global.history, 'replaceState', this.handlePushState.bind(this));
+        if (!(window as any).mediciRegisterIds) {
+            (window as any).mediciRegisterIds = [];
+        }
+        const mediciRegisterIds = (window as any).mediciRegisterIds;
+        if (!Array.from(mediciRegisterIds).includes(this._appId) && this._appId) {
+            global.document.addEventListener('readystatechange', this.handleReadystatechange.bind(this), true);
+            global.history.pushState = hook(global.history, 'pushState', this.handlePushState.bind(this));
+            global.history.replaceState = hook(global.history, 'replaceState', this.handlePushState.bind(this));
+            (window as any).mediciRegisterIds.push(this._appId);
+        }
+
         this.initPlayload();
     }
 

+ 8 - 5
packages/core/src/sdk.ts

@@ -23,20 +23,23 @@ export class SDK extends BaseTrack {
     protected _platform: PlatformType;
     protected _config: SDKInitConfig;
 
-    static _stopTrack = false;
-
     public eventer = new Eventer(this);
     public _history: IHistory;
 
     constructor(params: SDKConfigType) {
         super(params);
         console.log('params.appId', params.appId);
+
         this._history = new IHistory(this, params.appId);
         super._history = this._history;
         if ('stopTrack' in params) {
-            SDK._stopTrack = params.stopTrack;
+            BaseTrack._stopTrack = params.stopTrack;
         }
         this.setConfig(params.config);
+        //
+    }
+    get trackStatus() {
+        return BaseTrack._stopTrack;
     }
     public setConfig(config: SDKInitConfig): void {
         this._config = Object.assign({ user: '', version: '' }, config);
@@ -44,9 +47,9 @@ export class SDK extends BaseTrack {
     }
 
     public stop(): void {
-        SDK._stopTrack = true;
+        BaseTrack._stopTrack = true;
     }
     public resume(): void {
-        SDK._stopTrack = false;
+        BaseTrack._stopTrack = false;
     }
 }

+ 6 - 0
play/index.html

@@ -10,6 +10,12 @@
          data-url="/track_api"
          src="https://allreally.oss-cn-shenzhen.aliyuncs.com/wasm/test_legacy/medici.js?v=111">
         </script>
+           <script async defer
+           data-app-id="7b5958d5-1ae6-4ad5-8a87-5fc8a4b92999" 
+           data-url="/track_api"
+           src="https://allreally.oss-cn-shenzhen.aliyuncs.com/wasm/test_legacy/medici.js?v=111">
+          </script>
+        
 
         <!-- <script async defer data-website-id="7b5958d5-1ae6-4ad5-8a87-5fc8a4b92999" src="http://192.168.0.186:3000/medici.js"></script> -->
     </head>

+ 2 - 2
play/src/App.vue

@@ -2,12 +2,12 @@
 // This starter template is using Vue 3 <script setup> SFCs
 // Check out https://vuejs.org/api/sfc-script-setup.html#script-setup
 import HelloWorld from './components/HelloWorld.vue';
-import * as Medici from '@medici/web';
+// import * as Medici from '@medici/web';
 
 // const medici = Medici.init({
 //     platform: 'web',
 //     appId: '7b5958d5-1ae6-4ad5-8a87-5fc8a4b92999',
-//     endPoint: 'https://v4-test.4dkankan.com', //服务器
+//     endPoint: '/track_api', //服务器
 //     stopTrack: false,
 //     config: {
 //         user: 'testUser',