UNPKG

mtl-js-sdk

Version:

ynf-fw-mtl-api

115 lines (106 loc) 3.15 kB
import {invokeFail, invokeSuccess} from '../callback' import {execUpesnBridgeify,canExecUpesnBridge} from '../bridge/index' export function getStorageSync(param={}) { return api.getStorageSync(param.key, param.domain); } export function getStorage(param) { const { callbackSuccess = false } = param if (typeof param.key == 'undefined') { invokeFail(param, {message:'key is null'}); return; } api.getStorage({ key: param.key, domain: param.domain, success: function(res) { if (callbackSuccess && (!res || (res && typeof res.data == 'undefined'))) { invokeSuccess(param, {code: 200, data: null}); } else if (res && typeof res.data != 'undefined') { invokeSuccess(param, res); } else { invokeFail(param, {message:'not found data for key: '+param.key}); } }, fail: function(err) { if (callbackSuccess) { invokeSuccess(param, {code: 200, data: null}); } else { invokeFail(param, {message:'not found data for key: '+param.key}); } } }); } export function setStorage(param) { api.setStorage(param); } export function removeStorage(param) { api.removeStorage(param); } export function clearStorage(param) { api.clearStorage(param); } export function executeDBOperate(param={}) { let db = api.require('sqlCipher'); if (!db) { if (canExecUpesnBridge()) { execUpesnBridgeify('executeDBOperate', param); return; } invokeFail(param, {msg:'module sqlCipher not added'}); return; } const dbName = api.fsDir; const pwd = 'mtl$' + 'db'; db.openDatabase({ name:dbName, path:'box://data.db', passWord:pwd }, function(ret0, err0){ if (ret0.status) { let method; if (param.executeType && parseInt(param.executeType) == 5) { method = 'selectSql'; } else { method = 'executeSql'; } db[method]({ name: dbName, sql: param.executeSql }, function(ret, err){ if (ret && ret.status) { invokeSuccess(param, ret.data); } else { invokeFail(param, err); } }); } else { invokeFail(param, err0); } }); } export function getConfig(param) { api.readFile({ path: 'package://widget/www/config.json' }, function(ret, err){ if (ret && ret.status) { invokeSuccess(param, JSON.parse(ret.data)); } else { invokeFail(param, err); } }); } export function getConfigSync(param) { let data = api.readFile({ sync: true, path: 'package://widget/www/config.json' }); if (data) { return JSON.parse(data); } return {}; } export function clearCache(param) { api.clearCache(function(){ invokeSuccess(param, {}); }); }