interface.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import app from "@system.app";
  2. import device from "@system.device";
  3. import prompt from "@system.prompt";
  4. import shortcut from "@system.shortcut";
  5. import router from "@system.router";
  6. const SHORT_CUT_FORBIDDEN_INFO = "User forbidden";
  7. // 获取设备信息
  8. const getDeviceInfo = () => {
  9. device.getDeviceId({
  10. success: (ret) => {
  11. console.log(ret)
  12. }
  13. })
  14. }
  15. //获取应用信息
  16. const getAppInfo = () => {
  17. const appInfo = app.getInfo();
  18. return appInfo;
  19. }
  20. //获取设备明细
  21. const getAppDev = (fn) => {
  22. device.getInfo({
  23. success: (ret)=> {
  24. fn&&fn(ret)
  25. }
  26. })
  27. }
  28. //获取是否创建图标
  29. const getShortCut = (fn) => {
  30. shortcut.hasInstalled({
  31. success: (ret) => {
  32. fn(ret);
  33. }
  34. })
  35. }
  36. // 判断用户是否创建图标
  37. const hasCreateShortCut = (isPayPage = false,fn) => {
  38. shortcut.hasInstalled({
  39. success: (ret) => {
  40. if (!ret) {
  41. shortcut.install({
  42. message: "添加【追书云】到桌面,方便下次阅读",
  43. success: () => {
  44. prompt.showToast("添加成功!");
  45. fn && fn(true);
  46. },
  47. fail: (code) => {
  48. if (code === SHORT_CUT_FORBIDDEN_INFO && !isPayPage) app.exit();
  49. }
  50. })
  51. }
  52. }
  53. })
  54. }
  55. // 右上角菜单点击
  56. const showMenu = () => {
  57. const appInfo = app.getInfo();
  58. prompt.showContextMenu({
  59. itemList: ["保存桌面", "关于", "取消"],
  60. success: function (ret) {
  61. switch (ret.index) {
  62. case 0:
  63. // 保存桌面
  64. hasCreateShortCut();
  65. break;
  66. case 1:
  67. // 关于
  68. router.push({
  69. uri: "/views/About",
  70. params: {
  71. name: appInfo.name,
  72. icon: appInfo.icon
  73. }
  74. });
  75. break;
  76. case 2:
  77. // 取消
  78. break;
  79. default:
  80. prompt.showToast({
  81. message: "错误"
  82. });
  83. }
  84. }
  85. });
  86. }
  87. export default {
  88. shortcut,
  89. getDeviceInfo,
  90. hasCreateShortCut,
  91. showMenu,
  92. getShortCut,
  93. getAppInfo,
  94. getAppDev
  95. }