Skip to content

Interface Description

Routing interface

InterfaceDescription
wx.miniProgram.navigateToKeep the current page and jump to a page in the app. But you cannot jump to the tabBar page
wx.miniProgram.navigateBackClose the current page and return to the previous page or multi-level pages
wx.miniProgram.switchTabJump to the tabBar page and close all other non-tabBar pages
wx.miniProgram.reLaunchClose all pages and open to a page in the app
wx.miniProgram.redirectToClose the current page and jump to a page in the app. But jumping to the tabBar page is not allowed

TIP

JSSDK routing interface parameters are consistent with Mini program routing interface , but the success/fail/complete callback function cannot be triggered.

Sample code:

js
wx.miniProgram.navigateTo({url: '/path/to/page'})
wx.miniProgram.navigateBack({delta: 1})
wx.miniProgram.switchTab({url: '/path/to/page'})
wx.miniProgram.reLaunch({url: '/path/to/page'})
wx.miniProgram.redirectTo({url: '/path/to/page'})

Jump interface

InterfaceDescription
wx.navigateToMiniProgramOpen another applet
wx.exitMiniProgramClose the current mini program
wx.navigateBackMiniProgramReturn to the previous mini program, which can only be called successfully when the current mini program is opened by other mini programs

TIP

JSSDK jump interface parameters are the same as Mini program jump interface same.

Sample code:

js
wx.navigateToMiniProgram({
  appId: 'appId',
  envVersion: 'release',
  success: (res) => {
    console.log('navigateToMiniProgram success', res)
  },
  fail: (err) => {
    console.log('navigateToMiniProgram fail', err)
  }
})

wx.exitMiniProgram({
  success: (res) => {
    console.log('exitMiniProgram success', res)
  },
  fail: (err) => {
    console.log('exitMiniProgram fail', err)
  }
})

wx.navigateBackMiniProgram({
  success: (res) => {
    console.log('navigateBackMiniProgram success', res)
  },
  fail: (err) => {
    console.log('navigateBackMiniProgram fail', err)
  }
})

Navigation bar interface

InterfaceDescription
wx.setNavigationBarTitleDynamically set the title of the navigation bar of the current page

Sample code:

TIP

JSSDK navigation bar interface parameters are the same as Mini program navigation bar interface same.

Sample code:

js
wx.setNavigationBarTitle({
  title: 'TCMPP',
  success: (res) => {
    console.log('setNavigationBarTitle success', res)
  },
  fail: (err) => {
    console.log('setNavigationBarTitle fail', err)
  }
})

Mini program data cache interface

InterfaceDescription
wx.getStorageAsynchronously obtain the content of the specified key from the local cache
wx.setStorageStore data in the specified key in the local cache. Will overwrite the original content corresponding to the key
wx.removeStorageRemove the specified key from the local cache
wx.clearStorageClean up the local data cache
wx.getStorageInfoAsynchronously obtain the relevant information of the current storage

TIP

JSSDK data cache interface parameters and Mini program data cache interface same.

Sample code:

js
wx.getStorage({
  key: 'name',
  success: (res) => {
    console.log('getStorage success', res)
  },
  fail: (err) => {
    console.log('getStorage fail', err)
  }
})

wx.setStorage({
  key: 'name',
  data: 'TCMPP',
  success: (res) => {
    console.log('setStorage success', res)
  },
  fail: (err) => {
    console.log('setStorage fail', err)
  }
})

wx.removeStorage({
  key: 'name',
  success: (res) => {
    console.log('removeStorage success', res)
  },
  fail: (err) => {
    console.log('removeStorage fail', err)
  }
})

wx.clearStorage({
  success: (res) => {
    console.log('clearStorage success', res)
  },
  fail: (err) => {
    console.log('clearStorage fail', err)
  }
})
  
wx.getStorageInfo({
  success: (res) => {
    console.log('getStorageInfo success', res)
  },
  fail: (err) => {
    console.log('getStorageInfo fail', err)
  }
})

Media interface

Event classificationDescriptionInterface
Image interfaceSelect an image from the local album or take a photo with the camera.wx.chooseImage
Preview the image in full screen on a new pagewx.previewImage
Get local image base64 (only supported by IOS)wx.getLocalImgData
Recording interfaceStart recordingwx.startRecord
Stop recordingwx.stopRecord
Audio interfaceStart playing audiowx.playVoice
Pause the audio being playedwx.pauseVoice
End audio playbackwx.stopVoice

TIP

JSSDK media interface parameters and Mini program media interface same.

Sample code:

js
wx.chooseImage({
  count: 2,
  sizeType: ['original', 'compressed'],
  sourceType: ['album', 'camera'],
  success: (res) => {
    console.log('chooseImage success', res)
    
    const imagePaths = res.tempFilePaths
    wx.previewImage({
      urls: imagePaths,
      success: (res) => {
        console.log('previewImage success', res)
      },
      fail: (err) => {
        console.log('previewImage fail', err)
      }
    })
      
    wx.getLocalImgData({
      filePath: imagePaths[0],
      success: (res) => {
        console.log('getLocalImgData success', res)
      },
      fail: (err) => {
        console.log('getLocalImgData fail', err)
      }
    })
  },
  fail: (err) => {
    console.log('chooseImage fail', err)
  }
})

wx.startRecord({
  success: (res) => {
    console.log('startRecord success', res)
  
    const recordFilePath = res.tempFilePath
    wx.playVoice({
      filePath: recordFilePath,
      success: (res) => {
        console.log('playVoice success', res)
      },
      fail: (err) => {
        console.log('playVoice fail', err)
      }
    })
  },
  fail: (err) => {
    console.log('startRecord fail', err)
  }
})
  
wx.stopRecord({
  success: (res) => {
    console.log('stopRecord success', res)
  },
  fail: (err) => {
    console.log('stopRecord fail', err)
  }
})

wx.pauseVoice({
  success: (res) => {
    console.log('pauseVoice success', res)
  },
  fail: (err) => {
    console.log('pauseVoice fail', err)
  }
})
  
wx.stopVoice({
  success: (res) => {
    console.log('stopVoice success', res)
  },
  fail: (err) => {
    console.log('stopVoice fail', err)
  }
})

Location interface

InterfaceDescription
wx.getLocationGet current geographic location information
wx.openLocationUse the built-in map of the App to view the location
wx.chooseLocationOpen the map to select the location

TIP

JSSDK location interface parameters and Mini program location interface same.

Sample code:

js
wx.getLocation({
  success: (res) => {
    console.log('getLocation success', res)
  
    const latitude = res.latitude
    const longitude = res.longitude
    wx.openLocation({
      latitude,
      longitude,
      success: (res) => {
        console.log('openLocation success', res)
      },
      fail: (err) => {
        console.log('openLocation fail', err)
      }
    })
     wx.chooseLocation({
      latitude,
      longitude,
      success: (res) => {
        console.log('chooseLocation success', res)
      },
      fail: (err) => {
        console.log('chooseLocation fail', err)
      }
    })
  fail: (err) => {
    console.log('getLocation fail', err)
  }
})

File interface

InterfaceDescription
wx.openDocumentOpen document (Android only supports PDF, iOS supports the same file types as the system's wkwebview)

TIP

Sample code:

js
wx.openDocument({
  filePath: '/path/to/file',
  success: (res) => {
    console.log('openDocument success', res)
  },
  fail: (err) => {
    console.log('openDocument fail', err)
  }
})

Device interface

InterfaceDescription
wx.scanCodeCall up the client code scanning interface to scan
wx.getNetworkTypeGet network type

TIP

JSSDK device interface parameters and Mini program device interface same.

Sample code:

js
wx.scanCode({
  success: (res) => {
    console.log('scanCode success', res)
  },
  fail: (err) => {
    console.log('scanCode fail', err)
  }
})

wx.getNetworkType({
  success: (res) => {
    console.log('getNetworkType success', res)
  },
  fail: (err) => {
    console.log('getNetworkType fail', err)
  }
})

Communication interface

InterfaceDescription
wx.miniProgram.postMessageSending a message to the mini program will trigger the following specific timesweb-viewComponent message events: mini program back, component destruction, sharing, etc.
wx.miniProgram.onWebviewEventH5 listens to the message event sent by the mini program
wx.miniProgram.offWebviewEventH5 removes the message event sent by the mini program
wx.miniProgram.sendWebviewEventSending a message to the mini program triggers web-view Component event

H5 sends a message event to the mini program

js
// H5 javascript
wx.miniProgram.postMessage({ data: {name: 'TCMPP'} })

wx.miniProgram.sendWebviewEvent({ data: {message: 'Hi TCMPP'} })
html
<!-- Mini program /pages/index/index.wxml -->
<web-view src="https://xxx.xxx" bindmessage="onMessage" bindevent="onEvent"></web-view>
js
Page({
  onMessage(e) {
    console.log(e.detail.data) // [{"name":"TCMPP"}]
  },
  onEvent(e) {
    console.log(e.detail.data) // {"message":"Hi TCMPP"}
  }
})

Mini program sends a message event to H9

js
wx.sendWebviewEvent({
  message: "I'm Miniprogram, I received"
})
js
// H5 javascript
function onEvent(e) {
  console.log(e.message) // I'm Miniprogram, I received
}
// Register a listener
wx.miniProgram.onWebviewEvent(onEvent)
// Remove the listener
wx.miniProgram.offWebviewEvent(onEvent)

Other interfaces

InterfaceDescription
wx.miniProgram.getEnvGet the current environment
wx.canGoBackCurrent H5 Whether the page can return to the previous page
wx.checkJsApiDetermine whether the current client version supports the specified JS interface
wx.invokeNativePluginH5 calls the client custom API

Get the current environment

In the H5 web page, you can determine whether it is in the mini-program web-view environment in the following three ways:

  • Use the wx.miniProgram.getEnv interface.
js
// H5 javascript
wx.miniProgram.getEnv((res) => {
  console.log(res.miniprogram) // true
})
  • Use the window.__wxjs_environment variable to determine. It is recommended to use it in the callback function of the WeixinJSBridgeReady event.

Sample code:

js
// H5 javascript
function ready() {
  console.log(window.__wxjs_environment === 'miniprogram') // true
}
if (!window.WeixinJSBridge || !WeixinJSBridge.invoke) {
  document.addEventListener('WeixinJSBridgeReady', ready, false)
} else {
  ready()
}
  • Determine whether the userAgent contains the miniProgram keyword.

Sample code:

js
// H5 javascript
// "Mozilla/...... miniProgram TMA/mp6iu47n4rps6zwz": Contains miniProgram and the AppID(Mini Program ID).
console.log(window.navigator.userAgent);

Get the URL of the web-view

When the user shares, you can get the URL of the current web-view, that is, return the webViewUrl parameter in the onShareAppMessage callback.

Sample code:

js
// Miniprogram javascript
Page({
  onShareAppMessage(options) {
    console.log(options.webViewUrl)
  }
})

Determine whether H5 can return to the previous page

A Boolean value used to determine whether you can return to the previous page or state of H5, that is, whether there is a page that can be returned in the current navigation stack.

Sample code:

js
// H5 javascript
wx.canGoBack({
  success: (res) => {
    console.log('canGoBack success', res.canGoBack) // true/false
  },
  fail: (err) => {
    console.log('canGoBack fail', err)
  }
})

Get client interface support

Used to determine whether the current client version supports the specified JS interface.

Sample code:

js
// H5 javascript
wx.checkJsApi({
  jsApiList: ['chooseImage'],
  success: (res) => {
    // Return as a key-value pair, true for available APIs and false for unavailable APIs.
    // For example: {"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
    console.log('checkJsApi success', res)
  },
  fail: (err) => {
    console.log('checkJsApi fail', err)
  }
})

Call the client custom API

Call the client's custom API in H5.

Sample code:

js
// H5 javascript
invokeNativePlugin() {
  let opts = {
    api_name: 'customApiName', // Custom API name
    data: JSON.parse(this.apiData || '{}'), // API request parameters
    success: (res) => {
      console.log('invokeNativePlugin success', res)
    },
    fail: (err) => {
      console.log('invokeNativePlugin fail', err)
    }
  }
  wx.invokeNativePlugin(opts); // Call custom APIs
}