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
// API-DEMO page/API/scan-code/scan-code.json
{
"defaultTitle": "Scan"
}
<!-- API-DEMO page/API/scan-code/scan-code.axml-->
<view class="page">
<view class="page-section">
<form onSubmit="scanCode">
<view>
<button type="primary" onTap="scan">Scan</button>
</view>
</form>
</view>
</view>
// API-DEMO page/API/scan-code/scan-code.js
Page({
scan() {
my.scan({
scanType: ['qrCode','barCode'],
success: (res) => {
my.alert({ title: res.code });
},
});
}
})
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.