mtl-js-sdk
Version:
ynf-fw-mtl-api
397 lines (380 loc) • 14.8 kB
JavaScript
import {invokeSuccess, invokeFail} from '../callback'
import {handleMessage} from '../message'
import {requestPermission} from '../permission'
import {localizedString} from '../localization'
import {getUserAgentInternal, setYhtCookie} from '../util'
let customScanQRCodeType;
export function scanQRCode(param = {}) {
let obj = api.require('hwScanner'); // 友空间及友空间私有版内置插件.
if (!obj) {
obj = api.require('FNScanner');
}
if (!obj) {
if (api.scanCode) {
api.scanCode({
...param,
success:function(res) {
if (param.needResult == 1) {
invokeSuccess(param, {resultStr:res.result});
} else {
setTimeout(function(){
mtl.navigateTo({
url:res.result,
progress: {type:'page',color:'#EE2233'}
});
}, 500);
}
},
fail:function(res) {
invokeFail(param, res);
}
});
return;
}
invokeFail(param, {msg:'module hwScanner not added'});
return;
}
requestPermission({
list:['camera'],
success:function(){
let obj = api.require('hwScanner');
if (obj) {
let scan = obj?.multiCodeScanner || obj?.defaultScanner
if (param?.onlySignalScanner) { // 友空间 8.0.2、mtl 6.2.19 版本开始,
scan = obj?.defaultScanner
}
scan({
formatType: 'ALL',
scanType: param?.scanType
}, function(ret, err) {
if (ret && ret.status) {
if (param.needResult == 1 || (param.needResult == 0 && !(ret.result.startsWith('http')))) {
invokeSuccess(param, {resultStr:ret.result});
} else {
setTimeout(function(){
mtl.navigateTo({
url:ret.result,
progress: {type: 'page',color: '#EE2233'}
});
}, 500);
}
} else {
invokeFail(param, err || {message:'failed'});
}
});
} else {
obj = api.require('FNScanner');
obj.openScanner({
hintText:localizedString('scannerHintText')
}, function(ret, err) {
if (ret) {
let eventType = ret.eventType;
if (eventType == 'success') {
if (param.needResult == 1) {
invokeSuccess(param, {resultStr:ret.content});
} else {
setTimeout(function(){
mtl.navigateTo({
url:ret.content,
progress: {type: 'page',color: '#EE2233'}
});
}, 500);
}
} else if (eventType == 'fail') {
invokeFail(param, {message:'fail'});
} else if (eventType == 'cancel') {
invokeFail(param, {message:'cancel'});
} else if (eventType == 'cameraError' || eventType == 'albumError') {
invokeFail(param, {message:'permission error'});
}
} else {
invokeFail(param, err);
}
});
}
},
fail:param.fail
});
}
export function generateQRCode(param = {}) {
let size = param.size || 100;
let obj = api.require('hwScanner');
if (obj) {
obj.encodeContent({
formatType: 'QR_CODE',
content:param.str,
size:{
w:size,
h:size
}
}, function(res, error){
if (res && res.status) {
api.require('trans').decodeImgToBase64({
imgPath:res.imagePath
}, function(ret, err){
if (ret && ret.status) {
invokeSuccess(param, {imgSrc:'data:image/jpeg;base64,' + ret.base64Str});
} else {
invokeFail(param, err);
}
});
} else {
invokeFail(param, error || {message:'failed'});
}
});
} else {
let savePath = 'box://qrcode.jpg';
obj = api.require('FNScanner');
if (!obj) {
invokeFail(param, {msg:'module hwScanner not added'});
return;
}
obj.encodeImg({
content:param.str,
saveImg:{
path:savePath,
w:size,
h:size
}
}, function(res, error){
if (res && res.status) {
api.require('trans').decodeImgToBase64({
imgPath:savePath
}, function(ret, err){
if (ret && ret.status) {
invokeSuccess(param, {imgSrc:'data:image/jpeg;base64,' + ret.base64Str});
} else {
invokeFail(param, err);
}
});
} else {
invokeFail(param, error);
}
});
}
}
export function customScanQRCode(param = {}) {
requestPermission({
list:['camera'],
success:function(){
setYhtCookie(param.url);
const {customNavi,leftItems,rightItems,centerItems} = param;
param.headers = {'User-Agent':getUserAgentInternal()};
let p = {
name:'customScanQRCode',
url:'package://res_yonBuilder/widget/build/scan/scan.js',
bgColor:'#fff',
pageParam:param,
title:localizedString('qrcode'),
navigationBar:{},
animation:{
type:'movein',
subType:'from_bottom'
}
};
if (customNavi == 1) {
let leftButtons = [];
leftItems && leftItems.forEach(item => {
leftButtons.push({
text:item.title
});
});
p.navigationBar.leftButtons = leftButtons;
let rightButtons = [];
rightItems && rightItems.forEach(item => {
rightButtons.push({
text:item.title
});
});
p.navigationBar.rightButtons = rightButtons;
let title = '';
if (centerItems && centerItems.length > 0) {
title = centerItems[0].title;
}
p.title = title;
} else {
p.navigationBar.leftButtons = [{text:localizedString('cancel')}];
p.navigationBar.rightButtons = [{text:localizedString('album')}];
}
api.openTabLayout(p);
},
fail:param.fail
});
}
export function changeCustomScanMode(param = {}) {
let type = param.type;
customScanQRCodeType = type;
api.sendEvent({
name:'mtl$customScanQRCodeTypeChanged',
extra:{type}
});
invokeSuccess(param, {type});
}
export function changeFlashLightStatus(param = {}) {
let obj = api.require("hwScanner");
if (!obj) {
obj = api.require('FNScanner');
}
if (!obj) {
invokeFail(param, {msg:'module hwScanner not added'});
return;
}
let type = param.type;
api.sendEvent({
name:'mtl$changeFlashLightStatus',
extra:{type}
});
obj.switchLight({
status: (type && type === 1) ? 'on' : 'off'
});
if (param.callback && type && type == 1) {
api.addEventListener({
name:'pause'
}, function(){
handleMessage(param.callback, {});
});
}
invokeSuccess(param, {type});
}
export function toggleCamera(param = {}) {
api.sendEvent({
name:'mtl$toggleCamera',
extra:{type:param.type}
});
invokeSuccess(param, {});
}
export function relayoutCustomWebview(param = {}) {
api.sendEvent({
name:'mtl$relayoutCustomWebview',
extra:{type:param.type}
});
invokeSuccess(param, {type:param.type});
}
export function webViewCustomScanQRCode(param = {}) {
requestPermission({
list:['camera'],
success:function(){
let {type,hide,callback,scanLeftRatio,scanTopRatio,scanWidthRatio,scanHeightRatio,lineColor,scanUIStyle,backgroundHeightRatio,isShowScanDesc,scanDescription,isShowLight,cornerLenthRatio} = param;
customScanQRCodeType = type;
let info = api.getNavBarAttr() || {};
let x = api.winWidth * (typeof scanLeftRatio == 'number' ? scanLeftRatio : 0);
let y = api.winHeight * (typeof scanTopRatio == 'number' ? scanTopRatio : 0.1);
let w = api.winWidth * (typeof scanWidthRatio == 'number' ? scanWidthRatio : 1);
let h = api.winHeight * (typeof scanHeightRatio == 'number' ? scanHeightRatio : 0.4);
if (typeof scanHeightRatio == 'number' && scanHeightRatio == 0) {
h = w;
}
let offy = info.hidden ? api.safeArea.top : api.safeArea.top + info.height;
if (typeof isShowScanDesc == 'undefined') {
isShowScanDesc = false;
}
let rect, rectOfInterest;
if (scanUIStyle == 1 && typeof backgroundHeightRatio == 'number' && backgroundHeightRatio > 0) {
rect = {x:0, y:offy, w:api.winWidth, h:api.winHeight * backgroundHeightRatio};
rectOfInterest = {x, y, w, h};
} else {
rect = {x, y:y+offy, w, h};
rectOfInterest = {x:0, y:0, w, h};
}
let scanLine = lineColor?{color:lineColor}:undefined;
let obj = api.require('hwScanner');
if (obj) {
if (hide == 1) {
obj.closeCustomizedScanner();
return;
}
if (obj.customizedScannerNew) {
obj.customizedScannerNew({
formatType: 'ALL',
rect,
rectOfInterest,
...param,
scanUIStyle,
isShowScanDesc,
tipString:scanDescription,
isShowLight: isShowLight === 1 ? true : false,
scanLine,
cornerLenthRatio
}, function(ret, err) {
if (ret && ret.eventType == 'scan') {
if (callback) {
handleMessage(callback, {qrString:ret.result});
}
if (typeof customScanQRCodeType == 'number' && customScanQRCodeType == 0) {
obj.closeCustomizedScanner();
}
} else {
if (callback) {
handleMessage(callback, ret);
}
}
});
} else {
obj.customizedScanner({
formatType: 'ALL',
rect,
...param,
scanUIStyle,
isShowScanDesc,
tipString:scanDescription,
isShowLight: isShowLight === 1 ? true : false,
scanLine,
cornerLenthRatio
}, function(ret, err) {
if (ret && ret.status) {
if (callback) {
handleMessage(callback, {qrString:ret.result});
}
if (typeof customScanQRCodeType == 'number' && customScanQRCodeType == 0) {
obj.closeCustomizedScanner();
}
} else {
invokeFail(param, err || {message:'failed'});
}
});
}
} else {
obj = api.require('FNScanner');
if (!obj) {
invokeFail(param, {msg:'module hwScanner not added'});
return;
}
if (hide == 1) {
obj.closeView();
return;
}
obj.openView({
interval: 1,
rect: {x, y, w, h}
}, function(ret, err) {
if (ret) {
if (ret.eventType == 'success') {
if (callback) {
handleMessage(callback, {qrString:ret.content});
}
if (typeof customScanQRCodeType == 'number' && customScanQRCodeType == 0) {
obj.closeView();
}
} else if (ret.eventType == 'cameraError') {
invokeFail(param, {message:'permission error'});
} else if (ret.eventType == 'albumError') {
invokeFail(param, {message:'permission error'});
}
} else {
invokeFail(param, err);
}
});
}
},
fail:param.fail
});
}
export function mdfCustomScanQRCode(param) {
webViewCustomScanQRCode(param);
}
export function mdfChangeCustomScanMode(param = {}) {
changeCustomScanMode(param);
}
export function mdfChangeFlashLightStatus(param = {}) {
changeFlashLightStatus(param);
}