想必做App应用开发的,接触过微信小程序的,都知道app应用内可以跳转到微信小程序界面。这方面的应用知识,官方也给了很详细的文档说明。微信小程序官网
题目所示的跳转动态微信小程序,意思是跳转的页面布局固定,但是跳转的微信小程序不固定,微信小程序的id是通过后台接口,动态获取的,这样做的目的是不用因为小程序更换了,相应的app也要做新版本。后台数据接口处理好就ok。
看上面两张图片,图一是固定的跳转到应用内的页面,等审核通过,下个版本就可以换成跳转小程序的(有人问为什么下个版本上,不这个版本上,审核呗)。图二就是可以跳转到微信小程序的apk,上面的试发型、换发型、发型设计、脸型分析的名字是动态获取的,图标也可以动态获取(图片动态获取这个我们的项目还没弄,后面应该会加上),然后就是点击跳转到相应的微信小程序了。下面上代码(废话一堆,终于说到重点了,-_-||)
private void requestApplet() {
RestClient.builder()//数据请求
.url("这里填获取微信小程序id的接口")
.success(this::handleApplet)
.build()
.post();
}
private void handleApplet(String response) {
JLogger.e(response);//返回数据解析
JSONObject jsonObject = JSON.parseObject(response);
if (JConstants.OK.equals(jsonObject.getString("code"))) {
try {
final JSONArray jsonArray = jsonObject.getJSONArray("data");
final int size = jsonArray.size();
final List<AppletEntity> dataList = new ArrayList<>();
for (int i = 0; i < size; i++) {
final JSONObject jsonData = jsonArray.getJSONObject(i);
final String name = jsonData.getString("name");//获取小程序名字
final String initial_id = jsonData.getString("initial_id");//获取小程序id
AppletEntity entity = new AppletEntity(name, initial_id);//AppletEntity 实体类
dataList.add(entity);
}
List<AppletEntity> convert = IndexHeaderDataUtil.convert(dataList);//IndexHeaderDataUtil 工具类,避免if else语句
//获取小程序1、小程序2、小程序3
AppletEntity entity = convert.get(0);
AppletEntity entity1 = convert.get(1);
AppletEntity entity2 = convert.get(2);
((TextView) headerView.findViewById(R.id.tv_free_exchange)).setText(entity.getName());
((TextView) headerView.findViewById(R.id.tv_hair_design)).setText(entity1.getName());
((TextView) headerView.findViewById(R.id.tv_test_face)).setText(entity2.getName());
//点击事件
headerView.findViewById(R.id.tv_free_exchange).setOnClickListener(view -> onClickFreeExchange(entity));
headerView.findViewById(R.id.tv_hair_design).setOnClickListener(view -> startAction(entity1));
headerView.findViewById(R.id.tv_test_face).setOnClickListener(view -> startAction(entity2));
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void onClickFreeExchange(AppletEntity entity) {
if (TextUtils.isEmpty(entity.getInitial_id())) {
startSignGoldDelegate();
} else {
startAction(entity);
}
}
private void startSignGoldDelegate() {
if (LoginHelper.isLogin()) {
String uid = LoginHelper.uid();
WebViewNoTitleDelegate delegate = WebViewNoTitleDelegate.create("签到",
"http://wap.faxingw.cn/wapapp.php?g=Newunder&m=sign&a=index&device=android&uid=" + uid);
getParentDelegate().getSupportDelegate().start(delegate);
} else if (launcherListener != null) {
launcherListener.onLauncherFinish(OnLauncherFinishTag.NOT_SIGNED);
}
}
private void startAction(AppletEntity entity) {
String appId = Jumei.getConfiguration(ConfigKeys.WE_CHAT_APP_ID); // 填应用AppId
IWXAPI api = WXAPIFactory.createWXAPI(getContext(), appId);
WXLaunchMiniProgram.Req req = new WXLaunchMiniProgram.Req();
req.userName = entity.getInitial_id(); // 填小程序原始id
req.miniprogramType = WXLaunchMiniProgram.Req.MINIPTOGRAM_TYPE_RELEASE;// 可选打开 开发版,体验版和正式版
api.sendReq(req);
}
AppletEntity:
package com.jm.ec.main.index.applet;
public class AppletEntity {
private String id;
private String name;
private String initial_id;
private String status;
private String sort;
private String time;
public AppletEntity(String name, String initial_id) {
this.name = name;
this.initial_id = initial_id;
}
public String getId() {
return id;
}
public AppletEntity setId(String id) {
this.id = id;
return this;
}
public String getName() {
return name;
}
public AppletEntity setName(String name) {
this.name = name;
return this;
}
public String getInitial_id() {
return initial_id;
}
public AppletEntity setInitial_id(String initial_id) {
this.initial_id = initial_id;
return this;
}
public String getStatus() {
return status;
}
public AppletEntity setStatus(String status) {
this.status = status;
return this;
}
public String getSort() {
return sort;
}
public AppletEntity setSort(String sort) {
this.sort = sort;
return this;
}
public String getTime() {
return time;
}
public AppletEntity setTime(String time) {
this.time = time;
return this;
}
}
IndexHeaderDataUtil:
public class IndexHeaderDataUtil {
public static List<AppletEntity> convert(List<AppletEntity> list) {
int size = list.size();
if (size >= 3) {
return list;
} else {
list.add(0, new AppletEntity("免费兑换", ""));
if (size == 0) {
list.add(1, new AppletEntity("测脸型", "小程序id"));
list.add(2, new AppletEntity("发型设计", "小程序id"));
} else if (size == 1) {
list.add(1, new AppletEntity("测脸型", "小程序id"));
}
}
return list;
}
}
以上即为跳转动态小程序的主要方法。