react单页应用与app原生通信的总结
程序员文章站
2022-04-21 10:22:02
...
import React,{Component} from 'react'
import {connect} from 'react-redux'
import NavBars from '@/coms/nav_bar'
import axios from 'axios'
import {Toast} from 'antd-mobile'
import Qs from 'qs'
//需要回调
function controllerCenterCallBack(type,cmd,params){
if(navigator.userAgent.indexOf("Android")>0){//判断是安卓浏览器
eval('javascript:QM_APP_WEBVIEW_ENGINE_'+type+'.'+cmd+'("'+params+'")');
}
if(navigator.userAgent.toLowerCase().indexOf('iphone')>0){
eval('window.webkit.messageHandlers.'+cmd+'.postMessage("'+params+'")');
}
}
//不需要回调
function controllerCenter(type,cmd){
if(navigator.userAgent.indexOf("Android")>0){//判断是安卓浏览器
eval('javascript:QM_APP_WEBVIEW_ENGINE_'+type+'.'+cmd+'()');
}
if(navigator.userAgent.toLowerCase().indexOf('iphone')>0){
eval('window.webkit.messageHandlers.'+cmd+'.postMessage("")');
}
}
function scan(params){
controllerCenterCallBack('scan','scan_bar',params);
}
class Source extends Component{
constructor(props){
super(props)
this.state={}
// app扫一扫完成后调用js的方法并返回扫描后的信息
window.getQcodeInfo = (res) =>this.callback(res)
}
// 发起扫一扫后获取app返回的二维码用户信息
callback =(res)=>{
const resultId = JSON.parse(JSON.stringify(res)).Result // 拿到扫描后的
}
// 调用app端扫描二维码
toApp = ()=> {
scan("getQcodeInfo")
}
componentWillMount(){
}
componentDidMount(){
}
goBack=()=>this.props.history.go(-1)
render(){
return(
<div className='outer law_check'>
<div className='sao_btn' onClick={this.toApp}>
<i className='iconfont icon-saoyisaoerweimasaomasaomiao1 sao_icon'></i>
开始扫描
</div>
</div>
)
}
}
const mapStateToProps = ()=> ({
})
const mapDispatchToProps = () => ({
sendQcodeINfo(){
}
})
export default connect(mapStateToProps,mapDispatchToProps)(Source)
上一篇: RiftSaw 2.0.0 最终版发布
下一篇: Text 可以优化的细节
推荐阅读