app-library.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 fontScaleMixin from '@/mixins/font-scale.js'
  66. export default {
  67. mixins: [fontScaleMixin],
  68. data() {
  69. return {
  70. fixedApps: [],
  71. configurableApps: [],
  72. quickApps: [],
  73. myShortcuts: [],
  74. appUsage: {}
  75. }
  76. },
  77. onShow() {
  78. this.loadAppLibrary()
  79. this.loadQuickApps()
  80. },
  81. methods: {
  82. goBack() {
  83. uni.navigateBack()
  84. },
  85. loadAppLibrary() {
  86. const allApps = APP_LIBRARY
  87. this.fixedApps = allApps.filter(app => app.canShortcut === false).slice(0, 20)
  88. this.configurableApps = allApps.filter(app => app.canShortcut === true).slice(0, 20)
  89. },
  90. loadQuickApps() {
  91. var self = this
  92. getAppLibrary().then(function(res) {
  93. if (res && res.code === 200 && res.data) {
  94. self.myShortcuts = res.data.myShortcuts || []
  95. self.appUsage = res.data.usage || {}
  96. // 计算 quickApps:锁定占位 + 频度填充
  97. var lockedApps = (self.myShortcuts || []).filter(function(s) { return s.locked === 1 })
  98. .sort(function(a, b) { return a.sortOrder - b.sortOrder })
  99. if (lockedApps.length >= 6) {
  100. self.quickApps = lockedApps.slice(0, 6).map(function(s) {
  101. var app = findAppByKey(s.appKey)
  102. return app ? { key: app.key, name: app.name, icon: app.icon, route: app.route, locked: s.locked } : null
  103. }).filter(function(x) { return x })
  104. return
  105. }
  106. var remaining = 6 - lockedApps.length
  107. var lockedKeys = {}
  108. lockedApps.forEach(function(s) { lockedKeys[s.appKey] = true })
  109. var candidates = getShortcutableApps().filter(function(app) { return !lockedKeys[app.key] })
  110. candidates.sort(function(a, b) {
  111. var ca = (self.appUsage && self.appUsage[a.key]) || 0
  112. var cb = (self.appUsage && self.appUsage[b.key]) || 0
  113. return cb - ca
  114. })
  115. var shown = lockedApps.concat(candidates.slice(0, remaining))
  116. self.quickApps = shown.map(function(item) {
  117. var app = findAppByKey(item.appKey)
  118. return app ? { key: app.key, name: app.name, icon: app.icon, route: app.route, locked: item.locked } : null
  119. }).filter(function(x) { return x })
  120. }
  121. }).catch(function() {
  122. self.loadAppLibrary()
  123. })
  124. },
  125. isAppInQuickApps: function(appKey) {
  126. return this.quickApps.some(function(app) { return app.key === appKey })
  127. },
  128. toggleApp: function(appKey) {
  129. var isInQuick = this.isAppInQuickApps(appKey)
  130. var items = []
  131. // 保留其他已配置项,更新当前项
  132. var existingItems = this.myShortcuts.map(function(s) { return { appKey: s.appKey, locked: s.locked, sortOrder: s.sortOrder } })
  133. var idx = existingItems.findIndex(function(s) { return s.appKey === appKey })
  134. if (idx >= 0) {
  135. existingItems[idx].locked = !isInQuick
  136. } else {
  137. existingItems.push({ appKey: appKey, locked: !isInQuick, sortOrder: existingItems.length })
  138. }
  139. saveShortcutConfig(existingItems).then(function(res) {
  140. if (res && res.code === 200) {
  141. this.loadAppLibrary()
  142. this.loadQuickApps()
  143. uni.showToast({ title: isInQuick ? '已移出快捷区' : '已添加至快捷区', icon: 'none' })
  144. }
  145. }.bind(this)).catch(function() {
  146. uni.showToast({ title: '操作失败', icon: 'none' })
  147. })
  148. },
  149. resetToDefault() {
  150. resetShortcutConfig().then(function(res) {
  151. if (res && res.code === 200) {
  152. this.loadAppLibrary()
  153. this.loadQuickApps()
  154. uni.showToast({ title: '已恢复默认配置', icon: 'success' })
  155. }
  156. }.bind(this)).catch(function() {
  157. uni.showToast({ title: '恢复失败', icon: 'none' })
  158. })
  159. }
  160. }
  161. }
  162. </script>
  163. <style lang="scss">
  164. .app-library-page {
  165. padding: 20rpx;
  166. background: $facade-fill-page;
  167. min-height: 100vh;
  168. box-sizing: border-box;
  169. }
  170. .page-header {
  171. display: flex;
  172. justify-content: space-between;
  173. align-items: center;
  174. margin-bottom: $uni-space-lg;
  175. padding-bottom: 20rpx;
  176. border-bottom: 1rpx solid $facade-fill-4;
  177. }
  178. .header-left {
  179. display: flex;
  180. align-items: center;
  181. gap: 8rpx;
  182. color: $facade-text-strong;
  183. font-size: calc(30rpx * var(--font-scale, 1));
  184. }
  185. .header-icon {
  186. font-size: calc(32rpx * var(--font-scale, 1));
  187. }
  188. .header-title {
  189. font-size: calc(32rpx * var(--font-scale, 1));
  190. font-weight: bold;
  191. color: $facade-text-strong;
  192. }
  193. .section {
  194. margin-bottom: $uni-space-lg;
  195. }
  196. .section-title {
  197. font-size: calc(28rpx * var(--font-scale, 1));
  198. font-weight: bold;
  199. color: $facade-text-strong;
  200. margin-bottom: 16rpx;
  201. padding-left: 8rpx;
  202. }
  203. .fixed-list,
  204. .configurable-list {
  205. display: flex;
  206. flex-wrap: wrap;
  207. gap: 16rpx;
  208. }
  209. .fixed-item,
  210. .configurable-item {
  211. width: calc(50% - 8rpx);
  212. background: $app-surface;
  213. border-radius: 16rpx;
  214. padding: 20rpx;
  215. box-sizing: border-box;
  216. position: relative;
  217. }
  218. .fixed-info,
  219. .configurable-detail {
  220. display: flex;
  221. flex-direction: column;
  222. gap: 8rpx;
  223. }
  224. .fixed-icon,
  225. .configurable-icon {
  226. width: 48rpx;
  227. height: 48rpx;
  228. background: $facade-fill-blue-ghost;
  229. border-radius: 16rpx;
  230. display: flex;
  231. align-items: center;
  232. justify-content: center;
  233. font-size: calc(28rpx * var(--font-scale, 1));
  234. }
  235. .fixed-name,
  236. .configurable-name {
  237. font-size: calc(26rpx * var(--font-scale, 1));
  238. font-weight: bold;
  239. color: $facade-text-strong;
  240. }
  241. .fixed-desc,
  242. .configurable-desc {
  243. font-size: calc(20rpx * var(--font-scale, 1));
  244. color: $facade-text-muted;
  245. line-height: 1.4;
  246. }
  247. .fixed-badge {
  248. position: absolute;
  249. top: 12rpx;
  250. right: 12rpx;
  251. background: $facade-fill;
  252. color: $facade-text-body;
  253. font-size: calc(18rpx * var(--font-scale, 1));
  254. padding: 4rpx 12rpx;
  255. border-radius: 24rpx;
  256. }
  257. .status-badge {
  258. font-size: calc(20rpx * var(--font-scale, 1));
  259. color: $facade-text-muted;
  260. margin-top: 8rpx;
  261. }
  262. .empty-state {
  263. padding: 120rpx 40rpx;
  264. text-align: center;
  265. }
  266. .empty-icon {
  267. font-size: calc($uni-font-size-5xl * var(--font-scale, 1));
  268. display: block;
  269. margin-bottom: 20rpx;
  270. }
  271. .empty-title {
  272. font-size: calc(30rpx * var(--font-scale, 1));
  273. font-weight: bold;
  274. color: $facade-text-strong;
  275. margin-bottom: 10rpx;
  276. }
  277. .empty-desc {
  278. font-size: calc(24rpx * var(--font-scale, 1));
  279. color: $facade-text-muted;
  280. }
  281. .bottom-bar {
  282. display: flex;
  283. justify-content: space-between;
  284. align-items: center;
  285. padding: 24rpx 0;
  286. border-top: 1rpx solid $facade-fill-4;
  287. margin-top: 20rpx;
  288. background: $app-surface;
  289. border-radius: 16rpx;
  290. padding: 24rpx;
  291. }
  292. .bottom-action {
  293. flex: 1;
  294. margin-right: 20rpx;
  295. }
  296. .action-text {
  297. font-size: calc(24rpx * var(--font-scale, 1));
  298. color: $facade-text-body;
  299. display: block;
  300. margin-bottom: 12rpx;
  301. }
  302. .sort-info {
  303. display: flex;
  304. flex-wrap: wrap;
  305. gap: 8rpx;
  306. }
  307. .sort-chip {
  308. background: $facade-fill-blue-ghost;
  309. border-radius: 24rpx;
  310. padding: 8rpx 16rpx;
  311. font-size: calc(22rpx * var(--font-scale, 1));
  312. color: $facade-text-strong;
  313. display: flex;
  314. align-items: center;
  315. gap: 6rpx;
  316. }
  317. .chip-icon {
  318. font-size: calc(22rpx * var(--font-scale, 1));
  319. }
  320. .chip-label {
  321. font-size: calc(22rpx * var(--font-scale, 1));
  322. }
  323. .chip-lock {
  324. font-size: calc(20rpx * var(--font-scale, 1));
  325. }
  326. .bottom-btn {
  327. flex: 0 0 auto;
  328. background: linear-gradient(135deg, $brand-primary, $brand-primary-light);
  329. color: $app-surface;
  330. font-size: calc(26rpx * var(--font-scale, 1));
  331. font-weight: bold;
  332. padding: 16rpx 32rpx;
  333. border-radius: 24rpx;
  334. white-space: nowrap;
  335. }
  336. </style>