app.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import DiyApi from '@/sheep/api/promotion/diy';
  2. import { getTenantByWebsite } from '@/sheep/api/infra/tenant';
  3. import { getTenantId } from '@/sheep/request';
  4. import { defineStore } from 'pinia';
  5. import $platform from '@/sheep/platform';
  6. import $router from '@/sheep/router';
  7. import user from './user';
  8. import sys from './sys';
  9. import { baseUrl, h5Url } from '@/sheep/config';
  10. const app = defineStore({
  11. id: 'app',
  12. state: () => ({
  13. paramsForTabbar: {}, // 为全局tabbar跳转传参用。原因是 tabbar 无法传参,只能通过全局状态传递
  14. info: {
  15. // 应用信息
  16. name: '', // 商城名称
  17. logo: '', // logo
  18. version: '', // 版本号
  19. copyright: '', // 版权信息 I
  20. copytime: '', // 版权信息 II
  21. cdnurl: '', // 云存储域名
  22. filesystem: '', // 云存储平台
  23. },
  24. platform: {
  25. share: {
  26. methods: [], // 支持的分享方式
  27. forwardInfo: {}, // 默认转发信息
  28. posterInfo: {}, // 海报信息
  29. linkAddress: '', // 复制链接地址
  30. },
  31. bind_mobile: 0, // 登陆后绑定手机号提醒 (弱提醒,可手动关闭)
  32. },
  33. template: {
  34. // 店铺装修模板
  35. basic: {}, // 基本信息
  36. home: {
  37. // 首页模板
  38. style: {},
  39. data: [],
  40. },
  41. user: {
  42. // 个人中心模板
  43. style: {},
  44. data: [],
  45. },
  46. },
  47. shareInfo: {}, // 全局分享信息
  48. has_wechat_trade_managed: 0, // 小程序发货信息管理 0 没有 || 1 有
  49. }),
  50. actions: {
  51. // 获取Shopro应用配置和模板
  52. async init(templateId = null) {
  53. // 检查网络
  54. const networkStatus = await $platform.checkNetwork();
  55. if (!networkStatus) {
  56. $router.error('NetworkError');
  57. }
  58. // 检查配置
  59. if (typeof baseUrl === 'undefined') {
  60. $router.error('EnvError');
  61. }
  62. // 加载租户
  63. await adaptTenant();
  64. // 加载装修配置
  65. await adaptTemplate(this.template, templateId);
  66. // TODO 芋艿:【初始化优化】未来支持管理后台可配;对应 https://api.shopro.sheepjs.com/shop/api/init
  67. if (true) {
  68. this.info = {
  69. name: '芋道商城',
  70. logo: 'https://static.iocoder.cn/ruoyi-vue-pro-logo.png',
  71. version: '2026.03',
  72. copyright: '全部开源,个人与企业可 100% 免费使用',
  73. copytime: 'Copyright© 2018-2025',
  74. cdnurl: 'https://file.sheepjs.com', // 云存储域名
  75. filesystem: 'qcloud', // 云存储平台
  76. };
  77. this.platform = {
  78. share: {
  79. methods: ['forward', 'poster', 'link'],
  80. linkAddress: h5Url,
  81. posterInfo: {
  82. user_bg: '/static/img/shop/config/user-poster-bg.png',
  83. goods_bg: '/static/img/shop/config/goods-poster-bg.png',
  84. groupon_bg: '/static/img/shop/config/groupon-poster-bg.png',
  85. },
  86. forwardInfo: {
  87. title: '',
  88. image: '',
  89. desc: '',
  90. },
  91. },
  92. bind_mobile: 0,
  93. };
  94. this.has_wechat_trade_managed = 0;
  95. // 加载主题
  96. const sysStore = sys();
  97. sysStore.setTheme();
  98. // 模拟用户登录
  99. const userStore = user();
  100. if (userStore.isLogin) {
  101. userStore.loginAfter();
  102. }
  103. return Promise.resolve(true);
  104. } else {
  105. $router.error('InitError', res.msg || '加载失败');
  106. }
  107. },
  108. // 设置 paramsForTabbar
  109. setParamsForTabbar(params = {}) {
  110. this.paramsForTabbar = params;
  111. },
  112. clearParamsForTabbar() {
  113. this.paramsForTabbar = {};
  114. },
  115. },
  116. persist: {
  117. enabled: true,
  118. strategies: [
  119. {
  120. key: 'app-store',
  121. },
  122. ],
  123. },
  124. });
  125. /** 初始化租户编号 */
  126. const adaptTenant = async () => {
  127. // 1. 获取当前租户 ID
  128. const oldTenantId = getTenantId();
  129. let newTenantId = null;
  130. try {
  131. // 2.1 情况一:H5:根据 url 参数、域名来获取新的租户ID
  132. // #ifdef H5
  133. // H5 环境下的处理逻辑
  134. if (window?.location) {
  135. // 优先从 URL 查询参数获取 tenantId
  136. const urlParams = new URLSearchParams(window.location.search);
  137. newTenantId = urlParams.get('tenantId');
  138. // 如果 URL 参数中没有,则通过 host 获取
  139. if (!newTenantId && window.location.host) {
  140. const { data } = await getTenantByWebsite(window.location.host);
  141. newTenantId = data?.id;
  142. }
  143. }
  144. // #endif
  145. // 2.2 情况二:微信小程序:小程序环境下的处理逻辑 - 根据 appId 获取租户
  146. // #ifdef MP
  147. const appId = uni.getAccountInfoSync()?.miniProgram?.appId;
  148. if (appId) {
  149. const { data } = await getTenantByWebsite(appId);
  150. newTenantId = data?.id;
  151. }
  152. // #endif
  153. // 3. 如果是新租户(不相等),则进行切换
  154. // noinspection EqualityComparisonWithCoercionJS
  155. if (newTenantId && newTenantId != oldTenantId) {
  156. // 清理掉登录用户的 token
  157. const userStore = user();
  158. userStore.setToken();
  159. // 设置新的 tenantId 到本地存储
  160. uni.setStorageSync('tenant-id', newTenantId);
  161. console.log('租户 ID 已更新:', `${oldTenantId} -> ${newTenantId}`);
  162. }
  163. } catch (error) {
  164. console.error('adaptTenant 执行失败:', error);
  165. }
  166. };
  167. /** 初始化装修模版 */
  168. const adaptTemplate = async (appTemplate, templateId) => {
  169. const { data: diyTemplate } = templateId
  170. ? // 查询指定模板,一般是预览时使用
  171. await DiyApi.getDiyTemplate(templateId)
  172. : await DiyApi.getUsedDiyTemplate();
  173. // 模板不存在
  174. if (!diyTemplate) {
  175. $router.error('TemplateError');
  176. return;
  177. }
  178. const tabBar = diyTemplate?.property?.tabBar;
  179. if (tabBar) {
  180. appTemplate.basic.tabbar = tabBar;
  181. // TODO 商城装修没有对 tabBar 进行角标配置,测试角标需打开以下注释
  182. // appTemplate.basic.tabbar.items.forEach((tabBar) => {
  183. // tabBar.dot = false
  184. // tabBar.badge = 100
  185. // })
  186. // appTemplate.basic.tabbar.badgeStyle = {
  187. // backgroundColor: '#882222',
  188. // }
  189. if (tabBar?.theme) {
  190. appTemplate.basic.theme = tabBar?.theme;
  191. }
  192. }
  193. appTemplate.home = diyTemplate?.home;
  194. appTemplate.user = diyTemplate?.user;
  195. };
  196. export default app;