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) { (function(window) {
// 单例局部变量
var _instance = null; var _instance = null;
// 对外暴露TallPlugin类
window.TallPlugin = function(config) { window.TallPlugin = function(config) {
this.config = config; this.config = config;
this.props = null; this.props = null;
this.parent = '*';
}; };
// 初始化并保证是单例 // 初始化并保证是单例
TallPlugin.init = function(config) { TallPlugin.init = function(callback, config) {
if (!_instance) { if (!_instance) {
_instance = new TallPlugin(config); _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; return _instance;
}; };
function postMsg(message) { TallPlugin.prototype.onMessage = function(callback) {
let origin = '*';
window.postMessage(message, origin);
}
TallPlugin.prototype.created = function(callback) {
console.log('created begin');
var _this = this; var _this = this;
postMsg('created');
window.addEventListener( window.addEventListener(
'message', 'message',
function({ data, origin }) { function(event) {
console.log('on created message, data, origin: ', data, origin);
try { try {
var target = JSON.parse(data); if (_this.parent === event.origin) {
if (target.success) { // 是父窗体传来的消息
_this.props = JSON.parse(data); console.log('event.data:', event.data);
callback && typeof callback === 'function' && callback.call(_this, _this.props); 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) { } catch (e) {
// console.error(`TallPlugin warn: ${e}`)
_this.props = null; _this.props = null;
} }
}, },
false, 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; return this;
}; };
// DOMContentLoaded DOM加载完成触发
TallPlugin.prototype.mounted = function(callback) { TallPlugin.prototype.mounted = function(callback) {
console.log('mounted'); var _this = this;
postMsg('mounted'); window.postMessage('created', _this.parent);
callback && typeof callback === 'function' && callback.call(this); callback && typeof callback === 'function' && callback.call(this);
return this; return this;
}; };
// load window onload触发
TallPlugin.prototype.loaded = function(callback) { TallPlugin.prototype.loaded = function(callback) {
console.log('loaded'); var _this = this;
postMsg('loaded'); window.postMessage('loaded', _this.parent);
callback && typeof callback === 'function' && callback.call(this); callback && typeof callback === 'function' && callback.call(this);
return this; return this;
}; };
// 子窗体销毁触发
TallPlugin.prototype.destroy = function(callback) { TallPlugin.prototype.destroy = function(callback) {
console.log('destroy'); var _this = this;
postMsg('destroy'); window.postMessage('destroy', _this.parent);
callback && typeof callback === 'function' && callback.call(this); callback && typeof callback === 'function' && callback.call(this);
return this; return this;
}; };
// error触发
TallPlugin.prototype.error = function(callback) { TallPlugin.prototype.error = function(callback) {
console.log('error'); var _this = this;
postMsg('error'); window.postMessage('error', _this.parent);
callback && typeof callback === 'function' && callback.call(this); callback && typeof callback === 'function' && callback.call(this);
return this; return this;
}; };

91
src/App.vue

@ -3,12 +3,12 @@
* @email: 18603454788@163.com * @email: 18603454788@163.com
* @Date: 2021-04-19 10:23:19 * @Date: 2021-04-19 10:23:19
* @LastEditors: aBin * @LastEditors: aBin
* @LastEditTime: 2021-05-31 15:55:52 * @LastEditTime: 2021-06-11 15:50:57
--> -->
<template> <template>
<a-config-provider :locale="zh_CN"> <a-config-provider :locale="zh_CN">
<div class="d-flex flex-row flex-nowrap" id="app"> <div v-if="isRouter" class="d-flex flex-row flex-nowrap" id="app">
<btn-con /> <!-- <btn-con /> -->
<router-view class="flex-1 bg" style="padding: 6px"></router-view> <router-view class="flex-1 bg" style="padding: 6px"></router-view>
</div> </div>
</a-config-provider> </a-config-provider>
@ -17,33 +17,30 @@
<script> <script>
import { mapState, mapActions, mapMutations } from 'vuex'; import { mapState, mapActions, mapMutations } from 'vuex';
import zh_CN from 'ant-design-vue/lib/locale-provider/zh_CN'; 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'; import { getHId } from 'config/api';
export default { export default {
name: 'App', name: 'App',
components: { BtnCon }, // components: { BtnCon },
data() { data() {
return { zh_CN }; return { zh_CN, isRouter: false };
}, },
computed: mapState('home', ['anyringToken', 'ptProps']),
watch: { watch: {
ptProps(val) { anyringToken(val) {
if (val.userId) { if (val) {
const params = { userId: val.userId }; this.isRouter = true;
this.getUserId(params);
this.getHospitalId(val.projectId);
} }
}, },
}, },
// created() { computed: mapState('home', ['anyringToken', 'ptProps']),
async created() { async created() {
const userId = '1387952026067013632'; // async created() {
const params = { userId }; // const userId = '1387952026067013632';
await this.getUserId(params); // const params = { userId };
await this.getToken(); // await this.getUserId(params);
// await this.getToken();
if (localStorage.getItem('patientId')) { if (localStorage.getItem('patientId')) {
this.setPatientId(localStorage.getItem('patientId')); this.setPatientId(localStorage.getItem('patientId'));
} }
@ -53,15 +50,10 @@ export default {
if (localStorage.getItem('recordCode')) { if (localStorage.getItem('recordCode')) {
this.setRecordCode(localStorage.getItem('recordCode')); this.setRecordCode(localStorage.getItem('recordCode'));
} }
const that = this; await this.isMobile();
window.plugin = window.TallPlugin.init(); if (this.anyringToken) {
// created created便 this.isRouter = true;
// created created }
// PT ididididuserId
window.plugin.created(function(props) {
that.setPtProps(props);
that.getHospitalId(props.projectId);
});
}, },
methods: { methods: {
@ -77,6 +69,49 @@ export default {
} }
} catch (error) {} } 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> </script>

5
src/plugins/axios.js

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

Loading…
Cancel
Save