// function ready(cb: Function) { // if (typeof my !== 'undefined') { // cb(); // } else { // // h5 // if (window.AlipayJSBridge) { // cb(); // } else { // // 如果没有注入则监听注入的事件 // document.addEventListener('AlipayJSBridgeReady', cb, false); // } // } // } import { getDebuggerSpaceCode } from './util'; function getResult(result, single = false) { const { spaceObjectList = [] } = result; if (spaceObjectList && spaceObjectList.length > 0) { // sigle开关取数组第一条数据 if (single) { return spaceObjectList[0]; } else { return result; } } else { return; } } interface sdkOption { type: string; } interface feedbackParams { spaceCode: string; objectId: string; behavior: string; } class SDK { /** * { type: 'h5' } * @param options */ constructor(options: sdkOption) { const type = options.type; this.options = options; if (options.type !== 'h5' && options.type !== 'mini-app' && options.type !== 'test') { throw new Error(`不支持的type ${type}类型`); } } options: sdkOption; get isMiniApp(): boolean { return this.options.type === 'mini-app'; } fetch(info: object, single: boolean): object { return this.innerFetch(info).then(result => { return getResult(result, single); }); } innerFetch(params: object): Promise<{}> { // 参数检查 return new Promise((resolve, reject) => { // TODO: 处理reject状态 if (this.isMiniApp) { my.call('getCdpSpaceInfo', params, function(res) { resolve(res); }); } else if (this.options.type === 'h5') { AlipayJSBridge.call('getCdpSpaceInfo', params, function(result) { resolve(result); }); } else { // 针对测试用例返回空的数据 resolve({ spaceObjectList: [], }); } }); } fetchDebugger(options): object { const { type, single } = options; const upperType = type && type.toUpperCase(); let spaceCode = getDebuggerSpaceCode(upperType); return this.innerFetch({ spaceCode, }).then(result => { return getResult(result, single); }); } feedback(params: feedbackParams, callback): void { if (this.isMiniApp) { my.call('cdpFeedback', params, callback); } else if (this.options.type === 'h5') { AlipayJSBridge.call('cdpFeedback', params, callback); } else { // 针对测试用例返回空的数据 callback && callback(params); } } } export default SDK;