公众号
This commit is contained in:
parent
fbe03f443f
commit
f989615ba0
@ -22,6 +22,10 @@
|
||||
android:name="notch.config"
|
||||
android:value="portrait|landscape" />
|
||||
|
||||
<activity
|
||||
android:name=".core.widget.base.CommonWebActivity"
|
||||
android:exported="true" />
|
||||
|
||||
<activity
|
||||
android:name=".core.widget.base.ConditionActivity"
|
||||
android:exported="true" />
|
||||
|
@ -0,0 +1,217 @@
|
||||
package com.sucstepsoft.cm_utils.core.widget.base;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.net.http.SslError;
|
||||
import android.os.Build;
|
||||
import android.text.TextUtils;
|
||||
import android.webkit.SslErrorHandler;
|
||||
import android.webkit.WebResourceError;
|
||||
import android.webkit.WebResourceRequest;
|
||||
import android.webkit.WebSettings;
|
||||
import android.webkit.WebView;
|
||||
import android.webkit.WebViewClient;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.sucstepsoft.cm_utils.R;
|
||||
import com.sucstepsoft.cm_utils.constant.PathConfig;
|
||||
import com.sucstepsoft.cm_utils.core.beans.AccessToken;
|
||||
import com.sucstepsoft.cm_utils.core.beans.PostWeChatTicketBean;
|
||||
import com.sucstepsoft.cm_utils.core.beans.TicketBean;
|
||||
import com.sucstepsoft.cm_utils.core.retrofit_net.BaseUrlApi;
|
||||
import com.sucstepsoft.cm_utils.core.retrofit_net.RetrofitManager;
|
||||
import com.sucstepsoft.cm_utils.core.retrofit_net.api.BaseApiService;
|
||||
import com.sucstepsoft.cm_utils.utils.ExceptionHandler;
|
||||
import com.sucstepsoft.cm_utils.utils.LogUtils;
|
||||
|
||||
import io.reactivex.Observer;
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.disposables.Disposable;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.RequestBody;
|
||||
|
||||
public class CommonWebActivity extends BaseActivity {
|
||||
|
||||
private WebView mWvWeb;
|
||||
|
||||
|
||||
@Override
|
||||
protected int setLayoutId() {
|
||||
return R.layout.activity_common_web;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initData() {
|
||||
mTvBaseTitle.setText("公众号");
|
||||
this.mWvWeb = findViewById(R.id.wv_web);
|
||||
WebSettings settings = this.mWvWeb.getSettings();
|
||||
settings.setJavaScriptEnabled(true);
|
||||
settings.setDomStorageEnabled(true);
|
||||
settings.setJavaScriptCanOpenWindowsAutomatically(true);
|
||||
settings.setAllowFileAccess(true);
|
||||
settings.setSupportMultipleWindows(true);
|
||||
settings.setUseWideViewPort(true);
|
||||
settings.setLoadWithOverviewMode(true);
|
||||
settings.setSupportZoom(false);
|
||||
settings.setBuiltInZoomControls(true);
|
||||
settings.setDisplayZoomControls(false);
|
||||
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
|
||||
if (Build.VERSION.SDK_INT >= 21) {
|
||||
settings.setMixedContentMode(0);
|
||||
}
|
||||
if (Build.VERSION.SDK_INT >= 17) {
|
||||
settings.setMediaPlaybackRequiresUserGesture(false);
|
||||
}
|
||||
this.mWvWeb.setWebViewClient(new WebViewClient() {
|
||||
public void onReceivedSslError(WebView webView, SslErrorHandler sslErrorHandler, SslError sslError) {
|
||||
sslErrorHandler.proceed();
|
||||
}
|
||||
|
||||
public void onPageFinished(WebView webView, String str) {
|
||||
super.onPageFinished(webView, str);
|
||||
LogUtils.e(webView.getUrl() + "===" + str);
|
||||
}
|
||||
|
||||
public boolean shouldOverrideUrlLoading(WebView webView, String str) {
|
||||
webView.loadUrl(str);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void onReceivedError(WebView webView, WebResourceRequest webResourceRequest,
|
||||
WebResourceError webResourceError) {
|
||||
super.onReceivedError(webView, webResourceRequest, webResourceError);
|
||||
if (Build.VERSION.SDK_INT >= 23) {
|
||||
LogUtils.e(webResourceError.getErrorCode() + webResourceError.getDescription().toString());
|
||||
}
|
||||
}
|
||||
|
||||
public void onReceivedError(WebView webView, int i, String str, String str2) {
|
||||
super.onReceivedError(webView, i, str, str2);
|
||||
LogUtils.e(i);
|
||||
}
|
||||
|
||||
public boolean shouldOverrideUrlLoading(WebView webView, WebResourceRequest webResourceRequest) {
|
||||
if (Build.VERSION.SDK_INT < 21) {
|
||||
return false;
|
||||
}
|
||||
webView.loadUrl(webResourceRequest.getUrl().toString());
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
getAccessToken();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取微信小程序accessToken
|
||||
*/
|
||||
private void getAccessToken() {
|
||||
RetrofitManager.getInstance()
|
||||
.create(BaseApiService.class)
|
||||
.getWechatAccessToken(BaseUrlApi.WECHAT_ACCESSTOKEN,
|
||||
"client_credential",
|
||||
PathConfig.WECHAT_APPID,
|
||||
PathConfig.WECHAT_SECERT)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new Observer<AccessToken>() {
|
||||
@Override
|
||||
public void onSubscribe(Disposable d) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNext(AccessToken areaBean) {
|
||||
if (!TextUtils.isEmpty(areaBean.getAccess_token())) {
|
||||
getOpenLink(areaBean.getAccess_token());
|
||||
} else {
|
||||
refreshView(STATE_LOAD_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable e) {
|
||||
refreshView(STATE_LOAD_ERROR);
|
||||
ExceptionHandler.handleException(e);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取微信小程序ticket
|
||||
*
|
||||
* @param token
|
||||
*/
|
||||
private void getOpenLink(String token) {
|
||||
LogUtils.e(token);
|
||||
RequestBody body = buildRequestBody();
|
||||
RetrofitManager.getInstance()
|
||||
.create(BaseApiService.class)
|
||||
.getWechatOpenLink(BaseUrlApi.WECHAT_OPENLINK + token,
|
||||
body)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new Observer<TicketBean>() {
|
||||
@Override
|
||||
public void onSubscribe(Disposable d) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNext(TicketBean b) {
|
||||
LogUtils.e(b.getErrcode());
|
||||
if (b.getErrcode() == 0) {
|
||||
refreshView(STATE_LOAD_SUCCESS);
|
||||
startWeChat(b.getOpenlink());
|
||||
} else {
|
||||
refreshView(STATE_LOAD_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable e) {
|
||||
ExceptionHandler.handleException(e);
|
||||
refreshView(STATE_LOAD_ERROR);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void startWeChat(String openlink) {
|
||||
LogUtils.e(openlink);
|
||||
try {
|
||||
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(openlink));
|
||||
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
startActivity(intent);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private RequestBody buildRequestBody() {
|
||||
PostWeChatTicketBean bean = new PostWeChatTicketBean();
|
||||
PostWeChatTicketBean.JumpWxaBean subBean = new PostWeChatTicketBean.JumpWxaBean();
|
||||
subBean.setEnv_version("release");//默认 release 正式版 trial 体验版 develop 开发版
|
||||
// subBean.setPath("/pages/commonQrCode/commonQrCode");
|
||||
subBean.setPath("/pages/heritage/heritage");
|
||||
subBean.setQuery("");
|
||||
bean.setJump_wxa(subBean);
|
||||
bean.setIs_expire(true);
|
||||
bean.setExpire_type(1);
|
||||
bean.setExpire_interval(1);
|
||||
Gson gson = new Gson();
|
||||
String obj = gson.toJson(bean);
|
||||
RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), obj);
|
||||
return body;
|
||||
}
|
||||
}
|
13
cm_utils/src/main/res/layout/activity_common_web.xml
Normal file
13
cm_utils/src/main/res/layout/activity_common_web.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".core.widget.base.CommonWebActivity">
|
||||
<WebView
|
||||
android:id="@+id/wv_web"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"/>
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
Loading…
Reference in New Issue
Block a user