products.vue 7.4 KB

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