快应用激活工具

需快应用引擎

适配设备:Pro

作者:lor

下载量:32 · 点赞:2

可信源码风险提示:保存或下载前请阅读源码和权限声明。
SHA-256eca9477d8b011dbeb92d4c5e8a66443fba96026feebd839e330392d5c7a7b27d
// 快应用工具 · 极简验证版
// 目的:先确认页面能打开,再逐步加功能

function safe(fn, fb) {
  try { return fn(); } catch (e) { return fb; }
}

function showAlert(title, msg) {
  safe(function () {
    var d = safe(function () { return dialog; }, null);
    if (!d) return;
    safe(function () { d.alert({ title: title, message: msg }); });
  });
}

function doDiag() {
  var lines = [];
  lines.push('ui: ' + safe(function () { return typeof ui; }, '?'));
  lines.push('ui.render: ' + safe(function () { return typeof ui.render; }, '?'));
  lines.push('ui.heading: ' + safe(function () { return typeof ui.heading; }, '?'));
  lines.push('ui.text: ' + safe(function () { return typeof ui.text; }, '?'));
  lines.push('ui.button: ' + safe(function () { return typeof ui.button; }, '?'));
  lines.push('ui.hide: ' + safe(function () { return typeof ui.hide; }, '?'));
  lines.push('dialog: ' + safe(function () { return typeof dialog; }, '?'));
  lines.push('dialog.alert: ' + safe(function () { return typeof dialog.alert; }, '?'));
  lines.push('dialog.text: ' + safe(function () { return typeof dialog.text; }, '?'));
  lines.push('system: ' + safe(function () { return typeof system; }, '?'));
  lines.push('require: ' + safe(function () { return typeof require; }, '?'));
  lines.push('script: ' + safe(function () { return typeof script; }, '?'));

  var r = safe(function () { return system.router; }, null);
  lines.push('system.router: ' + (r ? '有' : '无'));
  if (r) {
    lines.push('  push: ' + safe(function () { return typeof r.push; }, '?'));
    lines.push('  pushUrl: ' + safe(function () { return typeof r.pushUrl; }, '?'));
    lines.push('  replace: ' + safe(function () { return typeof r.replace; }, '?'));
    lines.push('  navigate: ' + safe(function () { return typeof r.navigate; }, '?'));
  }

  var a = safe(function () { return system.app; }, null);
  lines.push('system.app: ' + (a ? '有' : '无'));
  if (a) {
    lines.push('  startApplication: ' + safe(function () { return typeof a.startApplication; }, '?'));
    lines.push('  launchApp: ' + safe(function () { return typeof a.launchApp; }, '?'));
  }

  showAlert('诊断结果', lines.join('\n'));
}

function doActivate() {
  var r = safe(function () { return system.router; }, null);
  if (!r || safe(function () { return typeof r.push; }, '?') !== 'function') {
    showAlert('无法激活', '没有找到 system.router.push。\n请先点"诊断"查看环境。');
    return;
  }

  var d = safe(function () { return dialog; }, null);
  if (!d || safe(function () { return typeof d.text; }, '?') !== 'function') {
    showAlert('无法输入', 'dialog.text 不可用。');
    return;
  }

  var ret = safe(function () {
    return d.text({
      title: '激活快应用',
      message: '输入 URI',
      placeholder: 'hap://app/包名',
    });
  }, null);

  if (!ret) return;

  // 兼容 Promise 和对象两种返回
  if (typeof ret.then === 'function') {
    ret.then(function (res) {
      if (res && res.action === 'confirm') pushUri(r, res.value);
    }, function () {});
  } else if (ret.action === 'confirm') {
    pushUri(r, ret.value);
  }
}

function pushUri(router, uri) {
  if (!uri) return;
  safe(function () {
    router.push({
      uri: uri,
      success: function () { showAlert('成功', '请求已发送'); },
      fail: function (data, code) {
        showAlert('失败', 'code=' + code + '\n' + (data || ''));
      },
    });
  });
}

function doClose() {
  safe(function () { ui.hide(); });
}

// ===== 渲染 =====
safe(function () {
  ui.render(function () {
    var out = [];

    safe(function () {
      out.push(ui.heading('快应用工具', { lines: 1, size: 30 }));
    });

    safe(function () {
      out.push(ui.text('点"诊断"查看环境,点"激活"尝试跳转', {
        lines: 2, size: 18, color: '#888888',
      }));
    });

    safe(function () {
      out.push(ui.button('诊断', doDiag, { tone: 'primary', height: 56 }));
    });

    safe(function () {
      out.push(ui.button('激活快应用', doActivate, { tone: 'primary', height: 56 }));
    });

    safe(function () {
      out.push(ui.button('关闭', doClose, { tone: 'neutral', height: 48 }));
    });

    return out;
  });
});