index.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <template>
  2. <view class="all-services-page">
  3. <nav-bar title="全部分类" :showBack="false"></nav-bar>
  4. <!-- 顶部搜索栏(暂时注释)
  5. <view class="header-search">
  6. <view class="search-input-wrap">
  7. <uni-icons type="search" size="14" color="#999"></uni-icons>
  8. <input class="search-input" v-model="searchValue" placeholder="搜索服务内容"
  9. placeholder-class="placeholder-style" />
  10. </view>
  11. </view>
  12. -->
  13. <!-- 主体分类区域 -->
  14. <view class="main-content">
  15. <!-- 左侧侧边栏 -->
  16. <scroll-view scroll-y class="sidebar">
  17. <view v-for="(item, index) in allData" :key="index"
  18. :class="['sidebar-item', { active: activeSidebar === index }]" @click="activeSidebar = index">
  19. <text>{{ item.title }}</text>
  20. </view>
  21. </scroll-view>
  22. <!-- 右侧服务列表 -->
  23. <scroll-view scroll-y class="content-view">
  24. <view class="category-section" v-for="(cat, index) in currentCategories" :key="index">
  25. <view class="service-grid">
  26. <view class="service-cell" v-for="(service, sIndex) in cat.items" :key="sIndex"
  27. @click="onServiceClick(service)">
  28. <view class="icon-wrapper">
  29. <image :src="service.icon" class="service-icon" mode="aspectFit"></image>
  30. </view>
  31. <text class="service-name">{{ service.name }}</text>
  32. </view>
  33. </view>
  34. </view>
  35. </scroll-view>
  36. </view>
  37. <custom-tabbar></custom-tabbar>
  38. </view>
  39. </template>
  40. <script setup>
  41. import { ref, computed, onMounted } from 'vue'
  42. import navBar from '@/components/nav-bar/index.vue'
  43. import customTabbar from '@/components/custom-tabbar/index.vue'
  44. import { listAll as getClassifications } from '@/api/service/classification'
  45. import { listAll as getServices } from '@/api/service/list'
  46. import { listMyServices } from '@/api/system/store'
  47. const searchValue = ref('')
  48. const activeSidebar = ref(0)
  49. const allData = ref([])
  50. /** 当前用户可用的服务ID列表 */
  51. const myServiceIds = ref([])
  52. /** 前端搜索 + 权限过滤 */
  53. const filteredData = computed(() => {
  54. let data = allData.value
  55. // 先按权限过滤:只展示用户有权限的服务类型
  56. if (myServiceIds.value.length > 0) {
  57. const idSet = new Set(myServiceIds.value)
  58. data = data.map(group => ({
  59. ...group,
  60. categories: group.categories.map(cat => ({
  61. ...cat,
  62. items: cat.items.filter(s => idSet.has(s.id))
  63. })).filter(cat => cat.items.length > 0)
  64. })).filter(group => group.categories.some(cat => cat.items.length > 0))
  65. }
  66. // 再按搜索关键词过滤
  67. const keyword = searchValue.value.trim().toLowerCase()
  68. if (!keyword) return data
  69. return data.map(group => ({
  70. ...group,
  71. categories: group.categories.map(cat => ({
  72. ...cat,
  73. items: cat.items.filter(s => s.name.toLowerCase().includes(keyword))
  74. })).filter(cat => cat.items.length > 0)
  75. })).filter(group => group.categories.some(cat => cat.items.length > 0))
  76. })
  77. const currentCategories = computed(() => {
  78. if (filteredData.value.length === 0 || !filteredData.value[activeSidebar.value]) return []
  79. return filteredData.value[activeSidebar.value].categories
  80. })
  81. /** 获取当前用户可下单的服务ID列表 */
  82. const fetchMyServiceIds = async () => {
  83. try {
  84. const res = await listMyServices()
  85. // 兼容多种响应格式,确保取到数组并保持字符串类型
  86. const ids = res?.data ?? res?.rows ?? res ?? []
  87. myServiceIds.value = Array.isArray(ids) ? ids.map(String) : []
  88. console.log('我的可用服务ID:', myServiceIds.value)
  89. } catch (error) {
  90. console.error('获取可用服务列表失败,将展示全部服务', error)
  91. myServiceIds.value = []
  92. }
  93. }
  94. const loadData = async () => {
  95. try {
  96. const [classifications, services] = await Promise.all([
  97. getClassifications(),
  98. getServices()
  99. ])
  100. console.log('分类数据:', classifications)
  101. console.log('服务数据:', services)
  102. allData.value = classifications.map(classification => {
  103. const categoryServices = services.filter(
  104. service => service.classificationId === classification.id
  105. )
  106. console.log(`分类 ${classification.name} 的服务:`, categoryServices)
  107. return {
  108. title: classification.name,
  109. categories: [{
  110. items: categoryServices.map(service => ({
  111. ...service,
  112. name: service.name,
  113. icon: service.iconUrl,
  114. type: service.id
  115. }))
  116. }]
  117. }
  118. })
  119. console.log('最终数据结构:', allData.value)
  120. } catch (error) {
  121. console.error('加载服务数据失败:', error)
  122. uni.showToast({ title: '加载失败', icon: 'none' })
  123. }
  124. }
  125. onMounted(() => {
  126. loadData()
  127. fetchMyServiceIds()
  128. })
  129. const onServiceClick = (service) => {
  130. if (service.id) {
  131. uni.setStorageSync('currentService', service)
  132. uni.navigateTo({ url: `/pages/service/detail/index?serviceId=${service.id}` })
  133. } else {
  134. uni.showToast({ title: service.name + ' 功能即将上线', icon: 'none' })
  135. }
  136. }
  137. </script>
  138. <style lang="scss" scoped>
  139. .all-services-page {
  140. height: 100vh;
  141. display: flex;
  142. flex-direction: column;
  143. background-color: #fff;
  144. }
  145. .header-search {
  146. padding: 16rpx 32rpx;
  147. background-color: #fff;
  148. border-bottom: 2rpx solid #EEEEEE;
  149. }
  150. .search-input-wrap {
  151. flex: 1;
  152. display: flex;
  153. align-items: center;
  154. background: #f5f5f5;
  155. border-radius: 32rpx;
  156. padding: 12rpx 20rpx;
  157. gap: 12rpx;
  158. }
  159. .search-input {
  160. flex: 1;
  161. font-size: 26rpx;
  162. }
  163. .placeholder-style {
  164. color: #999;
  165. font-size: 26rpx;
  166. }
  167. .main-content {
  168. flex: 1;
  169. display: flex;
  170. overflow: hidden;
  171. }
  172. .sidebar {
  173. width: 200rpx;
  174. background-color: #f7f8fa;
  175. }
  176. .sidebar-item {
  177. padding: 28rpx 0;
  178. text-align: center;
  179. font-size: 26rpx;
  180. color: #666;
  181. position: relative;
  182. }
  183. .sidebar-item.active {
  184. background-color: #fff;
  185. color: #333;
  186. font-weight: bold;
  187. }
  188. .sidebar-item.active::before {
  189. content: '';
  190. position: absolute;
  191. left: 0;
  192. top: 50%;
  193. transform: translateY(-50%);
  194. width: 6rpx;
  195. height: 40rpx;
  196. background-color: #f7ca3e;
  197. border-radius: 0 6rpx 6rpx 0;
  198. }
  199. .content-view {
  200. flex: 1;
  201. padding: 32rpx;
  202. }
  203. .category-section {
  204. margin-bottom: 48rpx;
  205. }
  206. .service-grid {
  207. display: flex;
  208. flex-wrap: wrap;
  209. }
  210. .service-cell {
  211. width: 33.33%;
  212. display: flex;
  213. flex-direction: column;
  214. align-items: center;
  215. margin-bottom: 32rpx;
  216. }
  217. .icon-wrapper {
  218. width: 96rpx;
  219. height: 96rpx;
  220. background-color: #fff;
  221. border-radius: 24rpx;
  222. display: flex;
  223. align-items: center;
  224. justify-content: center;
  225. box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.04);
  226. margin-bottom: 16rpx;
  227. }
  228. .service-icon {
  229. width: 56rpx;
  230. height: 56rpx;
  231. }
  232. .service-name {
  233. font-size: 24rpx;
  234. color: #666;
  235. text-align: center;
  236. }
  237. </style>