index.vue 6.3 KB

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