interface.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 getShortCut = (fn) =>{
  17. shortcut.hasInstalled({
  18. success: (ret) => {
  19. fn(ret);
  20. }
  21. })
  22. }
  23. // 判断用户是否创建图标
  24. const hasCreateShortCut = (isPayPage = false,fn) => {
  25. shortcut.hasInstalled({
  26. success: (ret) => {
  27. if (!ret) {
  28. shortcut.install({
  29. message: "添加【追书云】到桌面,方便下次阅读",
  30. success: () => {
  31. prompt.showToast("添加成功!");
  32. fn && fn(true);
  33. },
  34. fail: (code) => {
  35. if (code === SHORT_CUT_FORBIDDEN_INFO && !isPayPage) app.exit();
  36. }
  37. })
  38. }
  39. }
  40. })
  41. }
  42. // 右上角菜单点击
  43. const showMenu = () => {
  44. const appInfo = app.getInfo();
  45. prompt.showContextMenu({
  46. itemList: ["保存桌面", "关于", "取消"],
  47. success: function (ret) {
  48. switch (ret.index) {
  49. case 0:
  50. // 保存桌面
  51. hasCreateShortCut();
  52. break;
  53. case 1:
  54. // 关于
  55. router.push({
  56. uri: "/views/About",
  57. params: {
  58. name: appInfo.name,
  59. icon: appInfo.icon
  60. }
  61. });
  62. break;
  63. case 2:
  64. // 取消
  65. break;
  66. default:
  67. prompt.showToast({
  68. message: "错误"
  69. });
  70. }
  71. }
  72. });
  73. }
  74. export default {
  75. shortcut,
  76. getDeviceInfo,
  77. hasCreateShortCut,
  78. showMenu,
  79. getShortCut
  80. }