All Products
Search
Document Center

Mobile Platform as a Service:Scan code

Last Updated:Feb 04, 2021

my.scan

Note: This interface is only supported in mPaaS 10.1.32 and later versions.

Call the scan function.

After my.scan is called, the onHide() and onShow() lifecycle functions of app and page are executed in sequence. The following is the executing sequence:

(Arouse) app.onHide() > page.onHide() > (return) app.onShow() > page.onShow().

Parameters

Parameter
Type
Required
Description
scanType
String
No
Code scanning type. The default value is ['qrCode','barCode'].
hideAlbum
Boolean
No
Hide album or not. It is “false” by default, namely there will be an entry for users to select a picture from the album as the scanned picture. Otherwise, users are only allowed to scan code via camera.
success
Function
No
Callback function upon call success.
fail
Function
No
Callback function upon call failure.
complete
Function
No
Callback function upon call completion (to be executed upon either call success or failure).

Return value on success

Parameter Type Description
code String Data returned from scanning.
qrCode String QR code data returned from scanning.
barCode String Bar code data returned from scanning.

Error code

Error code Description Solution
10 User canceled This is normal user interaction, and you don’t have to take any action.
11 Operation failed Check the client to find the cause of problem, and then troubleshoot accordingly.

Code example

  1. // API-DEMO page/API/scan-code/scan-code.json
  2. {
  3. "defaultTitle": "Scan"
  4. }
  1. <!-- API-DEMO page/API/scan-code/scan-code.axml-->
  2. <view class="page">
  3. <view class="page-section">
  4. <form onSubmit="scanCode">
  5. <view>
  6. <button type="primary" onTap="scan">Scan</button>
  7. </view>
  8. </form>
  9. </view>
  10. </view>
  1. // API-DEMO page/API/scan-code/scan-code.js
  2. Page({
  3. scan() {
  4. my.scan({
  5. scanType: ['qrCode','barCode'],
  6. success: (res) => {
  7. my.alert({ title: res.code });
  8. },
  9. });
  10. }
  11. })

FAQ

  • Q: Why does the page keep loading after code scaning?

    A: Check the domain whitelist configured in backend. If the home page has network requests, it is required to configure whitelist.