address.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. <script setup>
  2. import { onShow } from '@dcloudio/uni-app';
  3. import { computed, ref } from 'vue';
  4. import { useUserStore } from '@/stores/user';
  5. import { request, parseResponseJson } from '@/utils/request';
  6. import PhoneDialInline from '@/components/phone-dial-inline.vue';
  7. import { IMG_EMPTY } from '@/utils/staticAsset';
  8. const userStore = useUserStore();
  9. const list = ref([]);
  10. const sortedList = computed(() => {
  11. const raw = Array.isArray(list.value) ? list.value : [];
  12. return raw
  13. .map((it, idx) => ({ it, idx }))
  14. .sort((a, b) => {
  15. const ad = a.it?.isDefault ? 1 : 0;
  16. const bd = b.it?.isDefault ? 1 : 0;
  17. if (ad !== bd)
  18. return bd - ad;
  19. return a.idx - b.idx;
  20. })
  21. .map((x) => x.it);
  22. });
  23. const loading = ref(false);
  24. const loadingMore = ref(false);
  25. const hasMore = ref(true);
  26. const nextPage = ref(1);
  27. const deleteDialogVisible = ref(false);
  28. const deleting = ref(false);
  29. const deletingItem = ref(null);
  30. const PAGE_SIZE = 20;
  31. async function fetchList() {
  32. const reset = arguments.length === 0 || arguments[0] === true;
  33. if (reset) {
  34. loading.value = true;
  35. list.value = [];
  36. nextPage.value = 1;
  37. hasMore.value = true;
  38. }
  39. else {
  40. if (!hasMore.value || loadingMore.value || loading.value)
  41. return;
  42. loadingMore.value = true;
  43. }
  44. const page = reset ? 1 : nextPage.value;
  45. try {
  46. const res = await request({
  47. url: '/mini/supplier/pickup-addresses',
  48. method: 'GET',
  49. data: { page, pageSize: PAGE_SIZE }
  50. });
  51. const body = parseResponseJson(res);
  52. if (body.code === 200 && body.data) {
  53. const d = body.data;
  54. const items = Array.isArray(d) ? d : (Array.isArray(d.list) ? d.list : []);
  55. const totalPages = Array.isArray(d) ? 0 : Number(d.totalPages ?? 0);
  56. if (reset) {
  57. list.value = items;
  58. }
  59. else {
  60. const seen = new Set(list.value.map((x) => x.id));
  61. for (const it of items) {
  62. if (it.id && !seen.has(it.id)) {
  63. list.value.push(it);
  64. seen.add(it.id);
  65. }
  66. }
  67. }
  68. hasMore.value = totalPages > 0 ? page < totalPages : items.length >= PAGE_SIZE;
  69. nextPage.value = page + 1;
  70. }
  71. else if (reset) {
  72. list.value = [];
  73. hasMore.value = false;
  74. }
  75. }
  76. finally {
  77. loading.value = false;
  78. loadingMore.value = false;
  79. }
  80. }
  81. function onScrollToLower() {
  82. void fetchList(false);
  83. }
  84. function onH5ListScroll(e) {
  85. const el = e && e.target;
  86. if (!el || typeof el.scrollTop !== 'number')
  87. return;
  88. const { scrollTop, scrollHeight, clientHeight } = el;
  89. if (scrollHeight - scrollTop - clientHeight < 100) {
  90. void fetchList(false);
  91. }
  92. }
  93. function fullRegion(addr) {
  94. const p = addr.province || '';
  95. const c = addr.city || '';
  96. const d = addr.district || '';
  97. return `${p}${c}${d}`;
  98. }
  99. function goAdd() {
  100. uni.navigateTo({ url: '/pages/supplier/address/edit' });
  101. }
  102. function goEdit(item) {
  103. uni.navigateTo({ url: `/pages/supplier/address/edit?id=${item.id}` });
  104. }
  105. async function handleSetDefault(id) {
  106. try {
  107. const res = await request({
  108. url: `/mini/supplier/addresses/${id}/set-default`,
  109. method: 'POST'
  110. });
  111. const body = parseResponseJson(res);
  112. if (body.code === 200) {
  113. uni.showToast({ title: '已设为默认', icon: 'success' });
  114. await fetchList();
  115. }
  116. else {
  117. uni.showToast({ title: body.message || '操作失败', icon: 'none' });
  118. }
  119. }
  120. catch {
  121. uni.showToast({ title: '网络异常', icon: 'none' });
  122. }
  123. }
  124. function handleDelete(item) {
  125. deletingItem.value = item;
  126. deleteDialogVisible.value = true;
  127. }
  128. function closeDeleteDialog(force = false) {
  129. if (deleting.value && !force)
  130. return;
  131. deleteDialogVisible.value = false;
  132. deletingItem.value = null;
  133. }
  134. async function confirmDelete() {
  135. const item = deletingItem.value;
  136. if (!item?.id || deleting.value)
  137. return;
  138. deleting.value = true;
  139. try {
  140. const req = await request({
  141. url: `/mini/supplier/addresses/${item.id}`,
  142. method: 'DELETE'
  143. });
  144. const body = parseResponseJson(req);
  145. if (body.code === 200) {
  146. uni.showToast({ title: '已删除', icon: 'success' });
  147. closeDeleteDialog(true);
  148. await fetchList(true);
  149. }
  150. else {
  151. uni.showToast({ title: body.message || '删除失败', icon: 'none' });
  152. }
  153. }
  154. catch {
  155. uni.showToast({ title: '网络异常', icon: 'none' });
  156. }
  157. finally {
  158. deleting.value = false;
  159. }
  160. }
  161. onShow(() => {
  162. if (!userStore.isLoggedIn) {
  163. uni.reLaunch({ url: '/pages/login/login' });
  164. return;
  165. }
  166. if (!userStore.isSupplier) {
  167. uni.showToast({ title: '仅供应商可访问', icon: 'none' });
  168. setTimeout(() => userStore.navigateToHome(), 1200);
  169. return;
  170. }
  171. void fetchList(true);
  172. });
  173. </script>
  174. <template>
  175. <view class="page client-theme client-subpage">
  176. <view v-if="loading" class="state">
  177. <ClientSkeleton variant="list" :rows="3" />
  178. </view>
  179. <template v-else>
  180. <view class="client-subpage-hero">
  181. <text class="client-subpage-hero__title">取货地址</text>
  182. <text class="client-subpage-hero__sub">管理取货信息,便于备货和配送衔接</text>
  183. </view>
  184. <view v-if="list.length === 0" class="empty-wrap">
  185. <image class="empty-image" :src="IMG_EMPTY" mode="aspectFit" />
  186. <text class="empty-text">暂无取货地址</text>
  187. <text class="empty-hint">添加后便于备货与配送对接</text>
  188. <button class="add-btn" type="default" @click="goAdd">新增取货地址</button>
  189. </view>
  190. <view v-else class="list-shell">
  191. <!-- #ifdef H5 -->
  192. <view class="list-scroll list-scroll--h5" @scroll.passive="onH5ListScroll">
  193. <view class="list-inner">
  194. <view v-for="item in sortedList" :key="item.id" class="card" hover-class="card--pressed" :hover-stay-time="100">
  195. <view class="card-main" @click="goEdit(item)">
  196. <view class="card-top">
  197. <text class="name">{{ item.contactPerson }}</text>
  198. <PhoneDialInline class="phone-wrap" :phone="item.contactPhone" color="#0a9f6d" hide-icon />
  199. <view v-if="item.isDefault" class="tag-default">默认</view>
  200. </view>
  201. <text class="addr-line">{{ fullRegion(item) }}{{ item.detailAddress }}</text>
  202. </view>
  203. <view class="card-actions">
  204. <text
  205. v-if="!item.isDefault"
  206. class="act"
  207. @click.stop="handleSetDefault(item.id)"
  208. >设为默认</text>
  209. <text class="act" @click.stop="goEdit(item)">编辑</text>
  210. <text class="act danger" @click.stop="handleDelete(item)">删除</text>
  211. </view>
  212. </view>
  213. <view v-if="loadingMore" class="list-footer-tip">加载中...</view>
  214. <view v-else-if="!hasMore && list.length > 0" class="list-footer-tip muted">没有更多了</view>
  215. </view>
  216. </view>
  217. <!-- #endif -->
  218. <!-- #ifndef H5 -->
  219. <scroll-view
  220. scroll-y
  221. class="list-scroll"
  222. :show-scrollbar="true"
  223. :enable-flex="true"
  224. :lower-threshold="100"
  225. @scrolltolower="onScrollToLower"
  226. >
  227. <view class="list-inner">
  228. <view v-for="item in sortedList" :key="item.id" class="card" hover-class="card--pressed" :hover-stay-time="100">
  229. <view class="card-main" @click="goEdit(item)">
  230. <view class="card-top">
  231. <text class="name">{{ item.contactPerson }}</text>
  232. <PhoneDialInline class="phone-wrap" :phone="item.contactPhone" color="#0a9f6d" hide-icon />
  233. <view v-if="item.isDefault" class="tag-default">默认</view>
  234. </view>
  235. <text class="addr-line">{{ fullRegion(item) }}{{ item.detailAddress }}</text>
  236. </view>
  237. <view class="card-actions">
  238. <text
  239. v-if="!item.isDefault"
  240. class="act"
  241. @click.stop="handleSetDefault(item.id)"
  242. >设为默认</text>
  243. <text class="act" @click.stop="goEdit(item)">编辑</text>
  244. <text class="act danger" @click.stop="handleDelete(item)">删除</text>
  245. </view>
  246. </view>
  247. <view v-if="loadingMore" class="list-footer-tip">加载中...</view>
  248. <view v-else-if="!hasMore && list.length > 0" class="list-footer-tip muted">没有更多了</view>
  249. </view>
  250. </scroll-view>
  251. <!-- #endif -->
  252. <view class="footer-bar">
  253. <button class="add-btn-bottom" type="default" @click="goAdd">新增取货地址</button>
  254. </view>
  255. </view>
  256. </template>
  257. <view v-if="deleteDialogVisible" class="delete-dialog-mask" @click="closeDeleteDialog">
  258. <view class="delete-dialog" @click.stop>
  259. <view class="delete-dialog__icon">!</view>
  260. <text class="delete-dialog__title">删除取货地址</text>
  261. <text class="delete-dialog__desc">删除后将无法恢复,确定继续吗?</text>
  262. <view class="delete-dialog__actions">
  263. <view class="delete-dialog__action delete-dialog__action--cancel" @click="closeDeleteDialog">取消</view>
  264. <button
  265. class="delete-dialog__action delete-dialog__action--danger"
  266. :disabled="deleting"
  267. :loading="deleting"
  268. @click="confirmDelete"
  269. >
  270. 删除
  271. </button>
  272. </view>
  273. </view>
  274. </view>
  275. </view>
  276. </template>
  277. <style lang="scss">
  278. page {
  279. height: 100%;
  280. overflow: hidden;
  281. background-color: #f3f7f5;
  282. }
  283. </style>
  284. <style lang="scss" scoped>
  285. /* 供应商主题色(与 sanqiang-supplier-ui-demo 一致) */
  286. $supplier: #0a9f6d;
  287. .page {
  288. height: 100%;
  289. display: flex;
  290. flex-direction: column;
  291. background: var(--c-bg);
  292. box-sizing: border-box;
  293. overflow: hidden;
  294. }
  295. .card--pressed {
  296. opacity: 0.94;
  297. }
  298. .state {
  299. flex: 1;
  300. min-height: 0;
  301. padding: 0;
  302. }
  303. .empty-wrap {
  304. flex: 1;
  305. display: flex;
  306. flex-direction: column;
  307. align-items: center;
  308. padding: 88rpx 40rpx 40rpx;
  309. box-sizing: border-box;
  310. overflow-y: auto;
  311. .empty-image {
  312. width: 160rpx;
  313. height: 160rpx;
  314. margin-bottom: 12rpx;
  315. }
  316. .empty-text {
  317. font-size: 30rpx;
  318. color: var(--c-text);
  319. margin-bottom: 8rpx;
  320. text-align: center;
  321. }
  322. .empty-hint {
  323. font-size: 24rpx;
  324. color: var(--c-muted);
  325. margin-bottom: 28rpx;
  326. text-align: center;
  327. }
  328. }
  329. .add-btn,
  330. .add-btn-bottom {
  331. width: 100%;
  332. max-width: 600rpx;
  333. height: 88rpx;
  334. line-height: 88rpx;
  335. background: linear-gradient(145deg, #14b86f, #0a9f6d);
  336. color: #ffffff;
  337. border-radius: 44rpx;
  338. font-size: 30rpx;
  339. border: none;
  340. font-weight: 700;
  341. }
  342. .add-btn-bottom {
  343. max-width: none;
  344. margin: 0;
  345. }
  346. .list-shell {
  347. flex: 1;
  348. min-height: 0;
  349. display: flex;
  350. flex-direction: column;
  351. overflow: hidden;
  352. }
  353. .list-scroll {
  354. flex: 1;
  355. flex-basis: 0;
  356. min-height: 0;
  357. height: 0;
  358. width: 100%;
  359. box-sizing: border-box;
  360. }
  361. /* #ifdef H5 */
  362. .list-scroll--h5 {
  363. overflow-x: hidden;
  364. overflow-y: auto;
  365. -webkit-overflow-scrolling: touch;
  366. }
  367. /* #endif */
  368. .list-inner {
  369. padding: 24rpx 32rpx 16rpx;
  370. padding-bottom: calc(24rpx + 180rpx + env(safe-area-inset-bottom, 0px));
  371. box-sizing: border-box;
  372. }
  373. .list-footer-tip {
  374. text-align: center;
  375. font-size: 24rpx;
  376. color: #9ba8a2;
  377. padding: 24rpx 12rpx 32rpx;
  378. &.muted {
  379. color: #c7d6cf;
  380. }
  381. }
  382. .footer-bar {
  383. position: fixed;
  384. left: 0;
  385. right: 0;
  386. bottom: 0;
  387. z-index: 20;
  388. padding: 16rpx 32rpx;
  389. padding-bottom: calc(16rpx + constant(safe-area-inset-bottom));
  390. padding-bottom: calc(16rpx + env(safe-area-inset-bottom));
  391. background: var(--c-bg);
  392. border-top: 1rpx solid rgba(220, 235, 228, 0.65);
  393. box-sizing: border-box;
  394. }
  395. .card {
  396. background: var(--c-card);
  397. border-radius: var(--c-radius-card);
  398. margin-bottom: 24rpx;
  399. overflow: hidden;
  400. box-shadow: none;
  401. border: 1rpx solid var(--c-border);
  402. }
  403. .card-main {
  404. padding: 28rpx 24rpx 20rpx;
  405. }
  406. .card-top {
  407. display: flex;
  408. align-items: center;
  409. flex-wrap: wrap;
  410. gap: 16rpx;
  411. margin-bottom: 12rpx;
  412. .name {
  413. font-size: 30rpx;
  414. font-weight: 700;
  415. color: var(--c-text);
  416. }
  417. .phone-wrap :deep(.phone-dial-inline__text) {
  418. font-size: 28rpx;
  419. color: var(--c-muted);
  420. }
  421. .tag-default {
  422. font-size: 22rpx;
  423. color: var(--c-brand-green);
  424. background: var(--c-brand-green-soft);
  425. padding: 0 16rpx;
  426. min-height: 44rpx;
  427. border-radius: 999rpx;
  428. display: inline-flex;
  429. align-items: center;
  430. }
  431. }
  432. .addr-line {
  433. font-size: 26rpx;
  434. color: var(--c-muted);
  435. line-height: 1.5;
  436. }
  437. .card-actions {
  438. display: flex;
  439. justify-content: flex-end;
  440. align-items: center;
  441. gap: 32rpx;
  442. padding: 16rpx 24rpx;
  443. border-top: 1rpx solid rgba(220, 235, 228, 0.65);
  444. background: #ffffff;
  445. .act {
  446. font-size: 26rpx;
  447. color: var(--c-brand-green);
  448. }
  449. .danger {
  450. color: #ee0a24;
  451. }
  452. }
  453. .delete-dialog-mask {
  454. position: fixed;
  455. inset: 0;
  456. z-index: 120;
  457. background: rgba(18, 28, 24, 0.45);
  458. display: flex;
  459. align-items: center;
  460. justify-content: center;
  461. padding: 40rpx;
  462. box-sizing: border-box;
  463. }
  464. .delete-dialog {
  465. width: 100%;
  466. max-width: 620rpx;
  467. background: #ffffff;
  468. border-radius: 26rpx;
  469. padding: 40rpx 34rpx 32rpx;
  470. box-sizing: border-box;
  471. box-shadow: 0 12rpx 48rpx rgba(0, 0, 0, 0.15);
  472. }
  473. .delete-dialog__icon {
  474. width: 76rpx;
  475. height: 76rpx;
  476. margin: 0 auto 20rpx;
  477. border-radius: 50%;
  478. background: #fff1f0;
  479. color: #ee0a24;
  480. font-size: 42rpx;
  481. font-weight: 700;
  482. display: flex;
  483. align-items: center;
  484. justify-content: center;
  485. }
  486. .delete-dialog__title {
  487. display: block;
  488. text-align: center;
  489. color: var(--c-text);
  490. font-size: 34rpx;
  491. font-weight: 700;
  492. }
  493. .delete-dialog__desc {
  494. display: block;
  495. margin-top: 14rpx;
  496. text-align: center;
  497. color: var(--c-muted);
  498. font-size: 26rpx;
  499. line-height: 1.6;
  500. }
  501. .delete-dialog__actions {
  502. margin-top: 28rpx;
  503. display: grid;
  504. grid-template-columns: repeat(2, 1fr);
  505. column-gap: 16rpx;
  506. }
  507. .delete-dialog__action {
  508. width: 100%;
  509. min-height: 76rpx;
  510. border-radius: 999rpx;
  511. display: flex;
  512. align-items: center;
  513. justify-content: center;
  514. font-size: 28rpx;
  515. font-weight: 600;
  516. border: none;
  517. box-sizing: border-box;
  518. }
  519. .delete-dialog__action--cancel {
  520. background: #f4f7f6;
  521. color: #5b6b64;
  522. border: 1rpx solid #ddebe5;
  523. }
  524. .delete-dialog__action--danger {
  525. background: linear-gradient(145deg, #ff6565, #ee0a24);
  526. color: #ffffff;
  527. }
  528. .delete-dialog__action--danger[disabled] {
  529. color: #ffffff !important;
  530. opacity: 0.92;
  531. }
  532. </style>