products.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <template>
  2. <view class="container">
  3. <view class="status-tabs">
  4. <view
  5. v-for="item in statusList"
  6. :key="item.value"
  7. :class="['tab', currentStatus === item.value ? 'active' : '']"
  8. @click="onStatusChange(item.value)"
  9. >
  10. {{ item.label }}
  11. </view>
  12. </view>
  13. <scroll-view scroll-y class="product-list" @scrolltolower="onLoadMore" refresher-enabled :refresher-triggered="refreshing" @refresherrefresh="onRefresh">
  14. <view v-if="loading && products.length === 0" class="loading-wrap">
  15. <text class="loading-text">加载中...</text>
  16. </view>
  17. <view v-else-if="products.length === 0" class="empty-wrap">
  18. <text class="empty-icon">📦</text>
  19. <text class="empty-text">暂无商品</text>
  20. <button class="btn-add" @click="goCreate">发布商品</button>
  21. </view>
  22. <view v-else>
  23. <view
  24. v-for="item in products"
  25. :key="item.id"
  26. class="product-card"
  27. >
  28. <view class="card-top">
  29. <image class="cover" :src="item.coverImage || '/static/default-product.png'" mode="aspectFill" />
  30. <view class="card-info">
  31. <text class="product-name">{{ item.name }}</text>
  32. <text class="product-price">{{ formatPriceWithSymbol(item.price) }}</text>
  33. <text class="product-stock">库存 {{ item.stock || 0 }}</text>
  34. </view>
  35. </view>
  36. <view class="card-bottom">
  37. <text :class="['status-badge', 'status-' + item.status]">{{ statusLabel(item.status) }}</text>
  38. <view class="card-actions">
  39. <button v-if="item.status === 'approved'" class="btn-xs" @click="onShelve(item.id, item.status === 'on_shelf' ? 'unshelve' : 'shelve')">
  40. {{ item.status === 'on_shelf' ? '下架' : '上架' }}
  41. </button>
  42. <button class="btn-xs btn-edit" @click="goEdit(item.id)">编辑</button>
  43. </view>
  44. </view>
  45. </view>
  46. </view>
  47. <view v-if="noMore && products.length > 0" class="no-more">
  48. <text class="no-more-text">— 没有更多了 —</text>
  49. </view>
  50. </scroll-view>
  51. </view>
  52. </template>
  53. <script>
  54. import { productMy, productShelve } from '@/utils/api.js'
  55. export default {
  56. data() {
  57. return {
  58. statusList: [
  59. { label: '全部', value: '' },
  60. { label: '待审核', value: 'pending' },
  61. { label: '已通过', value: 'approved' },
  62. { label: '已拒绝', value: 'rejected' },
  63. { label: '已上架', value: 'on_shelf' }
  64. ],
  65. currentStatus: '',
  66. products: [],
  67. page: 1,
  68. size: 20,
  69. loading: false,
  70. refreshing: false,
  71. noMore: false
  72. }
  73. },
  74. onLoad() {
  75. this.loadProducts()
  76. },
  77. methods: {
  78. onStatusChange(value) {
  79. this.currentStatus = value
  80. this.page = 1
  81. this.products = []
  82. this.noMore = false
  83. this.loadProducts()
  84. },
  85. loadProducts() {
  86. if (this.loading) return
  87. this.loading = true
  88. productMy({ page: this.page, size: this.size, status: this.currentStatus }).then(res => {
  89. this.loading = false
  90. this.refreshing = false
  91. if (res.code === 200 && res.data) {
  92. const list = res.data.records || res.data || []
  93. if (this.page === 1) {
  94. this.products = list
  95. } else {
  96. this.products = this.products.concat(list)
  97. }
  98. const total = res.data.total || 0
  99. this.noMore = this.products.length >= total
  100. }
  101. }).catch(() => {
  102. this.loading = false
  103. this.refreshing = false
  104. })
  105. },
  106. onRefresh() {
  107. this.refreshing = true
  108. this.page = 1
  109. this.noMore = false
  110. this.loadProducts()
  111. },
  112. onLoadMore() {
  113. if (this.noMore) return
  114. this.page = this.page + 1
  115. this.loadProducts()
  116. },
  117. onShelve(id, action) {
  118. productShelve({ productId: id, action }).then(res => {
  119. if (res.code === 200) {
  120. uni.showToast({ title: action === 'shelve' ? '上架成功' : '下架成功', icon: 'success' })
  121. this.onRefresh()
  122. } else {
  123. uni.showToast({ title: res.message || '操作失败', icon: 'none' })
  124. }
  125. })
  126. },
  127. goCreate() {
  128. uni.navigateTo({ url: '/pages/vendor/product-edit/product-edit' })
  129. },
  130. goEdit(id) {
  131. uni.navigateTo({ url: '/pages/vendor/product-edit/product-edit?id=' + id })
  132. },
  133. statusLabel(status) {
  134. const map = {
  135. draft: '草稿',
  136. pending: '待审核',
  137. approved: '已通过',
  138. rejected: '已拒绝',
  139. on_shelf: '已上架',
  140. off_shelf: '已下架'
  141. }
  142. return map[status] || status
  143. }
  144. }
  145. }
  146. </script>
  147. <style scoped>
  148. .container {
  149. display: flex;
  150. flex-direction: column;
  151. height: 100vh;
  152. background: #f5f5f5;
  153. }
  154. .status-tabs {
  155. display: flex;
  156. background: #fff;
  157. padding: 20rpx 0;
  158. border-bottom: 1rpx solid #eee;
  159. }
  160. .tab {
  161. flex: 1;
  162. text-align: center;
  163. font-size: 26rpx;
  164. color: #666;
  165. padding: 10rpx 0;
  166. }
  167. .tab.active {
  168. color: #F97316;
  169. font-weight: bold;
  170. border-bottom: 4rpx solid #F97316;
  171. }
  172. .product-list {
  173. flex: 1;
  174. height: calc(100vh - 100rpx);
  175. }
  176. .loading-wrap,
  177. .empty-wrap {
  178. display: flex;
  179. flex-direction: column;
  180. align-items: center;
  181. padding-top: 200rpx;
  182. }
  183. .loading-text {
  184. font-size: 26rpx;
  185. color: #999;
  186. }
  187. .empty-icon {
  188. font-size: 100rpx;
  189. margin-bottom: 30rpx;
  190. }
  191. .empty-text {
  192. font-size: 28rpx;
  193. color: #999;
  194. margin-bottom: 40rpx;
  195. }
  196. .btn-add {
  197. background: linear-gradient(135deg, #F97316, #FB923C);
  198. color: #fff;
  199. font-size: 28rpx;
  200. padding: 0 60rpx;
  201. height: 72rpx;
  202. line-height: 72rpx;
  203. border-radius: 36rpx;
  204. border: none;
  205. }
  206. .btn-add::after {
  207. border: none;
  208. }
  209. .product-card {
  210. background: #fff;
  211. margin: 20rpx;
  212. border-radius: 16rpx;
  213. padding: 24rpx;
  214. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.06);
  215. }
  216. .card-top {
  217. display: flex;
  218. margin-bottom: 16rpx;
  219. }
  220. .cover {
  221. width: 160rpx;
  222. height: 160rpx;
  223. border-radius: 12rpx;
  224. background: #f0f0f0;
  225. flex-shrink: 0;
  226. }
  227. .card-info {
  228. flex: 1;
  229. margin-left: 20rpx;
  230. display: flex;
  231. flex-direction: column;
  232. justify-content: space-between;
  233. }
  234. .product-name {
  235. font-size: 28rpx;
  236. color: #333;
  237. overflow: hidden;
  238. text-overflow: ellipsis;
  239. white-space: nowrap;
  240. }
  241. .product-price {
  242. font-size: 30rpx;
  243. color: #F97316;
  244. font-weight: bold;
  245. }
  246. .product-stock {
  247. font-size: 22rpx;
  248. color: #999;
  249. }
  250. .card-bottom {
  251. display: flex;
  252. justify-content: space-between;
  253. align-items: center;
  254. border-top: 1rpx solid #f5f5f5;
  255. padding-top: 16rpx;
  256. }
  257. .status-badge {
  258. font-size: 22rpx;
  259. padding: 4rpx 16rpx;
  260. border-radius: 20rpx;
  261. }
  262. .status-draft { background: #f5f5f5; color: #999; }
  263. .status-pending { background: #fff7e6; color: #fa8c16; }
  264. .status-approved { background: #f6ffed; color: #52c41a; }
  265. .status-rejected { background: #fff2f0; color: #ff4d4f; }
  266. .status-on_shelf { background: #e6f7ff; color: #1890ff; }
  267. .status-off_shelf { background: #f5f5f5; color: #999; }
  268. .card-actions {
  269. display: flex;
  270. gap: 12rpx;
  271. }
  272. .btn-xs {
  273. font-size: 22rpx;
  274. padding: 8rpx 24rpx;
  275. border-radius: 30rpx;
  276. border: 1rpx solid #F97316;
  277. color: #F97316;
  278. background: #fff;
  279. line-height: 1.5;
  280. }
  281. .btn-edit {
  282. border-color: #1890ff;
  283. color: #1890ff;
  284. }
  285. .btn-xs::after {
  286. border: none;
  287. }
  288. .no-more {
  289. text-align: center;
  290. padding: 30rpx;
  291. }
  292. .no-more-text {
  293. font-size: 24rpx;
  294. color: #ccc;
  295. }
  296. </style>