# Flutter API

# 1. Initialize Mini-Program

Before using the APIs provided by the SDK, it is mandatory to initialize the SDK. The interface for initializing the SDK is:

  ///
  ///
  /// initialize mop miniprogram engine.
  /// Initialize Mini-Program
  /// [sdkkey] is required. it can be getted from api.finclip.com
  /// [secret] is required. it can be getted from api.finclip.com
  /// [apiServer] is optional. the mop server address. default is https://mp.finogeek.com
  /// [apiPrefix] is optional. the mop server prefix. default is /api/v1/mop
  /// [cryptType] is optional. cryptType, should be MD5/SM
  /// [disablePermission] is optional.
  /// [encryptServerData] Whether to encrypt server data, requires server support
  /// [userId] User ID
  /// [finStoreConfigs] Multi-service configuration
  /// [uiConfig] UI configuration
  /// [debug] Set debug mode, affects debugging and logging
  /// [customWebViewUserAgent] Set custom WebView User Agent
  /// [appletIntervalUpdateLimit] Set the batch update cycle for Mini-Programs
  /// [maxRunningApplet] Set the maximum number of concurrently running Mini-Programs
  ///
  Future<Map> initialize(
    String sdkkey,
    String secret, {
    String? apiServer,
    String? apiPrefix,
    String? cryptType,
    bool encryptServerData = false,
    bool disablePermission = false,
    String? userId,
    bool debug = false,
    bool bindAppletWithMainProcess = false,
    List<FinStoreConfig>? finStoreConfigs,
    UIConfig? uiConfig,
    String? customWebViewUserAgent,
    int? appletIntervalUpdateLimit,
    int? maxRunningApplet,
  })

Usage example:

final res = await Mop.instance.initialize(
          '22LyZEib0gLTQdU3MUauARlLry7JL/2fRpscC9kpGZQA', '1c11d7252c53e0b6',
          apiServer: 'https://api.finclip.com', apiPrefix: '/api/v1/mop');

# 2. Open Mini-Program

  /// open the miniprogram [appId] from the  mop server.
  /// Open Mini-Program
  /// [appId] is required.
  /// [path] is miniprogram open path. example /pages/index/index
  /// [query] is miniprogram query parameters. example key1=value1&key2=value2
  /// [sequence] is miniprogram sequence. example 0,1.2.3,4,5...
  /// [apiServer] is optional. the mop server address. default is https://mp.finogeek.com
  /// [apiPrefix] is optional. the mop server prefix. default is /api/v1/mop
  /// [fingerprint] is optional. the mop sdk fingerprint. is nullable
  /// [cryptType] is optional. cryptType, should be MD5/SM
  Future<Map> openApplet(
    final String appId, {
    final String? path,
    final String? query,
    final int? sequence,
    final String? apiServer,
    final String? scene,
  })

# 3. Get Current Running Mini-Program Information

The current Mini-Program information includes fields: appId, name, icon, description, version, thumbnail

  ///
  ///  get current using applet
  ///  Get current running Mini-Program information
  ///  {appId,name,icon,description,version,thumbnail}
  ///
  ///
  Future<Map<String, dynamic>> currentApplet()

# 4. Close All Currently Opened Mini-Programs

  ///
  /// close all running mini programs
  /// Close all currently opened Mini-Programs
  ///
  Future closeAllApplets()

# 5. Clear Cached Mini-Programs

Clear cached Mini-Programs. When opened again, the Mini-Program will be re-downloaded.

  ///
  /// clear mini programs cache
  /// Clear cached Mini-Programs
  ///
  Future clearApplets()

# 6. Register Mini-Program Event Handlers

When a specific event is triggered within the Mini-Program, the user will be notified. For example, when a Mini-Program is shared, or when the Mini-Program requires user information. Register handlers to provide corresponding responses.

 ///
  /// register handler to provide custom info or behaviour
  /// Register Mini-Program event handlers
  ///
  void registerAppletHandler(AppletHandler handler)

Handler structure:

abstract class AppletHandler {
  ///
  /// Share Mini-Program
  ///
  ///
  ///
  void forwardApplet(Map<String, dynamic> appletInfo);

  ///
  /// Get user information
  ///  "userId"
  ///  "nickName"
  ///  "avatarUrl"
  ///  "jwt"
  ///  "accessToken"
  ///
  Future<Map<String, dynamic>> getUserInfo();

  /// Get custom menus
  Future<List<CustomMenu>> getCustomMenus(String appId);

  /// Handle custom menu clicks
  Future onCustomMenuClick(String appId, int menuId);
}

# 7. Register Extension APIs

If the APIs provided by our Mini-Program SDK do not meet your requirements, you can register custom Mini-Program APIs. Then, you can call your own defined APIs within the Mini-Program.

///
/// register extension api
/// Register extension APIs
///
void registerExtensionApi(String name, ExtensionApiHandler handler) ···

For iOS, you need to create a FinClipConf.js file in the Mini-Program root directory. Configuration example:

module.exports = {
  extApi:[
    { // General interaction API
      name: 'onCustomEvent', // Extension API name, this API must be implemented by the Native side
      params: { // Parameter format for the extension API, required
        url: ''
      }
    }
  ]
}

For more configuration information about custom APIs on the Mini-Program side, please refer to ft.loadExtApi

# 8. Register WebView Extension APIs

Custom APIs can also be used within WebViews inside Mini-Programs, simply by using:

  ///
  /// register webview extension api
  /// Register WebView extension APIs
  ///
  void addWebExtentionApi(String name, ExtensionApiHandler handler)

# 9. Native Calls JS Methods in WebView

  ///
  /// Native calls JS methods in WebView
  /// [appId] Mini-Program ID
  /// [eventName] Method name
  /// [nativeViewId] WebView ID
  /// [eventData] Parameters
  ///
  Future<void> callJS(String appId, String eventName, String nativeViewId,
      Map<String, dynamic> eventData)

# 10. Native Sends Events to Mini-Program

  ///
  /// Native sends events to Mini-Program
  /// [appId] Mini-Program ID
  /// [eventData] Event object
  Future<void> sendCustomEvent(
      String appId, Map<String, dynamic> eventData)

# 11. Close Mini-Program

  ///
  /// Close Mini-Program. The Mini-Program remains in memory.
  ///
  Future<void> closeApplet(String appletId, bool animated)

# 12. Finish Mini-Program

  ///
  /// Finish Mini-Program. The Mini-Program is cleared from memory.
  ///
  Future<void> finishRunningApplet(String appletId, bool animated)

# 13. Set Mini-Program Transition Animation

  ///
  /// Set Mini-Program transition animation (Android)
  ///
  Future setActivityTransitionAnim(Anim anim)

# 14. Open Mini-Program via QR Code

  ///
  /// Open Mini-Program via QR Code
  /// [qrcode] QR Code content
  ///
  Future qrcodeOpenApplet(String qrcode)
© FinClip with ❤ , Since 2017