index.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. display: flex;
  172. align-items: center;
  173. background: #f5f5f5;
  174. border-radius: 32rpx;
  175. padding: 12rpx 20rpx;
  176. gap: 12rpx;
  177. }
  178. .search-input {
  179. flex: 1;
  180. font-size: 26rpx;
  181. background: transparent;
  182. }
  183. .filter-btn {
  184. display: flex;
  185. align-items: center;
  186. gap: 8rpx;
  187. background: #f5f5f5;
  188. border-radius: 32rpx;
  189. padding: 12rpx 20rpx;
  190. font-size: 24rpx;
  191. color: #666;
  192. }
  193. .add-btn {
  194. font-size: 24rpx;
  195. font-weight: bold;
  196. background: linear-gradient(90deg, #ffd53f, #ff9500);
  197. color: #333;
  198. border: none;
  199. border-radius: 32rpx;
  200. padding: 12rpx 24rpx;
  201. white-space: nowrap;
  202. }
  203. .list-container {
  204. padding: 24rpx;
  205. }
  206. .user-card {
  207. background: #fff;
  208. border-radius: 24rpx;
  209. padding: 28rpx;
  210. margin-bottom: 24rpx;
  211. }
  212. .user-header {
  213. display: flex;
  214. align-items: center;
  215. margin-bottom: 24rpx;
  216. padding-bottom: 24rpx;
  217. border-bottom: 1rpx solid #f9f9f9;
  218. }
  219. .user-avatar {
  220. width: 88rpx;
  221. height: 88rpx;
  222. border-radius: 50%;
  223. background: #f0f0f0;
  224. margin-right: 24rpx;
  225. }
  226. .user-info-main {
  227. flex: 1;
  228. }
  229. .user-name {
  230. display: block;
  231. font-size: 32rpx;
  232. font-weight: bold;
  233. color: #333;
  234. margin-bottom: 8rpx;
  235. }
  236. .phone-row {
  237. display: block;
  238. font-size: 26rpx;
  239. color: #666;
  240. }
  241. .user-status {
  242. display: flex;
  243. flex-direction: column;
  244. align-items: center;
  245. gap: 4rpx;
  246. }
  247. .status-text {
  248. font-size: 20rpx;
  249. color: #ff9800;
  250. }
  251. .user-body {
  252. font-size: 26rpx;
  253. }
  254. .info-row {
  255. display: flex;
  256. margin-bottom: 16rpx;
  257. }
  258. .label {
  259. color: #999;
  260. width: 100rpx;
  261. flex-shrink: 0;
  262. }
  263. .value {
  264. color: #333;
  265. flex: 1;
  266. }
  267. .info-grid {
  268. display: flex;
  269. background: #fdfdfd;
  270. border: 1rpx solid #f2f2f2;
  271. border-radius: 12rpx;
  272. padding: 20rpx;
  273. margin-bottom: 20rpx;
  274. }
  275. .grid-cell {
  276. flex: 1;
  277. display: flex;
  278. flex-direction: column;
  279. align-items: center;
  280. gap: 8rpx;
  281. }
  282. .grid-cell .label {
  283. width: auto;
  284. font-size: 24rpx;
  285. }
  286. .grid-cell .value {
  287. font-size: 30rpx;
  288. font-weight: bold;
  289. }
  290. .text-warning {
  291. color: #ff9800;
  292. }
  293. .source-box {
  294. background: #fff8e1;
  295. padding: 16rpx;
  296. border-radius: 8rpx;
  297. }
  298. .source-tag {
  299. display: block;
  300. color: #ff9800;
  301. font-weight: bold;
  302. font-size: 24rpx;
  303. margin-bottom: 8rpx;
  304. }
  305. .create-time {
  306. font-size: 22rpx;
  307. color: #a1887f;
  308. }
  309. .card-actions {
  310. display: flex;
  311. justify-content: flex-end;
  312. gap: 20rpx;
  313. margin-top: 24rpx;
  314. padding-top: 24rpx;
  315. border-top: 1rpx dashed #eee;
  316. }
  317. .action-btn {
  318. border: 1rpx solid #e0e0e0;
  319. color: #666;
  320. font-size: 24rpx;
  321. background: transparent;
  322. border-radius: 12rpx;
  323. padding: 8rpx 24rpx;
  324. }
  325. </style>