app-library.vue 9.2 KB

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