app.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import appApi from '@/sheep/api/app';
  2. import diyTemplateApi from '@/sheep/api/promotion/diy/template';
  3. import { defineStore } from 'pinia';
  4. import $platform from '@/sheep/platform';
  5. import $router from '@/sheep/router';
  6. import user from './user';
  7. import sys from './sys';
  8. const app = defineStore({
  9. id: 'app',
  10. state: () => ({
  11. info: {
  12. // 应用信息
  13. name: '', // 商城名称
  14. logo: '', // logo
  15. version: '', // 版本号
  16. copyright: '', // 版权信息 I
  17. copytime: '', // 版权信息 II
  18. cdnurl: '', // 云存储域名
  19. filesystem: '', // 云存储平台
  20. },
  21. platform: {
  22. share: {
  23. methods: [], // 支持的分享方式
  24. forwardInfo: {}, // 默认转发信息
  25. posterInfo: {}, // 海报信息
  26. linkAddress: '', // 复制链接地址
  27. },
  28. bind_mobile: 0, // 登陆后绑定手机号提醒 (弱提醒,可手动关闭)
  29. },
  30. chat: {},
  31. template: {
  32. // 店铺装修模板
  33. basic: {}, // 基本信息
  34. home: {
  35. // 首页模板
  36. style: {},
  37. data: [],
  38. },
  39. user: {
  40. // 个人中心模板
  41. style: {},
  42. data: [],
  43. },
  44. },
  45. shareInfo: {}, // 全局分享信息
  46. has_wechat_trade_managed: 0 // 小程序发货信息管理 0 没有 || 1 有
  47. }),
  48. actions: {
  49. // 获取Shopro应用配置和模板
  50. async init(templateId = null) {
  51. //检查网络
  52. const networkStatus = await $platform.checkNetwork();
  53. if (!networkStatus) {
  54. $router.error('NetworkError');
  55. }
  56. // 加载装修配置
  57. await adaptTemplate(this.template, templateId)
  58. const res = await appApi.init(templateId);
  59. if (res.error === 0) {
  60. this.info = res.data.app;
  61. this.platform = res.data.platform;
  62. // TODO 芋艿:未接入
  63. // this.template = res.data.template;
  64. // this.has_wechat_trade_managed = res.data.has_wechat_trade_managed;
  65. // if (!res.data.template) {
  66. // $router.error('TemplateError');
  67. // }
  68. // TODO 芋艿:未接入
  69. // this.chat = res.data.chat;
  70. // 加载主题
  71. const sysStore = sys();
  72. sysStore.setTheme();
  73. // 模拟用户登录
  74. const userStore = user();
  75. if (userStore.isLogin) {
  76. userStore.loginAfter();
  77. }
  78. return Promise.resolve(true);
  79. } else {
  80. $router.error('InitError', res.msg || '加载失败');
  81. }
  82. },
  83. },
  84. persist: {
  85. enabled: true,
  86. strategies: [
  87. {
  88. key: 'app-store',
  89. },
  90. ],
  91. },
  92. });
  93. // todo: @owen 先做数据适配,后期重构
  94. const adaptTemplate = async (appTemplate, templateId) => {
  95. const { data: diyTemplate } = templateId
  96. // 查询指定模板,一般是预览时使用
  97. ? await diyTemplateApi.getDiyTemplate(templateId)
  98. : await diyTemplateApi.getUsedDiyTemplate();
  99. // 模板不存在
  100. if (!diyTemplate) {
  101. $router.error('TemplateError');
  102. return
  103. }
  104. const tabBar = diyTemplate?.property?.tabBar;
  105. if (tabBar) {
  106. appTemplate.basic.tabbar = tabBar
  107. if (tabBar?.theme) {
  108. appTemplate.basic.theme = tabBar?.theme;
  109. }
  110. }
  111. appTemplate.home = diyTemplate?.home;
  112. appTemplate.user = diyTemplate?.user;
  113. }
  114. export default app;