Browse Source

传参接收修改

master
aBin 4 years ago
parent
commit
e859639e0f
  1. 87
      public/sdk.js
  2. 91
      src/App.vue
  3. 5
      src/plugins/axios.js

87
public/sdk.js

@ -1,81 +1,98 @@
/*
* @Author: aBin
* @email: binbin0314@126.com
* @Date: 2021-04-19 10:23:19
* @LastEditors: aBin
* @LastEditTime: 2021-06-09 09:24:35
*/
/**
* ccsens tall sdk.js
* v1.0.0
* 父组件调用 TallPlugin.init()即可
* 监听message消息, 如果是created消息 就把tall的参数传递过来
*/
(function(window) {
// 单例局部变量
var _instance = null;
// 对外暴露TallPlugin类
window.TallPlugin = function(config) {
this.config = config;
this.props = null;
this.parent = '*';
};
// 初始化并保证是单例
TallPlugin.init = function(config) {
TallPlugin.init = function(callback, config) {
if (!_instance) {
_instance = new TallPlugin(config);
_instance.config = config;
_instance.parent = window.parent.origin;
// DOM加载完成
window.addEventListener('DOMContentLoaded', _instance.mounted, false);
// window onload
window.addEventListener('load', _instance.loaded, false);
// destroy
window.addEventListener('unload', _instance.destroy, false);
// error
window.addEventListener('error', _instance.error, false);
_instance.onMessage(callback);
}
return _instance;
};
function postMsg(message) {
let origin = '*';
window.postMessage(message, origin);
}
TallPlugin.prototype.created = function(callback) {
console.log('created begin');
TallPlugin.prototype.onMessage = function(callback) {
var _this = this;
postMsg('created');
window.addEventListener(
'message',
function({ data, origin }) {
console.log('on created message, data, origin: ', data, origin);
function(event) {
try {
var target = JSON.parse(data);
if (target.success) {
_this.props = JSON.parse(data);
callback && typeof callback === 'function' && callback.call(_this, _this.props);
if (_this.parent === event.origin) {
// 是父窗体传来的消息
console.log('event.data:', event.data);
var data = JSON.parse(event.data);
_this.props = data || null;
callback && typeof callback === 'function' && callback.call(_this, data);
} else {
_this.props = null;
}
// else {
// _this.props = null;
// }
} catch (e) {
// console.error(`TallPlugin warn: ${e}`)
_this.props = null;
}
},
false,
);
// DOM加载完成
window.addEventListener('DOMContentLoaded', this.mounted, false);
// window onload
window.addEventListener('load', this.loaded, false);
// destroy
window.addEventListener('unload', this.destroy, false);
// error
window.addEventListener('error', this.error, false);
return this;
};
// DOMContentLoaded DOM加载完成触发
TallPlugin.prototype.mounted = function(callback) {
console.log('mounted');
postMsg('mounted');
var _this = this;
window.postMessage('created', _this.parent);
callback && typeof callback === 'function' && callback.call(this);
return this;
};
// load window onload触发
TallPlugin.prototype.loaded = function(callback) {
console.log('loaded');
postMsg('loaded');
var _this = this;
window.postMessage('loaded', _this.parent);
callback && typeof callback === 'function' && callback.call(this);
return this;
};
// 子窗体销毁触发
TallPlugin.prototype.destroy = function(callback) {
console.log('destroy');
postMsg('destroy');
var _this = this;
window.postMessage('destroy', _this.parent);
callback && typeof callback === 'function' && callback.call(this);
return this;
};
// error触发
TallPlugin.prototype.error = function(callback) {
console.log('error');
postMsg('error');
var _this = this;
window.postMessage('error', _this.parent);
callback && typeof callback === 'function' && callback.call(this);
return this;
};

91
src/App.vue

@ -3,12 +3,12 @@
* @email: 18603454788@163.com
* @Date: 2021-04-19 10:23:19
* @LastEditors: aBin
* @LastEditTime: 2021-05-31 15:55:52
* @LastEditTime: 2021-06-11 15:50:57
-->
<template>
<a-config-provider :locale="zh_CN">
<div class="d-flex flex-row flex-nowrap" id="app">
<btn-con />
<div v-if="isRouter" class="d-flex flex-row flex-nowrap" id="app">
<!-- <btn-con /> -->
<router-view class="flex-1 bg" style="padding: 6px"></router-view>
</div>
</a-config-provider>
@ -17,33 +17,30 @@
<script>
import { mapState, mapActions, mapMutations } from 'vuex';
import zh_CN from 'ant-design-vue/lib/locale-provider/zh_CN';
import BtnCon from 'components/BtnCom/BtnCon.vue';
// import BtnCon from 'components/BtnCom/BtnCon.vue';
import { getHId } from 'config/api';
export default {
name: 'App',
components: { BtnCon },
// components: { BtnCon },
data() {
return { zh_CN };
return { zh_CN, isRouter: false };
},
computed: mapState('home', ['anyringToken', 'ptProps']),
watch: {
ptProps(val) {
if (val.userId) {
const params = { userId: val.userId };
this.getUserId(params);
this.getHospitalId(val.projectId);
anyringToken(val) {
if (val) {
this.isRouter = true;
}
},
},
// created() {
computed: mapState('home', ['anyringToken', 'ptProps']),
async created() {
const userId = '1387952026067013632';
const params = { userId };
await this.getUserId(params);
await this.getToken();
// async created() {
// const userId = '1387952026067013632';
// const params = { userId };
// await this.getUserId(params);
// await this.getToken();
if (localStorage.getItem('patientId')) {
this.setPatientId(localStorage.getItem('patientId'));
}
@ -53,15 +50,10 @@ export default {
if (localStorage.getItem('recordCode')) {
this.setRecordCode(localStorage.getItem('recordCode'));
}
const that = this;
window.plugin = window.TallPlugin.init();
// created created便
// created created
// PT ididididuserId
window.plugin.created(function(props) {
that.setPtProps(props);
that.getHospitalId(props.projectId);
});
await this.isMobile();
if (this.anyringToken) {
this.isRouter = true;
}
},
methods: {
@ -77,6 +69,49 @@ export default {
}
} catch (error) {}
},
isMobile() {
this.flag = navigator.userAgent.match(
/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i,
);
if (this.flag === null) {
console.log('pc端');
this.getPtpropsPC();
} else {
console.log('移动端');
this.getPtpropsMove();
}
},
getPtpropsMove() {
const that = this;
window.parent.postMessage('created', '*');
window.addEventListener('message', async function(event) {
try {
const data = JSON.parse(event.data);
if (data.type !== 'data') return;
console.log('Props', data.data);
await that.setPtProps(data.data);
console.log('ptProps', that.ptProps);
const params = { userId: data.data.userId };
await that.getUserId(params);
await that.getToken();
await that.getHospitalId(data.data.projectId);
} catch (error) {
console.log('error:', error);
}
});
},
getPtpropsPC() {
const that = this;
window.TallPlugin.init(async function(props) {
console.log('props:', props);
that.setPtProps(props);
const params = { userId: props.userId };
await that.getUserId(params);
await that.getToken();
await that.getHospitalId(data.data.projectId);
});
},
},
};
</script>

5
src/plugins/axios.js

@ -2,8 +2,8 @@
* @Author: wally
* @email: 18603454788@163.com
* @Date: 2021-04-19 10:23:19
* @LastEditors: wally
* @LastEditTime: 2021-05-06 11:42:20
* @LastEditors: aBin
* @LastEditTime: 2021-06-10 11:51:56
*/
'use strict';
@ -26,7 +26,6 @@ const _axios = axios.create(config);
axios.interceptors.request.use(
function(config) {
let token = store.state.anyringToken || sessionStorage.getItem('anyringToken');
if (token) {
config.headers.Authorization = `Bearer ${token}`;
}

Loading…
Cancel
Save