app-library.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <template>
  2. <view class="app-library-page" :style="{ '--font-scale': fontScale }">
  3. <view class="page-header">
  4. <view class="header-left" @click="goBack">
  5. <text class="header-icon">←</text>
  6. <text class="header-text">应用库</text>
  7. </view>
  8. <text class="header-title">自定义首页快捷功能</text>
  9. </view>
  10. <!-- 已在首页有显著入口的功能 -->
  11. <view class="section" v-if="fixedApps.length > 0">
  12. <view class="section-title">已在首页显著位置</view>
  13. <view class="fixed-list">
  14. <view class="fixed-item" v-for="app in fixedApps" :key="app.key">
  15. <view class="fixed-info">
  16. <view class="fixed-icon">{{ app.icon }}</view>
  17. <view class="fixed-detail">
  18. <text class="fixed-name">{{ app.name }}</text>
  19. <text class="fixed-desc">{{ app.description }}</text>
  20. </view>
  21. </view>
  22. <view class="fixed-badge">已在首页</view>
  23. </view>
  24. </view>
  25. </view>
  26. <!-- 可配置的功能 -->
  27. <view class="section" v-if="configurableApps.length > 0">
  28. <view class="section-title">可配置的功能</view>
  29. <view class="configurable-list">
  30. <view class="configurable-item" v-for="app in configurableApps" :key="app.key" @click="toggleApp(app.key)">
  31. <view class="configurable-icon">{{ app.icon }}</view>
  32. <view class="configurable-detail">
  33. <text class="configurable-name">{{ app.name }}</text>
  34. <view v-if="isAppInQuickApps(app.key)" class="status-badge">已在快捷区</view>
  35. <view v-else class="status-badge">不在快捷区</view>
  36. </view>
  37. </view>
  38. </view>
  39. </view>
  40. <!-- 暂无配置 -->
  41. <view class="empty-state" v-if="fixedApps.length === 0 && configurableApps.length === 0">
  42. <text class="empty-icon">📦</text>
  43. <text class="empty-title">暂无应用</text>
  44. <text class="empty-desc">所有功能均在首页显著位置,无需额外配置</text>
  45. </view>
  46. <!-- 底部操作栏 -->
  47. <view class="bottom-bar">
  48. <view class="bottom-action">
  49. <text class="action-text">快捷区当前排序:</text>
  50. <view class="sort-info">
  51. <view v-for="(app, idx) in quickApps" :key="app.key" class="sort-chip">
  52. <text class="chip-icon">{{ app.icon }}</text>
  53. <text class="chip-label">{{ app.name }}</text>
  54. <text v-if="app.locked" class="chip-lock">🔒</text>
  55. </view>
  56. </view>
  57. </view>
  58. <view class="bottom-btn" @click="resetToDefault">恢复默认</view>
  59. </view>
  60. </view>
  61. </template>
  62. <script>
  63. import { APP_LIBRARY, getShortcutableApps, findAppByKey } from '@/utils/app-library'
  64. import { getAppLibrary, saveShortcutConfig, resetShortcutConfig } from '@/utils/api.js'
  65. import { isActiveMember } from '@/utils/membershipGate.js'
  66. import fontScaleMixin from '@/mixins/font-scale.js'
  67. export default {
  68. mixins: [fontScaleMixin],
  69. data() {
  70. return {
  71. fixedApps: [],
  72. configurableApps: [],
  73. quickApps: [],
  74. myShortcuts: [],
  75. appUsage: {},
  76. // 会员有效态(isActive):控制 AI 管家入口可见性,默认 false(fail-closed)
  77. memberActive: false
  78. }
  79. },
  80. onShow() {
  81. // 每次进入刷新会员有效态(覆盖从升级/续费页返回),再按最新口径重建列表
  82. this.refreshMemberActive()
  83. this.loadAppLibrary()
  84. this.loadQuickApps()
  85. },
  86. methods: {
  87. // 会员有效态刷新:唯一判据 /api/ai/gate 的 data.isActive(不随运营开关,fail-closed)
  88. refreshMemberActive: function() {
  89. var self = this
  90. isActiveMember().then(function(active) {
  91. var changed = self.memberActive !== active
  92. self.memberActive = active
  93. if (changed) {
  94. self.loadAppLibrary()
  95. self.loadQuickApps()
  96. }
  97. })
  98. },
  99. // 非有效会员剔除 AI 管家(key='ai');会员原样返回
  100. filterAiApps: function(list) {
  101. var active = this.memberActive
  102. return (list || []).filter(function(app) { return app.key !== 'ai' || active })
  103. },
  104. goBack() {
  105. uni.navigateBack()
  106. },
  107. loadAppLibrary() {
  108. const allApps = APP_LIBRARY
  109. this.fixedApps = allApps.filter(app => app.canShortcut === false).slice(0, 20)
  110. this.configurableApps = this.filterAiApps(allApps.filter(app => app.canShortcut === true).slice(0, 20))
  111. },
  112. loadQuickApps() {
  113. var self = this
  114. getAppLibrary().then(function(res) {
  115. if (res && res.code === 200 && res.data) {
  116. self.myShortcuts = res.data.myShortcuts || []
  117. self.appUsage = res.data.usage || {}
  118. // 计算 quickApps:锁定占位 + 频度填充
  119. var lockedApps = (self.myShortcuts || []).filter(function(s) { return s.locked === 1 })
  120. .sort(function(a, b) { return a.sortOrder - b.sortOrder })
  121. if (lockedApps.length >= 6) {
  122. self.quickApps = self.filterAiApps(lockedApps.slice(0, 6).map(function(s) {
  123. var app = findAppByKey(s.appKey)
  124. return app ? { key: app.key, name: app.name, icon: app.icon, route: app.route, locked: s.locked } : null
  125. }).filter(function(x) { return x }))
  126. return
  127. }
  128. var remaining = 6 - lockedApps.length
  129. var lockedKeys = {}
  130. lockedApps.forEach(function(s) { lockedKeys[s.appKey] = true })
  131. var selfActive = self.memberActive
  132. var candidates = getShortcutableApps().filter(function(app) { return !lockedKeys[app.key] && (app.key !== 'ai' || selfActive) })
  133. candidates.sort(function(a, b) {
  134. var ca = (self.appUsage && self.appUsage[a.key]) || 0
  135. var cb = (self.appUsage && self.appUsage[b.key]) || 0
  136. return cb - ca
  137. })
  138. var shown = lockedApps.concat(candidates.slice(0, remaining))
  139. self.quickApps = self.filterAiApps(shown.map(function(item) {
  140. var app = findAppByKey(item.appKey)
  141. return app ? { key: app.key, name: app.name, icon: app.icon, route: app.route, locked: item.locked } : null
  142. }).filter(function(x) { return x }))
  143. }
  144. }).catch(function() {
  145. self.loadAppLibrary()
  146. })
  147. },
  148. isAppInQuickApps: function(appKey) {
  149. return this.quickApps.some(function(app) { return app.key === appKey })
  150. },
  151. toggleApp: function(appKey) {
  152. var isInQuick = this.isAppInQuickApps(appKey)
  153. var items = []
  154. // 保留其他已配置项,更新当前项
  155. var existingItems = this.myShortcuts.map(function(s) { return { appKey: s.appKey, locked: s.locked, sortOrder: s.sortOrder } })
  156. var idx = existingItems.findIndex(function(s) { return s.appKey === appKey })
  157. if (idx >= 0) {
  158. existingItems[idx].locked = !isInQuick
  159. } else {
  160. existingItems.push({ appKey: appKey, locked: !isInQuick, sortOrder: existingItems.length })
  161. }
  162. saveShortcutConfig(existingItems).then(function(res) {
  163. if (res && res.code === 200) {
  164. this.loadAppLibrary()
  165. this.loadQuickApps()
  166. uni.showToast({ title: isInQuick ? '已移出快捷区' : '已添加至快捷区', icon: 'none' })
  167. }
  168. }.bind(this)).catch(function() {
  169. uni.showToast({ title: '操作失败', icon: 'none' })
  170. })
  171. },
  172. resetToDefault() {
  173. resetShortcutConfig().then(function(res) {
  174. if (res && res.code === 200) {
  175. this.loadAppLibrary()
  176. this.loadQuickApps()
  177. uni.showToast({ title: '已恢复默认配置', icon: 'success' })
  178. }
  179. }.bind(this)).catch(function() {
  180. uni.showToast({ title: '恢复失败', icon: 'none' })
  181. })
  182. }
  183. }
  184. }
  185. </script>
  186. <style lang="scss">
  187. .app-library-page {
  188. padding: 20rpx;
  189. background: $facade-fill-page;
  190. min-height: 100vh;
  191. box-sizing: border-box;
  192. }
  193. .page-header {
  194. display: flex;
  195. justify-content: space-between;
  196. align-items: center;
  197. margin-bottom: $uni-space-lg;
  198. padding-bottom: 20rpx;
  199. border-bottom: 1rpx solid $facade-fill-4;
  200. }
  201. .header-left {
  202. display: flex;
  203. align-items: center;
  204. gap: 8rpx;
  205. color: $facade-text-strong;
  206. font-size: calc(30rpx * var(--font-scale, 1));
  207. }
  208. .header-icon {
  209. font-size: calc(32rpx * var(--font-scale, 1));
  210. }
  211. .header-title {
  212. font-size: calc(32rpx * var(--font-scale, 1));
  213. font-weight: bold;
  214. color: $facade-text-strong;
  215. }
  216. .section {
  217. margin-bottom: $uni-space-lg;
  218. }
  219. .section-title {
  220. font-size: calc(28rpx * var(--font-scale, 1));
  221. font-weight: bold;
  222. color: $facade-text-strong;
  223. margin-bottom: 16rpx;
  224. padding-left: 8rpx;
  225. }
  226. .fixed-list,
  227. .configurable-list {
  228. display: flex;
  229. flex-wrap: wrap;
  230. gap: 16rpx;
  231. }
  232. .fixed-item,
  233. .configurable-item {
  234. width: calc(50% - 8rpx);
  235. background: $app-surface;
  236. border-radius: 16rpx;
  237. padding: 20rpx;
  238. box-sizing: border-box;
  239. position: relative;
  240. }
  241. .fixed-info,
  242. .configurable-detail {
  243. display: flex;
  244. flex-direction: column;
  245. gap: 8rpx;
  246. }
  247. .fixed-icon,
  248. .configurable-icon {
  249. width: 48rpx;
  250. height: 48rpx;
  251. background: $facade-fill-blue-ghost;
  252. border-radius: 16rpx;
  253. display: flex;
  254. align-items: center;
  255. justify-content: center;
  256. font-size: calc(28rpx * var(--font-scale, 1));
  257. }
  258. .fixed-name,
  259. .configurable-name {
  260. font-size: calc(26rpx * var(--font-scale, 1));
  261. font-weight: bold;
  262. color: $facade-text-strong;
  263. }
  264. .fixed-desc,
  265. .configurable-desc {
  266. font-size: calc(20rpx * var(--font-scale, 1));
  267. color: $facade-text-muted;
  268. line-height: 1.4;
  269. }
  270. .fixed-badge {
  271. position: absolute;
  272. top: 12rpx;
  273. right: 12rpx;
  274. background: $facade-fill;
  275. color: $facade-text-body;
  276. font-size: calc(18rpx * var(--font-scale, 1));
  277. padding: 4rpx 12rpx;
  278. border-radius: 24rpx;
  279. }
  280. .status-badge {
  281. font-size: calc(20rpx * var(--font-scale, 1));
  282. color: $facade-text-muted;
  283. margin-top: 8rpx;
  284. }
  285. .empty-state {
  286. padding: 120rpx 40rpx;
  287. text-align: center;
  288. }
  289. .empty-icon {
  290. font-size: calc($uni-font-size-5xl * var(--font-scale, 1));
  291. display: block;
  292. margin-bottom: 20rpx;
  293. }
  294. .empty-title {
  295. font-size: calc(30rpx * var(--font-scale, 1));
  296. font-weight: bold;
  297. color: $facade-text-strong;
  298. margin-bottom: 10rpx;
  299. }
  300. .empty-desc {
  301. font-size: calc(24rpx * var(--font-scale, 1));
  302. color: $facade-text-muted;
  303. }
  304. .bottom-bar {
  305. display: flex;
  306. justify-content: space-between;
  307. align-items: center;
  308. padding: 24rpx 0;
  309. border-top: 1rpx solid $facade-fill-4;
  310. margin-top: 20rpx;
  311. background: $app-surface;
  312. border-radius: 16rpx;
  313. padding: 24rpx;
  314. }
  315. .bottom-action {
  316. flex: 1;
  317. margin-right: 20rpx;
  318. }
  319. .action-text {
  320. font-size: calc(24rpx * var(--font-scale, 1));
  321. color: $facade-text-body;
  322. display: block;
  323. margin-bottom: 12rpx;
  324. }
  325. .sort-info {
  326. display: flex;
  327. flex-wrap: wrap;
  328. gap: 8rpx;
  329. }
  330. .sort-chip {
  331. background: $facade-fill-blue-ghost;
  332. border-radius: 24rpx;
  333. padding: 8rpx 16rpx;
  334. font-size: calc(22rpx * var(--font-scale, 1));
  335. color: $facade-text-strong;
  336. display: flex;
  337. align-items: center;
  338. gap: 6rpx;
  339. }
  340. .chip-icon {
  341. font-size: calc(22rpx * var(--font-scale, 1));
  342. }
  343. .chip-label {
  344. font-size: calc(22rpx * var(--font-scale, 1));
  345. }
  346. .chip-lock {
  347. font-size: calc(20rpx * var(--font-scale, 1));
  348. }
  349. .bottom-btn {
  350. flex: 0 0 auto;
  351. background: linear-gradient(135deg, $brand-primary, $brand-primary-light);
  352. color: $app-surface;
  353. font-size: calc(26rpx * var(--font-scale, 1));
  354. font-weight: bold;
  355. padding: 16rpx 32rpx;
  356. border-radius: 24rpx;
  357. white-space: nowrap;
  358. }
  359. </style>