index.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. <template>
  2. <view class="user-list-page">
  3. <NavBar title="用户列表" bgColor="#fff" color="#000"></NavBar>
  4. <!-- 顶部操作栏 -->
  5. <view class="action-bar">
  6. <view class="search-box">
  7. <uni-icons type="search" size="14" color="#999"></uni-icons>
  8. <input type="text" v-model="searchValue" placeholder="搜索姓名/手机号" class="search-input" confirm-type="search" @confirm="onSearch" />
  9. </view>
  10. <picker :range="statusOptions" range-key="label" @change="onStatusFilterChange">
  11. <view class="filter-btn">
  12. <text>{{ statusOptions[currentStatusOption].label }}</text>
  13. <uni-icons type="bottom" size="12" color="#666"></uni-icons>
  14. </view>
  15. </picker>
  16. <button size="mini" class="add-btn" @click="goToAdd">+ 新增</button>
  17. </view>
  18. <!-- 用户列表 -->
  19. <view class="list-container">
  20. <view class="user-card" v-for="user in users" :key="user.id">
  21. <view class="user-header">
  22. <image :src="user.avatarUrl || '/static/default-avatar.png'" class="user-avatar" mode="aspectFill"></image>
  23. <view class="user-info-main">
  24. <text class="user-name">{{ user.name }}</text>
  25. <text class="phone-row">{{ user.phone }}</text>
  26. </view>
  27. <view class="user-status">
  28. <switch :checked="user.status === 0" color="#ff9800" style="transform: scale(0.6);"
  29. @change="(e) => onStatusChange(e, user)" />
  30. <text class="status-text">{{ user.status === 0 ? '正常' : '禁用' }}</text>
  31. </view>
  32. </view>
  33. <view class="user-body">
  34. <view class="info-row">
  35. <text class="label">住址:</text>
  36. <text class="value">{{ user.address || '-' }}</text>
  37. </view>
  38. <view class="info-grid">
  39. <view class="grid-cell" @click="goToPetList(user)">
  40. <text class="label">关联宠物</text>
  41. <text class="value text-warning">{{ user.petCount || 0 }}只</text>
  42. </view>
  43. <view class="grid-cell" @click="goToOrderList(user)">
  44. <text class="label">订单数量</text>
  45. <text class="value">{{ user.orderCount || 0 }}单</text>
  46. </view>
  47. </view>
  48. <view class="source-box" v-if="user.source || user.createTime">
  49. <text class="source-tag" v-if="user.source">{{ user.source }}</text>
  50. <text class="create-time" v-if="user.createTime">创建时间: {{ user.createTime }}</text>
  51. </view>
  52. </view>
  53. <view class="card-actions">
  54. <button size="mini" class="action-btn" @click.stop="goToDetail(user)">详情</button>
  55. <button size="mini" class="action-btn" @click.stop="goToEdit(user)">编辑</button>
  56. </view>
  57. </view>
  58. <view v-if="users.length === 0" style="text-align: center; color: #999; padding: 50rpx 0; font-size: 28rpx;">
  59. 暂无用户数据
  60. </view>
  61. </view>
  62. </view>
  63. </template>
  64. <script setup>
  65. import { ref } from 'vue'
  66. import { onShow, onPullDownRefresh, onReachBottom } from '@dcloudio/uni-app'
  67. import NavBar from '@/components/nav-bar/index.vue'
  68. import { listCustomer, changeCustomerStatus } from '@/api/archieves/customer'
  69. const searchValue = ref('')
  70. const users = ref([])
  71. const statusOptions = [{ label: '状态', value: undefined }, { label: '正常', value: 0 }, { label: '停用', value: 1 }]
  72. const currentStatusOption = ref(0) // Default to 全部状态
  73. const onStatusFilterChange = (e) => {
  74. currentStatusOption.value = e.detail.value
  75. onSearch()
  76. }
  77. const pageNum = ref(1)
  78. const pageSize = ref(10)
  79. const hasMore = ref(true)
  80. const loadUsers = async (isLoadMore = false) => {
  81. try {
  82. uni.showNavigationBarLoading()
  83. const res = await listCustomer({
  84. pageNum: pageNum.value,
  85. pageSize: pageSize.value,
  86. keyword: searchValue.value,
  87. status: statusOptions[currentStatusOption.value].value
  88. })
  89. const rows = res?.rows || []
  90. if (isLoadMore) {
  91. users.value = [...users.value, ...rows]
  92. } else {
  93. users.value = rows
  94. }
  95. hasMore.value = users.value.length < (res?.total || 0)
  96. } catch (error) {
  97. console.error('获取用户列表失败', error)
  98. } finally {
  99. uni.hideNavigationBarLoading()
  100. uni.stopPullDownRefresh()
  101. }
  102. }
  103. onShow(() => {
  104. pageNum.value = 1
  105. loadUsers()
  106. })
  107. onPullDownRefresh(() => {
  108. pageNum.value = 1
  109. loadUsers()
  110. })
  111. onReachBottom(() => {
  112. if (hasMore.value) {
  113. pageNum.value++
  114. loadUsers(true)
  115. }
  116. })
  117. const onSearch = () => {
  118. pageNum.value = 1
  119. loadUsers()
  120. }
  121. const goToAdd = () => uni.navigateTo({ url: '/pages/my/user/add/index' })
  122. const goToDetail = (user) => uni.navigateTo({ url: `/pages/my/user/detail/index?id=${user.id}` })
  123. const goToEdit = (user) => uni.navigateTo({ url: `/pages/my/user/edit/index?id=${user.id}` })
  124. const goToPetList = (user) => uni.navigateTo({ url: `/pages/my/pet/list/index?userId=${user.id}` })
  125. const goToOrderList = (user) => uni.reLaunch({ url: '/pages/order/list/index' })
  126. const onStatusChange = (e, user) => {
  127. const originalStatus = user.status
  128. const targetStatus = e.detail.value ? 0 : 1
  129. const text = targetStatus === 0 ? '启用' : '停用'
  130. uni.showModal({
  131. title: '提示',
  132. content: `确认要${text}该用户吗?`,
  133. success: async (res) => {
  134. if (res.confirm) {
  135. try {
  136. uni.showLoading({ title: '处理中...' })
  137. await changeCustomerStatus(user.id, targetStatus)
  138. uni.hideLoading()
  139. user.status = targetStatus
  140. uni.showToast({ title: `已${text}`, icon: 'success' })
  141. } catch(err) {
  142. uni.hideLoading()
  143. // 恢复源数据渲染,利用 setTimeout 强制触发 vue 的重算
  144. user.status = targetStatus
  145. setTimeout(() => { user.status = originalStatus }, 50)
  146. }
  147. } else {
  148. // 用户取消,恢复 switch
  149. user.status = targetStatus
  150. setTimeout(() => { user.status = originalStatus }, 50)
  151. }
  152. }
  153. })
  154. }
  155. </script>
  156. <style lang="scss" scoped>
  157. .user-list-page {
  158. min-height: 100vh;
  159. background: #f2f2f2;
  160. padding-bottom: 40rpx;
  161. }
  162. .action-bar {
  163. display: flex;
  164. align-items: center;
  165. padding: 20rpx 24rpx;
  166. background: #fff;
  167. gap: 16rpx;
  168. }
  169. .search-box {
  170. flex: 1;
  171. height: 64rpx;
  172. display: flex;
  173. align-items: center;
  174. background: #f5f5f5;
  175. border-radius: 32rpx;
  176. padding: 0 24rpx;
  177. gap: 12rpx;
  178. }
  179. .search-input {
  180. flex: 1;
  181. font-size: 26rpx;
  182. background: transparent;
  183. height: 100%;
  184. }
  185. .filter-btn {
  186. height: 64rpx;
  187. display: flex;
  188. align-items: center;
  189. gap: 8rpx;
  190. background: #f5f5f5;
  191. border-radius: 32rpx;
  192. padding: 0 24rpx;
  193. font-size: 24rpx;
  194. color: #666;
  195. }
  196. .add-btn {
  197. height: 64rpx;
  198. line-height: 64rpx;
  199. font-size: 24rpx;
  200. font-weight: bold;
  201. background: linear-gradient(90deg, #ffd53f, #ff9500);
  202. color: #fff;
  203. border: none;
  204. border-radius: 32rpx;
  205. padding: 0 28rpx;
  206. margin: 0;
  207. white-space: nowrap;
  208. display: flex;
  209. align-items: center;
  210. justify-content: center;
  211. &::after {
  212. border: none;
  213. }
  214. }
  215. .list-container {
  216. padding: 24rpx;
  217. }
  218. .user-card {
  219. background: #fff;
  220. border-radius: 24rpx;
  221. padding: 28rpx;
  222. margin-bottom: 24rpx;
  223. }
  224. .user-header {
  225. display: flex;
  226. align-items: center;
  227. margin-bottom: 24rpx;
  228. padding-bottom: 24rpx;
  229. border-bottom: 2rpx solid #EEEEEE;
  230. }
  231. .user-avatar {
  232. width: 88rpx;
  233. height: 88rpx;
  234. border-radius: 50%;
  235. background: #f0f0f0;
  236. margin-right: 24rpx;
  237. }
  238. .user-info-main {
  239. flex: 1;
  240. }
  241. .user-name {
  242. display: block;
  243. font-size: 32rpx;
  244. font-weight: bold;
  245. color: #333;
  246. margin-bottom: 8rpx;
  247. }
  248. .phone-row {
  249. display: block;
  250. font-size: 26rpx;
  251. color: #666;
  252. }
  253. .user-status {
  254. display: flex;
  255. flex-direction: column;
  256. align-items: center;
  257. gap: 4rpx;
  258. }
  259. .status-text {
  260. font-size: 20rpx;
  261. color: #ff9800;
  262. }
  263. .user-body {
  264. font-size: 26rpx;
  265. }
  266. .info-row {
  267. display: flex;
  268. margin-bottom: 16rpx;
  269. }
  270. .label {
  271. color: #999;
  272. width: 100rpx;
  273. flex-shrink: 0;
  274. }
  275. .value {
  276. color: #333;
  277. flex: 1;
  278. }
  279. .info-grid {
  280. display: flex;
  281. background: transparent;
  282. border: 2rpx solid #EEEEEE;
  283. border-radius: 12rpx;
  284. padding: 20rpx;
  285. margin-bottom: 20rpx;
  286. }
  287. .grid-cell {
  288. flex: 1;
  289. display: flex;
  290. flex-direction: column;
  291. align-items: center;
  292. gap: 8rpx;
  293. }
  294. .grid-cell .label {
  295. width: auto;
  296. font-size: 24rpx;
  297. }
  298. .grid-cell .value {
  299. font-size: 30rpx;
  300. font-weight: bold;
  301. }
  302. .text-warning {
  303. color: #ff9800;
  304. }
  305. .source-box {
  306. background: transparent;
  307. padding: 16rpx;
  308. border-radius: 12rpx;
  309. border: 1rpx solid #f9e8d4;
  310. }
  311. .source-tag {
  312. display: block;
  313. color: #ff9800;
  314. font-weight: bold;
  315. font-size: 24rpx;
  316. margin-bottom: 8rpx;
  317. }
  318. .create-time {
  319. font-size: 22rpx;
  320. color: #a1887f;
  321. }
  322. .card-actions {
  323. display: flex;
  324. justify-content: center;
  325. gap: 40rpx;
  326. margin-top: 24rpx;
  327. padding-top: 24rpx;
  328. border-top: 2rpx dashed #EEEEEE;
  329. }
  330. .action-btn {
  331. width: 160rpx;
  332. height: 64rpx;
  333. line-height: 60rpx;
  334. border: 1rpx solid #e0e0e0;
  335. color: #666;
  336. font-size: 24rpx;
  337. background: transparent;
  338. border-radius: 12rpx;
  339. padding: 0;
  340. margin: 0;
  341. display: flex;
  342. align-items: center;
  343. justify-content: center;
  344. &::after {
  345. border: none;
  346. }
  347. }
  348. </style>