index.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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="aspectFill"></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. uni.showToast({ title: typeof error === 'string' ? error : '获取可用服务失败', icon: 'none' })
  92. myServiceIds.value = []
  93. }
  94. }
  95. const loadData = async () => {
  96. try {
  97. const [classifications, services] = await Promise.all([
  98. getClassifications(),
  99. getServices()
  100. ])
  101. console.log('分类数据:', classifications)
  102. console.log('服务数据:', services)
  103. allData.value = classifications.map(classification => {
  104. const categoryServices = services.filter(
  105. service => service.classificationId === classification.id
  106. )
  107. console.log(`分类 ${classification.name} 的服务:`, categoryServices)
  108. return {
  109. title: classification.name,
  110. categories: [{
  111. items: categoryServices.map(service => ({
  112. ...service,
  113. name: service.name,
  114. icon: service.iconUrl,
  115. type: service.id
  116. }))
  117. }]
  118. }
  119. })
  120. console.log('最终数据结构:', allData.value)
  121. } catch (error) {
  122. console.error('加载服务数据失败:', error)
  123. uni.showToast({ title: typeof error === 'string' ? error : '加载失败', icon: 'none' })
  124. }
  125. }
  126. onMounted(() => {
  127. loadData()
  128. fetchMyServiceIds()
  129. })
  130. const onServiceClick = (service) => {
  131. if (service.id) {
  132. uni.setStorageSync('currentService', service)
  133. uni.navigateTo({ url: `/pages/service/detail/index?serviceId=${service.id}` })
  134. } else {
  135. uni.showToast({ title: service.name + ' 功能即将上线', icon: 'none' })
  136. }
  137. }
  138. </script>
  139. <style lang="scss" scoped>
  140. .all-services-page {
  141. height: 100vh;
  142. display: flex;
  143. flex-direction: column;
  144. background-color: #fff;
  145. }
  146. .header-search {
  147. padding: 16rpx 32rpx;
  148. background-color: #fff;
  149. border-bottom: 2rpx solid #EEEEEE;
  150. }
  151. .search-input-wrap {
  152. flex: 1;
  153. display: flex;
  154. align-items: center;
  155. background: #f5f5f5;
  156. border-radius: 32rpx;
  157. padding: 12rpx 20rpx;
  158. gap: 12rpx;
  159. }
  160. .search-input {
  161. flex: 1;
  162. font-size: 26rpx;
  163. }
  164. .placeholder-style {
  165. color: #999;
  166. font-size: 26rpx;
  167. }
  168. .main-content {
  169. flex: 1;
  170. display: flex;
  171. overflow: hidden;
  172. }
  173. .sidebar {
  174. width: 200rpx;
  175. background-color: #f7f8fa;
  176. }
  177. .sidebar-item {
  178. padding: 28rpx 0;
  179. text-align: center;
  180. font-size: 26rpx;
  181. color: #666;
  182. position: relative;
  183. }
  184. .sidebar-item.active {
  185. background-color: #fff;
  186. color: #333;
  187. font-weight: bold;
  188. }
  189. .sidebar-item.active::before {
  190. content: '';
  191. position: absolute;
  192. left: 0;
  193. top: 50%;
  194. transform: translateY(-50%);
  195. width: 6rpx;
  196. height: 40rpx;
  197. background-color: #f7ca3e;
  198. border-radius: 0 6rpx 6rpx 0;
  199. }
  200. .content-view {
  201. flex: 1;
  202. padding: 32rpx;
  203. }
  204. .category-section {
  205. margin-bottom: 48rpx;
  206. }
  207. .service-grid {
  208. display: flex;
  209. flex-wrap: wrap;
  210. }
  211. .service-cell {
  212. width: 33.33%;
  213. display: flex;
  214. flex-direction: column;
  215. align-items: center;
  216. margin-bottom: 32rpx;
  217. }
  218. .icon-wrapper {
  219. width: 96rpx;
  220. height: 96rpx;
  221. background-color: #fff;
  222. border-radius: 24rpx;
  223. display: flex;
  224. align-items: center;
  225. justify-content: center;
  226. box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.04);
  227. margin-bottom: 16rpx;
  228. }
  229. .service-icon {
  230. width: 100%;
  231. height: 100%;
  232. }
  233. .service-name {
  234. font-size: 24rpx;
  235. color: #666;
  236. text-align: center;
  237. }
  238. </style>