文档

设置 AP 数据

更新时间:

setAPDataStorage 接口用于保存一个字符串到客户端统一存储,字符串长度不得超过 200×1024。

说明

  • 底层存储服务组件在 iOS 和 Android 中实现不一致。Android 统一存储组件不支持 type=user 属性,为了与前端接口一致,当设置 type=user 时,Android 底层会设置为 key=key + “_” +MD5(userId + userId + userId) 并进行存储。业务用客户端取的时候也要对 key 做相应处理。

  • 在 10.1.60 及以下版本的基线中,客户端需要做适配工作才能使接口能够获得 userId,否则存储接口将无法按 userId 区分存储,参见下方 实现 H5LoginProvider 接口

  • 在 10.1.68 及以上版本的基线中,userId 默认使用 MPLogger.setUserId 中的值,若实现 H5LoginProvider,则取用 H5LoginProvider。

实现 H5LoginProvider 接口

Android

实现 H5LoginProvider 接口,并将实例类设置到 H5ProviderManager 中。

代码示例

package com.mpaas.nebula.provider;

import android.os.Bundle;

import com.alipay.mobile.common.logging.api.LoggerFactory;
import com.alipay.mobile.nebula.provider.H5LoginProvider;

public class H5LoginProviderImpl implements H5LoginProvider {
    // 其他代码省略

    @Override
    public String getUserId() {
        // 此方法返回 userId 即可
        return LoggerFactory.getLogContext().getUserId();
    }

    // 其他代码省略
}

设置 H5LoginProvider

H5Utils.setProvider(H5LoginProvider.class.getName(), new H5LoginProviderImpl());

setAPDataStorage 接口的使用方法

AlipayJSBridge.call('setAPDataStorage', {
  type: "common",
  business: "customBusinessKey",
  key: "customKey",
  value: "customValue"
}, function(result) {
  alert(JSON.stringify(result));
});

代码示例

<button id="J_saveDataBtn" class="btn">保存数据</button>
<button id="J_getDataBtn" class="btn">查看数据</button>
<button id="J_removeDataBtn" class="btn">删除数据</button>

<script>
function ready(callback) {
  // 如果 jsbridge 已经注入则直接调用
  if (window.AlipayJSBridge) {
    callback && callback();
  } else {
    // 如果没有注入则监听注入的事件
    document.addEventListener('AlipayJSBridgeReady', callback, false);
  }
}
ready(function() {
  document.querySelector('#J_saveDataBtn').addEventListener('click', function(e) {
    AlipayJSBridge.call('setAPDataStorage', {
      type: "common",
      business: "customBusinessKey",
      key: "customKey",
      value: "customValue"
    }, function(result) {
      alert(JSON.stringify(result));
    });
  }, false);

  document.querySelector('#J_getDataBtn').addEventListener('click', function(e) {
    AlipayJSBridge.call('getAPDataStorage', {
      type: "common",
      business: "customBusinessKey",
      key: "customKey"
    }, function(result) {
      alert(JSON.stringify(result));
    });
  }, false);

  document.querySelector('#J_removeDataBtn').addEventListener('click', function(e) {
    AlipayJSBridge.call('removeAPDataStorage', {
      type: "common",
      business: "customBusinessKey",
      key: "customKey"
    }, function(result) {
      alert(JSON.stringify(result));
    });
  }, false);
}, false);
</script>

API 说明

AlipayJSBridge.call('setAPDataStorage', {
  type, business, key, value
});

入参

属性

类型

描述

必填

默认值

type

String

(user、common) 用户维度存储还是公共存储。

N

“common”

business

String

自定义的业务标识,可与相应的客户端存取代码约定,默认值为:NebulaBiz。

在 Android 中,该业务标识与创建 APSharedPreferences 时所传入的 GROUP_ID 对应。

N

“”

key

String

自定义数据的 key。

Y

“”

value

String

需要存储的值,仅支持字符串类型。JSON 数据需要先字符串化。

Y

“”

出参

回调函数带入的参数 result: {success}

属性

类型

描述

success

bool

是否保存成功。

错误码

错误码

描述

11

字符串长度超出限制。

  • 本页导读 (0)
文档反馈