Skip to content

Commit ec06644

Browse files
author
w-xuefeng
committed
feat: add plugin
1 parent 9f9e7eb commit ec06644

File tree

11 files changed

+191
-45
lines changed

11 files changed

+191
-45
lines changed

.eslintrc.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@ module.exports = {
33
env: {
44
node: true
55
},
6-
extends: ["plugin:vue/essential", "@vue/prettier"],
6+
extends: ['plugin:vue/essential', '@vue/prettier'],
77
rules: {
8-
"no-console": process.env.NODE_ENV === "production" ? "error" : "off",
9-
"no-debugger": process.env.NODE_ENV === "production" ? "error" : "off"
8+
'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off',
9+
'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off'
1010
},
1111
parserOptions: {
12-
parser: "babel-eslint"
12+
parser: 'babel-eslint'
13+
},
14+
globals: {
15+
api: 'readonly',
16+
'window.api': 'readonly',
17+
apiready: true,
18+
'window.apiready': true
1319
}
1420
};

public/res/css/api.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ html,body,header,section,footer,div,ul,ol,li,img,a,span,em,del,legend,center,str
66
border:0;
77
padding:0;
88
font-style:normal;
9+
overflow: hidden;
910
}
1011
html,body {
1112
-webkit-touch-callout:none;

src/assets/logo.png

-6.69 KB
Binary file not shown.

src/components/BottomNav.vue

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<template>
22
<div class="bottomNav">
3-
13245879
3+
<span>lala</span>
4+
<span>haha</span>
5+
<span>dada</span>
6+
<span>kaka</span>
47
</div>
58
</template>
69

@@ -18,9 +21,20 @@ export default {
1821
.bottomNav {
1922
height: 60px;
2023
width: 100%;
21-
padding-bottom: 20px;
24+
display: flex;
25+
justify-content: space-between;
26+
align-items: center;
27+
padding-bottom: 10px;
2228
position: absolute;
2329
bottom: 0;
2430
left: 0;
31+
border-top: 1px solid #666666;
32+
background: dodgerblue;
33+
span {
34+
width: 25%;
35+
height: 40px;
36+
text-align: center;
37+
line-height: 50px;
38+
}
2539
}
2640
</style>

src/main.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
import App from './app';
2-
import Vue from 'vue';
3-
import apicloud from '@/utils/common';
2+
import { initApp } from '@/plugins';
43

5-
Vue.use(apicloud);
6-
Vue.config.productionTip = false;
7-
8-
new Vue({
9-
render: h => h(App)
10-
}).$mount('#app');
4+
initApp(App);

src/pages/bottomframe/index.vue

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
<template>
2-
<div>
3-
bottomframe
4-
</div>
2+
<bottom-nav />
53
</template>
64

75
<script>
6+
import BottomNav from '@/components/BottomNav';
7+
88
export default {
9-
name: 'bottomframe'
9+
name: 'bottomframe',
10+
components: {
11+
BottomNav
12+
}
1013
};
1114
</script>
1215

src/pages/index/index.vue

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<com-header :title="title" />
44
<button
55
style="height:100px; width: 100%; background: deepskyblue"
6-
@click="$openPage({ name: 'login', url: 'loginindex.html' })"
6+
@click="$page.open('loginindex', { name: 'login' })"
77
>
88
123456
99
</button>
@@ -23,6 +23,18 @@ export default {
2323
title: '首页'
2424
};
2525
},
26+
onReady() {
27+
this.$frame.open({
28+
name: 'bottomframe',
29+
rect: {
30+
x: 0,
31+
y: Number(document.body.getRect().height) - 60,
32+
w: 'auto',
33+
h: 60
34+
},
35+
url: 'bottomframeindex'
36+
});
37+
},
2638
mounted() {}
2739
};
2840
</script>

src/plugins/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import initApp from './init.vue';
2+
export { initApp };

src/plugins/init.vue.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import Vue from 'vue';
2+
import ReadyPlugin from './ready.plugin';
3+
4+
Vue.config.productionTip = false;
5+
6+
Vue.use(ReadyPlugin);
7+
8+
Object.defineProperty(Vue.prototype, 'api', {
9+
get: function() {
10+
return window.api;
11+
}
12+
});
13+
14+
Object.defineProperty(Vue.prototype, '$api', {
15+
get: function() {
16+
return window.$api;
17+
}
18+
});
19+
20+
var _openw = null;
21+
Vue.prototype.$page = {
22+
open(url, { pageParam, animation, winOpts } = {}) {
23+
if (_openw) {
24+
return;
25+
}
26+
url = url.endsWith('.html') ? url : url + '.html';
27+
var api = window.api;
28+
if (!api) {
29+
window.location.href = url;
30+
return;
31+
}
32+
var name = `win_${url}`;
33+
var params = {
34+
name,
35+
url,
36+
pageParam,
37+
animation,
38+
...(winOpts || {})
39+
};
40+
api.openWin(params);
41+
},
42+
close() {
43+
if (!window.api) {
44+
window.history.back();
45+
return;
46+
}
47+
window.api.closeWin();
48+
}
49+
};
50+
51+
Vue.prototype.$frame = {
52+
open(params) {
53+
let { url } = params;
54+
url = url.endsWith('.html') ? url : url + '.html';
55+
if (typeof api !== 'undefined') {
56+
api.openFrame({ ...params, url });
57+
} else {
58+
const { name, rect } = params;
59+
const iframe = document.createElement('iframe');
60+
iframe.setAttribute('frameborder', 0);
61+
iframe.setAttribute('name', name);
62+
iframe.src = url;
63+
iframe.style.position = 'absolute';
64+
if (rect.x) {
65+
iframe.style.left = `${rect.x}px`;
66+
}
67+
if (rect.y) {
68+
iframe.style.top = `${rect.y}px`;
69+
}
70+
if (rect.w) {
71+
iframe.style.width = rect.w === 'auto' ? '100%' : `${rect.w}px`;
72+
}
73+
if (rect.h) {
74+
iframe.style.height = rect.h === 'auto' ? '100%' : `${rect.h}px`;
75+
}
76+
document.body.appendChild(iframe);
77+
}
78+
}
79+
};
80+
81+
const initApp = (App, opts = {}) => {
82+
new Vue({
83+
...opts,
84+
render: h => h(App)
85+
}).$mount('#app');
86+
};
87+
export default initApp;

src/plugins/ready.plugin.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const install = Vue => {
2+
if (install.installed) return;
3+
install.installed = true;
4+
Vue.mixin({
5+
beforeCreate() {
6+
this._isApiready = false;
7+
document.addEventListener('apiready', () => {
8+
if ('apiEvent' in this.$options) {
9+
const apiEvent = this.$options.apiEvent;
10+
for (const key in apiEvent) {
11+
if (apiEvent.hasOwnProperty(key)) {
12+
const eventListener = apiEvent[key];
13+
if (typeof eventListener === 'function') {
14+
window.api.addEventListener({ name: key }, (ret, err) => {
15+
eventListener.bind(this).call(this, ret, err);
16+
});
17+
}
18+
}
19+
}
20+
}
21+
this._isApiready = true;
22+
if (this._isMounted) {
23+
this.__ready();
24+
}
25+
});
26+
document.addEventListener('updateOrientation', () => {
27+
this.$options.onWindowChange &&
28+
this.$options.onWindowChange.bind(this).call();
29+
});
30+
},
31+
mounted() {
32+
if (this._isApiready) {
33+
this.__ready();
34+
} else {
35+
setTimeout(() => {
36+
this.__ready();
37+
}, 200);
38+
}
39+
},
40+
methods: {
41+
__ready() {
42+
if (this.__readyed) {
43+
return;
44+
}
45+
this.__readyed = true;
46+
this.$options.onReady && this.$options.onReady.bind(this).call();
47+
}
48+
}
49+
});
50+
};
51+
export default {
52+
install
53+
};

0 commit comments

Comments
 (0)