一个Android平台上Weex容器,实现MPA,Bundle缓存、验签等通用功能。
http://www.masonliu.com/app/digua/download
https://github.com/MasonLiuChn/WeexExplorer
//example
WeexPageActivity.startFrom(
LauncherActivity.this,
//"file://local/weex/main.js",
//"file://sdcard/xx",
"http://192.168.12.20:10004/dist/pages/main.js",
null);
public void onBackPressed() {
if (mWXSDKInstance != null && renderSuccess) {
Map<String, Object> params = new HashMap<>();
params.put("name", "returnmsg");
mWXSDKInstance.fireGlobalEventCallback("androidback", params);
} else {
super.onBackPressed();
}
}
const globalEvent = weex.requireModule('globalEvent');
export default {
created() {
this.globalEvent = globalEvent;
this.globalEvent.addEventListener("androidback", e => {
this.router.back()
});
},
methods: {}
}
const basePath = 'http://192.168.12.20:10004/dist';
export default [
{path: '/', component: basePath + '/pages/index.js'},
{path: '/main', component: basePath + '/pages/main.js'}
];
...
router.push('/main')
WeexUtil.setNavigatorPushHandler(new WXNavigatorManager.WXNavigatorPushHandler(){
public void push(WXSDKInstance mWXSDKInstance, Uri uri, String instanceId){
}
});
WeexUtil.setURLIntercepter(url -> {
return url;
});
weex.requireModule("CommonModule").handle('/activity/movieDetail?id=123')
WeexUtil.setCommonModuleHandler((content, mWXSDKInstance, commonModule) -> {
//我这里使用了ARouter处理Native端的跳转
//RouterUtil.go((Activity) mWXSDKInstance.getContext(), content);
});
/*****example jscallback*****/
@JSMethod(uiThread = false)
public void nativeHttpGet(String url, JSCallback callback) {}
WeexUtil.setCacheHandler(new WXLoadAndCacheManager.WXCacheHandler(){
public void cache(InputStream inputStream, String url){
}
});
SDK没有提供默认的验签功能,因为这属于应用方的业务。但SDK提供了拦截Bundle下载的方法,在该方法里开发者可以校验下载Bundle url里https的证书 或者 bundle文件的md5
WeexUtil.setNetworkHandler(new WXLoadAndCacheManager.WXNetworkHandler(){
public InputStream executeDownload(String url) throws Exception{
return null;
}
});
WeexUtil.init(this,true,BuildConfig.BUILD_IP,null);
网络请求模块除了weex自带的stream,还额外提供了nativeHttpGet方法,使用okhttp做请求,后续会增加post、put、delete等方法
var commonModule=weex.requireModule("CommonModule");
commonModule.nativeHttpGet(url,(response)=>{
if(!response.ok){
reject(response)
}else{
resolve(response)
}
}
);
repositories {
maven { url "https://jitpack.io" }
maven { url "https://github.com/MasonLiuChn/MasonMavenRepository/raw/maven/releases" }
}
dependencies {
compile 'com.github.MasonLiuChn:WeexContainer-Android:1.0.0'
}
//在Applicaiton里设置 WeexUtil.init(this, false, BuildConfig.BUILD_IP,null);
WeexPageActivity.startFrom(
LauncherActivity.this,
"http://192.168.12.20:10004/dist/pages/main.js",
null);
public static void init(Application application,
boolean connectDebuggerOnAppDebug,
@Nullable String debuggerHost,
@Nullable IWXImgLoaderAdapter iwxImgLoaderAdapter) {
}
public static void setDebugable(boolean isDebug) {
}
public static void setNavigatorPushHandler(WXNavigatorManager.WXNavigatorPushHandler handler) {
}
public static void setURLIntercepter(WXURLManager.WXURLHandler handler) {
}
public static void setCommonModuleHandler(WXCommonModuleManager.WXCommonModuleHandler handler) {
}
public static void setCacheHandler(WXLoadAndCacheManager.WXCacheHandler handler) {
}
public static void setNetworkHandler(WXLoadAndCacheManager.WXNetworkHandler handler) {
}
Blog: http://www.masonliu.com
Email: MasonLiuChn@gmail.com