客户端集成JsBridge

参考(有很多实现,不一定是下面的库)

bridge.js(部分依赖没有引入可以自行引入相关库)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import root from 'main' //main.js中需要导出 Vue的实例 作为root
import Vue from 'vue'
// bridge
const adapter = {
callHandler(handler, params, cb) {
switch(handler) {
case 'login':
if(IS_WECHAT) {
location.href = 'http://xxxx/wechat?redirect_uri=' +
encodeURIComponent(location.href.replace(/(wxuser=[^&]+&?|user=\d[^&]+&?)/g, ''))
} else if(!IS_PROD) {
root.$emit('popup', 'login', cb)
} else {
location.href = DOWNLOAD_URL
}
break;
case 'user':
let user = store.get('user');
const query = Qs.parse(location.search.replace(/^\?/, ''));
if(query.wxuser && query.wxuser !== 'undefined') {
const wxuser = JSON.parse(decodeURIComponent(query.wxuser));
store.set('user', wxuser);
cb(wxuser)
} else if(user) {
cb(user)
} else {
cb()
}
break
case 'toast':
root.$emit('popup', 'toast', params)
break
case 'shareable':
console.log('shareable', params)
// 注册微信分享接口
if(IS_WECHAT && IS_PROD) {
new Promise(resolve => {
if(window.wx) {
resolve()
} else {
const script = document.createElement('script')
script.setAttribute('type', 'text/javascript')
script.setAttribute('src', '//res.wx.qq.com/open/js/jweixin-1.0.0.js')
document.getElementsByTagName('head')[0].appendChild(script)
script.onload = resolve
}
}).then(() => { // 微信脚本加载完成
const [noncestr, timestamp, url] = ['myxx-!@#$#', Math.round(Date.now() / 1000), location.href]
const formData = new FormData()
formData.append('noncestr', noncestr)
formData.append('timestamp', timestamp)
formData.append('url', url)
axios.post('wx/jsapisignature', formData)
.then(resp => {
wx.config({
appId: 'wxcc40bf300d6200a3',
timestamp: timestamp,
nonceStr: noncestr,
signature: resp.signature,
jsApiList: [
'checkJsApi',
'onMenuShareTimeline',
'onMenuShareAppMessage',
'onMenuShareQQ',
'onMenuShareWeibo',
'onMenuShareQZone'
]
})
// 注册分享接口
const shareOpts = {
title: params.title,
desc: params.desc,
link: params.url,
imgUrl: /^http/.test(params.icon) ? params.icon : `http://xxxxx/${data.icon}?imageView2/1/w/310`,
success: () => {
if(/invitation/.test(location.pathname)) {
axios.post('log/share', {id: params.id, type: 'h5', channel: 'wechat'}).then(resp => {console.log('re-share', resp)})
}
}
}
wx.ready(() => {
wx.onMenuShareAppMessage(shareOpts)
wx.onMenuShareTimeline(shareOpts)
wx.onMenuShareQQ(shareOpts)
wx.onMenuShareWeibo(shareOpts)
wx.onMenuShareQZone(shareOpts)
})
})
})
} else {
console.log('shareable', params)
}
break
case 'share':
root.$emit('popup', 'share', params)
break
case 'keyboard':
root.$emit('popup', 'keyboard', params, cb)
break
case 'confirm':
root.$emit('popup', 'confirm', params, cb)
break
case 'regular':
root.$emit('popup', 'regular', cb)
break
default:
console.debug('unknow handler', handler)
}
}
}
const bridgeReady = () => {
return new Promise(resolve => {
if(!IS_APP) {
resolve(adapter)
} else if(IS_IOS) {
if (window.WebViewJavascriptBridge) {
resolve(window.WebViewJavascriptBridge)
} else {
window.WVJBCallbacks = [resolve]
var WVJBIframe = document.createElement('iframe')
WVJBIframe.style.display = 'none'
WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__'
document.documentElement.appendChild(WVJBIframe)
setTimeout(() => { document.documentElement.removeChild(WVJBIframe) }, 0)
}
} else {
(cb => {
if (window.WebViewJavascriptBridge) {
cb(window.WebViewJavascriptBridge)
} else {
window.document.addEventListener('WebViewJavascriptBridgeReady', () => cb(window.WebViewJavascriptBridge), false)
}
})(bridge => {
resolve(bridge)
if(!bridge.initalized) {
bridge.initalized = true // 防止bridge多次初始化报错
bridge.init(_.noop)
}
})
}
})
}
export const bridge = (handler, params='') => {
let callback = undefined
return new Promise(resolve => {
switch(handler) {
case 'user':
callback = resp => {
const user = parseJson(resp)
store.set('user', user)
resolve(user)
}
break
case 'login':
store.remove('user')
callback = resp => {
resolve(parseJson(resp))
}
break
case 'keyboard':
case 'confirm':
callback = resp => {
resolve(resp)
}
break
default:
resolve(true)
}
// 所有参数采用字符串形式传递
params = _.isObject(params) ? _.mapValues(params, _.toString) : _.toString(params)
if (handler === 'share' || handler === 'shareable') {
params.url = params.url || location.href
}
bridgeReady().then(bridge => bridge.callHandler.apply(this, _.without([handler, params, callback], undefined)))
})
}
Vue.use({
install(Vue, options) {
Vue.prototype.$bridge = bridge
}
})

main.js

1
2
3
4
5
6
7
8
import './bridge' //引入bridge.js文件
const app = new Vue({
router,
...App
}).$mount('#app')
export default app

Vue中使用

1
2
3
4
5
6
7
8
9
10
11
12
13
this.$bridge('user').then(user => {
if(user) {
this.self = _.isObject(user) ? user : JSON.parse(user)
}
})
this.$on('popup', (handler, params={}, cb=undefined) => {
if(_.isFunction(params)) {
cb = params
params = {}
}
this.popup = { handler, ...params, cb }
})