UNPKG

mtl-js-sdk

Version:

ynf-fw-mtl-api

123 lines (114 loc) 3.16 kB
"use strict"; function Plugin(options, exports = {}) { const { platform } = options || { platform: window.mtl.platform }; const FAIL_CODE = 1; function successCallBack(object = {}) { object.success && object.success({}); object.complete && object.complete({}); } function remindersAuth(obj = {}) { if (platform === "APIIos") { execApi("remindersAuth", obj); } else { successCallBack(obj); } } function addEvent(obj = {}) { if (platform === "APIAndroid" || platform === "APIIos") { execApiBridge("addEvent", obj); } else { successCallBack(obj); } } function removeEvent(obj = {}) { if (platform === "APIAndroid" || platform === "APIIos") { execApiBridge("removeEvent", {...obj, success:function(res){ if (res && res.status) { obj.success && obj.success(res) obj.complete && obj.complete(res) } else { obj.fail && obj.fail(res) obj.complete && obj.complete(res) } }}); } else { successCallBack(obj); } } function updateEvent(obj = {}) { if (platform === "APIAndroid" || platform === "APIIos") { execApiBridge("updateEvent", {...obj, success:function(res){ if (res && res.status) { obj.success && obj.success(res) obj.complete && obj.complete(res) } else { obj.fail && obj.fail(res) obj.complete && obj.complete(res) } }}); } else { successCallBack(obj); } } function getAllEvent(obj = {}) { if (platform === "APIAndroid" || platform === "APIIos") { execApiBridge("getAllEvent", obj); } else { successCallBack(obj); } } function getEventById(obj = {}) { if (platform === "APIAndroid" || platform === "APIIos") { execApiBridge("getEventById", obj); } else { successCallBack(obj); } } // 内部方法 function execApiBridge(method, object) { let requireName = 'calendarMemo' if (platform === "APIAndroid") { mtl.requestPermission({ list: ["calendar"], success: function success() { execApi(method, object) }, fail: object.fail, }); } else if (platform === "APIIos") { mtl.execPluginBridge({ method: 'requestAuth', requireName, success: function(res){ if (res && res.granted) { execApi(method, object) } else { let error = res || {} error.message = 'permission not enabled' error.code = FAIL_CODE object.fail && object.fail(error) object.complete && object.complete(error) } }, fail: object.fail, }); } } function execApi(method, object) { let requireName = 'calendarMemo' mtl.execPluginBridge({...object, method, requireName}); } let apiMethods = { remindersAuth, addEvent, removeEvent, updateEvent, getAllEvent, getEventById, }; const methods = apiMethods; exports.module = methods; exports.symbolPath = "calendar"; return exports; } export default Plugin;