tencent cloud

开放接口
最后更新时间:2025-12-05 22:18:02
开放接口
最后更新时间: 2025-12-05 22:18:02
小程序 SDK 提供部分开放接口,用于调用和实现登录、获取用户信息、支付等 superapp 提供的能力。
注意:
从 SDK 的 2.1.0 版本开始,SDK 接口默认实现了登录(wx.login)、用户信息( wx.getPhoneNumber、wx.getEmail、wx.chooseAvatar、wx.getNickName )和登录态检查(wx.checkSession)的默认实现,用户可以通过简单的配置就完成小程序用户登录和 superapp 用户信息的相关逻辑串联。
2.1.0版本开始的默认实现,仅在公有云版本支持。

SDK 2.1.0及之后版本

SDK 2.1.0及之前版本支持自定义的开放接口如下表格所示:
小程序方法
MiniOpenApiProxy 代理实现方法
描述
默认实现
wx.login
login
登录接口 SDK
2.1.0版本起,提供默认实现
wx.getUserInfo
getUserInfo
获取用户基本信息
无默认实现
wx.getUserProfile
getUserProfile
获取用户属性信息
无默认实现
wx.getPhoneNumber
getPhoneNumber
获取电话号码
2.1.0版本起,提供默认实现
wx.requestPayment
requestPayment
发起支付
无默认实现
wx.requestMidasPaymentGameItem
requestMidasPaymentGameItem
发起小游戏支付
2.2.4版本起支持,无默认实现
wx.checkSession
checkSession
检查登录状态是否过期
2.1.0版本起,提供默认实现
wx.getEmail
getEmail
获取用户邮箱
2.1.0版本起,提供默认实现
wx.chooseAvatar
chooseAvatar
获取用户头像
2.1.0版本起,提供默认实现
wx.getNickName
getNickName
获取用户昵称
2.1.0版本起,提供默认实现
wx.requestVirtualPayment
requestVirtualPayment
小程序虚拟支付
2.2.17版本起支持,无默认实现
应用开发者可以通过重写 MiniOpenApiProxy 的代理实现,实现获取用户基本信息(wx.getUserInfo)、获取用户属性信息(wx.getUserProfile)、发起支付(wx.requestPayment)、发起虚拟支付(wx.requestVirtualPayment)以及小游戏支付(wx.requestMidasPaymentGameItem)的逻辑。
@ProxyService(proxy = MiniOpenApiProxy.class)
public class MiniOpenApiProxyImpl extends MiniOpenApiProxy {
private static final String TAG = "MiniOpenApiProxyImpl";

@Override
public void getUserInfo(IMiniAppContext miniAppContext, JSONObject params, AsyncResult result) {
QMLog.d(TAG, "getUserInfo:" + params);
JSONObject jsonObject = new JSONObject();
try {
final JSONObject userInfo = new JSONObject();
TmfMiniSDK.callMainProcessPlugin(OpenDataIPC.OPEN_DATA_IPC_EVENT_GET_USER_ID, new Bundle(), new IpcCallback() {
@Override
public void result(boolean b, Bundle bundle) {
try {
userInfo.put("nickName", bundle.getString("userId"));
userInfo.put("avatarUrl", bundle.getString("avatarUrl"));
} catch (JSONException e) {
e.printStackTrace();
}
}
});
userInfo.put("gender", 0);
userInfo.put("country", "CN");
userInfo.put("province", "BeiJing");
userInfo.put("city", "BeiJing");
userInfo.put("language", "en");
jsonObject.put("userInfo", userInfo);
} catch (JSONException e) {
e.printStackTrace();
}
result.onReceiveResult(true, jsonObject);
}

@Override
public void getUserProfile(IMiniAppContext miniAppContext, JSONObject params, AsyncResult result) {
QMLog.d(TAG, "getUserProfile:" + params);
JSONObject jsonObject = new JSONObject();
try {
final JSONObject userInfo = new JSONObject();
TmfMiniSDK.callMainProcessPlugin(OpenDataIPC.OPEN_DATA_IPC_EVENT_GET_USER_ID, new Bundle(), new IpcCallback() {
@Override
public void result(boolean b, Bundle bundle) {
try {
userInfo.put("nickName", bundle.getString("userId"));
userInfo.put("avatarUrl", bundle.getString("avatarUrl"));
} catch (JSONException e) {
e.printStackTrace();
}
}
});
userInfo.put("gender", 0);
userInfo.put("country", "CN");
userInfo.put("province", "BeiJing");
userInfo.put("city", "BeiJing");
userInfo.put("language", "en");
jsonObject.put("userInfo", userInfo);
} catch (JSONException e) {
e.printStackTrace();
}
result.onReceiveResult(true, jsonObject);
}

@Override
public void requestPayment(IMiniAppContext miniAppContext, JSONObject params, AsyncResult result) {
QMLog.d(TAG, "requestPayment:" + params);
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("key", "wx.requestPayment");
} catch (JSONException e) {
e.printStackTrace();
}
PaymentManager.g(miniAppContext.getContext()).startPayment(miniAppContext, params, result);
}
@Override
public void requestMidasPaymentGameItem(IMiniAppContext miniAppContext, JSONObject params, AsyncResult result) {
// call your custom payment implementation
boolean paySuccess = PaymentManagerV2.g().startMidasPayment(miniAppContext, params, result);
// notify payment result with AsynResult
if(paySuccess){
result.onReceiveResult(true,successData);
}else{
result.onReceiveResult(false,failedData);
}
}
@Override
public void requestVirtualPayment(IMiniAppContext miniAppContext, JSONObject params, AsyncResult result) {
// call your custom payment implementation
boolean paySuccess = PaymentManagerV2.g().startMidasPayment(miniAppContext, params, result);
// notify payment result with AsynResult
if(paySuccess){
result.onReceiveResult(true,successData);
}else{
result.onReceiveResult(false,failedData);
}
}

}
为了使用 SDK 默认的登录逻辑,开发者需要通过重写 MiniAppProxy 的 getAccount 方法,将 superapp 的 UID 传给 SDK,SDK 会将此 UID 用于和小程序后台服务交换和获取用户信息。
@ProxyService(proxy = MiniAppProxy.class)
public class MiniAppProxyImpl extends BaseMiniAppProxyImpl {
@Override
public String getAccount() {
//superapp的用户标识符
String uid = getHostAppUserId();
return uid;
}
}

SDK 2.0.15及之前的版本

SDK 2.0.15及之前版本支持自定义的开放接口如下表格所示:
小程序方法
MiniOpenApiProxy 方法
描述
wx.login
login
登录接口
wx.getUserInfo
getUserInfo
获取用户基本信息
wx.getUserProfile
getUserProfile
获取用户属性信息
wx.getPhoneNumber
getPhoneNumber
获取电话号码
wx.requestPayment
requestPayment
发起支付
wx.checkSession
checkSession
检查登录状态是否过期
用户可以通过实现 MiniOpenApiProxy 代理,将小程序和 superapp 的数据交互进行关联,示例代码如下:
说明:
参数 IMiniAppContext 为当前小程序的上下文信息;
参数 JSONObject 为小程序调用开放接口时传递的参数;
参数 AsyncResult 为异步回调接口,用户需要通过此接口将superapp实现开放接口的返回值返回给小程序。
@ProxyService(proxy = MiniOpenApiProxy.class)
public class MiniOpenApiProxyImpl implements MiniOpenApiProxy {
private static final String TAG = "MiniOpenApiProxyImpl";
@Override
public void login(IMiniAppContext miniAppContext, JSONObject params, AsyncResult result) {
QMLog.d(TAG, "login:" + params);
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("key", "wx.login");
} catch (JSONException e) {
e.printStackTrace();
}
result.onReceiveResult(true, jsonObject);
}

@Override
public void checkSession(IMiniAppContext miniAppContext, JSONObject params, AsyncResult result) {
QMLog.d(TAG, "checkSession:" + params);
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("key", "wx.checkSession");
} catch (JSONException e) {
e.printStackTrace();
}
result.onReceiveResult(true, jsonObject);
}

@Override
public void getUserInfo(IMiniAppContext miniAppContext, JSONObject params, AsyncResult result) {
QMLog.d(TAG, "getUserInfo:" + params);
JSONObject jsonObject = new JSONObject();
try {
final JSONObject userInfo = new JSONObject();

userInfo.put("nickName", "userInfo测试");
// userInfo.put("avatarUrl", bundle.getString("avatarUrl"));

userInfo.put("gender", 0);
userInfo.put("country", "CN");
userInfo.put("province", "BeiJing");
userInfo.put("city", "BeiJing");
userInfo.put("language", "en");
jsonObject.put("userInfo", userInfo);
} catch (JSONException e) {
e.printStackTrace();
}
result.onReceiveResult(true, jsonObject);
}

@Override
public void getUserProfile(IMiniAppContext miniAppContext, JSONObject params, AsyncResult result) {
QMLog.d(TAG, "getUserProfile:" + params);
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("key", "wx.getUserProfile");
} catch (JSONException e) {
e.printStackTrace();
}
result.onReceiveResult(true, jsonObject);
}

@Override
public void getPhoneNumber(IMiniAppContext miniAppContext, JSONObject params, AsyncResult result) {
QMLog.d(TAG, "getPhoneNumber:" + params);
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("key", "wx.getPhoneNumber");
} catch (JSONException e) {
e.printStackTrace();
}
result.onReceiveResult(true, jsonObject);
}

@Override
public void requestPayment(IMiniAppContext miniAppContext, JSONObject params, AsyncResult result) {
QMLog.d(TAG, "requestPayment:" + params);
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("key", "wx.requestPayment");
} catch (JSONException e) {
e.printStackTrace();
}
result.onReceiveResult(true, jsonObject);
}

自定义登录逻辑的兼容处理

由于 SDK 在 2.1.0 及之后的版本默认会使用内置的登录和用户信息获取相关逻辑,存量客户在更新 SDK 版本至2.1.0及之后的版本时,如果已经通过重写(MiniOpenApiProxy)代理,使用自定义的用户登录逻辑实现,必须通过下面的方式配置继续使用自定义的登录和用户信息获取逻辑。
@ProxyService(proxy = MiniOpenApiProxy.class)
public class MiniOpenApiProxyImpl extends MiniOpenApiProxy {
/**
* 指定默认 api 版本
*
* @param miniAppContext 小程序上下文,可以获取当前小程序信息、小程序绑定的Activity等
* @return 合法值: {@link #API_LEVEL_UNDEFINED}, {@link #API_LEVEL_V1}, {@link #API_LEVEL_V2}, {@link #API_LEVEL_V3}
*/
@Override
String defaultApiLevel(IMiniAppContext miniAppContext) {
return MiniOpenApiProxy.API_LEVEL_V1;
}
}
SDK 2.2.11 版本起废弃了 MiniOpenApiProxyV2接口,并将 MiniOpenApiProxy 由接口调整为抽象类,如果存量客户有实现 MiniOpenApiProxyV2代理,在更新 SDK 版本至 2.2.11 版本之后,需要将 MiniOpenApiProxyV2代理中的实现统一迁移到 MiniOpenApiProxy 代理实现中。

本页内容是否解决了您的问题?
您也可以 联系销售 提交工单 以寻求帮助。

文档反馈