index.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. uni.showToast({ title: typeof error === 'string' ? error : '获取用户列表失败', icon: 'none' })
  99. } finally {
  100. uni.hideNavigationBarLoading()
  101. uni.stopPullDownRefresh()
  102. }
  103. }
  104. onShow(() => {
  105. pageNum.value = 1
  106. loadUsers()
  107. })
  108. onPullDownRefresh(() => {
  109. pageNum.value = 1
  110. loadUsers()
  111. })
  112. onReachBottom(() => {
  113. if (hasMore.value) {
  114. pageNum.value++
  115. loadUsers(true)
  116. }
  117. })
  118. const onSearch = () => {
  119. pageNum.value = 1
  120. loadUsers()
  121. }
  122. const goToAdd = () => uni.navigateTo({ url: '/pages/my/user/add/index' })
  123. const goToDetail = (user) => uni.navigateTo({ url: `/pages/my/user/detail/index?id=${user.id}` })
  124. const goToEdit = (user) => uni.navigateTo({ url: `/pages/my/user/edit/index?id=${user.id}` })
  125. const goToPetList = (user) => uni.navigateTo({ url: `/pages/my/pet/list/index?userId=${user.id}` })
  126. const goToOrderList = (user) => uni.reLaunch({ url: '/pages/order/list/index' })
  127. const onStatusChange = (e, user) => {
  128. const originalStatus = user.status
  129. const targetStatus = e.detail.value ? 0 : 1
  130. const text = targetStatus === 0 ? '启用' : '停用'
  131. uni.showModal({
  132. title: '提示',
  133. content: `确认要${text}该用户吗?`,
  134. success: async (res) => {
  135. if (res.confirm) {
  136. try {
  137. uni.showLoading({ title: '处理中...' })
  138. await changeCustomerStatus(user.id, targetStatus)
  139. uni.hideLoading()
  140. user.status = targetStatus
  141. uni.showToast({ title: `已${text}`, icon: 'success' })
  142. } catch(err) {
  143. uni.hideLoading()
  144. uni.showToast({ title: typeof err === 'string' ? err : '操作失败', icon: 'none' })
  145. // 恢复源数据渲染,利用 setTimeout 强制触发 vue 的重算
  146. user.status = targetStatus
  147. setTimeout(() => { user.status = originalStatus }, 50)
  148. }
  149. } else {
  150. // 用户取消,恢复 switch
  151. user.status = targetStatus
  152. setTimeout(() => { user.status = originalStatus }, 50)
  153. }
  154. }
  155. })
  156. }
  157. </script>
  158. <style lang="scss" scoped>
  159. .user-list-page {
  160. min-height: 100vh;
  161. background: #f2f2f2;
  162. padding-bottom: 40rpx;
  163. }
  164. .action-bar {
  165. display: flex;
  166. align-items: center;
  167. padding: 20rpx 24rpx;
  168. background: #fff;
  169. gap: 16rpx;
  170. }
  171. .search-box {
  172. flex: 1;
  173. height: 64rpx;
  174. display: flex;
  175. align-items: center;
  176. background: #f5f5f5;
  177. border-radius: 32rpx;
  178. padding: 0 24rpx;
  179. gap: 12rpx;
  180. }
  181. .search-input {
  182. flex: 1;
  183. font-size: 26rpx;
  184. background: transparent;
  185. height: 100%;
  186. }
  187. .filter-btn {
  188. height: 64rpx;
  189. display: flex;
  190. align-items: center;
  191. gap: 8rpx;
  192. background: #f5f5f5;
  193. border-radius: 32rpx;
  194. padding: 0 24rpx;
  195. font-size: 24rpx;
  196. color: #666;
  197. }
  198. .add-btn {
  199. height: 64rpx;
  200. line-height: 64rpx;
  201. font-size: 24rpx;
  202. font-weight: bold;
  203. background: linear-gradient(90deg, #ffd53f, #ff9500);
  204. color: #fff;
  205. border: none;
  206. border-radius: 32rpx;
  207. padding: 0 28rpx;
  208. margin: 0;
  209. white-space: nowrap;
  210. display: flex;
  211. align-items: center;
  212. justify-content: center;
  213. &::after {
  214. border: none;
  215. }
  216. }
  217. .list-container {
  218. padding: 24rpx;
  219. }
  220. .user-card {
  221. background: #fff;
  222. border-radius: 24rpx;
  223. padding: 28rpx;
  224. margin-bottom: 24rpx;
  225. }
  226. .user-header {
  227. display: flex;
  228. align-items: center;
  229. margin-bottom: 24rpx;
  230. padding-bottom: 24rpx;
  231. border-bottom: 2rpx solid #EEEEEE;
  232. }
  233. .user-avatar {
  234. width: 88rpx;
  235. height: 88rpx;
  236. border-radius: 50%;
  237. background: #f0f0f0;
  238. margin-right: 24rpx;
  239. }
  240. .user-info-main {
  241. flex: 1;
  242. }
  243. .user-name {
  244. display: block;
  245. font-size: 32rpx;
  246. font-weight: bold;
  247. color: #333;
  248. margin-bottom: 8rpx;
  249. }
  250. .phone-row {
  251. display: block;
  252. font-size: 26rpx;
  253. color: #666;
  254. }
  255. .user-status {
  256. display: flex;
  257. flex-direction: column;
  258. align-items: center;
  259. gap: 4rpx;
  260. }
  261. .status-text {
  262. font-size: 20rpx;
  263. color: #ff9800;
  264. }
  265. .user-body {
  266. font-size: 26rpx;
  267. }
  268. .info-row {
  269. display: flex;
  270. margin-bottom: 16rpx;
  271. }
  272. .label {
  273. color: #999;
  274. width: 100rpx;
  275. flex-shrink: 0;
  276. }
  277. .value {
  278. color: #333;
  279. flex: 1;
  280. }
  281. .info-grid {
  282. display: flex;
  283. background: transparent;
  284. border: 2rpx solid #EEEEEE;
  285. border-radius: 12rpx;
  286. padding: 20rpx;
  287. margin-bottom: 20rpx;
  288. }
  289. .grid-cell {
  290. flex: 1;
  291. display: flex;
  292. flex-direction: column;
  293. align-items: center;
  294. gap: 8rpx;
  295. }
  296. .grid-cell .label {
  297. width: auto;
  298. font-size: 24rpx;
  299. }
  300. .grid-cell .value {
  301. font-size: 30rpx;
  302. font-weight: bold;
  303. }
  304. .text-warning {
  305. color: #ff9800;
  306. }
  307. .source-box {
  308. background: transparent;
  309. padding: 16rpx;
  310. border-radius: 12rpx;
  311. border: 1rpx solid #f9e8d4;
  312. }
  313. .source-tag {
  314. display: block;
  315. color: #ff9800;
  316. font-weight: bold;
  317. font-size: 24rpx;
  318. margin-bottom: 8rpx;
  319. }
  320. .create-time {
  321. font-size: 22rpx;
  322. color: #a1887f;
  323. }
  324. .card-actions {
  325. display: flex;
  326. justify-content: center;
  327. gap: 40rpx;
  328. margin-top: 24rpx;
  329. padding-top: 24rpx;
  330. border-top: 2rpx dashed #EEEEEE;
  331. }
  332. .action-btn {
  333. width: 160rpx;
  334. height: 64rpx;
  335. line-height: 60rpx;
  336. border: 1rpx solid #e0e0e0;
  337. color: #666;
  338. font-size: 24rpx;
  339. background: transparent;
  340. border-radius: 12rpx;
  341. padding: 0;
  342. margin: 0;
  343. display: flex;
  344. align-items: center;
  345. justify-content: center;
  346. &::after {
  347. border: none;
  348. }
  349. }
  350. </style>